BVB Source Codes

mars Show is_sorted.hpp Source code

Return Download mars: download is_sorted.hpp Source code - Download mars Source code - Type:.hpp
  1. //  Copyright (c) 2010 Nuovation System Designs, LLC
  2. //    Grant Erickson <gerickson@nuovations.com>
  3. //
  4. //  Reworked somewhat by Marshall Clow; August 2010
  5. //  
  6. //  Distributed under the Boost Software License, Version 1.0. (See
  7. //  accompanying file LICENSE_1_0.txt or copy at
  8. //  http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. //  See http://www.boost.org/ for latest version.
  11. //
  12.  
  13. #ifndef BOOST_ALGORITHM_ORDERED_HPP
  14. #define BOOST_ALGORITHM_ORDERED_HPP
  15.  
  16. #include <algorithm>
  17. #include <functional>
  18. #include <iterator>
  19.  
  20. #include <boost/range/begin.hpp>
  21. #include <boost/range/end.hpp>
  22.  
  23. #include <boost/utility/enable_if.hpp>
  24. #include <boost/type_traits/is_same.hpp>
  25. #include <boost/mpl/identity.hpp>
  26.  
  27. namespace mars_boost {} namespace boost = mars_boost; namespace mars_boost {  namespace algorithm {
  28.  
  29. #if __cplusplus >= 201103L
  30. //  Use the C++11 versions of is_sorted/is_sorted_until if they are available
  31. using std::is_sorted_until; // Section 25.4.1.5
  32. using std::is_sorted;       // Section 25.4.1.5
  33. #else
  34. /// \fn is_sorted_until ( ForwardIterator first, ForwardIterator last, Pred p )
  35. /// \return the point in the sequence [first, last) where the elements are unordered
  36. ///     (according to the comparison predicate 'p').
  37. ///
  38. /// \param first The start of the sequence to be tested.
  39. /// \param last  One past the end of the sequence
  40. /// \param p     A binary predicate that returns true if two elements are ordered.
  41. ///
  42.     template <typename ForwardIterator, typename Pred>
  43.     ForwardIterator is_sorted_until ( ForwardIterator first, ForwardIterator last, Pred p )
  44.     {
  45.         if ( first == last ) return last;  // the empty sequence is ordered
  46.         ForwardIterator next = first;
  47.         while ( ++next != last )
  48.         {
  49.             if ( p ( *next, *first ))
  50.                 return next;
  51.             first = next;
  52.         }
  53.         return last;    
  54.     }
  55.  
  56. /// \fn is_sorted_until ( ForwardIterator first, ForwardIterator last )
  57. /// \return the point in the sequence [first, last) where the elements are unordered
  58. ///
  59. /// \param first The start of the sequence to be tested.
  60. /// \param last  One past the end of the sequence
  61. ///
  62.     template <typename ForwardIterator>
  63.     ForwardIterator is_sorted_until ( ForwardIterator first, ForwardIterator last )
  64.     {
  65.         typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
  66.         return mars_boost::algorithm::is_sorted_until ( first, last, std::less<value_type>());
  67.     }
  68.  
  69.  
  70. /// \fn is_sorted ( ForwardIterator first, ForwardIterator last, Pred p )
  71. /// \return whether or not the entire sequence is sorted
  72. ///
  73. /// \param first The start of the sequence to be tested.
  74. /// \param last  One past the end of the sequence
  75. /// \param p     A binary predicate that returns true if two elements are ordered.
  76. ///
  77.     template <typename ForwardIterator, typename Pred>
  78.     bool is_sorted ( ForwardIterator first, ForwardIterator last, Pred p )
  79.     {
  80.         return mars_boost::algorithm::is_sorted_until (first, last, p) == last;
  81.     }
  82.  
  83. /// \fn is_sorted ( ForwardIterator first, ForwardIterator last )
  84. /// \return whether or not the entire sequence is sorted
  85. ///
  86. /// \param first The start of the sequence to be tested.
  87. /// \param last  One past the end of the sequence
  88. ///
  89.     template <typename ForwardIterator>
  90.     bool is_sorted ( ForwardIterator first, ForwardIterator last )
  91.     {
  92.         return mars_boost::algorithm::is_sorted_until (first, last) == last;
  93.     }
  94. #endif
  95.  
  96. ///
  97. /// -- Range based versions of the C++11 functions
  98. ///
  99.  
  100. /// \fn is_sorted_until ( const R &range, Pred p )
  101. /// \return the point in the range R where the elements are unordered
  102. ///     (according to the comparison predicate 'p').
  103. ///
  104. /// \param range The range to be tested.
  105. /// \param p     A binary predicate that returns true if two elements are ordered.
  106. ///
  107.     template <typename R, typename Pred>
  108.     typename mars_boost::lazy_disable_if_c<
  109.         mars_boost::is_same<R, Pred>::value,
  110.         typename mars_boost::range_iterator<const R>
  111.     >::type is_sorted_until ( const R &range, Pred p )
  112.     {
  113.         return mars_boost::algorithm::is_sorted_until ( mars_boost::begin ( range ), mars_boost::end ( range ), p );
  114.     }
  115.  
  116.  
  117. /// \fn is_sorted_until ( const R &range )
  118. /// \return the point in the range R where the elements are unordered
  119. ///
  120. /// \param range The range to be tested.
  121. ///
  122.     template <typename R>
  123.     typename mars_boost::range_iterator<const R>::type is_sorted_until ( const R &range )
  124.     {
  125.         return mars_boost::algorithm::is_sorted_until ( mars_boost::begin ( range ), mars_boost::end ( range ));
  126.     }
  127.  
  128. /// \fn is_sorted ( const R &range, Pred p )
  129. /// \return whether or not the entire range R is sorted
  130. ///     (according to the comparison predicate 'p').
  131. ///
  132. /// \param range The range to be tested.
  133. /// \param p     A binary predicate that returns true if two elements are ordered.
  134. ///
  135.     template <typename R, typename Pred>
  136.     typename mars_boost::lazy_disable_if_c< mars_boost::is_same<R, Pred>::value, mars_boost::mpl::identity<bool> >::type
  137.     is_sorted ( const R &range, Pred p )
  138.     {
  139.         return mars_boost::algorithm::is_sorted ( mars_boost::begin ( range ), mars_boost::end ( range ), p );
  140.     }
  141.  
  142.  
  143. /// \fn is_sorted ( const R &range )
  144. /// \return whether or not the entire range R is sorted
  145. ///
  146. /// \param range The range to be tested.
  147. ///
  148.     template <typename R>
  149.     bool is_sorted ( const R &range )
  150.     {
  151.         return mars_boost::algorithm::is_sorted ( mars_boost::begin ( range ), mars_boost::end ( range ));
  152.     }
  153.  
  154.  
  155. ///
  156. /// -- Range based versions of the C++11 functions
  157. ///
  158.  
  159. /// \fn is_increasing ( ForwardIterator first, ForwardIterator last )
  160. /// \return true if the entire sequence is increasing; i.e, each item is greater than or  
  161. ///     equal to the previous one.
  162. ///
  163. /// \param first The start of the sequence to be tested.
  164. /// \param last  One past the end of the sequence
  165. ///
  166. /// \note This function will return true for sequences that contain items that compare
  167. ///     equal. If that is not what you intended, you should use is_strictly_increasing instead.
  168.     template <typename ForwardIterator>
  169.     bool is_increasing ( ForwardIterator first, ForwardIterator last )
  170.     {
  171.         typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
  172.         return mars_boost::algorithm::is_sorted (first, last, std::less<value_type>());
  173.     }
  174.  
  175.  
  176. /// \fn is_increasing ( const R &range )
  177. /// \return true if the entire sequence is increasing; i.e, each item is greater than or  
  178. ///     equal to the previous one.
  179. ///
  180. /// \param range The range to be tested.
  181. ///
  182. /// \note This function will return true for sequences that contain items that compare
  183. ///     equal. If that is not what you intended, you should use is_strictly_increasing instead.
  184.     template <typename R>
  185.     bool is_increasing ( const R &range )
  186.     {
  187.         return is_increasing ( mars_boost::begin ( range ), mars_boost::end ( range ));
  188.     }
  189.  
  190.  
  191.  
  192. /// \fn is_decreasing ( ForwardIterator first, ForwardIterator last )
  193. /// \return true if the entire sequence is decreasing; i.e, each item is less than
  194. ///     or equal to the previous one.
  195. ///
  196. /// \param first The start of the sequence to be tested.
  197. /// \param last  One past the end of the sequence
  198. ///
  199. /// \note This function will return true for sequences that contain items that compare
  200. ///     equal. If that is not what you intended, you should use is_strictly_decreasing instead.
  201.     template <typename ForwardIterator>
  202.     bool is_decreasing ( ForwardIterator first, ForwardIterator last )
  203.     {
  204.         typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
  205.         return mars_boost::algorithm::is_sorted (first, last, std::greater<value_type>());
  206.     }
  207.  
  208. /// \fn is_decreasing ( const R &range )
  209. /// \return true if the entire sequence is decreasing; i.e, each item is less than
  210. ///     or equal to the previous one.
  211. ///
  212. /// \param range The range to be tested.
  213. ///
  214. /// \note This function will return true for sequences that contain items that compare
  215. ///     equal. If that is not what you intended, you should use is_strictly_decreasing instead.
  216.     template <typename R>
  217.     bool is_decreasing ( const R &range )
  218.     {
  219.         return is_decreasing ( mars_boost::begin ( range ), mars_boost::end ( range ));
  220.     }
  221.  
  222.  
  223.  
  224. /// \fn is_strictly_increasing ( ForwardIterator first, ForwardIterator last )
  225. /// \return true if the entire sequence is strictly increasing; i.e, each item is greater
  226. ///     than the previous one
  227. ///
  228. /// \param first The start of the sequence to be tested.
  229. /// \param last  One past the end of the sequence
  230. ///
  231. /// \note This function will return false for sequences that contain items that compare
  232. ///     equal. If that is not what you intended, you should use is_increasing instead.
  233.     template <typename ForwardIterator>
  234.     bool is_strictly_increasing ( ForwardIterator first, ForwardIterator last )
  235.     {
  236.         typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
  237.         return mars_boost::algorithm::is_sorted (first, last, std::less_equal<value_type>());
  238.     }
  239.  
  240. /// \fn is_strictly_increasing ( const R &range )
  241. /// \return true if the entire sequence is strictly increasing; i.e, each item is greater
  242. ///     than the previous one
  243. ///
  244. /// \param range The range to be tested.
  245. ///
  246. /// \note This function will return false for sequences that contain items that compare
  247. ///     equal. If that is not what you intended, you should use is_increasing instead.
  248.     template <typename R>
  249.     bool is_strictly_increasing ( const R &range )
  250.     {
  251.         return is_strictly_increasing ( mars_boost::begin ( range ), mars_boost::end ( range ));
  252.     }
  253.  
  254.  
  255. /// \fn is_strictly_decreasing ( ForwardIterator first, ForwardIterator last )
  256. /// \return true if the entire sequence is strictly decreasing; i.e, each item is less than
  257. ///     the previous one
  258. ///
  259. /// \param first The start of the sequence to be tested.
  260. /// \param last  One past the end of the sequence
  261. ///
  262. /// \note This function will return false for sequences that contain items that compare
  263. ///     equal. If that is not what you intended, you should use is_decreasing instead.
  264.     template <typename ForwardIterator>
  265.     bool is_strictly_decreasing ( ForwardIterator first, ForwardIterator last )
  266.     {
  267.         typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
  268.         return mars_boost::algorithm::is_sorted (first, last, std::greater_equal<value_type>());
  269.     }
  270.  
  271. /// \fn is_strictly_decreasing ( const R &range )
  272. /// \return true if the entire sequence is strictly decreasing; i.e, each item is less than
  273. ///     the previous one
  274. ///
  275. /// \param range The range to be tested.
  276. ///
  277. /// \note This function will return false for sequences that contain items that compare
  278. ///     equal. If that is not what you intended, you should use is_decreasing instead.
  279.     template <typename R>
  280.     bool is_strictly_decreasing ( const R &range )
  281.     {
  282.         return is_strictly_decreasing ( mars_boost::begin ( range ), mars_boost::end ( range ));
  283.     }
  284.  
  285. }} // namespace boost
  286.  
  287. #endif  // BOOST_ALGORITHM_ORDERED_HPP
  288.  
downloadis_sorted.hpp Source code - Download mars Source code
Related Source Codes/Software:
Hero - Elegant transition library for iOS & tvOS 2017-06-09
deep-photo-styletransfer - Code and data for paper "Deep Photo Style Transfer... 2017-06-09
mastodon - A GNU Social-compatible microblogging server ... 2017-06-09
plyr - A simple HTML5, YouTube and Vimeo player ... 2017-06-08
prepack - Prepack is a partial evaluator for JavaScript. Pre... 2017-06-08
Public-APIs - 2017-06-09
lottie-ios - An iOS library to natively render After Effects ve... 2017-06-09
Awesome-Hacking - A collection of various awesome lists for hackers,... 2017-06-09
algorithms - Minimal examples of data structures and algorithms... 2017-06-10
lectures - Oxford Deep NLP 2017 course 2017-06-10
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top