Publishing R3
[platform/upstream/dldt.git] / inference-engine / thirdparty / ade / common / include / util / iota_range.hpp
1 // Copyright (C) 2018 Intel Corporation
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5
6 #ifndef UTIL_IOTA_RANGE_HPP
7 #define UTIL_IOTA_RANGE_HPP
8
9 #include <type_traits>
10 #include <cassert>
11 #include <cinttypes>
12 #include <limits>
13
14 namespace util
15 {
16
17 inline namespace Range
18 {
19
20 template<typename T, std::int32_t step = 0>
21 struct IotaRange
22 {
23    static_assert(std::is_integral<T>::value,"T must be integral");
24
25    inline void check() const
26    {
27       if ( step > 0)
28       {
29          assert(to >= from);
30       }
31       else if (step < 0)
32       {
33          assert(from >= to);
34       }
35       else assert(!"Zero step");
36
37       assert(0 == ((to - from) % step));
38    }
39
40    struct iterator
41    {
42       inline bool operator==(iterator const& other) const
43       {
44          return value == other.value;
45       }
46
47       inline bool operator!=(iterator const& other) const
48       {
49          return value != other.value;
50       }
51
52       inline T const& operator*() const
53       {
54          return value;
55       }
56
57       inline iterator& operator++()
58       {
59          value += step;
60          return *this;
61       }
62
63       T value;
64    };
65
66    bool empty() const
67    {
68        return to == from;
69    }
70
71    const T& front() const
72    {
73        assert(!empty());
74        return from;
75    }
76
77    void popFront()
78    {
79        assert(!empty());
80        from += step;
81    }
82
83    inline iterator begin() const
84    {
85       check();
86       return {from};
87    }
88
89    inline iterator end() const
90    {
91       check();
92       return {to};
93    }
94
95    bool operator==(const IotaRange<T,step>& rhs) const
96    {
97        return from == rhs.from && to == rhs.to;
98    }
99    bool operator!=(const IotaRange<T,step>& rhs) const
100    {
101        return !(*this == rhs);
102    }
103
104    /*const*/ T from;
105    const T to;
106 };
107
108 template<typename T>
109 struct IotaRange<T,0>
110 {
111    static_assert(std::is_integral<T>::value,"T must be integral");
112
113    inline void check() const
114    {
115       if ( step > 0)
116       {
117          assert(to >= from);
118       }
119       else if (step < 0)
120       {
121          assert(from >= to);
122       }
123       else assert(!"Zero step");
124
125       assert(0 == ((to - from) % step));
126    }
127
128    struct iterator
129    {
130       inline bool operator==(iterator const& other) const
131       {
132          assert(step == other.step);
133          return value == other.value;
134       }
135
136       inline bool operator!=(iterator const& other) const
137       {
138          assert(step == other.step);
139          return value != other.value;
140       }
141
142       inline T const& operator*() const
143       {
144          return value;
145       }
146
147       inline iterator& operator++()
148       {
149          value += step;
150          return *this;
151       }
152
153       T value;
154       T step;
155    };
156
157    bool empty() const
158    {
159        return to == from;
160    }
161
162    const T& front() const
163    {
164        assert(!empty());
165        return from;
166    }
167
168    void popFront()
169    {
170        assert(!empty());
171        from += step;
172    }
173
174    inline iterator begin() const
175    {
176       check();
177       return {from, step};
178    }
179
180    inline iterator end() const
181    {
182       check();
183       return {to, step};
184    }
185
186    bool operator==(const IotaRange<T,0>& rhs) const
187    {
188        return from == rhs.from && to == rhs.to && step == rhs.step;
189    }
190    bool operator!=(const IotaRange<T,0>& rhs) const
191    {
192        return !(*this == rhs);
193    }
194
195    /*const*/ T from;
196    const T to;
197    const T step;
198 };
199
200 template<typename T>
201 inline IotaRange<T, 1> iota()
202 { return {   0, std::numeric_limits<T>::max()}; }
203
204 template<typename T>
205 inline IotaRange<T, 1> iota(const T to)
206 { return {   0, to}; }
207
208 template<typename T>
209 inline IotaRange<T, 1> iota(const T from, const T to)
210 { return {from, to}; }
211
212 template<typename T>
213 inline IotaRange<T>    iota(const T from, const T to, const T step)
214 { return {from, to, step}; }
215
216 }
217 }
218
219
220 #endif // UTIL_IOTA_RANGE_HPP