SyDEVS  v0.6.7
Multiscale Simulation and Systems Modeling Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
range.h
Go to the documentation of this file.
1 #pragma once
2 #ifndef SYDEVS_RANGE_H_
3 #define SYDEVS_RANGE_H_
4 
6 #include <stdexcept>
7 
8 namespace sydevs {
9 
10 
51 class range
52 {
53 public:
58  constexpr range();
59 
60 private:
61  constexpr range(int64 start_limit, int64 stop_limit, int64 stride, bool after, bool before);
62 
63 public:
64  constexpr range(const range&) = default;
65  range& operator=(const range&) = default;
66  range(range&&) = default;
67  range& operator=(range&&) = default;
68  ~range() = default;
69 
70  constexpr int64 start() const;
71  constexpr int64 stop() const;
72  constexpr int64 stride() const;
73 
74  constexpr range start_at(int64 limit) const;
75  constexpr range start_after(int64 limit) const;
76 
77  constexpr range stop_at(int64 limit) const;
78  constexpr range stop_before(int64 limit) const;
79 
80  constexpr range stride_by(int64 stride) const;
81 
82 private:
83  int64 start_limit_;
84  int64 stop_limit_;
85  int64 stride_;
86  bool after_;
87  bool before_;
88 };
89 
90 
91 constexpr range::range()
92  : start_limit_(0)
93  , stop_limit_(std::numeric_limits<int64>::max())
94  , stride_(1)
95  , after_(false)
96  , before_(false)
97 {
98 }
99 
100 
101 constexpr range::range(int64 start_limit, int64 stop_limit, int64 stride, bool after, bool before)
102  : start_limit_(start_limit)
103  , stop_limit_(stop_limit)
104  , stride_(stride)
105  , after_(after)
106  , before_(before)
107 {
108 }
109 
110 
111 constexpr int64 range::start() const
112 {
113  return start_limit_ + (after_ ? 2*int64(stride_ > 0) - 1 : 0);
114 }
115 
116 
117 constexpr int64 range::stop() const
118 {
119  return stop_limit_ + (before_ ? 2*int64(stride_ < 0) - 1 : 0);
120 }
121 
122 
123 constexpr int64 range::stride() const
124 {
125  return stride_;
126 }
127 
128 
129 constexpr range range::start_at(int64 limit) const
130 {
131  return range(limit, stop_limit_, stride_, false, before_);
132 }
133 
134 
135 constexpr range range::start_after(int64 limit) const
136 {
137  return range(limit, stop_limit_, stride_, true, before_);
138 }
139 
140 
141 constexpr range range::stop_at(int64 limit) const
142 {
143  return range(start_limit_, limit, stride_, after_, false);
144 }
145 
146 
147 constexpr range range::stop_before(int64 limit) const
148 {
149  return range(start_limit_, limit, stride_, after_, true);
150 }
151 
152 
153 constexpr range range::stride_by(int64 stride) const
154 {
155  return range(start_limit_,
156  (stride_ > 0 && stride < 0 && stop_limit_ == std::numeric_limits<int64>::max()) ? std::numeric_limits<int64>::min() :
157  (stride_ < 0 && stride > 0 && stop_limit_ == std::numeric_limits<int64>::min()) ? std::numeric_limits<int64>::max() :
158  stop_limit_,
159  stride,
160  after_,
161  before_);
162 }
163 
164 
165 } // namespace
166 
167 #endif
A data type which represents a range of array indices along a single dimension.
Definition: range.h:51
~range()=default
Destructor.
constexpr range start_after(int64 limit) const
Returns a new range object starting immediate after limit.
Definition: range.h:135
range & operator=(const range &)=default
Copy assignment.
constexpr range()
Constructs a range object representing a sequence that starts at 0 and increases by 1...
Definition: range.h:91
constexpr range stop_before(int64 limit) const
Returns a new range object stopping immediate before limit.
Definition: range.h:147
constexpr int64 stop() const
Returns the last possible index in the sequence.
Definition: range.h:117
constexpr range stop_at(int64 limit) const
Returns a new range object stopping at limit.
Definition: range.h:141
constexpr range start_at(int64 limit) const
Returns a new range object starting at limit.
Definition: range.h:129
constexpr int64 stride() const
Returns the increment between sequence indices.
Definition: range.h:123
int64_t int64
Definition: number_types.h:14
constexpr int64 start() const
Returns the first index in the sequence.
Definition: range.h:111
constexpr range stride_by(int64 stride) const
Returns a new range object incremending by stride.
Definition: range.h:153