Cypress  1.0
C++ Spiking Neural Network Simulation Framework
range.hpp
Go to the documentation of this file.
1 /*
2  * Cypress -- C++ Spiking Neural Network Simulation Framework
3  * Copyright (C) 2016 Andreas Stöckel
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more 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 
28 #ifndef CYPRESS_UTIL_RANGE_HPP
29 #define CYPRESS_UTIL_RANGE_HPP
30 
31 #include <cstddef>
32 #include <cmath>
33 
34 namespace cypress {
35 
39 template <typename T>
40 struct Range {
41  T x0;
42  T step;
43  size_t n;
44 
45  struct Iterator {
46  T x0;
47  T step;
48  size_t i;
49 
50  T operator*() const { return x0 + step * T(i); }
51  T operator->() const { return x0 + step * T(i); }
53  {
54  ++i;
55  return *this;
56  }
58  {
59  Iterator tmp = *this;
60  ++i;
61  return tmp;
62  }
63 
64  bool operator==(Iterator &o) const { return i == o.i; }
65  bool operator!=(Iterator &o) const { return i != o.i; }
66  bool operator<(Iterator &o) const { return i < o.i; }
67  };
68 
69  Iterator begin() const { return Iterator{x0, step, 0}; }
70  Iterator end() const { return Iterator{x0, step, n}; }
71 };
72 
73 constexpr Range<float> linspace(float x0, float x1, ptrdiff_t num)
74 {
75  return Range<float>{x0, (x1 - x0) / (num - 1),
76  (num > 0) ? size_t(num) : 0U};
77 }
78 
79 constexpr Range<double> linspace(double x0, double x1, ptrdiff_t num)
80 {
81  return Range<double>{x0, (x1 - x0) / (num - 1),
82  (num > 0) ? size_t(num) : 0U};
83 }
84 
85 constexpr Range<size_t> range(ptrdiff_t i)
86 {
87  return Range<size_t>{0U, 1U, (i > 0) ? size_t(i) : 0U};
88 }
89 
90 constexpr Range<ptrdiff_t> range(ptrdiff_t i0, ptrdiff_t i1)
91 {
92  return Range<ptrdiff_t>{i0, 1, (i1 > i0) ? size_t(i1 - i0) : 0U};
93 }
94 
95 constexpr Range<float> range(float x0, float x1, float step)
96 {
97  return Range<float>{x0, step,
98  (x1 > x0) ? size_t(std::ceil((x1 - x0) / step)) : 0U};
99 }
100 
101 constexpr Range<double> range(double x0, double x1, double step)
102 {
103  return Range<double>{x0, step,
104  (x1 > x0) ? size_t(std::ceil((x1 - x0) / step)) : 0U};
105 }
106 }
107 
108 #endif /* CYPRESS_UTIL_RANGE_HPP */
Iterator & operator++()
Definition: range.hpp:52
bool operator==(Iterator &o) const
Definition: range.hpp:64
size_t i
Definition: range.hpp:48
bool operator<(Iterator &o) const
Definition: range.hpp:66
constexpr Range< size_t > range(ptrdiff_t i)
Definition: range.hpp:85
bool operator!=(Iterator &o) const
Definition: range.hpp:65
T x0
Definition: range.hpp:46
size_t n
Definition: range.hpp:43
Iterator begin() const
Definition: range.hpp:69
Iterator end() const
Definition: range.hpp:70
Definition: range.hpp:45
Iterator operator++(int)
Definition: range.hpp:57
Definition: range.hpp:40
T step
Definition: range.hpp:47
Definition: brainscales_lib.hpp:39
T operator->() const
Definition: range.hpp:51
T operator*() const
Definition: range.hpp:50
T step
Definition: range.hpp:42
constexpr Range< float > linspace(float x0, float x1, ptrdiff_t num)
Definition: range.hpp:73
T x0
Definition: range.hpp:41