1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 // File: vogl_rh_hash_map.cpp
28 #include "vogl_core.h"
29 #include "vogl_rh_hash_map.h"
30 #include "vogl_rand.h"
37 rh_counted_obj(uint v = 0)
43 rh_counted_obj(const rh_counted_obj &obj)
51 VOGL_ASSERT(m_count > 0);
59 operator size_t() const
64 bool operator==(const rh_counted_obj &rhs) const
66 return m_val == rhs.m_val;
68 bool operator==(const uint rhs) const
74 uint rh_counted_obj::m_count;
76 #define VOGL_HASHMAP_VERIFY(x) \
80 bool rh_hash_map_test()
85 for (uint t = 0; t < 2000; t++)
89 typedef vogl::rh_hash_map<rh_counted_obj, rh_counted_obj> my_hash_map;
92 const uint n = r0.irand(1, 100000);
100 VOGL_HASHMAP_VERIFY(m.check());
103 for (uint i = 0; i < n; i++)
105 uint v = r1.urand32() & 0x7FFFFFFF;
106 my_hash_map::insert_result res = m.insert(rh_counted_obj(v), rh_counted_obj(v ^ 0xdeadbeef));
114 VOGL_HASHMAP_VERIFY(m.check());
116 VOGL_HASHMAP_VERIFY(m.size() == count);
121 VOGL_HASHMAP_VERIFY(m == cm);
122 VOGL_HASHMAP_VERIFY(!(m != cm));
123 VOGL_HASHMAP_VERIFY(cm == m);
124 VOGL_HASHMAP_VERIFY(cm.check());
127 VOGL_HASHMAP_VERIFY(m.check());
129 VOGL_HASHMAP_VERIFY(cm.check());
131 for (uint i = 0; i < n; i++)
133 uint v = r1.urand32() & 0x7FFFFFFF;
134 my_hash_map::const_iterator it = m.find(rh_counted_obj(v));
135 VOGL_HASHMAP_VERIFY(it != m.end());
136 VOGL_HASHMAP_VERIFY(it->first == v);
137 VOGL_HASHMAP_VERIFY(it->second == (v ^ 0xdeadbeef));
140 for (uint t = 0; t < 2; t++)
142 const uint nd = r0.irand(1, q.size() + 1);
143 for (uint i = 0; i < nd; i++)
145 uint p = r0.irand(0, q.size());
152 if (r0.irand(0, 1) == 0)
154 bool s = m.erase(m.find(rh_counted_obj(k)));
155 VOGL_HASHMAP_VERIFY(s);
159 bool s = m.erase(rh_counted_obj(k));
160 VOGL_HASHMAP_VERIFY(s);
165 typedef vogl::rh_hash_map<uint, empty_type> uint_hash_set;
168 for (uint i = 0; i < q.size(); i++)
174 my_hash_map::const_iterator it = m.find(rh_counted_obj(v));
175 VOGL_HASHMAP_VERIFY(it != m.end());
176 VOGL_HASHMAP_VERIFY(it->first == (uint)v);
177 VOGL_HASHMAP_VERIFY(it->second == ((uint)v ^ 0xdeadbeef));
183 my_hash_map::const_iterator it = m.find(rh_counted_obj(-v - 1));
184 VOGL_HASHMAP_VERIFY(it == m.end());
188 uint found_count = 0;
189 for (my_hash_map::const_iterator it = m.begin(); it != m.end(); ++it)
191 VOGL_HASHMAP_VERIFY(it->second == ((uint)it->first ^ 0xdeadbeef));
193 uint_hash_set::const_iterator fit(s.find((uint)it->first));
194 VOGL_HASHMAP_VERIFY(fit != s.end());
196 VOGL_HASHMAP_VERIFY(fit->first == it->first);
201 VOGL_HASHMAP_VERIFY(found_count == s.size());
204 VOGL_HASHMAP_VERIFY(m.check());
206 VOGL_HASHMAP_VERIFY(rh_counted_obj::m_count == m.size() * 2);