library of assembled shared sources

http://lass.cocamware.com

range_algorithm.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 /** @defgroup range_algorithm
00046  *  @brief extra algorithms
00047  *  @author Bram de Greve [BdG]
00048  */
00049 
00050 #ifndef LASS_GUARDIAN_OF_INCLUSION_STDE_RANGE_ALGORITHM_H
00051 #define LASS_GUARDIAN_OF_INCLUSION_STDE_RANGE_ALGORITHM_H
00052 
00053 #include "stde_common.h"
00054 
00055 #if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC
00056 #   pragma warning(push)
00057 #   pragma warning(disable: 4996)
00058 #endif
00059 
00060 namespace lass
00061 {
00062 namespace stde
00063 {
00064 
00065 /** std::for_each wrapper for ranges
00066  *  @ingroup range_algorithm 
00067  */ 
00068 template <typename InputRange, typename Op> inline
00069 Op for_each_r(InputRange& range, Op op)
00070 {
00071     return std::for_each(range.begin(), range.end(), op);
00072 }
00073 
00074 /** std::find wrapper for ranges
00075  *  @ingroup range_algorithm 
00076  */ 
00077 template <typename InputRange, typename T> inline
00078 typename InputRange::iterator 
00079 find_r(const InputRange& range, const T& value)
00080 {
00081     return std::find(range.begin(), range.end(), value);
00082 }
00083 
00084 /** std::find_if wrapper for ranges
00085  *  @ingroup range_algorithm 
00086  */ 
00087 template <typename InputRange, typename Predicate> inline
00088 typename InputRange::iterator 
00089 find_if_r(const InputRange& range, Predicate pred)
00090 {
00091     return std::find(range.begin(), range.end(), pred);
00092 }
00093 
00094 /** std::find_end wrapper for ranges
00095  *  @ingroup range_algorithm 
00096  */ 
00097 template <typename ForwardRange1, typename ForwardRange2> inline
00098 typename ForwardRange1::iterator 
00099 find_end_r(const ForwardRange1& range1, const ForwardRange2& range2)
00100 {
00101     return std::find_end(range1.begin(), range1.end(), range2.begin(), range2.end());
00102 }
00103 
00104 /** std::find_end wrapper for ranges
00105  *  @ingroup range_algorithm 
00106  */ 
00107 template <typename ForwardRange1, typename ForwardRange2, typename BinaryPredicate> inline
00108 typename ForwardRange1::iterator 
00109 find_end_r(const ForwardRange1& range1, const ForwardRange2& range2, BinaryPredicate pred)
00110 {
00111     return std::find_end(range1.begin(), range1.end(), range2.begin(), range2.end(), pred);
00112 }
00113 
00114 /** std::find_first_of wrapper for ranges
00115  *  @ingroup range_algorithm 
00116  */ 
00117 template <typename ForwardRange1, typename ForwardRange2> inline
00118 typename ForwardRange1::iterator 
00119 find_first_of_r(const ForwardRange1& range1, const ForwardRange2& range2)
00120 {
00121     return std::find_first_of(range1.begin(), range1.end(), range2.begin(), range2.end());
00122 }
00123 
00124 /** std::find_first_of wrapper for ranges
00125  *  @ingroup range_algorithm 
00126  */ 
00127 template <typename ForwardRange1, typename ForwardRange2, typename BinaryPredicate> inline
00128 typename ForwardRange1::iterator 
00129 find_first_of_r(const ForwardRange1& range1,const ForwardRange2& range2, BinaryPredicate pred)
00130 {
00131     return std::find_first_of(range1.begin(), range1.end(), range2.begin(), range2.end(), pred);
00132 }
00133 
00134 /** std::adjacent_find wrapper for ranges
00135  *  @ingroup range_algorithm 
00136  */ 
00137 template <typename ForwardRange> inline
00138 typename ForwardRange::iterator 
00139 adjacent_find_r(const ForwardRange& range)
00140 {
00141     return std::adjacent_find(range.begin(), range.end());
00142 }
00143 
00144 /** std::adjacent_find wrapper for ranges
00145  *  @ingroup range_algorithm 
00146  */ 
00147 template <typename ForwardRange, typename Predicate> inline
00148 typename ForwardRange::iterator 
00149 adjacent_find_r(const ForwardRange& range, Predicate pred)
00150 {
00151     return std::adjacent_find(range.begin(), range.end(), pred);
00152 }
00153 
00154 /** std::count wrapper for ranges
00155  *  @ingroup range_algorithm 
00156  */ 
00157 template <typename InputRange, typename T> inline
00158 typename std::iterator_traits<typename InputRange::iterator>::difference_type
00159 count_r(const InputRange& range, const T& value)
00160 {
00161     return std::count(range.begin(), range.end(), value);
00162 }
00163 
00164 /** std::count_if wrapper for ranges
00165  *  @ingroup range_algorithm 
00166  */ 
00167 template <typename InputRange, typename Predicate> inline
00168 typename std::iterator_traits<typename InputRange::iterator>::difference_type
00169 count_if_r(const InputRange& range, Predicate pred)
00170 {
00171     return std::count_if(range.begin(), range.end(), pred);
00172 }
00173 
00174 /** std::mismatch wrapper for ranges
00175  *  @ingroup range_algorithm 
00176  */ 
00177 template <typename InputRange1, typename InputRange2, typename BinaryPredicate> inline
00178 std::pair<typename InputRange1::iterator, typename InputRange2::iterator>
00179 mismatch_r(const InputRange1& range1, const InputRange2& range2, BinaryPredicate pred)
00180 {
00181     return std::mismatch(range1.begin(), range1.end(), range2.begin(), range2.end(), pred);
00182 }
00183 
00184 /** std::mismatch wrapper for ranges
00185  *  @ingroup range_algorithm 
00186  */ 
00187 template <typename InputRange1, typename InputRange2> inline
00188 std::pair<typename InputRange1::iterator, typename InputRange2::iterator>
00189 mismatch_r(const InputRange1& range1, const InputRange2& range2)
00190 {
00191     return std::mismatch(range1.begin(), range1.end(), range2.begin(), range2.end());
00192 }
00193 
00194 /** std::mismatch wrapper for ranges
00195  *  @ingroup range_algorithm 
00196  */ 
00197 template <typename InputRange1, typename InputRange2> inline
00198 bool equal_r(const InputRange1& range1, const InputRange2& range2)
00199 {
00200     return std::equal(range1.begin(), range1.end(), range2.begin());
00201 }
00202 
00203 /** std::mismatch wrapper for ranges
00204  *  @ingroup range_algorithm 
00205  */ 
00206 template <typename InputRange1, typename InputRange2, typename BinaryPredicate> inline
00207 bool equal_r(const InputRange1& range1, const InputRange2& range2, BinaryPredicate pred)
00208 {
00209     return std::equal(range1.begin(), range1.end(), range2.begin(), pred);
00210 }
00211 
00212 /** std::search wrapper for ranges
00213  *  @ingroup range_algorithm 
00214  */ 
00215 template <typename ForwardRange1, typename ForwardRange2> inline
00216 typename ForwardRange1::iterator 
00217 search_r(const ForwardRange1& range1,const ForwardRange2& range2)
00218 {
00219     return std::search(range1.begin(), range1.end(), range2.begin(), range2.end());
00220 }
00221 
00222 /** std::search wrapper for ranges
00223  *  @ingroup range_algorithm 
00224  */ 
00225 template <typename ForwardRange1, typename ForwardRange2, typename BinaryPredicate> inline
00226 typename ForwardRange1::iterator 
00227 search_r(const ForwardRange1& range1,const ForwardRange2& range2, BinaryPredicate pred)
00228 {
00229     return std::search(range1.begin(), range1.end(), range2.begin(), range2.end(), pred);
00230 }
00231 
00232 /** std::search_n wrapper for ranges
00233  *  @ingroup range_algorithm 
00234  */ 
00235 template <typename ForwardRange, typename Size, typename T> inline
00236 typename ForwardRange::iterator 
00237 search_n_r(const ForwardRange& range, Size count, const T& value)
00238 {
00239     return std::search_n(range.begin(), range.end(), count, value);
00240 }
00241 
00242 /** std::search_n wrapper for ranges
00243  *  @ingroup range_algorithm 
00244  */ 
00245 template <typename ForwardRange, typename Size, typename T, typename BinaryPredicate> inline
00246 typename ForwardRange::iterator 
00247 search_n_r(const ForwardRange& range, Size count, const T& value, BinaryPredicate pred)
00248 {
00249     return std::search_n(range.begin(), range.end(), count, value, pred);
00250 }
00251 
00252 /** std::copy wrapper for ranges
00253  *  @ingroup range_algorithm 
00254  */ 
00255 template <typename InputRange, typename OutputIterator> inline
00256 OutputIterator copy_r(const InputRange& range, OutputIterator result)
00257 {
00258     return std::copy(range.begin(), range.end(), result);
00259 }
00260 
00261 /** std::copy_backward wrapper for ranges
00262  *  @ingroup range_algorithm 
00263  */ 
00264 template <typename BidirectionalRange, typename BidirectionalIterator> inline
00265 BidirectionalIterator copy_backward_r(const BidirectionalRange& range, BidirectionalIterator result)
00266 {
00267     return std::copy_backward(range.begin(), range.end(), result);
00268 }
00269 
00270 /** std::swap_ranges wrapper for ranges
00271  *  @ingroup range_algorithm 
00272  */ 
00273 template <typename ForwardRange1, typename ForwardRange2> inline
00274 typename ForwardRange2::iterator
00275 swap_ranges_r(ForwardRange1& range1, ForwardRange2& range2)
00276 {
00277     return std::swap_ranges(range1.begin(), range1.end(), range2.begin());
00278 }
00279 
00280 /** std::transform wrapper for ranges
00281  *  @ingroup range_algorithm 
00282  */ 
00283 template <typename InputRange, typename OutputIterator, typename UnaryOperation> inline
00284 OutputIterator transform_r(const InputRange& range, OutputIterator result, UnaryOperation op)
00285 {
00286     return std::transform(range.begin(), range.end(), result, op);
00287 }
00288 
00289 /** std::transform wrapper for ranges
00290  *  @ingroup range_algorithm 
00291  */ 
00292 template <typename InputRange1, typename InputRange2, typename OutputIterator, typename BinaryOperation> inline
00293 OutputIterator transform_r(const InputRange1& range1, const InputRange2& range2, OutputIterator result, BinaryOperation op)
00294 {
00295     return std::transform(range1.begin(), range1.end(), range2.begin(), range2.end(), result, op);
00296 }
00297 
00298 /** std::replace wrapper for ranges
00299  *  @ingroup range_algorithm 
00300  */ 
00301 template <typename ForwardRange, typename T> inline
00302 void replace_r(ForwardRange& range, const T& old_value, const T& new_value)
00303 {
00304     std::replace(range.begin(), range.end(), old_value, new_value);
00305 }
00306 
00307 /** std::replace_if wrapper for ranges
00308  *  @ingroup range_algorithm 
00309  */ 
00310 template <typename ForwardRange, typename Predicate, typename T> inline
00311 void replace_if_r(ForwardRange& range, Predicate pred, const T& new_value)
00312 {
00313     std::replace_if(range.begin(), range.end(), pred, new_value);
00314 }
00315 
00316 /** std::replace_copy wrapper for ranges
00317  *  @ingroup range_algorithm 
00318  */ 
00319 template <typename InputRange, typename OutputIterator, typename T> inline
00320 OutputIterator replace_copy_r(const InputRange& range, OutputIterator result, const T& old_value, const T& new_value)
00321 {
00322     return std::replace(range.begin(), range.end(), result, old_value, new_value);
00323 }
00324 
00325 /** std::replace_copy_if wrapper for ranges
00326  *  @ingroup range_algorithm 
00327  */ 
00328 template <typename InputRange, typename OutputIterator, typename Predicate, typename T> inline
00329 OutputIterator replace_copy_if_r(const InputRange& range, OutputIterator result, Predicate pred, const T& new_value)
00330 {
00331     return std::replace_if(range.begin(), range.end(), result, pred, new_value);
00332 }
00333 
00334 /** std::fill wrapper for ranges
00335  *  @ingroup range_algorithm 
00336  */ 
00337 template <typename ForwardRange, typename T> inline
00338 void fill_r(ForwardRange& range, const T& value)
00339 {
00340     std::fill(range.begin(), range.end(), value);
00341 }
00342 
00343 /** std::generate wrapper for ranges
00344  *  @ingroup range_algorithm 
00345  */ 
00346 template <typename ForwardRange, typename Generator> inline
00347 void generate_r(ForwardRange& range, Generator gen)
00348 {
00349     std::generate(range.begin(), range.end(), gen);
00350 }
00351 
00352 /** std::remove wrapper for ranges
00353  *  @ingroup range_algorithm 
00354  */ 
00355 template <typename ForwardRange, typename T> inline
00356 typename ForwardRange::iterator
00357 remove_r(ForwardRange& range, const T& value)
00358 {
00359     return std::remove(range.begin(), range.end(), value);
00360 }
00361 
00362 /** std::remove wrapper for ranges
00363  *  @ingroup range_algorithm 
00364  */ 
00365 template <typename ForwardRange, typename Predicate> inline
00366 typename ForwardRange::iterator
00367 remove_if_r(ForwardRange& range, Predicate pred)
00368 {
00369     return std::remove_if(range.begin(), range.end(), pred);
00370 }
00371 
00372 /** std::remove_copy wrapper for ranges
00373  *  @ingroup range_algorithm 
00374  */ 
00375 template <typename InputRange, typename OutputIterator, typename T> inline
00376 OutputIterator remove_copy_r(const InputRange& range, OutputIterator result, const T& value)
00377 {
00378     return std::remove_copy(range.begin(), range.end(), result, value);
00379 }
00380 
00381 /** std::remove_copy wrapper for ranges
00382  *  @ingroup range_algorithm 
00383  */ 
00384 template <typename InputRange, typename OutputIterator, typename Predicate> inline
00385 OutputIterator remove_copy_if_r(const InputRange& range, OutputIterator result, Predicate pred)
00386 {
00387     return std::remove_copy_if(range.begin(), range.end(), result, pred);
00388 }
00389 
00390 /** std::unique wrapper for ranges
00391  *  @ingroup range_algorithm 
00392  */ 
00393 template <typename ForwardRange> inline
00394 typename ForwardRange::iterator
00395 unique_r(ForwardRange& range)
00396 {
00397     return std::unique(range.begin(), range.end());
00398 }
00399 
00400 /** std::unique wrapper for ranges
00401  *  @ingroup range_algorithm 
00402  */ 
00403 template <typename ForwardRange, typename Predicate> inline
00404 typename ForwardRange::iterator
00405 unique_r(ForwardRange& range, Predicate pred)
00406 {
00407     return std::unique(range.begin(), range.end(), pred);
00408 }
00409 
00410 /** std::unique_copy_r wrapper for ranges
00411  *  @ingroup range_algorithm 
00412  */ 
00413 template <typename InputRange, typename OutputIterator> inline
00414 OutputIterator unique_copy_r(const InputRange& range, OutputIterator result)
00415 {
00416     return std::unique_copy(range.begin(), range.end(), result);
00417 }
00418 
00419 /** std::unique_copy_r wrapper for ranges
00420  *  @ingroup range_algorithm 
00421  */ 
00422 template <typename InputRange, typename OutputIterator, typename Predicate> inline
00423 OutputIterator unique_copy_r(const InputRange& range, OutputIterator result, Predicate pred)
00424 {
00425     return std::unique_copy(range.begin(), range.end(), result, pred);
00426 }
00427 
00428 /** std::reverse wrapper for ranges
00429  *  @ingroup range_algorithm 
00430  */ 
00431 template <typename BidirectionalRange> inline
00432 void reverse_r(BidirectionalRange& range)
00433 {
00434     std::reverse(range.begin(), range.end());
00435 }
00436 
00437 /** std::reverse_copy wrapper for ranges
00438  *  @ingroup range_algorithm 
00439  */ 
00440 template <typename BidirectionalRange, typename OutputIterator> inline
00441 OutputIterator reverse_copy_r(const BidirectionalRange& range, OutputIterator result)
00442 {
00443     return std::reverse_copy(range.begin(), range.end(), result);
00444 }
00445 
00446 /** std::rotate wrapper for ranges
00447  *  @ingroup range_algorithm 
00448  */ 
00449 template <typename ForwardRange> inline
00450 void rotate_r(ForwardRange& range, typename ForwardRange::iterator middle)
00451 {
00452     return std::rotate(range.begin(), middle, range.end());
00453 }
00454 
00455 /** std::rotate_copy wrapper for ranges
00456  *  @ingroup range_algorithm 
00457  */ 
00458 template <typename ForwardRange, typename OutputIterator> inline
00459 void rotate_copy_r(const ForwardRange& range, typename ForwardRange::iterator middle, OutputIterator result)
00460 {
00461     return std::rotate_copy(range.begin(), middle, range.end(), result);
00462 }
00463 
00464 /** std::random_shuffle wrapper for ranges
00465  *  @ingroup range_algorithm 
00466  */ 
00467 template <typename RandomAccessRange> inline
00468 void random_shuffle_r(RandomAccessRange& range)
00469 {
00470     return std::random_shuffle(range.begin(), range.end());
00471 }
00472 
00473 /** std::random_shuffle wrapper for ranges
00474  *  @ingroup range_algorithm
00475  *  @pre @a rand must take a value n so that it returns a value in the range [0, n)
00476  */ 
00477 template <typename RandomAccessRange, typename RandomNumberGenerator> inline
00478 void random_shuffle_r(RandomAccessRange& range, RandomNumberGenerator& rand)
00479 {
00480     return std::random_shuffle(range.begin(), range.end(), rand);
00481 }
00482 
00483 /** std::partition wrapper for ranges
00484  *  @ingroup range_algorithm
00485  */
00486 template <typename BidirectionalRange, typename Predicate>
00487 typename BidirectionalRange::iterator 
00488 partition_r(BidirectionalRange& range, Predicate pred)
00489 {
00490     return std::partition(range.begin(), range.end(), pred);
00491 }
00492 
00493 /** std::stable_partition wrapper for ranges
00494  *  @ingroup range_algorithm
00495  */
00496 template <typename BidirectionalRange, typename Predicate>
00497 typename BidirectionalRange::iterator 
00498 stable_partition_r(BidirectionalRange& range, Predicate pred)
00499 {
00500     return std::stable_partition(range.begin(), range.end(), pred);
00501 }
00502 
00503 }
00504 
00505 }
00506 
00507 #if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC
00508 #   pragma warning(pop)
00509 #endif
00510 
00511 #endif
00512 
00513 // EOF

Generated on Mon Nov 10 14:21:07 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo