HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_HashTable.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_HashTable.h
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_HashTable_h__
12 #define __UT_HashTable_h__
13 
14 #include "UT_API.h"
15 #include "UT_Hash.h"
16 #include "UT_NonCopyable.h"
17 #include "UT_ThingList.h"
18 #include <SYS/SYS_Deprecated.h>
19 #include <SYS/SYS_Types.h>
20 #include <iosfwd>
21 #include <stdio.h>
22 
24 
26 {
27 public:
28  explicit UT_HashTable();
29  virtual ~UT_HashTable();
30 
31  UT_NON_COPYABLE(UT_HashTable)
32 
33  // Set this to true before adding any items to the hash table if your
34  // hash function implements a proper strcmp() (returning -1 for <, 0 for =
35  // and 1 for >). This can shorten adds and queries to buckets with more
36  // than 1 entry.
37  void useOrderedLists(bool ordered);
38 
39  void addSymbol(const UT_Hash& symbol, const UT_Thing &data);
40  int findSymbol(const UT_Hash& symbol, UT_Thing *datap,
41  const UT_Hash **hash_symbol = 0) const;
42 
43  int contains(const UT_Hash &symbol) const
44  { UT_Thing value; return findSymbol(symbol, &value); }
45 
46  int count(const UT_Hash &symbol) const
47  { return contains(symbol) ? 1 : 0; }
48 
49  template <typename T>
50  T *findPointer(const UT_Hash &symbol) const
51  {
52  UT_Thing thing;
53  if (!findSymbol(symbol, &thing))
54  return NULL;
55  return thing.template asPointer<T>();
56  }
57 
58  int deleteSymbol(const UT_Hash& symbol);
59  int moveSymbol(const UT_Hash &oldsymbol,
60  const UT_Hash &newsymbol);
61  void clear();
62 
63  unsigned entries() const { return myNbrEntries; }
64  unsigned size() const { return myNbrEntries; }
65  bool empty() const { return !myNbrEntries; }
66 
67  // Retrieve an entry in the table, creating it if it doesn't already exist.
68  UT_Thing &operator[](const UT_Hash& symbol);
69 
70  // Call this method when you're adding entries to a table inside a loop
71  // and you know how many entries you'll be adding.
72  void reserveTableSize(int size);
73 
74  // The table will automatically rebuild itself to keep the load factor
75  // within the specified range. A minimum load factor of 0 means that
76  // the table will never shrink when elements are deleted from it.
77  float getMaxLoadFactor() const { return myMaxLoadFactor; }
78  void setMaxLoadFactor(float max_load_factor);
79  float getMinLoadFactor() const { return myMinLoadFactor; }
80  void setMinLoadFactor(float min_load_factor);
81 
82  // Merge the specified table with ourselves.
83  void mergeTable(const UT_HashTable &table);
84 
85  // "getHashReference" returns a pointer to the actual UT_Hash that
86  // is maintained in the hash table. This can be used to implement
87  // a shared hash symbol table.
88  const UT_Hash *getHashReference(const UT_Hash &hash);
89 
90  void outputStats(FILE *output) const;
91  void outputStats(std::ostream &os) const;
92  void dumpTable(FILE *output) const;
93 
94  int64 getMemoryUsage(bool inclusive) const;
95 
96  // "traverse" invokes the given function with every member of the
97  // hash table. The order of traversal is undefined. If the function
98  // returns a non-zero value the traversal continues. If it returns
99  // zero then traversal stops. The return value is zero if the traversal
100  // was interupted and one otherwise.
101  //
102  // Call traverseConst() if you will not be removing things from the table
103  // while traversing, and traverse() if you might be. If you call traverse()
104  // and add something to the table, you may or may not encounter it later
105  // in the traversal.
106  int traverseConst(
107  int (*function)(UT_Thing &, const UT_Hash&, void *),
108  void *data) const;
109  int traverse(int (*function)(UT_Thing &, const UT_Hash&, void *),
110  void *data);
111 
113  {
114  public:
116  : myCurrList(0), myEndList(0)
117  { }
119  { *this = src; }
121  { }
122 
123  UT_Thing &thing() const
124  { return myCurrEntry.thing(); }
125  UT_Hash *hash() const
126  { return myCurrEntry.hash(); }
127 
128  int operator==(const traverser &cmp) const
129  {
130  return ((!myCurrList && !cmp.myCurrList) ||
131  ((myCurrList == cmp.myCurrList) &&
132  (myCurrEntry == cmp.myCurrEntry)));
133  }
134  int operator!=(const traverser &cmp) const
135  { return !(*this == cmp); }
136 
137  bool atEnd() const
138  {
139  return !myCurrList;
140  }
141 
143  {
144  advance();
145  return *this;
146  }
148  {
149  advance();
150  return *this;
151  }
152 
154  {
155  myCurrList = src.myCurrList;
156  myEndList = src.myEndList;
157  myCurrEntry = src.myCurrEntry;
158  return *this;
159  }
160 
161  void advance()
162  {
163  if( ++myCurrEntry == myCurrList->end() )
164  {
165  ++myCurrList;
166  advanceToNextList();
167  }
168  }
169 
170  private:
171  traverser(const UT_ThingList *list, const UT_ThingList *endlist)
172  : myCurrList(list), myEndList(endlist)
173  { advanceToNextList(); }
174 
175  void advanceToNextList()
176  {
177  while( myCurrList != myEndList &&
178  myCurrList->begin() == myCurrList->end() )
179  ++myCurrList;
180  if( myCurrList == myEndList )
181  myCurrList = 0;
182  else
183  myCurrEntry = myCurrList->begin();
184  }
185 
186  const UT_ThingList *myCurrList;
187  const UT_ThingList *myEndList;
188  UT_ThingList::traverser myCurrEntry;
189 
190  friend class UT_HashTable;
191  };
192 
193  traverser begin() const
194  {
195  if( myTable )
196  return traverser(myTable, myTable + mySize);
197  return traverser(); // same as end()
198  }
199  traverser end() const
200  { return traverser(); }
201 
202 private:
203  enum UT_AdjustType { UT_ADJUST_INCREASE, UT_ADJUST_DECREASE };
204  void adjustTableSize(UT_AdjustType adjust_type);
205  void allocateTableOfNewSize(int size);
206 
207  // Data:
208  UT_ThingList *myTable;
209  int mySize;
210  unsigned int myNbrEntries;
211  fpreal myMaxLoadFactor;
212  fpreal myMinLoadFactor;
213  bool myUseOrdered;
214  bool myIsTraversing;
215 };
216 
218 
219 #endif
int contains(const UT_Hash &symbol) const
Definition: UT_HashTable.h:43
Unsorted map container.
Definition: UT_Map.h:107
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:623
unsigned size() const
Definition: UT_HashTable.h:64
#define SYS_DEPRECATED_PUSH_DISABLE()
#define SYS_DEPRECATED_POP_DISABLE()
float getMaxLoadFactor() const
Definition: UT_HashTable.h:77
GLsizei const GLfloat * value
Definition: glcorearb.h:824
int count(const UT_Hash &symbol) const
Definition: UT_HashTable.h:46
UT_Thing & thing() const
Definition: UT_HashTable.h:123
#define UT_API
Definition: UT_API.h:14
T * findPointer(const UT_Hash &symbol) const
Definition: UT_HashTable.h:50
traverser(const traverser &src)
Definition: UT_HashTable.h:118
traverser & operator++()
Definition: UT_HashTable.h:142
int operator!=(const traverser &cmp) const
Definition: UT_HashTable.h:134
IMATH_HOSTDEVICE constexpr int cmp(T a, T b) IMATH_NOEXCEPT
Definition: ImathFun.h:84
#define SYS_DEPRECATED_REPLACE(__V__, __R__)
traverser & operator++(int)
Definition: UT_HashTable.h:147
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
UT_Hash * hash() const
Definition: UT_HashTable.h:125
virtual unsigned hash() const =0
GLenum GLenum GLsizei void * table
Definition: glad.h:5129
UT_API void outputStats(std::ostream &os, exint size, exint bucket_count, exint capacity, float load_factor, float min_load_factor, float max_load_factor)
GLsizeiptr size
Definition: glcorearb.h:664
float getMinLoadFactor() const
Definition: UT_HashTable.h:79
fpreal64 fpreal
Definition: SYS_Types.h:277
traverser begin() const
Definition: UT_HashTable.h:193
traverser & operator=(const traverser &src)
Definition: UT_HashTable.h:153
GU_API ComputeHierarchyResult traverse(const GU_Detail *gdp, GA_OffsetArray &roots, GA_OffsetArray &nodes, GA_OffsetArray &parents, UT_Map< GA_Offset, GA_OffsetArray > *children=nullptr)
traverser end() const
Definition: UT_HashTable.h:199
bool empty() const
Definition: UT_HashTable.h:65
bool OIIO_UTIL_API contains(string_view a, string_view b)
Does 'a' contain the string 'b' within it?
int operator==(const traverser &cmp) const
Definition: UT_HashTable.h:128
Definition: format.h:895
unsigned entries() const
Definition: UT_HashTable.h:63
GLenum src
Definition: glcorearb.h:1793