| 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_PSHASHSET_H |
| 31 | #define PSFOUNDATION_PSHASHSET_H |
| 32 | |
| 33 | #include "PsHashInternals.h" |
| 34 | |
| 35 | // TODO: make this doxy-format |
| 36 | |
| 37 | // This header defines two hash sets. Hash sets |
| 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 | // |
| 44 | // Sets have STL-like copying semantics, and properly initialize and destruct copies of objects |
| 45 | // |
| 46 | // There are two forms of set: coalesced and uncoalesced. Coalesced sets 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 | // HashSet<T>: |
| 51 | // bool insert(const T& k) amortized O(1) (exponential resize policy) |
| 52 | // bool contains(const T& k) const; O(1) |
| 53 | // bool erase(const T& k); O(1) |
| 54 | // uint32_t size() const; constant |
| 55 | // void reserve(uint32_t size); O(MAX(size, currentOccupancy)) |
| 56 | // void clear(); O(currentOccupancy) (with zero constant for objects without |
| 57 | // destructors) |
| 58 | // Iterator getIterator(); |
| 59 | // |
| 60 | // Use of iterators: |
| 61 | // |
| 62 | // for(HashSet::Iterator iter = test.getIterator(); !iter.done(); ++iter) |
| 63 | // myFunction(*iter); |
| 64 | // |
| 65 | // CoalescedHashSet<T> does not support getIterator, but instead supports |
| 66 | // const Key *getEntries(); |
| 67 | // |
| 68 | // insertion into a set already containing the element fails returning false, as does |
| 69 | // erasure of an element not in the set |
| 70 | // |
| 71 | |
| 72 | namespace physx |
| 73 | { |
| 74 | namespace shdfnd |
| 75 | { |
| 76 | template <class Key, class HashFn = Hash<Key>, class Allocator = NonTrackingAllocator> |
| 77 | class HashSet : public internal::HashSetBase<Key, HashFn, Allocator, false> |
| 78 | { |
| 79 | public: |
| 80 | typedef internal::HashSetBase<Key, HashFn, Allocator, false> HashSetBase; |
| 81 | typedef typename HashSetBase::Iterator Iterator; |
| 82 | |
| 83 | HashSet(uint32_t initialTableSize = 64, float loadFactor = 0.75f) : HashSetBase(initialTableSize, loadFactor) |
| 84 | { |
| 85 | } |
| 86 | HashSet(uint32_t initialTableSize, float loadFactor, const Allocator& alloc) |
| 87 | : HashSetBase(initialTableSize, loadFactor, alloc) |
| 88 | { |
| 89 | } |
| 90 | HashSet(const Allocator& alloc) : HashSetBase(64, 0.75f, alloc) |
| 91 | { |
| 92 | } |
| 93 | Iterator getIterator() |
| 94 | { |
| 95 | return Iterator(HashSetBase::mBase); |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | template <class Key, class HashFn = Hash<Key>, class Allocator = NonTrackingAllocator> |
| 100 | class CoalescedHashSet : public internal::HashSetBase<Key, HashFn, Allocator, true> |
| 101 | { |
| 102 | public: |
| 103 | typedef typename internal::HashSetBase<Key, HashFn, Allocator, true> HashSetBase; |
| 104 | |
| 105 | CoalescedHashSet(uint32_t initialTableSize = 64, float loadFactor = 0.75f) |
| 106 | : HashSetBase(initialTableSize, loadFactor) |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | CoalescedHashSet(uint32_t initialTableSize, float loadFactor, const Allocator& alloc) |
| 111 | : HashSetBase(initialTableSize, loadFactor, alloc) |
| 112 | { |
| 113 | } |
| 114 | CoalescedHashSet(const Allocator& alloc) : HashSetBase(64, 0.75f, alloc) |
| 115 | { |
| 116 | } |
| 117 | |
| 118 | const Key* getEntries() const |
| 119 | { |
| 120 | return HashSetBase::mBase.getEntries(); |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | } // namespace shdfnd |
| 125 | } // namespace physx |
| 126 | |
| 127 | #endif // #ifndef PSFOUNDATION_PSHASHSET_H |
| 128 | |