paludis  Version 2.6.0
iterator_range.hh
1 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
2 /*
3  * Copyright (c) 2014 Saleem Abdulrasool
4  *
5  * This file is part of the Paludis package manager. Paludis is free software;
6  * you can redistribute it and/or modify it under the terms of the GNU General
7  * Public License version 2, as published by the Free Software Foundation.
8  *
9  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_ITERATOR_RANGE_HH
20 #define PALUDIS_GUARD_PALUDIS_UTIL_ITERATOR_RANGE_HH 1
21 
22 #include <iterator>
23 #include <utility>
24 
25 namespace paludis
26 {
27  template <typename Iterator_>
29  {
30  private:
31  Iterator_ _begin, _end;
32 
33  public:
34  // TODO(compnerd) use SFINAE to ensure that the container's
35  // iterators match the range's iterator
36  template <typename Container_>
37  IteratorRange(Container_ && container)
38  : _begin(std::begin(container)), _end(std::end(container))
39  {
40  }
41 
42  IteratorRange(Iterator_ begin, Iterator_ end)
43  : _begin(std::move(begin)), _end(std::move(end))
44  {
45  }
46 
47  Iterator_ begin() const
48  {
49  return _begin;
50  }
51 
52  Iterator_ end() const
53  {
54  return _end;
55  }
56 
57  size_t size() const
58  {
59  return std::distance(_end, _begin);
60  }
61  };
62 
63  template <typename Iterator_>
64  IteratorRange<Iterator_> make_range(Iterator_ begin, Iterator_ end)
65  {
66  return IteratorRange<Iterator_>(std::move(begin), std::move(end));
67  }
68 
69  template <typename Iterator_>
70  IteratorRange<Iterator_> make_range(std::pair<Iterator_, Iterator_> range)
71  {
72  return IteratorRange<Iterator_>(std::move(range.first),
73  std::move(range.second));
74  }
75 }
76 
77 #endif
78 
Definition: about_metadata-fwd.hh:23
Definition: iterator_range.hh:28