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_ValArray.h"
15 #include "UT_SharedPtr.h"
16 
17 /// Given an iterator and a comparison function, iterate in sorted order
18 ///
19 /// The iterator type must support:
20 /// - atEnd()
21 /// - T operator*()
22 /// Currently, the ordered iterator extracts the items from the source
23 /// iterator, storing them in an array which is then sorted.
24 template <typename T, typename ITERATOR_T>
26 {
27 public:
28  /// Storage for the list of items
30  typedef int (*Comparator)(const T *, const T *);
31 
32  /// Default c-tor
34  : myList()
35  , myCurr(0)
36  {
37  }
38  /// Copy c-tor
40  : myList(src.myList)
41  , myCurr(src.myCurr)
42  {
43  }
44  /// Construct an ordered list by creating a copy of the items and sorting
45  /// them.
47  : myList(new ListType())
48  , myCurr(0)
49  {
50  for (; !it.atEnd(); ++it)
51  myList->append(*it);
52  myList->sort(cmp);
53  }
54 
55  /// Test equality
57  {
58  if (atEnd() && src.atEnd())
59  return true;
60  return myList == src.myList && myCurr == src.myCurr;
61  }
62  /// Test inequality
64  {
65  return !(*this == src);
66  }
67  /// Assignment operator
69  {
70  myList = src.myList;
71  myCurr = src.myCurr;
72  return *this;
73  }
74 
75  /// Pre-increment operator
77  {
78  advance();
79  return *this;
80  }
81  /// Post-increment operator
83  {
85  advance();
86  return tmp;
87  }
88 
89  /// @{
90  /// Get the current item
91  T &item() const { return (*myList)(myCurr); }
92  T &operator*() const { return item(); }
93  /// @}
94 
95  /// Return current index
96  exint index() const { return myCurr; }
97 
98  /// Return the number of elemenets in the iteration
99  exint entries() const { return myList ? myList->entries():0; }
100 
101  /// Rewind and start iteration again
102  void rewind() { myCurr = 0; }
103  /// Test if at end of the list
104  bool atEnd() const { return !myList || myCurr>=myList->entries(); }
105  /// Advance to the next iteration
106  void advance() { ++myCurr; }
107 
108 private:
109  UT_SharedPtr<ListType> myList;
110  exint myCurr;
111 };
112 
113 #endif
bool operator==(const UT_OrderedIterator< T, ITERATOR_T > &src) const
Test equality.
UT_OrderedIterator()
Default c-tor.
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:28
exint index() const
Return current index.
int64 exint
Definition: SYS_Types.h:116
void advance()
Advance to the next iteration.
UT_OrderedIterator< T, ITERATOR_T > & operator++(int)
Post-increment operator.
UT_OrderedIterator< T, ITERATOR_T > & operator++()
Pre-increment operator.
UT_OrderedIterator< T, ITERATOR_T > & operator=(const UT_OrderedIterator< T, ITERATOR_T > &src)
Assignment operator.
int cmp(T a, T b)
Definition: ImathFun.h:119
UT_OrderedIterator(const UT_OrderedIterator &src)
Copy c-tor.
int(* Comparator)(const T *, const T *)
typedef int
Definition: png.h:1175
exint entries() const
Return the number of elemenets in the iteration.
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:1792