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