range.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2013, 2014, 2015, 2016, 2019.
  4. // Modifications copyright (c) 2013-2019 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_UTIL_RANGE_HPP
  10. #define BOOST_GEOMETRY_UTIL_RANGE_HPP
  11. #include <algorithm>
  12. #include <iterator>
  13. #include <boost/concept_check.hpp>
  14. #include <boost/config.hpp>
  15. #include <boost/core/addressof.hpp>
  16. #include <boost/range/concepts.hpp>
  17. #include <boost/range/begin.hpp>
  18. #include <boost/range/end.hpp>
  19. #include <boost/range/empty.hpp>
  20. #include <boost/range/difference_type.hpp>
  21. #include <boost/range/iterator.hpp>
  22. #include <boost/range/rbegin.hpp>
  23. #include <boost/range/reference.hpp>
  24. #include <boost/range/size.hpp>
  25. #include <boost/range/value_type.hpp>
  26. #include <boost/type_traits/is_convertible.hpp>
  27. #include <boost/geometry/core/assert.hpp>
  28. #include <boost/geometry/core/mutable_range.hpp>
  29. namespace boost { namespace geometry { namespace range {
  30. namespace detail {
  31. // NOTE: For SinglePassRanges pos could iterate over all elements until the i-th element was met.
  32. template <typename RandomAccessRange>
  33. struct pos
  34. {
  35. typedef typename boost::range_iterator<RandomAccessRange>::type iterator;
  36. typedef typename boost::range_size<RandomAccessRange>::type size_type;
  37. typedef typename boost::range_difference<RandomAccessRange>::type difference_type;
  38. static inline iterator apply(RandomAccessRange & rng, size_type i)
  39. {
  40. BOOST_RANGE_CONCEPT_ASSERT(( boost::RandomAccessRangeConcept<RandomAccessRange> ));
  41. return boost::begin(rng) + static_cast<difference_type>(i);
  42. }
  43. };
  44. } // namespace detail
  45. /*!
  46. \brief Short utility to conveniently return an iterator of a RandomAccessRange.
  47. \ingroup utility
  48. */
  49. template <typename RandomAccessRange>
  50. inline typename boost::range_iterator<RandomAccessRange const>::type
  51. pos(RandomAccessRange const& rng,
  52. typename boost::range_size<RandomAccessRange const>::type i)
  53. {
  54. BOOST_GEOMETRY_ASSERT(i <= boost::size(rng));
  55. return detail::pos<RandomAccessRange const>::apply(rng, i);
  56. }
  57. /*!
  58. \brief Short utility to conveniently return an iterator of a RandomAccessRange.
  59. \ingroup utility
  60. */
  61. template <typename RandomAccessRange>
  62. inline typename boost::range_iterator<RandomAccessRange>::type
  63. pos(RandomAccessRange & rng,
  64. typename boost::range_size<RandomAccessRange>::type i)
  65. {
  66. BOOST_GEOMETRY_ASSERT(i <= boost::size(rng));
  67. return detail::pos<RandomAccessRange>::apply(rng, i);
  68. }
  69. /*!
  70. \brief Short utility to conveniently return an element of a RandomAccessRange.
  71. \ingroup utility
  72. */
  73. template <typename RandomAccessRange>
  74. inline typename boost::range_reference<RandomAccessRange const>::type
  75. at(RandomAccessRange const& rng,
  76. typename boost::range_size<RandomAccessRange const>::type i)
  77. {
  78. BOOST_GEOMETRY_ASSERT(i < boost::size(rng));
  79. return * detail::pos<RandomAccessRange const>::apply(rng, i);
  80. }
  81. /*!
  82. \brief Short utility to conveniently return an element of a RandomAccessRange.
  83. \ingroup utility
  84. */
  85. template <typename RandomAccessRange>
  86. inline typename boost::range_reference<RandomAccessRange>::type
  87. at(RandomAccessRange & rng,
  88. typename boost::range_size<RandomAccessRange>::type i)
  89. {
  90. BOOST_GEOMETRY_ASSERT(i < boost::size(rng));
  91. return * detail::pos<RandomAccessRange>::apply(rng, i);
  92. }
  93. /*!
  94. \brief Short utility to conveniently return the front element of a Range.
  95. \ingroup utility
  96. */
  97. template <typename Range>
  98. inline typename boost::range_reference<Range const>::type
  99. front(Range const& rng)
  100. {
  101. BOOST_GEOMETRY_ASSERT(!boost::empty(rng));
  102. return *boost::begin(rng);
  103. }
  104. /*!
  105. \brief Short utility to conveniently return the front element of a Range.
  106. \ingroup utility
  107. */
  108. template <typename Range>
  109. inline typename boost::range_reference<Range>::type
  110. front(Range & rng)
  111. {
  112. BOOST_GEOMETRY_ASSERT(!boost::empty(rng));
  113. return *boost::begin(rng);
  114. }
  115. // NOTE: For SinglePassRanges back() could iterate over all elements until the last element is met.
  116. /*!
  117. \brief Short utility to conveniently return the back element of a BidirectionalRange.
  118. \ingroup utility
  119. */
  120. template <typename BidirectionalRange>
  121. inline typename boost::range_reference<BidirectionalRange const>::type
  122. back(BidirectionalRange const& rng)
  123. {
  124. BOOST_RANGE_CONCEPT_ASSERT(( boost::BidirectionalRangeConcept<BidirectionalRange const> ));
  125. BOOST_GEOMETRY_ASSERT(!boost::empty(rng));
  126. return *(boost::rbegin(rng));
  127. }
  128. /*!
  129. \brief Short utility to conveniently return the back element of a BidirectionalRange.
  130. \ingroup utility
  131. */
  132. template <typename BidirectionalRange>
  133. inline typename boost::range_reference<BidirectionalRange>::type
  134. back(BidirectionalRange & rng)
  135. {
  136. BOOST_RANGE_CONCEPT_ASSERT((boost::BidirectionalRangeConcept<BidirectionalRange>));
  137. BOOST_GEOMETRY_ASSERT(!boost::empty(rng));
  138. return *(boost::rbegin(rng));
  139. }
  140. /*!
  141. \brief Short utility to conveniently clear a mutable range.
  142. It uses traits::clear<>.
  143. \ingroup utility
  144. */
  145. template <typename Range>
  146. inline void clear(Range & rng)
  147. {
  148. // NOTE: this trait is probably not needed since it could be implemented using resize()
  149. geometry::traits::clear<Range>::apply(rng);
  150. }
  151. /*!
  152. \brief Short utility to conveniently insert a new element at the end of a mutable range.
  153. It uses boost::geometry::traits::push_back<>.
  154. \ingroup utility
  155. */
  156. template <typename Range>
  157. inline void push_back(Range & rng,
  158. typename boost::range_value<Range>::type const& value)
  159. {
  160. geometry::traits::push_back<Range>::apply(rng, value);
  161. }
  162. /*!
  163. \brief Short utility to conveniently resize a mutable range.
  164. It uses boost::geometry::traits::resize<>.
  165. \ingroup utility
  166. */
  167. template <typename Range>
  168. inline void resize(Range & rng,
  169. typename boost::range_size<Range>::type new_size)
  170. {
  171. geometry::traits::resize<Range>::apply(rng, new_size);
  172. }
  173. /*!
  174. \brief Short utility to conveniently remove an element from the back of a mutable range.
  175. It uses resize().
  176. \ingroup utility
  177. */
  178. template <typename Range>
  179. inline void pop_back(Range & rng)
  180. {
  181. BOOST_GEOMETRY_ASSERT(!boost::empty(rng));
  182. range::resize(rng, boost::size(rng) - 1);
  183. }
  184. namespace detail {
  185. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  186. template <typename It,
  187. typename OutIt,
  188. bool UseMove = boost::is_convertible
  189. <
  190. typename std::iterator_traits<It>::value_type &&,
  191. typename std::iterator_traits<OutIt>::value_type
  192. >::value>
  193. struct copy_or_move_impl
  194. {
  195. static inline OutIt apply(It first, It last, OutIt out)
  196. {
  197. return std::move(first, last, out);
  198. }
  199. };
  200. template <typename It, typename OutIt>
  201. struct copy_or_move_impl<It, OutIt, false>
  202. {
  203. static inline OutIt apply(It first, It last, OutIt out)
  204. {
  205. return std::copy(first, last, out);
  206. }
  207. };
  208. template <typename It, typename OutIt>
  209. inline OutIt copy_or_move(It first, It last, OutIt out)
  210. {
  211. return copy_or_move_impl<It, OutIt>::apply(first, last, out);
  212. }
  213. #else
  214. template <typename It, typename OutIt>
  215. inline OutIt copy_or_move(It first, It last, OutIt out)
  216. {
  217. return std::copy(first, last, out);
  218. }
  219. #endif
  220. } // namespace detail
  221. /*!
  222. \brief Short utility to conveniently remove an element from a mutable range.
  223. It uses std::copy() and resize(). Version taking mutable iterators.
  224. \ingroup utility
  225. */
  226. template <typename Range>
  227. inline typename boost::range_iterator<Range>::type
  228. erase(Range & rng,
  229. typename boost::range_iterator<Range>::type it)
  230. {
  231. BOOST_GEOMETRY_ASSERT(!boost::empty(rng));
  232. BOOST_GEOMETRY_ASSERT(it != boost::end(rng));
  233. typename boost::range_difference<Range>::type const
  234. d = std::distance(boost::begin(rng), it);
  235. typename boost::range_iterator<Range>::type
  236. next = it;
  237. ++next;
  238. detail::copy_or_move(next, boost::end(rng), it);
  239. range::resize(rng, boost::size(rng) - 1);
  240. // NOTE: In general this should be sufficient:
  241. // return it;
  242. // But in MSVC using the returned iterator causes
  243. // assertion failures when iterator debugging is enabled
  244. // Furthermore the code below should work in the case if resize()
  245. // invalidates iterators when the container is resized down.
  246. return boost::begin(rng) + d;
  247. }
  248. /*!
  249. \brief Short utility to conveniently remove an element from a mutable range.
  250. It uses std::copy() and resize(). Version taking non-mutable iterators.
  251. \ingroup utility
  252. */
  253. template <typename Range>
  254. inline typename boost::range_iterator<Range>::type
  255. erase(Range & rng,
  256. typename boost::range_iterator<Range const>::type cit)
  257. {
  258. BOOST_RANGE_CONCEPT_ASSERT(( boost::RandomAccessRangeConcept<Range> ));
  259. typename boost::range_iterator<Range>::type
  260. it = boost::begin(rng)
  261. + std::distance(boost::const_begin(rng), cit);
  262. return range::erase(rng, it);
  263. }
  264. /*!
  265. \brief Short utility to conveniently remove a range of elements from a mutable range.
  266. It uses std::copy() and resize(). Version taking mutable iterators.
  267. \ingroup utility
  268. */
  269. template <typename Range>
  270. inline typename boost::range_iterator<Range>::type
  271. erase(Range & rng,
  272. typename boost::range_iterator<Range>::type first,
  273. typename boost::range_iterator<Range>::type last)
  274. {
  275. typename boost::range_difference<Range>::type const
  276. diff = std::distance(first, last);
  277. BOOST_GEOMETRY_ASSERT(diff >= 0);
  278. std::size_t const count = static_cast<std::size_t>(diff);
  279. BOOST_GEOMETRY_ASSERT(count <= boost::size(rng));
  280. if ( count > 0 )
  281. {
  282. typename boost::range_difference<Range>::type const
  283. d = std::distance(boost::begin(rng), first);
  284. detail::copy_or_move(last, boost::end(rng), first);
  285. range::resize(rng, boost::size(rng) - count);
  286. // NOTE: In general this should be sufficient:
  287. // return first;
  288. // But in MSVC using the returned iterator causes
  289. // assertion failures when iterator debugging is enabled
  290. // Furthermore the code below should work in the case if resize()
  291. // invalidates iterators when the container is resized down.
  292. return boost::begin(rng) + d;
  293. }
  294. return first;
  295. }
  296. /*!
  297. \brief Short utility to conveniently remove a range of elements from a mutable range.
  298. It uses std::copy() and resize(). Version taking non-mutable iterators.
  299. \ingroup utility
  300. */
  301. template <typename Range>
  302. inline typename boost::range_iterator<Range>::type
  303. erase(Range & rng,
  304. typename boost::range_iterator<Range const>::type cfirst,
  305. typename boost::range_iterator<Range const>::type clast)
  306. {
  307. BOOST_RANGE_CONCEPT_ASSERT(( boost::RandomAccessRangeConcept<Range> ));
  308. typename boost::range_iterator<Range>::type
  309. first = boost::begin(rng)
  310. + std::distance(boost::const_begin(rng), cfirst);
  311. typename boost::range_iterator<Range>::type
  312. last = boost::begin(rng)
  313. + std::distance(boost::const_begin(rng), clast);
  314. return range::erase(rng, first, last);
  315. }
  316. // back_inserter
  317. template <class Container>
  318. class back_insert_iterator
  319. {
  320. public:
  321. typedef std::output_iterator_tag iterator_category;
  322. typedef void value_type;
  323. typedef void difference_type;
  324. typedef void pointer;
  325. typedef void reference;
  326. typedef Container container_type;
  327. explicit back_insert_iterator(Container & c)
  328. : container(boost::addressof(c))
  329. {}
  330. back_insert_iterator & operator=(typename Container::value_type const& value)
  331. {
  332. range::push_back(*container, value);
  333. return *this;
  334. }
  335. back_insert_iterator & operator* ()
  336. {
  337. return *this;
  338. }
  339. back_insert_iterator & operator++ ()
  340. {
  341. return *this;
  342. }
  343. back_insert_iterator operator++(int)
  344. {
  345. return *this;
  346. }
  347. private:
  348. Container * container;
  349. };
  350. template <typename Range>
  351. inline back_insert_iterator<Range> back_inserter(Range & rng)
  352. {
  353. return back_insert_iterator<Range>(rng);
  354. }
  355. }}} // namespace boost::geometry::range
  356. #endif // BOOST_GEOMETRY_UTIL_RANGE_HPP