RavEngine
Loading...
Searching...
No Matches
PxHashMap.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef PX_HASHMAP_H
30#define PX_HASHMAP_H
31
32#include "foundation/PxHashInternals.h"
33
34// TODO: make this doxy-format
35//
36// This header defines two hash maps. Hash maps
37// * support custom initial table sizes (rounded up internally to power-of-2)
38// * support custom static allocator objects
39// * auto-resize, based on a load factor (i.e. a 64-entry .75 load factor hash will resize
40// when the 49th element is inserted)
41// * are based on open hashing
42// * have O(1) contains, erase
43//
44// Maps have STL-like copying semantics, and properly initialize and destruct copies of objects
45//
46// There are two forms of map: coalesced and uncoalesced. Coalesced maps keep the entries in the
47// initial segment of an array, so are fast to iterate over; however deletion is approximately
48// twice as expensive.
49//
50// HashMap<T>:
51// bool insert(const Key& k, const Value& v) O(1) amortized (exponential resize policy)
52// Value & operator[](const Key& k) O(1) for existing objects, else O(1) amortized
53// const Entry * find(const Key& k); O(1)
54// bool erase(const T& k); O(1)
55// uint32_t size(); constant
56// void reserve(uint32_t size); O(MAX(currentOccupancy,size))
57// void clear(); O(currentOccupancy) (with zero constant for objects
58// without
59// destructors)
60// Iterator getIterator();
61//
62// operator[] creates an entry if one does not exist, initializing with the default constructor.
63// CoalescedHashMap<T> does not support getIterator, but instead supports
64// const Key *getEntries();
65//
66// Use of iterators:
67//
68// for(HashMap::Iterator iter = test.getIterator(); !iter.done(); ++iter)
69// myFunction(iter->first, iter->second);
70
71#if !PX_DOXYGEN
72namespace physx
73{
74#endif
75
76template <class Key, class Value, class HashFn = PxHash<Key>, class Allocator = PxAllocator>
77class PxHashMap : public physx::PxHashMapBase<Key, Value, HashFn, Allocator>
78{
79 public:
81 typedef typename HashMapBase::Iterator Iterator;
82
83 PxHashMap(uint32_t initialTableSize = 64, float loadFactor = 0.75f) : HashMapBase(initialTableSize, loadFactor)
84 {
85 }
86 PxHashMap(uint32_t initialTableSize, float loadFactor, const Allocator& alloc)
87 : HashMapBase(initialTableSize, loadFactor, alloc)
88 {
89 }
90 PxHashMap(const Allocator& alloc) : HashMapBase(64, 0.75f, alloc)
91 {
92 }
93 Iterator getIterator()
94 {
95 return Iterator(HashMapBase::mBase);
96 }
97};
98
99template <class Key, class Value, class HashFn = PxHash<Key>, class Allocator = PxAllocator>
100class PxCoalescedHashMap : public physx::PxHashMapBase<Key, Value, HashFn, Allocator>
101{
102 public:
104
105 PxCoalescedHashMap(uint32_t initialTableSize = 64, float loadFactor = 0.75f)
106 : HashMapBase(initialTableSize, loadFactor)
107 {
108 }
109 const PxPair<const Key, Value>* getEntries() const
110 {
111 return HashMapBase::mBase.getEntries();
112 }
113};
114#if !PX_DOXYGEN
115} // namespace physx
116#endif
117
118#endif
119
Definition PxHashMap.h:101
Definition PxHashInternals.h:453
Definition PxHashInternals.h:695
Definition PxHashMap.h:78
Definition PxBasicTemplates.h:67
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39