HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_OrderedIterator.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_OrderedIterator.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_OrderedIterator__
12 #define __UT_OrderedIterator__
13 
14 #include "UT_Compare.h"
15 #include "UT_ValArray.h"
16 #include "UT_SharedPtr.h"
17 
18 /// Given an iterator and a comparison function, iterate in sorted order
19 ///
20 /// The iterator type must support:
21 /// - atEnd()
22 /// - T operator*()
23 /// Currently, the ordered iterator extracts the items from the source
24 /// iterator, storing them in an array which is then sorted.
25 template <typename T, typename ITERATOR_T>
27 {
28 public:
29  /// Storage for the list of items
31  typedef int (*Comparator)(const T *, const T *);
32 
33  /// Default c-tor
35  : myList()
36  , myCurr(0)
37  {
38  }
39  /// Copy c-tor
41  : myList(src.myList)
42  , myCurr(src.myCurr)
43  {
44  }
45  /// Construct an ordered list by creating a copy of the items and sorting
46  /// them.
48  : myList(new ListType())
49  , myCurr(0)
50  {
51  for (; !it.atEnd(); ++it)
52  myList->append(*it);
53  myList->sort(UTcompareLess(cmp));
54  }
55 
56  /// Test equality
58  {
59  if (atEnd() && src.atEnd())
60  return true;
61  return myList == src.myList && myCurr == src.myCurr;
62  }
63  /// Test inequality
65  {
66  return !(*this == src);
67  }
68  /// Assignment operator
70  {
71  myList = src.myList;
72  myCurr = src.myCurr;
73  return *this;
74  }
75 
76  /// Pre-increment operator
78  {
79  advance();
80  return *this;
81  }
82  /// Post-increment operator
83  /// We used to have this operator, but it's the wrong signature (should not return a
84  /// reference) and our implementation had a bug. Please use the pre-increment...
86 
87  /// @{
88  /// Get the current item
89  T &item() const { return (*myList)(myCurr); }
90  T &operator*() const { return item(); }
91  /// @}
92 
93  /// Return current index
94  exint index() const { return myCurr; }
95 
96  /// Return the number of elemenets in the iteration
97  exint entries() const { return myList ? myList->entries():0; }
98 
99  /// Rewind and start iteration again
100  void rewind() { myCurr = 0; }
101  /// Test if at end of the list
102  bool atEnd() const { return !myList || myCurr>=myList->entries(); }
103  /// Advance to the next iteration
104  void advance() { ++myCurr; }
105 
106 private:
107  UT_SharedPtr<ListType> myList;
108  exint myCurr;
109 };
110 
111 #endif
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
int64 exint
Definition: SYS_Types.h:125
bool operator==(const UT_OrderedIterator< T, ITERATOR_T > &src) const
Test equality.
UT_OrderedIterator()
Default c-tor.
IMATH_HOSTDEVICE constexpr int cmp(T a, T b) IMATH_NOEXCEPT
Definition: ImathFun.h:84
bool atEnd() const
Test if at end of the list.
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
exint index() const
Return current index.
void advance()
Advance to the next iteration.
UT_OrderedIterator< T, ITERATOR_T > & operator++()
Pre-increment operator.
UT_OrderedIterator< T, ITERATOR_T > & operator=(const UT_OrderedIterator< T, ITERATOR_T > &src)
Assignment operator.
UT_OrderedIterator(const UT_OrderedIterator &src)
Copy c-tor.
int(* Comparator)(const T *, const T *)
exint entries() const
Return the number of elemenets in the iteration.
UT_Compare::Less< T > UTcompareLess(UT_Compare::Ternary< T > compare)
Definition: UT_Compare.h:64
UT_ValArray< T > ListType
Storage for the list of items.
UT_OrderedIterator(ITERATOR_T it, Comparator cmp)
bool operator!=(const UT_OrderedIterator< T, ITERATOR_T > &src) const
Test inequality.
void rewind()
Rewind and start iteration again.
GLenum src
Definition: glcorearb.h:1793