AGX Dynamics 2.42.3.0
Loading...
Searching...
No Matches
IndexRange.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGXDATA_RANGE_H
18#define AGXDATA_RANGE_H
19
20#include <agx/agx.h>
21#include <agx/Vector.h>
22#include <iosfwd>
23
24namespace agx
25{
26
30 template <typename T>
32 {
33 public:
34 class iterator;
35 typedef T Type;
36
38 IndexRangeT(T start, T end);
39
40 template <typename T2>
41 operator IndexRangeT<T2>() const;
42
46 T& begin();
47 const T& begin() const;
48
52 T& end();
53 const T& end() const;
54
58 T operator[] (T localIndex) const;
59
63 void set(T begin, T end);
64
65
69 T size() const;
70
74 bool empty() const;
75
79 bool contains(T index) const;
80
84 bool hasOverlap(const IndexRangeT<T>& other) const;
85
89 void clamp(const IndexRangeT<T>& other);
90
94 bool valid() const;
95
96
97 bool operator== (const IndexRangeT<T>& other) const;
98 bool operator!= (const IndexRangeT<T>& other) const;
99
100 // TODO Unclear semantics?
105
106
107 private:
108 T m_start;
109 T m_end;
110 };
111
112 template <typename T>
113 IndexRangeT<T> makeIndexRange(const T begin, const T end);
114
118 template <typename T>
120 {
121 public:
122 iterator();
123 iterator(T index);
124
125 iterator& operator++();
126 iterator operator++(int);
127
128 operator T() const;
129
130 T operator*() const;
131
132 private:
133 T m_index;
134 };
135
136
137
138
142
146
147
148 /* Implementation */
149 template <typename T>
151
152 template <typename T>
153 AGX_FORCE_INLINE IndexRangeT<T>::IndexRangeT(T start, T end) : m_start(start), m_end(end) { agxAssert(end >= start); }
154
155 template <typename T> template <typename T2>
157 {
158 return IndexRangeT<T2>(T2(m_start), T2(m_end));
159 }
160
161
162 template <typename T>
163 AGX_FORCE_INLINE void IndexRangeT<T>::set(T begin, T end) { agxAssert(end >= begin); m_start = begin; m_end = end; }
164
165
166 template <typename T>
167 AGX_FORCE_INLINE T IndexRangeT<T>::size() const { return m_end - m_start; }
168
169 template <typename T>
170 AGX_FORCE_INLINE bool IndexRangeT<T>::empty() const { return m_end == m_start; }
171
172 template <typename T>
173 AGX_FORCE_INLINE bool IndexRangeT<T>::contains(T index) const { return index >= m_start && index < m_end; }
174
175 template <typename T>
177 {
178 agxAbort();
179 return false;
180 }
181
182 template <>
184 {
185 agx::UInt64 a = m_start - other.m_end;
186 agx::UInt64 b = m_end - other.m_start;
187 return a != 0 && b != 0 && (a >> 63) != (b >> 63);
188 }
189
190 template <>
192 {
193 agx::UInt32 a = m_start - other.m_end;
194 agx::UInt32 b = m_end - other.m_start;
195 return a != 0 && b != 0 && (a >> 31) != (b >> 31);
196 }
197
198 template <typename T>
200 {
201 if (m_start < other.m_start) m_start = other.m_start;
202 if (m_end > other.m_end) m_end = other.m_end;
203 }
204
205 template <typename T>
207 {
208 return m_start <= m_end;
209 }
210
211
212 template <typename T>
214
215 template <typename T>
217
218
219 template <typename T>
220 AGX_FORCE_INLINE const T& IndexRangeT<T>::begin() const { return m_start; }
221
222 template <typename T>
223 AGX_FORCE_INLINE const T& IndexRangeT<T>::end() const { return m_end; }
224
225 template <typename T>
226 AGX_FORCE_INLINE T IndexRangeT<T>::operator[] (T localIndex) const { return m_start + localIndex; }
227
228
229 template <typename T>
231 {
232 return m_start == other.m_start && m_end == other.m_end;
233 }
234
235 template <typename T>
237 {
238 return m_start != other.m_start || m_end != other.m_end;
239 }
240
241
242 template <typename T>
243 AGX_FORCE_INLINE IndexRangeT<T> IndexRangeT<T>::operator + ( T rhs ) const { return IndexRangeT(m_start+rhs, m_end+rhs); }
244
245 template <typename T>
246 AGX_FORCE_INLINE IndexRangeT<T>& IndexRangeT<T>::operator += ( T rhs ) { m_start += rhs; m_end += rhs; return *this; }
247
248 template <typename T>
249 AGX_FORCE_INLINE IndexRangeT<T> IndexRangeT<T>::operator - ( T rhs ) const { return IndexRangeT(m_start-rhs, m_end-rhs); }
250
251 template <typename T>
252 AGX_FORCE_INLINE IndexRangeT<T>& IndexRangeT<T>::operator -= ( T rhs ) { m_start -= rhs; m_end -= rhs; return *this; }
253
254
255
256 template <typename T>
257 std::ostream& operator << ( std::ostream& output, const IndexRangeT<T>& range )
258 {
259 output << (T)range.begin() << ":" << (T)range.end();
260 return output;
261 }
262
263
264 // This function won't be necessary once we get class template argument
265 // deduction with C++17.
266 template <typename T>
268 {
269 return IndexRangeT<T>(begin, end);
270 }
271
272
273
275
276 template <typename T>
278 {
279 }
280
281 template <typename T>
283 {
284 }
285
286 template <typename T>
288 {
289 ++m_index;
290 return *this;
291 }
292
293 template <typename T>
295 {
296 ++m_index;
297 return IndexRangeT<T>::iterator(m_index-1);
298 }
299
300 template <typename T>
302
303 template <typename T>
305
306
307}
308
309
310#endif /* _AGXDATA_RANGE_H_ */
Index range iterator.
Definition: IndexRange.h:120
A range of indices.
Definition: IndexRange.h:32
IndexRangeT< T > & operator+=(T rhs)
Definition: IndexRange.h:246
bool contains(T index) const
Definition: IndexRange.h:173
IndexRangeT< T > & operator-=(T rhs)
Definition: IndexRange.h:252
IndexRangeT< T > operator-(T rhs) const
Definition: IndexRange.h:249
void set(T begin, T end)
Set the range.
Definition: IndexRange.h:163
IndexRangeT(T start, T end)
Definition: IndexRange.h:153
bool operator==(const IndexRangeT< T > &other) const
Definition: IndexRange.h:230
bool operator!=(const IndexRangeT< T > &other) const
Definition: IndexRange.h:236
IndexRangeT< T > operator+(T rhs) const
Definition: IndexRange.h:243
bool valid() const
Definition: IndexRange.h:206
T size() const
Definition: IndexRange.h:167
const T & begin() const
Definition: IndexRange.h:220
void clamp(const IndexRangeT< T > &other)
Clamp range using another range as limits.
Definition: IndexRange.h:199
T operator[](T localIndex) const
[]-operator
Definition: IndexRange.h:226
const T & end() const
Definition: IndexRange.h:223
bool empty() const
Definition: IndexRange.h:170
bool hasOverlap(const IndexRangeT< T > &other) const
Definition: IndexRange.h:176
Vector containing 'raw' data.
Definition: agx/Vector.h:246
#define agxAbort()
Definition: debug.h:164
#define agxAssert(expr)
Definition: debug.h:143
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:32
Vec3T< T > operator*(const Vec3T< T > &v, const AffineMatrix4x4T< T > &m)
uint64_t UInt64
Definition: Integer.h:33
IndexRangeT< agx::UInt32 > IndexRange32
Definition: IndexRange.h:140
IndexRangeT< agx::UInt > IndexRange
Definition: IndexRange.h:139
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
std::ostream & operator<<(std::ostream &os, const agx::AddedMassInteraction::Matrix6x6 &m)
VectorPOD< IndexRange > IndexRangeVector
Definition: IndexRange.h:143
IndexRangeT< agx::UInt64 > IndexRange64
Definition: IndexRange.h:141
VectorPOD< IndexRange32 > IndexRange32Vector
Definition: IndexRange.h:144
LinearProbingHashSetImplementation< KeyT, HashT >::iterator begin(LinearProbingHashSetImplementation< KeyT, HashT > &set)
AGXCORE_EXPORT const InvalidIndexStruct InvalidIndex
IndexRangeT< T > makeIndexRange(const T begin, const T end)
Definition: IndexRange.h:267
VectorPOD< IndexRange64 > IndexRange64Vector
Definition: IndexRange.h:145