SHOGUN  6.1.3
range.h
Go to the documentation of this file.
1 /*
2 * BSD 3-Clause License
3 *
4 * Copyright (c) 2017, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * * Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * Written (W) 2016 Sergey Lisitsyn
33 *
34 */
35 
36 #ifndef __SG_RANGE_H__
37 #define __SG_RANGE_H__
38 
39 #include <iterator>
40 
41 namespace shogun
42 {
43 
52  template <typename T>
53  class Range
54  {
55  public:
62  Range(T rbegin, T rend) : m_begin(rbegin), m_end(rend)
63  {
64  }
65 
67  class Iterator : public std::iterator<std::input_iterator_tag, T>
68  {
69  public:
70  Iterator(T value) : m_value(value)
71  {
72  }
73  Iterator(const Iterator& other) : m_value(other.m_value)
74  {
75  }
76  Iterator(Iterator&& other) : m_value(other.m_value)
77  {
78  }
79  Iterator& operator=(const Iterator&) = delete;
81  {
82  m_value++;
83  return *this;
84  }
86  {
87  Iterator tmp(*this);
88  ++*this;
89  return tmp;
90  }
92  {
93  return m_value;
94  }
95  bool operator!=(const Iterator& other)
96  {
97  return this->m_value != other.m_value;
98  }
99 
100  private:
101  T m_value;
102  };
107  Iterator begin() const
108  {
109  return Iterator(m_begin);
110  }
115  Iterator end() const
116  {
117  return Iterator(m_end);
118  }
119 
120  private:
122  T m_begin;
124  T m_end;
125  };
126 
135  template <typename T>
136  inline Range<T> range(T rend)
137  {
138  return Range<T>(0, rend);
139  }
140 
150  template <typename T>
151  inline Range<T> range(T rbegin, T rend)
152  {
153  return Range<T>(rbegin, rend);
154  }
155 }
156 
157 #endif /* __SG_RANGE_H__ */
Iterator(const Iterator &other)
Definition: range.h:73
Iterator(T value)
Definition: range.h:70
Range< T > range(T rend)
Definition: range.h:136
Iterator & operator++()
Definition: range.h:80
Iterator end() const
Definition: range.h:115
Iterator(Iterator &&other)
Definition: range.h:76
bool operator!=(const Iterator &other)
Definition: range.h:95
Iterator begin() const
Definition: range.h:107
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Range(T rbegin, T rend)
Definition: range.h:62
Iterator & operator=(const Iterator &)=delete
Iterator operator++(int)
Definition: range.h:85

SHOGUN Machine Learning Toolbox - Documentation