range.h
1 /* Copyright (C) 2012-2020 IBM Corp.
2  * This program is Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software
7  * distributed under the License is distributed on an "AS IS" BASIS,
8  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9  * See the License for the specific language governing permissions and
10  * limitations under the License. See accompanying LICENSE file.
11  */
12 
13 #ifndef HELIB_RANGE_H
14 #define HELIB_RANGE_H
15 
16 namespace helib {
17 
18 template <typename T>
20 {
21 public:
22  class iterator
23  {
24  friend class general_range;
25 
26  public:
27  T operator*() const { return i_; }
29  {
30  ++i_;
31  return *this;
32  }
33 
34  bool operator==(const iterator& other) const { return i_ == other.i_; }
35  bool operator!=(const iterator& other) const { return i_ != other.i_; }
36 
37  protected:
38  iterator(T start) : i_(start) {}
39 
40  private:
41  T i_;
42  };
43 
44  iterator begin() const { return begin_; }
45  iterator end() const { return end_; }
46  general_range(T begin, T end) : begin_(begin), end_(end)
47  {
48  if (end < begin)
49  end = begin;
50  }
51 
52 private:
53  iterator begin_;
54  iterator end_;
55 };
56 
57 inline general_range<long> range(long n) { return general_range<long>(0, n); }
58 
59 inline general_range<long> range(long m, long n)
60 {
61  return general_range<long>(m, n);
62 }
63 
64 } // namespace helib
65 
66 #endif // ifndef HELIB_RANGE_H
iterator & operator++()
Definition: range.h:28
general_range(T begin, T end)
Definition: range.h:46
Definition: range.h:23
iterator(T start)
Definition: range.h:38
iterator begin() const
Definition: range.h:44
bool operator==(const iterator &other) const
Definition: range.h:34
Definition: apiAttributes.h:21
Definition: range.h:20
bool operator!=(const iterator &other) const
Definition: range.h:35
iterator end() const
Definition: range.h:45
general_range< long > range(long n)
Definition: range.h:57
T operator*() const
Definition: range.h:27