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_hash_map.cpp
28 #include "vogl_core.h"
29 #include "vogl_hash_map.h"
30 #include "vogl_rand.h"
37 counted_obj(uint v = 0)
43 counted_obj(const counted_obj &obj)
51 VOGL_ASSERT(m_count > 0);
59 operator size_t() const
64 bool operator==(const counted_obj &rhs) const
66 return m_val == rhs.m_val;
68 bool operator==(const uint rhs) const
74 uint counted_obj::m_count;
76 #define VOGL_HASHMAP_VERIFY(x) \
85 for (uint t = 0; t < 2000; t++)
89 typedef vogl::hash_map<counted_obj, counted_obj> my_hash_map;
92 const uint n = r0.irand(1, 100000);
101 for (uint i = 0; i < n; i++)
103 uint v = r1.urand32() & 0x7FFFFFFF;
104 my_hash_map::insert_result res = m.insert(counted_obj(v), counted_obj(v ^ 0xdeadbeef));
112 VOGL_HASHMAP_VERIFY(m.size() == count);
121 for (uint i = 0; i < n; i++)
123 uint v = r1.urand32() & 0x7FFFFFFF;
124 my_hash_map::const_iterator it = m.find(counted_obj(v));
125 VOGL_HASHMAP_VERIFY(it != m.end());
126 VOGL_HASHMAP_VERIFY(it->first == v);
127 VOGL_HASHMAP_VERIFY(it->second == (v ^ 0xdeadbeef));
130 for (uint t = 0; t < 2; t++)
132 const uint nd = r0.irand(1, q.size() + 1);
133 for (uint i = 0; i < nd; i++)
135 uint p = r0.irand(0, q.size());
142 bool s = m.erase(counted_obj(k));
143 VOGL_HASHMAP_VERIFY(s);
147 typedef vogl::hash_map<uint, empty_type> uint_hash_set;
150 for (uint i = 0; i < q.size(); i++)
156 my_hash_map::const_iterator it = m.find(counted_obj(v));
157 VOGL_HASHMAP_VERIFY(it != m.end());
158 VOGL_HASHMAP_VERIFY(it->first == (uint)v);
159 VOGL_HASHMAP_VERIFY(it->second == ((uint)v ^ 0xdeadbeef));
165 my_hash_map::const_iterator it = m.find(counted_obj(-v - 1));
166 VOGL_HASHMAP_VERIFY(it == m.end());
170 uint found_count = 0;
171 for (my_hash_map::const_iterator it = m.begin(); it != m.end(); ++it)
173 VOGL_HASHMAP_VERIFY(it->second == ((uint)it->first ^ 0xdeadbeef));
175 uint_hash_set::const_iterator fit(s.find((uint)it->first));
176 VOGL_HASHMAP_VERIFY(fit != s.end());
178 VOGL_HASHMAP_VERIFY(fit->first == it->first);
183 VOGL_HASHMAP_VERIFY(found_count == s.size());
186 VOGL_HASHMAP_VERIFY(counted_obj::m_count == m.size() * 2);