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