cheshirekow  v0.1.0
range.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * mpblocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
24 #ifndef MPBLOCKS_UTIL_RANGE_HPP_
25 #define MPBLOCKS_UTIL_RANGE_HPP_
26 
27 namespace mpblocks {
28 namespace util {
29 
30 template <typename T>
31 struct Range_ {
32  struct Iterator {
33  T val;
34 
35  Iterator(T val) : val(val) {}
36  T operator*() { return val; }
37  bool operator!=(T other) { return val != other; }
39  ++val;
40  return *this;
41  }
42  operator T() { return val; }
43  };
44 
45  private:
46  T m_begin;
47  T m_end;
48 
49  public:
50  Range_(T begin, T end) : m_begin(begin), m_end(end) {}
51 
52  T size() { return m_end - m_begin; }
53 
54  Iterator begin() { return m_begin; }
55  Iterator end() { return m_end; }
56 };
57 
58 template <typename T>
59 Range_<T> Range(T begin, T end) {
60  return Range_<T>(begin, end);
61 }
62 
63 
64 } // namespace util
65 } // namespace mpblocks
66 
67 #endif // MPBLOCKS_UTIL_RANGE_HPP_
Range_(T begin, T end)
Definition: range.hpp:50
Iterator end()
Definition: range.hpp:55
T m_begin
the first integral value
Definition: range.hpp:46
BinaryKey other(const BinaryKey &key)
Definition: BinaryKey.h:44
Iterator begin()
Definition: range.hpp:54
T val
storage for the actual value
Definition: range.hpp:33
T m_end
one past the last integral value
Definition: range.hpp:47
Range_< T > Range(T begin, T end)
Definition: range.hpp:59