HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_CollapseIDs.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_CollapseIDs.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  * UT_CollapseIDs is a simple data structure that maps every (integral)
10  * input to a unique (also integral) output. These mapped outputs are
11  * guaranteed to be small, given the present mapping of values.
12  */
13 
14 #ifndef __UT_CollapseIDs_h__
15 #define __UT_CollapseIDs_h__
16 
17 #include "UT_API.h"
18 #include "UT_Assert.h"
19 #include "UT_Map.h"
20 #include "UT_Set.h"
21 
22 #include <SYS/SYS_Inline.h>
23 
24 /// This class is designed to map integral values to integral values. For each
25 /// newly-added mapping, the mapped value will be a unique integer that's
26 /// guaranteed to be small.
27 template <typename S, typename T>
29 {
30 public:
32 
33  /// Returns true if the id maps to something already.
34  bool contains(const S& id) const
35  {
36  return myMapping.contains(id);
37  }
38 
39  /// Returns the value that id maps to. If the mapping doesn't yet exist, a
40  /// non-clashing one is registered.
41  T operator[](const S& id)
42  {
43  auto iter = myMapping.find(id);
44  // If the mapping does not exist, find a free value and map it.
45  if (iter == myMapping.end())
46  {
47  T free_val = getFreeValue();
48  myMapping[id] = free_val;
49  return free_val;
50  }
51  // The mapping does exist: return it.
52  else
53  return iter->second;
54  }
55 
56  /// This call removes the mapping from id, marking whatever it was mapped to
57  /// as an available value.
58  void freeId(const S& id)
59  {
60  auto iter = myMapping.find(id);
61  UT_ASSERT(iter != myMapping.end());
62  returnValue(iter->second);
63  myMapping.erase(iter);
64  }
65 
66 protected:
67  /// The given value is returned as free, making it available for subsequent
68  /// requests for a free value.
70  void returnValue(const T& value)
71  {
72  if (value == myFreeStart - 1)
74  else
75  myFreeList.insert(value);
76  }
77 
78  /// Returns a free value, with some effort put into ensuring that the
79  /// smallest free value is returned.
82  {
83  T free_val;
84  // If the free list is empty, just use myFreeStart (making sure to
85  // appropriately update it).
86  if (myFreeList.empty())
87  {
88  free_val = myFreeStart;
89  myFreeStart++;
90  //if (myFreeStart < 0) UT_ASSERT(!"overflow");
91  }
92  // Otherwise, grab the first value in myFreeList (and remove it to mark
93  // it not free).
94  else
95  {
96  auto iter = myFreeList.begin();
97  free_val = *iter;
98  myFreeList.erase(iter);
99  }
100 
101  return free_val;
102  }
103 
104  /// Decreases myFreeStart by 1 and also sweeps the free list to remove any
105  /// entries that can be merged with it.
108  {
109  // Decrement the variable.
110  myFreeStart--;
111 
112  // As long as myFreeStart-1 is in the free list, remove it and decrement
113  // myFreeStart to ensure that myFreeStart-1 is not in the set.
114  auto iter = myFreeList.find(myFreeStart - 1);
115  while (iter != myFreeList.end())
116  {
117  myFreeStart--;
118  myFreeList.erase(iter);
119  iter = myFreeList.find(myFreeStart - 1);
120  }
121  }
122 
123 protected:
124  /// Set of free values; a value in this list is guaranteed to be free, and
125  /// no value equal to or greater than myFreeStart-1 is in this set.
127  /// All values equal to or greater than this value are free.
129  /// The actual mappings. This container ensures that no two myMapping[x]
130  /// values will be the same.
132 };
133 
134 #endif
135 
SYS_FORCE_INLINE void returnValue(const T &value)
T operator[](const S &id)
SYS_FORCE_INLINE void decrementFreeStart()
GLsizei const GLfloat * value
Definition: glcorearb.h:824
UT_Set< T > myFreeList
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
T myFreeStart
All values equal to or greater than this value are free.
GLuint id
Definition: glcorearb.h:655
SYS_FORCE_INLINE T getFreeValue()
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:165
void freeId(const S &id)
UT_Map< S, T > myMapping
bool contains(const S &id) const
Returns true if the id maps to something already.
bool contains(const key_type &key) const
Returns true if a value with the key is contained in the map.
Definition: UT_Map.h:166