1 // Copyright (c) 2014  GeometryFactory (France).  All rights reserved.
2 //
3 // This file is part of CGAL (www.cgal.org)
4 //
5 // $URL: https://github.com/CGAL/cgal/blob/v5.3/STL_Extension/include/CGAL/Iterator_range.h $
6 // $Id: Iterator_range.h 0779373 2020-03-26T13:31:46+01:00 Sébastien Loriot
7 // SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
8 //
9 //
10 // Author(s)     : Andreas Fabri
11 
12 #ifndef CGAL_ITERATOR_RANGE_H
13 #define CGAL_ITERATOR_RANGE_H
14 
15 #include <CGAL/tuple.h>
16 #include <utility>
17 #include <boost/foreach.hpp>
18 
19 namespace CGAL {
20 
21   /*!
22 \ingroup PkgSTLExtensionRef
23   /// `CGAL::Iterator_range` is a...
24   */
25   template <typename I>
26   class Iterator_range
27     : public std::pair<I,I>{
28 
29     typedef std::pair<I,I> Base;
30 
31   public:
32 
33     typedef I iterator;
34     typedef I const_iterator;
35 
Iterator_range(I b,I e)36     Iterator_range(I b, I e)
37       : Base(b,e)
38     {}
39 
40 
41     // Iterator_range(const Iterator_range& ip)
42     //   : Base(ip)
43     // {}
44 
Iterator_range(const std::pair<I,I> & ip)45     Iterator_range(const std::pair<I,I>& ip)
46       : Base(ip)
47     {}
48 
begin()49   I begin() const
50   {
51     return this->first;
52   }
53 
end()54   I end() const
55   {
56     return this->second;
57   }
58 
59   /// returns `std::distance(begin(), end())`
60   std::size_t
size()61   size() const
62   {
63     return static_cast<std::size_t>(std::distance(begin(), end()));
64   }
65 
66   /// returns `std::distance(begin(), end())==0`
empty()67   bool empty() const
68   {
69     return begin()==end();
70   }
71 #ifndef CGAL_CFG_NO_CPP0X_TUPLE
72 
73   operator std::tuple<I&, I&>()
74   {
75     return std::tuple<I&, I&>{this->first, this->second};
76   }
77 
78   operator std::tuple<const I&, const I&>() const
79   {
80     return std::tuple<const I&, const I&>{this->first, this->second};
81   }
82 #endif
83 
84 };
85 
86   template <typename T>
87   Iterator_range<T>
make_range(const T & b,const T & e)88   make_range(const T& b, const T&e)
89   {
90     return Iterator_range<T>(b,e);
91   }
92 
93   template <typename T>
94   Iterator_range<T>
make_range(const std::pair<T,T> & p)95   make_range(const std::pair<T,T>& p)
96   {
97     return Iterator_range<T>(p.first,p.second);
98   }
99 
100 
101 } // namespace CGAL
102 
103 // At global scope...
104 
105   template<typename T>
106 inline boost::mpl::true_ *
boost_foreach_is_lightweight_proxy(CGAL::Iterator_range<T> * &,boost::foreach::tag)107   boost_foreach_is_lightweight_proxy( CGAL::Iterator_range<T> *&, boost::foreach::tag )
108 {
109     return 0;
110 }
111 namespace boost { namespace foreach
112 {
113     template<typename T>
114     struct is_lightweight_proxy< CGAL::Iterator_range<T> >
115       : mpl::true_
116     {
117     };
118 }}
119 #endif // CGAL_ITERATOR_RANGE_H
120