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