37 template <
class T>
class Set
45 ThrUsagePct = MaxUsagePct * 3 / 4
55 Set (
void) { init(); }
59 int getSize (
void)
const {
return m_numItems; }
66 void clear (
void) { m_numItems = 0; m_numNonEmpty = 0; memset(m_hashes, Empty, m_capacity *
sizeof(
S32)); }
67 void reset (
void) {
delete[] m_hashes;
delete[] m_values; init(); }
74 T&
remove (
const T&
value);
80 const T&
getSlot (
int slot)
const {
FW_ASSERT(m_hashes[slot] >= 0);
return m_values[slot]; }
88 void init (
void) { m_capacity = 0; m_numItems = 0; m_numNonEmpty = 0; m_hashes =
NULL; m_values =
NULL; }
90 void rehash (
int capacity);
110 template <
class K,
class V>
class Hash
122 int getSize (
void)
const {
return m_entries.getSize(); }
123 bool contains (
const K& key)
const {
return m_entries.contains(keyEntry(key)); }
130 const Entry&
getEntry (
const K& key)
const {
return m_entries.get(keyEntry(key)); }
137 void clear (
void) { m_entries.clear(); }
138 void reset (
void) { m_entries.reset(); }
139 void setCapacity (
int numItems) { m_entries.setCapacity(numItems); }
144 V&
add (
const K& key) {
Entry& slot = m_entries.addNoAssign(keyEntry(key)); slot.
key = key;
return slot.
value; }
145 V&
remove (
const K& key) {
return m_entries.remove(keyEntry(key)).value; }
148 int findSlot (
const K& key)
const {
return m_entries.findSlot(keyEntry(key)); }
149 int firstSlot (
void)
const {
return m_entries.firstSlot(); }
150 int nextSlot (
int slot)
const {
return m_entries.nextSlot(slot); }
151 const Entry&
getSlot (
int slot)
const {
return m_entries.getSlot(slot); }
155 const V&
operator[] (
const K& key)
const {
return get(key); }
159 static const Entry& keyEntry (
const K& key) {
return *(
Entry*)&key; }
162 Set<Entry> m_entries;
169 #define FW_HASH_MAGIC (0x9e3779b9u)
172 #define FW_JENKINS_MIX(a, b, c) \
173 a -= b; a -= c; a ^= (c>>13); \
174 b -= c; b -= a; b ^= (a<<8); \
175 c -= a; c -= b; c ^= (b>>13); \
176 a -= b; a -= c; a ^= (c>>12); \
177 b -= c; b -= a; b ^= (a<<16); \
178 c -= a; c -= b; c ^= (b>>5); \
179 a -= b; a -= c; a ^= (c>>3); \
180 b -= c; b -= a; b ^= (a<<10); \
181 c -= a; c -= b; c ^= (b>>15);
186 inline bool equalsBuffer (
const void* ptrA,
const void* ptrB,
int size) {
return (memcmp(ptrA, ptrB, size) == 0); }
187 inline bool equalsBuffer (
const void* ptrA,
int sizeA,
const void* ptrB,
int sizeB) {
return (sizeA == sizeB && memcmp(ptrA, ptrB, sizeA) == 0); }
195 template <
class T>
inline bool equalsArray (
const T* ptrA,
int sizeA,
const T* ptrB,
int sizeB);
198 template <
class T>
inline bool equals (
const T& a,
const T& b) {
return equalsBuffer(&a, &b,
sizeof(T)); }
227 template <>
inline bool equals<S8> (
const S8& a,
const S8& b) {
return (a == b); }
228 template <>
inline bool equals<U8> (
const U8& a,
const U8& b) {
return (a == b); }
279 template <
class T,
class TT>
inline bool equals(TT*
const& a, TT*
const& b) {
return (a == b); }
311 int capacity = BlockSize;
312 S64 limit = (
S64)
max(numItems, m_numItems, (MinBytes + (
S32)
sizeof(T) - 1) / (
S32)
sizeof(T)) * 100;
313 while ((
S64)capacity * MaxUsagePct < limit)
316 if (capacity != m_capacity)
328 if (!other.m_numItems)
331 m_capacity = other.m_capacity;
332 m_numItems = other.m_numItems;
333 m_numNonEmpty = other.m_numNonEmpty;
334 m_hashes =
new S32[m_capacity];
335 m_values =
new T[m_capacity];
337 memcpy(m_hashes, other.m_hashes, m_capacity *
sizeof(
S32));
338 for (
int i = 0; i < m_capacity; i++)
339 m_values[i] = other.m_values[i];
355 else if ((
S64)m_numNonEmpty * 100 >= (
S64)m_capacity * MaxUsagePct)
357 int cap = m_capacity;
358 if ((
S64)m_numItems * 100 >= (
S64)cap * ThrUsagePct)
365 S32 hashValue = hash<T>(
value) >> 1;
366 int slot = findSlot(value, hashValue,
true);
372 if (m_hashes[slot] == Empty)
375 m_hashes[slot] = hashValue;
376 return m_values[slot];
383 int slot = findSlot(value, hash<T>(value) >> 1,
false);
387 m_hashes[slot] = Removed;
388 return m_values[slot];
395 int slot = findSlot(value, hash<T>(value) >> 1,
false);
398 T old = m_values[slot];
399 m_values[slot] =
value;
407 FW_ASSERT(slot >= -1 && slot < m_capacity);
408 for (slot++; slot < m_capacity; slot++)
409 if (m_hashes[slot] >= 0)
422 int blockMask = (m_capacity - 1) & -BlockSize;
423 int firstSlot = hashValue;
424 int firstBlock = firstSlot & blockMask;
425 int blockStep = BlockSize * 3 + ((hashValue >> 17) & (-4 * BlockSize));
427 int block = firstBlock;
432 for (
int i = 0; i < BlockSize; i++)
434 int slot = block + ((firstSlot + i) & (BlockSize - 1));
435 if (m_hashes[slot] < 0)
441 for (
int i = 0; i < BlockSize; i++)
443 int slot = block + ((firstSlot + i) & (BlockSize - 1));
444 S32 slotHash = m_hashes[slot];
446 if (slotHash == Empty)
449 if (slotHash == hashValue && equals<T>(m_values[slot], value))
454 block = (block + blockStep) & blockMask;
455 blockStep += BlockSize * 4;
457 while (block != firstBlock);
463 template <
class T>
void Set<T>::rehash(
int capacity)
468 int oldCapacity = m_capacity;
469 S32* oldHashes = m_hashes;
470 T* oldValues = m_values;
471 m_capacity = capacity;
472 m_numNonEmpty = m_numItems;
473 m_hashes =
new S32[capacity];
474 m_values =
new T[capacity];
476 memset(m_hashes, Empty, capacity *
sizeof(
S32));
478 for (
int i = 0; i < oldCapacity; i++)
480 S32 oldHash = oldHashes[i];
484 const T& oldValue = oldValues[i];
485 int slot = findSlot(oldValue, oldHash,
true);
488 m_hashes[slot] = oldHash;
489 m_values[slot] = oldValue;
498 template <
class T>
bool equalsArray(
const T* ptrA,
int sizeA,
const T* ptrB,
int sizeB)
503 for (
int i = 0; i < sizeA; i++)
504 if (!equals<T>(ptrA[i], ptrB[i]))
522 a += hash<T>(ptr[0]);
523 b += hash<T>(ptr[1]);
524 c += hash<T>(ptr[2]);
532 case 2: b += hash<T>(ptr[1]);
533 case 1: a += hash<T>(ptr[0]);
Set< T > & operator=(const Set< T > &other)
int nextSlot(int slot) const
bool equalsArray< U64 >(const U64 *ptrA, int sizeA, const U64 *ptrB, int sizeB)
U32 hash< S16 >(const S16 &value)
bool contains(const K &key) const
U32 hashBuffer(const void *ptr, int size)
const T & operator[](const T &value) const
const V & operator[](const K &key) const
int findSlot(const T &value) const
bool equals< GenericHashKey >(const GenericHashKey &a, const GenericHashKey &b)
Entry & getSlot(int slot)
bool equals< U32 >(const U32 &a, const U32 &b)
void set(const Hash< K, V > &other)
T * search(const T &value)
U32 hashArray< U16 >(const U16 *ptr, int size)
int findSlot(const K &key) const
bool equalsArray< F64 >(const F64 *ptrA, int sizeA, const F64 *ptrB, int sizeB)
bool equals< U16 >(const U16 &a, const U16 &b)
Entry & getEntry(const K &key)
const Entry * searchEntry(const K &key) const
Entry * searchEntry(const K &key)
bool equalsArray< S16 >(const S16 *ptrA, int sizeA, const S16 *ptrB, int sizeB)
bool equalsArray< S64 >(const S64 *ptrA, int sizeA, const S64 *ptrB, int sizeB)
bool equals< String >(const String &a, const String &b)
const T * search(const T &value) const
bool equals< U8 >(const U8 &a, const U8 &b)
U32 hash< Vec4i >(const Vec4i &value)
bool equals< U64 >(const U64 &a, const U64 &b)
bool equals< Vec2i >(const Vec2i &a, const Vec2i &b)
void set(const Set< T > &other)
Set< Entry > & getEntries(void)
const K & getKey(const K &key) const
int firstSlot(void) const
Hash< K, V > & operator=(const Hash< K, V > &other)
GenericHashKey(const T *p)
bool equals< F32 >(const F32 &a, const F32 &b)
U32 hash< F32 >(const F32 &value)
U32 hash< U64 >(const U64 &value)
bool equals< Vec3i >(const Vec3i &a, const Vec3i &b)
U32 hashArray< U64 >(const U64 *ptr, int size)
T & addNoAssign(const T &value)
U32 hash< Vec2i >(const Vec2i &value)
const Entry & getSlot(int slot) const
U32 hashBufferAlign(const void *ptr, int size)
bool equalsArray< U32 >(const U32 *ptrA, int sizeA, const U32 *ptrB, int sizeB)
U32 hashArray< S8 >(const S8 *ptr, int size)
bool contains(const T &value) const
bool equals< S64 >(const S64 &a, const S64 &b)
GenericHashKey(const void *p, int s)
bool equalsBuffer(const void *ptrA, const void *ptrB, int size)
bool equals< Vec4f >(const Vec4f &a, const Vec4f &b)
U32 hash< S64 >(const S64 &value)
void setCapacity(int numItems)
bool equals< Mat2f >(const Mat2f &a, const Mat2f &b)
bool equalsArray< S32 >(const S32 *ptrA, int sizeA, const S32 *ptrB, int sizeB)
bool equalsArray< U8 >(const U8 *ptrA, int sizeA, const U8 *ptrB, int sizeB)
bool equals< Mat4f >(const Mat4f &a, const Mat4f &b)
U32 hashArray< U8 >(const U8 *ptr, int size)
FW_CUDA_FUNC T max(const VectorBase< T, L, S > &v)
U32 hash< Vec3i >(const Vec3i &value)
U32 hashArray< F32 >(const F32 *ptr, int size)
int nextSlot(int slot) const
const Entry & getEntry(const K &key) const
bool equals< Vec3f >(const Vec3f &a, const Vec3f &b)
const T & getSlot(int slot) const
U32 hashArray< S64 >(const S64 *ptr, int size)
T replace(const T &value)
U32 hashArray< U32 >(const U32 *ptr, int size)
U32 hashArray< S32 >(const S32 *ptr, int size)
bool equals(const T &a, const T &b)
Set(const Set< T > &other)
FW_CUDA_FUNC U64 doubleToBits(F64 a)
V & add(const K &key, const V &value)
bool equals< S8 >(const S8 &a, const S8 &b)
U32 hashBits(U32 a, U32 b=FW_HASH_MAGIC, U32 c=0)
U32 hash< F64 >(const F64 &value)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction f
bool equals< S32 >(const S32 &a, const S32 &b)
K * searchKey(const K &key)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value
U32 hash< U8 >(const U8 &value)
U32 hashArray(const T *ptr, int size)
U32 hash< Mat2f >(const Mat2f &value)
const K * searchKey(const K &key) const
V replace(const K &key, const V &value)
U32 hashArray< S16 >(const S16 *ptr, int size)
bool equalsArray< U16 >(const U16 *ptrA, int sizeA, const U16 *ptrB, int sizeB)
U32 hash< S8 >(const S8 &value)
U32 hash< String >(const String &value)
U32 hash< Mat4f >(const Mat4f &value)
bool equals< F64 >(const F64 &a, const F64 &b)
bool equalsArray< F32 >(const F32 *ptrA, int sizeA, const F32 *ptrB, int sizeB)
T & remove(const T &value)
const V * search(const K &key) const
#define FW_JENKINS_MIX(a, b, c)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void * p
U32 hash< GenericHashKey >(const GenericHashKey &value)
U32 hashArray< F64 >(const F64 *ptr, int size)
Hash(const Hash< K, V > &other)
U32 hash< Mat3f >(const Mat3f &value)
U32 hash< U32 >(const U32 &value)
U32 hash< Vec3f >(const Vec3f &value)
void setCapacity(int numItems)
bool equalsArray(const T *ptrA, int sizeA, const T *ptrB, int sizeB)
const T * getPtr(S32idx=0) const
const Set< Entry > & getEntries(void) const
U32 hash< Vec2f >(const Vec2f &value)
bool equals< Mat3f >(const Mat3f &a, const Mat3f &b)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
U32 hash< U16 >(const U16 &value)
U32 hash< S32 >(const S32 &value)
bool equals< Vec2f >(const Vec2f &a, const Vec2f &b)
bool equals< Vec4i >(const Vec4i &a, const Vec4i &b)
int firstSlot(void) const
bool equalsArray< S8 >(const S8 *ptrA, int sizeA, const S8 *ptrB, int sizeB)
U32 hash< Vec4f >(const Vec4f &value)
bool equals< S16 >(const S16 &a, const S16 &b)