| 31 | |
| 32 | template<typename T, typename T_KEY = T, bool MEMMOVE_SAFE = false> |
| 33 | class semiorderedset |
| 34 | { |
| 35 | typedef compactvector<T, MEMMOVE_SAFE> vector_type; |
| 36 | |
| 37 | friend struct setiter; |
| 38 | std::vector<CowPtr<vector_type>> m_data; |
| 39 | size_t celem = 0; |
| 40 | static const size_t bits_min = 8; |
| 41 | size_t bits = bits_min; |
| 42 | size_t idxRehash = (1ULL << bits_min); |
| 43 | int cfPauseRehash = 0; |
| 44 | |
| 45 | inline size_t targetElementsPerBucket() |
| 46 | { |
| 47 | // Aim for roughly 4 cache lines per bucket (determined by imperical testing) |
| 48 | // lower values are faster but use more memory |
| 49 | if (g_semiOrderedSetTargetBucketSize == 0) |
| 50 | return std::max((64/sizeof(T))*8, (size_t)2); |
| 51 | else |
| 52 | return g_semiOrderedSetTargetBucketSize; |
| 53 | } |
| 54 | |
| 55 | public: |
| 56 | semiorderedset(size_t bitsStart = 0) |
| 57 | { |
| 58 | if (bitsStart < bits_min) |
| 59 | bitsStart = bits_min; |
| 60 | bits = bitsStart; |
| 61 | m_data.resize((1ULL << bits)); |
| 62 | } |
| 63 | |
| 64 | struct setiter |
| 65 | { |
| 66 | semiorderedset *set; |
| 67 | size_t idxPrimary = 0; |
| 68 | size_t idxSecondary = 0; |
| 69 | |
| 70 | setiter(const semiorderedset *set) |
| 71 | { |
| 72 | this->set = (semiorderedset*)set; |
| 73 | } |
| 74 | |
| 75 | bool operator==(const setiter &other) const |
| 76 | { |
| 77 | return (idxPrimary == other.idxPrimary) && (idxSecondary == other.idxSecondary); |
| 78 | } |
| 79 | |
| 80 | bool operator!=(const setiter &other) const { return !operator==(other); } |
| 81 | |
| 82 | inline T &operator*() { return set->m_data[idxPrimary]->operator[](idxSecondary); } |
| 83 | inline const T &operator*() const { return set->m_data[idxPrimary]->operator[](idxSecondary); } |
| 84 | |
| 85 | inline T *operator->() { return &set->m_data[idxPrimary]->operator[](idxSecondary); } |
| 86 | inline const T *operator->() const { return &set->m_data[idxPrimary]->operator[](idxSecondary); } |
| 87 | }; |
| 88 | |
| 89 | setiter find(const T_KEY &key) |
| 90 | { |