opennurbs_hash_table.h
1 /*
2 //
3 // Copyright (c) 1993-2016 Robert McNeel & Associates. All rights reserved.
4 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
5 // McNeel & Associates.
6 //
7 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
8 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
9 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
10 //
11 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
12 //
13 ////////////////////////////////////////////////////////////////
14 */
15 
16 ////////////////////////////////////////////////////////////////
17 //
18 // defines ON_Hash32Table
19 //
20 ////////////////////////////////////////////////////////////////
21 
22 #if !defined(OPENNURBS_HASH_TABLE_INC_)
23 #define OPENNURBS_HASH_TABLE_INC_
24 
25 class ON_CLASS ON_Hash32TableItem
26 {
27 public:
28  ON_Hash32TableItem() = default;
29  ~ON_Hash32TableItem() = default;
30  ON_Hash32TableItem(const ON_Hash32TableItem&) = default;
31  ON_Hash32TableItem& operator=(const ON_Hash32TableItem&) = default;
32 
33 public:
34  ON__UINT32 HashTableSerialNumber() const;
35 
36  static ON__UINT32 Hash32FromSHA1Hash(
37  const class ON_SHA1_Hash& sha1_hash
38  );
39 
40  static ON__UINT32 Hash32FromId(
41  const ON_UUID& id
42  );
43 
44 private:
45  friend class ON_Hash32Table;
46  mutable ON_Hash32TableItem* m_internal_next = nullptr;
47  mutable ON__UINT32 m_internal_hash32 = 0;
48  mutable ON__UINT32 m_internal_hash_table_sn = 0;
49 };
50 
51 /*
52 Description:
53  A hash table designed to be used for items with high quality 32-bit hash values.
54 */
55 class ON_CLASS ON_Hash32Table
56 {
57 public:
59  ~ON_Hash32Table();
60 
61 private:
62  ON_Hash32Table(const ON_Hash32Table&) = delete;
63  ON_Hash32Table& operator=(const ON_Hash32Table&) = delete;
64 
65 public:
66  ON__UINT32 HashTableSerialNumber() const;
67 
68  /*
69  Description:
70  Adds an item to the hash table.
71  Parameters:
72  hash32 - [in]
73  item - [in/out]
74  Returns:
75  The added item.
76  */
77  bool AddItem(
78  ON__UINT32 hash32,
79  class ON_Hash32TableItem* item
80  );
81 
82  /*
83  Returns:
84  The first item in the hash table with hash = hash32.
85  Parameters:
86  hash32 - [in]
87  Remarks:
88  This function is used to find the first element in the hash table with the
89  specified hash32 falue. Use ON_Hash32TableItem.NextItemWithSameHash() to get
90  the next item in the has table with the same hash value.
91  */
92  class ON_Hash32TableItem* FirstItemWithHash(
93  ON__UINT32 hash32
94  ) const;
95 
96  class ON_Hash32TableItem* NextItemWithHash(
97  const class ON_Hash32TableItem* current_item
98  ) const;
99 
100  /*
101  Returns:
102  The first item in the hash table.
103  Remarks:
104  This function is used for iterating throught every element in the hash table.
105  */
106  class ON_Hash32TableItem* FirstTableItem(
107  ) const;
108 
109  /*
110  Returns:
111  The next item in the hash table.
112  Remarks:
113  This function is used for iterating throught every element in the hash table.
114  */
115  class ON_Hash32TableItem* NextTableItem(
116  const ON_Hash32TableItem* item
117  ) const;
118 
119  /*
120  Description:
121  Remove an item from the hash table. Caller is responsible for managing item memory.
122  Parameters:
123  item - [in/out]
124  If the item is removed, the has table serial number is set to zero.
125  Returns:
126  The true if the item was removed.
127  */
128  bool RemoveItem(
129  class ON_Hash32TableItem* item
130  );
131 
132  /*
133  Description:
134  Removes all hash table items. Caller is responsible for managing the item memory.
135  */
136  unsigned int RemoveAllItems();
137 
138  /*
139  Description:
140  Removes all hash table items.
141  For each item memset(item,0,fsp.SizeofElement()) and fsp.ReturnElement(item) are called.
142  */
143  unsigned int RemoveAllItems(
144  class ON_FixedSizePool& fsp
145  );
146 
147  /*
148  Returns:
149  Number of items in the hash table
150  */
151  unsigned int ItemCount() const;
152 
153  bool IsValid() const;
154 
155 private:
156  const ON__UINT32 m_hash_table_sn;
157  ON__UINT32 m_reserved = 0;
158  mutable ON__UINT32 m_hash_table_capacity = 0;
159  ON__UINT32 m_item_count = 0;
160  mutable class ON_Hash32TableItem** m_hash_table = nullptr;
161 
162  void Internal_AdjustTableCapacity(
163  ON__UINT32 item_count
164  );
165 };
166 
167 
168 #endif
ON_UUID is a 16 byte universally unique identifier.
Definition: opennurbs_uuid.h:32
Definition: opennurbs_sha1.h:19
Definition: opennurbs_fsp.h:19
A hash table designed to be used for items with high quality 32-bit hash values.
Definition: opennurbs_hash_table.h:54
Definition: opennurbs_hash_table.h:25