InspIRCd  4.0
iterator_range.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2020 Sadie Powell <[email protected]>
5  *
6  * This file is part of InspIRCd. InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 
20 #pragma once
21 
22 namespace insp
23 {
24  template <typename Iterator>
25  class iterator_range;
26 
31  template <typename Collection, typename Value>
32  auto equal_range(const Collection& collection, const Value& value)
33  {
34  return iterator_range(collection.equal_range(value));
35  }
36 
40  template <typename Collection>
41  auto reverse_range(const Collection& collection)
42  {
43  return iterator_range(collection.rbegin(), collection.rend());
44  }
45 }
46 
48 template <typename Iterator>
50 {
51  private:
53  const Iterator begini;
54 
55  /* An iterator which points to one past the end of the range. */
56  const Iterator endi;
57 
58  public:
63  explicit iterator_range(Iterator begin, Iterator end)
64  : begini(begin)
65  , endi(end)
66  {
67  }
68 
72  explicit iterator_range(std::pair<Iterator, Iterator> range)
73  : begini(range.first)
74  , endi(range.second)
75  {
76  }
77 
79  bool empty() const { return begini == endi; }
80 
82  const Iterator& begin() const { return begini; }
83 
85  const Iterator& end() const { return endi; }
86 
88  typename Iterator::difference_type count() const { return std::distance(begini, endi); }
89 };
Definition: iterator_range.h:50
iterator_range(Iterator begin, Iterator end)
Definition: iterator_range.h:63
iterator_range(std::pair< Iterator, Iterator > range)
Definition: iterator_range.h:72
const Iterator & end() const
Definition: iterator_range.h:85
const Iterator & begin() const
Definition: iterator_range.h:82
Iterator::difference_type count() const
Definition: iterator_range.h:88
bool empty() const
Definition: iterator_range.h:79