iterator_range_core.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves & Thorsten Ottosen & Pavol Droba 2003-2004.
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see http://www.boost.org/libs/range/
  9. //
  10. // Credits:
  11. // 'michel' reported Trac 9072 which included a patch for allowing references
  12. // to function types.
  13. //
  14. #ifndef BOOST_RANGE_ITERATOR_RANGE_CORE_HPP_INCLUDED
  15. #define BOOST_RANGE_ITERATOR_RANGE_CORE_HPP_INCLUDED
  16. #include <boost/config.hpp> // Define __STL_CONFIG_H, if appropriate.
  17. #include <boost/detail/workaround.hpp>
  18. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))
  19. #pragma warning( push )
  20. #pragma warning( disable : 4996 )
  21. #endif
  22. #include <boost/assert.hpp>
  23. #include <boost/iterator/iterator_traits.hpp>
  24. #include <boost/iterator/iterator_facade.hpp>
  25. #include <boost/mpl/if.hpp>
  26. #include <boost/mpl/not.hpp>
  27. #include <boost/mpl/or.hpp>
  28. #include <boost/type_traits/is_abstract.hpp>
  29. #include <boost/type_traits/is_array.hpp>
  30. #include <boost/type_traits/is_base_and_derived.hpp>
  31. #include <boost/type_traits/is_convertible.hpp>
  32. #include <boost/type_traits/is_function.hpp>
  33. #include <boost/type_traits/is_pointer.hpp>
  34. #include <boost/type_traits/is_same.hpp>
  35. #include <boost/range/functions.hpp>
  36. #include <boost/range/iterator.hpp>
  37. #include <boost/range/difference_type.hpp>
  38. #include <boost/range/has_range_iterator.hpp>
  39. #include <boost/range/algorithm/equal.hpp>
  40. #include <boost/range/detail/safe_bool.hpp>
  41. #include <boost/utility/enable_if.hpp>
  42. #include <boost/next_prior.hpp>
  43. #include <iterator>
  44. #include <algorithm>
  45. #include <cstddef>
  46. /*! \file
  47. Defines the \c iterator_class and related functions.
  48. \c iterator_range is a simple wrapper of iterator pair idiom. It provides
  49. a rich subset of Container interface.
  50. */
  51. namespace boost
  52. {
  53. namespace iterator_range_detail
  54. {
  55. //
  56. // The functions adl_begin and adl_end are implemented in a separate
  57. // class for gcc-2.9x
  58. //
  59. template<class IteratorT>
  60. struct iterator_range_impl {
  61. template< class ForwardRange >
  62. static IteratorT adl_begin( ForwardRange& r )
  63. {
  64. return IteratorT( boost::begin( r ) );
  65. }
  66. template< class ForwardRange >
  67. static IteratorT adl_end( ForwardRange& r )
  68. {
  69. return IteratorT( boost::end( r ) );
  70. }
  71. };
  72. template< class Left, class Right >
  73. inline bool less_than( const Left& l, const Right& r )
  74. {
  75. return std::lexicographical_compare( boost::begin(l),
  76. boost::end(l),
  77. boost::begin(r),
  78. boost::end(r) );
  79. }
  80. template< class Left, class Right >
  81. inline bool greater_than( const Left& l, const Right& r )
  82. {
  83. return iterator_range_detail::less_than(r,l);
  84. }
  85. template< class Left, class Right >
  86. inline bool less_or_equal_than( const Left& l, const Right& r )
  87. {
  88. return !iterator_range_detail::less_than(r,l);
  89. }
  90. template< class Left, class Right >
  91. inline bool greater_or_equal_than( const Left& l, const Right& r )
  92. {
  93. return !iterator_range_detail::less_than(l,r);
  94. }
  95. // This version is maintained since it is used in other boost libraries
  96. // such as Boost.Assign
  97. template< class Left, class Right >
  98. inline bool equal(const Left& l, const Right& r)
  99. {
  100. return boost::equal(l, r);
  101. }
  102. struct range_tag
  103. {
  104. };
  105. struct const_range_tag
  106. {
  107. };
  108. struct iterator_range_tag
  109. {
  110. };
  111. typedef char (&incrementable_t)[1];
  112. typedef char (&bidirectional_t)[2];
  113. typedef char (&random_access_t)[3];
  114. incrementable_t test_traversal_tag(boost::incrementable_traversal_tag);
  115. bidirectional_t test_traversal_tag(boost::bidirectional_traversal_tag);
  116. random_access_t test_traversal_tag(boost::random_access_traversal_tag);
  117. template<std::size_t S>
  118. struct pure_iterator_traversal_impl
  119. {
  120. typedef boost::incrementable_traversal_tag type;
  121. };
  122. template<>
  123. struct pure_iterator_traversal_impl<sizeof(bidirectional_t)>
  124. {
  125. typedef boost::bidirectional_traversal_tag type;
  126. };
  127. template<>
  128. struct pure_iterator_traversal_impl<sizeof(random_access_t)>
  129. {
  130. typedef boost::random_access_traversal_tag type;
  131. };
  132. template<typename IteratorT>
  133. struct pure_iterator_traversal
  134. {
  135. typedef
  136. BOOST_DEDUCED_TYPENAME iterator_traversal<IteratorT>::type
  137. traversal_t;
  138. BOOST_STATIC_CONSTANT(
  139. std::size_t,
  140. traversal_i = sizeof(iterator_range_detail::test_traversal_tag((traversal_t())))
  141. );
  142. typedef
  143. BOOST_DEDUCED_TYPENAME pure_iterator_traversal_impl<traversal_i>::type
  144. type;
  145. };
  146. template<class IteratorT, class TraversalTag>
  147. class iterator_range_base
  148. : public iterator_range_tag
  149. {
  150. typedef range_detail::safe_bool<
  151. IteratorT iterator_range_base<IteratorT, TraversalTag>::*
  152. > safe_bool_t;
  153. typedef iterator_range_base<IteratorT, TraversalTag> type;
  154. protected:
  155. typedef iterator_range_impl<IteratorT> impl;
  156. public:
  157. typedef BOOST_DEDUCED_TYPENAME
  158. safe_bool_t::unspecified_bool_type unspecified_bool_type;
  159. typedef BOOST_DEDUCED_TYPENAME
  160. iterator_value<IteratorT>::type value_type;
  161. typedef BOOST_DEDUCED_TYPENAME
  162. iterator_difference<IteratorT>::type difference_type;
  163. typedef std::size_t size_type; // note: must be unsigned
  164. // Needed because value-type is the same for
  165. // const and non-const iterators
  166. typedef BOOST_DEDUCED_TYPENAME
  167. iterator_reference<IteratorT>::type reference;
  168. //! const_iterator type
  169. /*!
  170. There is no distinction between const_iterator and iterator.
  171. These typedefs are provides to fulfill container interface
  172. */
  173. typedef IteratorT const_iterator;
  174. //! iterator type
  175. typedef IteratorT iterator;
  176. protected:
  177. iterator_range_base()
  178. : m_Begin()
  179. , m_End()
  180. {
  181. }
  182. template<class Iterator>
  183. iterator_range_base(Iterator Begin, Iterator End)
  184. : m_Begin(Begin)
  185. , m_End(End)
  186. {
  187. }
  188. public:
  189. IteratorT begin() const
  190. {
  191. return m_Begin;
  192. }
  193. IteratorT end() const
  194. {
  195. return m_End;
  196. }
  197. bool empty() const
  198. {
  199. return m_Begin == m_End;
  200. }
  201. operator unspecified_bool_type() const
  202. {
  203. return safe_bool_t::to_unspecified_bool(
  204. m_Begin != m_End, &iterator_range_base::m_Begin);
  205. }
  206. bool operator!() const
  207. {
  208. return empty();
  209. }
  210. bool equal(const iterator_range_base& r) const
  211. {
  212. return m_Begin == r.m_Begin && m_End == r.m_End;
  213. }
  214. reference front() const
  215. {
  216. BOOST_ASSERT(!empty());
  217. return *m_Begin;
  218. }
  219. void drop_front()
  220. {
  221. BOOST_ASSERT(!empty());
  222. ++m_Begin;
  223. }
  224. void drop_front(difference_type n)
  225. {
  226. BOOST_ASSERT(n >= difference_type());
  227. std::advance(this->m_Begin, n);
  228. }
  229. // Deprecated
  230. void pop_front() { drop_front(); }
  231. protected:
  232. template<class Iterator>
  233. void assign(Iterator first, Iterator last)
  234. {
  235. m_Begin = first;
  236. m_End = last;
  237. }
  238. template<class SinglePassRange>
  239. void assign(const SinglePassRange& r)
  240. {
  241. m_Begin = impl::adl_begin(r);
  242. m_End = impl::adl_end(r);
  243. }
  244. template<class SinglePassRange>
  245. void assign(SinglePassRange& r)
  246. {
  247. m_Begin = impl::adl_begin(r);
  248. m_End = impl::adl_end(r);
  249. }
  250. IteratorT m_Begin;
  251. IteratorT m_End;
  252. };
  253. template<class IteratorT>
  254. class iterator_range_base<IteratorT, bidirectional_traversal_tag>
  255. : public iterator_range_base<IteratorT, incrementable_traversal_tag>
  256. {
  257. typedef iterator_range_base<IteratorT, incrementable_traversal_tag> base_type;
  258. protected:
  259. iterator_range_base()
  260. {
  261. }
  262. template<class Iterator>
  263. iterator_range_base(Iterator first, Iterator last)
  264. : base_type(first, last)
  265. {
  266. }
  267. public:
  268. typedef BOOST_DEDUCED_TYPENAME base_type::difference_type difference_type;
  269. typedef BOOST_DEDUCED_TYPENAME base_type::reference reference;
  270. reference back() const
  271. {
  272. BOOST_ASSERT(!this->empty());
  273. return *boost::prior(this->m_End);
  274. }
  275. void drop_back()
  276. {
  277. BOOST_ASSERT(!this->empty());
  278. --this->m_End;
  279. }
  280. void drop_back(difference_type n)
  281. {
  282. BOOST_ASSERT(n >= difference_type());
  283. std::advance(this->m_End, -n);
  284. }
  285. // Deprecated
  286. void pop_back() { drop_back(); }
  287. };
  288. template<class IteratorT>
  289. class iterator_range_base<IteratorT, random_access_traversal_tag>
  290. : public iterator_range_base<IteratorT, bidirectional_traversal_tag>
  291. {
  292. typedef iterator_range_base<
  293. IteratorT, bidirectional_traversal_tag> base_type;
  294. public:
  295. typedef BOOST_DEDUCED_TYPENAME
  296. boost::mpl::if_<
  297. boost::mpl::or_<
  298. boost::is_abstract<
  299. BOOST_DEDUCED_TYPENAME base_type::value_type
  300. >,
  301. boost::is_array<
  302. BOOST_DEDUCED_TYPENAME base_type::value_type
  303. >,
  304. boost::is_function<
  305. BOOST_DEDUCED_TYPENAME base_type::value_type
  306. >
  307. >,
  308. BOOST_DEDUCED_TYPENAME base_type::reference,
  309. BOOST_DEDUCED_TYPENAME base_type::value_type
  310. >::type abstract_value_type;
  311. // Rationale:
  312. // typedef these here to reduce verbiage in the implementation of this
  313. // type.
  314. typedef BOOST_DEDUCED_TYPENAME base_type::difference_type difference_type;
  315. typedef BOOST_DEDUCED_TYPENAME base_type::size_type size_type;
  316. typedef BOOST_DEDUCED_TYPENAME base_type::reference reference;
  317. protected:
  318. iterator_range_base()
  319. {
  320. }
  321. template<class Iterator>
  322. iterator_range_base(Iterator first, Iterator last)
  323. : base_type(first, last)
  324. {
  325. }
  326. public:
  327. reference operator[](difference_type at) const
  328. {
  329. BOOST_ASSERT(at >= 0);
  330. BOOST_ASSERT(static_cast<typename base_type::size_type>(at) < size());
  331. return this->m_Begin[at];
  332. }
  333. //
  334. // When storing transform iterators, operator[]()
  335. // fails because it returns by reference. Therefore
  336. // operator()() is provided for these cases.
  337. //
  338. abstract_value_type operator()(difference_type at) const
  339. {
  340. BOOST_ASSERT(at >= 0);
  341. BOOST_ASSERT(static_cast<typename base_type::size_type>(at) < size());
  342. return this->m_Begin[at];
  343. }
  344. BOOST_DEDUCED_TYPENAME base_type::size_type size() const
  345. {
  346. return this->m_End - this->m_Begin;
  347. }
  348. };
  349. }
  350. // iterator range template class -----------------------------------------//
  351. //! iterator_range class
  352. /*!
  353. An \c iterator_range delimits a range in a sequence by beginning and ending iterators.
  354. An iterator_range can be passed to an algorithm which requires a sequence as an input.
  355. For example, the \c toupper() function may be used most frequently on strings,
  356. but can also be used on iterator_ranges:
  357. \code
  358. boost::tolower( find( s, "UPPERCASE STRING" ) );
  359. \endcode
  360. Many algorithms working with sequences take a pair of iterators,
  361. delimiting a working range, as an arguments. The \c iterator_range class is an
  362. encapsulation of a range identified by a pair of iterators.
  363. It provides a collection interface,
  364. so it is possible to pass an instance to an algorithm requiring a collection as an input.
  365. */
  366. template<class IteratorT>
  367. class iterator_range
  368. : public iterator_range_detail::iterator_range_base<
  369. IteratorT,
  370. BOOST_DEDUCED_TYPENAME iterator_range_detail::pure_iterator_traversal<IteratorT>::type
  371. >
  372. {
  373. typedef iterator_range_detail::iterator_range_base<
  374. IteratorT,
  375. BOOST_DEDUCED_TYPENAME iterator_range_detail::pure_iterator_traversal<IteratorT>::type
  376. > base_type;
  377. template<class Source>
  378. struct is_compatible_range_
  379. : is_convertible<
  380. BOOST_DEDUCED_TYPENAME mpl::eval_if<
  381. has_range_iterator<Source>,
  382. range_iterator<Source>,
  383. mpl::identity<void>
  384. >::type,
  385. BOOST_DEDUCED_TYPENAME base_type::iterator
  386. >
  387. {
  388. };
  389. template<class Source>
  390. struct is_compatible_range
  391. : mpl::and_<
  392. mpl::not_<
  393. is_convertible<
  394. Source,
  395. BOOST_DEDUCED_TYPENAME base_type::iterator
  396. >
  397. >,
  398. is_compatible_range_<Source>
  399. >
  400. {
  401. };
  402. protected:
  403. typedef iterator_range_detail::iterator_range_impl<IteratorT> impl;
  404. public:
  405. typedef iterator_range<IteratorT> type;
  406. iterator_range()
  407. {
  408. }
  409. template<class Iterator>
  410. iterator_range(Iterator first, Iterator last)
  411. : base_type(first, last)
  412. {
  413. }
  414. template<class SinglePassRange>
  415. iterator_range(
  416. const SinglePassRange& r,
  417. BOOST_DEDUCED_TYPENAME ::boost::enable_if<
  418. is_compatible_range<const SinglePassRange>
  419. >::type* = 0
  420. )
  421. : base_type(impl::adl_begin(r), impl::adl_end(r))
  422. {
  423. }
  424. template<class SinglePassRange>
  425. iterator_range(
  426. SinglePassRange& r,
  427. BOOST_DEDUCED_TYPENAME ::boost::enable_if<
  428. is_compatible_range<SinglePassRange>
  429. >::type* = 0
  430. )
  431. : base_type(impl::adl_begin(r), impl::adl_end(r))
  432. {
  433. }
  434. template<class SinglePassRange>
  435. iterator_range(const SinglePassRange& r,
  436. iterator_range_detail::const_range_tag)
  437. : base_type(impl::adl_begin(r), impl::adl_end(r))
  438. {
  439. }
  440. template<class SinglePassRange>
  441. iterator_range(SinglePassRange& r,
  442. iterator_range_detail::range_tag)
  443. : base_type(impl::adl_begin(r), impl::adl_end(r))
  444. {
  445. }
  446. template<class Iterator>
  447. iterator_range& operator=(const iterator_range<Iterator>& other)
  448. {
  449. this->assign(other.begin(), other.end());
  450. return *this;
  451. }
  452. template<class Iterator>
  453. iterator_range& operator=(iterator_range<Iterator>& other)
  454. {
  455. this->assign(other.begin(), other.end());
  456. return *this;
  457. }
  458. template<class SinglePassRange>
  459. iterator_range& operator=(SinglePassRange& r)
  460. {
  461. this->assign(r);
  462. return *this;
  463. }
  464. template<class SinglePassRange>
  465. iterator_range& operator=(const SinglePassRange& r)
  466. {
  467. this->assign(r);
  468. return *this;
  469. }
  470. iterator_range& advance_begin(
  471. BOOST_DEDUCED_TYPENAME base_type::difference_type n)
  472. {
  473. std::advance(this->m_Begin, n);
  474. return *this;
  475. }
  476. iterator_range& advance_end(
  477. BOOST_DEDUCED_TYPENAME base_type::difference_type n)
  478. {
  479. std::advance(this->m_End, n);
  480. return *this;
  481. }
  482. protected:
  483. //
  484. // Allow subclasses an easy way to access the
  485. // base type
  486. //
  487. typedef iterator_range iterator_range_;
  488. };
  489. // iterator range free-standing operators ---------------------------//
  490. /////////////////////////////////////////////////////////////////////
  491. // comparison operators
  492. /////////////////////////////////////////////////////////////////////
  493. template< class IteratorT, class ForwardRange >
  494. inline BOOST_DEDUCED_TYPENAME boost::enable_if<
  495. mpl::not_<is_base_and_derived<iterator_range_detail::iterator_range_tag, ForwardRange> >,
  496. bool
  497. >::type
  498. operator==( const ForwardRange& l, const iterator_range<IteratorT>& r )
  499. {
  500. return boost::equal( l, r );
  501. }
  502. template< class IteratorT, class ForwardRange >
  503. inline BOOST_DEDUCED_TYPENAME boost::enable_if<
  504. mpl::not_<boost::is_base_and_derived<iterator_range_detail::iterator_range_tag, ForwardRange> >,
  505. bool
  506. >::type
  507. operator!=( const ForwardRange& l, const iterator_range<IteratorT>& r )
  508. {
  509. return !boost::equal( l, r );
  510. }
  511. template< class IteratorT, class ForwardRange >
  512. inline BOOST_DEDUCED_TYPENAME boost::enable_if<
  513. mpl::not_<boost::is_base_and_derived<iterator_range_detail::iterator_range_tag, ForwardRange> >,
  514. bool
  515. >::type
  516. operator<( const ForwardRange& l, const iterator_range<IteratorT>& r )
  517. {
  518. return iterator_range_detail::less_than( l, r );
  519. }
  520. template< class IteratorT, class ForwardRange >
  521. inline BOOST_DEDUCED_TYPENAME boost::enable_if<
  522. mpl::not_<boost::is_base_and_derived<iterator_range_detail::iterator_range_tag, ForwardRange> >,
  523. bool
  524. >::type
  525. operator<=( const ForwardRange& l, const iterator_range<IteratorT>& r )
  526. {
  527. return iterator_range_detail::less_or_equal_than( l, r );
  528. }
  529. template< class IteratorT, class ForwardRange >
  530. inline BOOST_DEDUCED_TYPENAME boost::enable_if<
  531. mpl::not_<boost::is_base_and_derived<iterator_range_detail::iterator_range_tag, ForwardRange> >,
  532. bool
  533. >::type
  534. operator>( const ForwardRange& l, const iterator_range<IteratorT>& r )
  535. {
  536. return iterator_range_detail::greater_than( l, r );
  537. }
  538. template< class IteratorT, class ForwardRange >
  539. inline BOOST_DEDUCED_TYPENAME boost::enable_if<
  540. mpl::not_<boost::is_base_and_derived<iterator_range_detail::iterator_range_tag, ForwardRange> >,
  541. bool
  542. >::type
  543. operator>=( const ForwardRange& l, const iterator_range<IteratorT>& r )
  544. {
  545. return iterator_range_detail::greater_or_equal_than( l, r );
  546. }
  547. #ifdef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  548. #else
  549. template< class Iterator1T, class Iterator2T >
  550. inline bool
  551. operator==( const iterator_range<Iterator1T>& l, const iterator_range<Iterator2T>& r )
  552. {
  553. return boost::equal( l, r );
  554. }
  555. template< class IteratorT, class ForwardRange >
  556. inline BOOST_DEDUCED_TYPENAME boost::enable_if<
  557. mpl::not_<boost::is_base_and_derived<iterator_range_detail::iterator_range_tag, ForwardRange> >,
  558. bool
  559. >::type
  560. operator==( const iterator_range<IteratorT>& l, const ForwardRange& r )
  561. {
  562. return boost::equal( l, r );
  563. }
  564. template< class Iterator1T, class Iterator2T >
  565. inline bool
  566. operator!=( const iterator_range<Iterator1T>& l, const iterator_range<Iterator2T>& r )
  567. {
  568. return !boost::equal( l, r );
  569. }
  570. template< class IteratorT, class ForwardRange >
  571. inline BOOST_DEDUCED_TYPENAME boost::enable_if<
  572. mpl::not_<boost::is_base_and_derived<iterator_range_detail::iterator_range_tag, ForwardRange> >,
  573. bool
  574. >::type
  575. operator!=( const iterator_range<IteratorT>& l, const ForwardRange& r )
  576. {
  577. return !boost::equal( l, r );
  578. }
  579. template< class Iterator1T, class Iterator2T >
  580. inline bool
  581. operator<( const iterator_range<Iterator1T>& l, const iterator_range<Iterator2T>& r )
  582. {
  583. return iterator_range_detail::less_than( l, r );
  584. }
  585. template< class IteratorT, class ForwardRange >
  586. inline BOOST_DEDUCED_TYPENAME boost::enable_if<
  587. mpl::not_<boost::is_base_and_derived<iterator_range_detail::iterator_range_tag, ForwardRange> >,
  588. bool
  589. >::type
  590. operator<( const iterator_range<IteratorT>& l, const ForwardRange& r )
  591. {
  592. return iterator_range_detail::less_than( l, r );
  593. }
  594. template< class Iterator1T, class Iterator2T >
  595. inline bool
  596. operator<=( const iterator_range<Iterator1T>& l, const iterator_range<Iterator2T>& r )
  597. {
  598. return iterator_range_detail::less_or_equal_than( l, r );
  599. }
  600. template< class IteratorT, class ForwardRange >
  601. inline BOOST_DEDUCED_TYPENAME boost::enable_if<
  602. mpl::not_<boost::is_base_and_derived<iterator_range_detail::iterator_range_tag, ForwardRange> >,
  603. bool
  604. >::type
  605. operator<=( const iterator_range<IteratorT>& l, const ForwardRange& r )
  606. {
  607. return iterator_range_detail::less_or_equal_than( l, r );
  608. }
  609. template< class Iterator1T, class Iterator2T >
  610. inline bool
  611. operator>( const iterator_range<Iterator1T>& l, const iterator_range<Iterator2T>& r )
  612. {
  613. return iterator_range_detail::greater_than( l, r );
  614. }
  615. template< class IteratorT, class ForwardRange >
  616. inline BOOST_DEDUCED_TYPENAME boost::enable_if<
  617. mpl::not_<boost::is_base_and_derived<iterator_range_detail::iterator_range_tag, ForwardRange> >,
  618. bool
  619. >::type
  620. operator>( const iterator_range<IteratorT>& l, const ForwardRange& r )
  621. {
  622. return iterator_range_detail::greater_than( l, r );
  623. }
  624. template< class Iterator1T, class Iterator2T >
  625. inline bool
  626. operator>=( const iterator_range<Iterator1T>& l, const iterator_range<Iterator2T>& r )
  627. {
  628. return iterator_range_detail::greater_or_equal_than( l, r );
  629. }
  630. template< class IteratorT, class ForwardRange >
  631. inline BOOST_DEDUCED_TYPENAME boost::enable_if<
  632. mpl::not_<boost::is_base_and_derived<iterator_range_detail::iterator_range_tag, ForwardRange> >,
  633. bool
  634. >::type
  635. operator>=( const iterator_range<IteratorT>& l, const ForwardRange& r )
  636. {
  637. return iterator_range_detail::greater_or_equal_than( l, r );
  638. }
  639. #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  640. // iterator range utilities -----------------------------------------//
  641. //! iterator_range construct helper
  642. /*!
  643. Construct an \c iterator_range from a pair of iterators
  644. \param Begin A begin iterator
  645. \param End An end iterator
  646. \return iterator_range object
  647. */
  648. template< typename IteratorT >
  649. inline iterator_range< IteratorT >
  650. make_iterator_range( IteratorT Begin, IteratorT End )
  651. {
  652. return iterator_range<IteratorT>( Begin, End );
  653. }
  654. template<typename IteratorT, typename IntegerT>
  655. inline iterator_range<IteratorT>
  656. make_iterator_range_n(IteratorT first, IntegerT n)
  657. {
  658. return iterator_range<IteratorT>(first, boost::next(first, n));
  659. }
  660. #ifdef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  661. template< typename Range >
  662. inline iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<Range>::type >
  663. make_iterator_range( Range& r )
  664. {
  665. return iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<Range>::type >
  666. ( boost::begin( r ), boost::end( r ) );
  667. }
  668. #else
  669. //! iterator_range construct helper
  670. /*!
  671. Construct an \c iterator_range from a \c Range containing the begin
  672. and end iterators.
  673. */
  674. template< class ForwardRange >
  675. inline iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<ForwardRange>::type >
  676. make_iterator_range( ForwardRange& r )
  677. {
  678. return iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<ForwardRange>::type >
  679. ( r, iterator_range_detail::range_tag() );
  680. }
  681. template< class ForwardRange >
  682. inline iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<const ForwardRange>::type >
  683. make_iterator_range( const ForwardRange& r )
  684. {
  685. return iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<const ForwardRange>::type >
  686. ( r, iterator_range_detail::const_range_tag() );
  687. }
  688. #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  689. namespace iterator_range_detail
  690. {
  691. template< class Range >
  692. inline iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<Range>::type >
  693. make_range_impl( Range& r,
  694. BOOST_DEDUCED_TYPENAME range_difference<Range>::type advance_begin,
  695. BOOST_DEDUCED_TYPENAME range_difference<Range>::type advance_end )
  696. {
  697. //
  698. // Not worth the effort
  699. //
  700. //if( advance_begin == 0 && advance_end == 0 )
  701. // return make_iterator_range( r );
  702. //
  703. BOOST_DEDUCED_TYPENAME range_iterator<Range>::type
  704. new_begin = boost::begin( r ),
  705. new_end = boost::end( r );
  706. std::advance( new_begin, advance_begin );
  707. std::advance( new_end, advance_end );
  708. return make_iterator_range( new_begin, new_end );
  709. }
  710. }
  711. #ifdef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  712. template< class Range >
  713. inline iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<Range>::type >
  714. make_iterator_range( Range& r,
  715. BOOST_DEDUCED_TYPENAME range_difference<Range>::type advance_begin,
  716. BOOST_DEDUCED_TYPENAME range_difference<Range>::type advance_end )
  717. {
  718. return iterator_range_detail::make_range_impl( r, advance_begin, advance_end );
  719. }
  720. #else
  721. template< class Range >
  722. inline iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<Range>::type >
  723. make_iterator_range( Range& r,
  724. BOOST_DEDUCED_TYPENAME range_difference<Range>::type advance_begin,
  725. BOOST_DEDUCED_TYPENAME range_difference<Range>::type advance_end )
  726. {
  727. return iterator_range_detail::make_range_impl( r, advance_begin, advance_end );
  728. }
  729. template< class Range >
  730. inline iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<const Range>::type >
  731. make_iterator_range( const Range& r,
  732. BOOST_DEDUCED_TYPENAME range_difference<Range>::type advance_begin,
  733. BOOST_DEDUCED_TYPENAME range_difference<Range>::type advance_end )
  734. {
  735. return iterator_range_detail::make_range_impl( r, advance_begin, advance_end );
  736. }
  737. #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  738. //! copy a range into a sequence
  739. /*!
  740. Construct a new sequence of the specified type from the elements
  741. in the given range
  742. \param Range An input range
  743. \return New sequence
  744. */
  745. template< typename SeqT, typename Range >
  746. inline SeqT copy_range( const Range& r )
  747. {
  748. return SeqT( boost::begin( r ), boost::end( r ) );
  749. }
  750. } // namespace 'boost'
  751. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))
  752. #pragma warning( pop )
  753. #endif
  754. #endif