1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
template <class V> struct HashNode { HashNode(const V& data = V()) : _data(data) , _next(nullptr) {} V _data; HashNode<V>* _next; };
template <class K, class V, class KeyOfValue, class HFun> struct _HashIterator;
template <class K, class V, class KeyOfValue, class HFun> class HashTable { public: typedef HashNode<V> Node; typedef Node* pNode;
typedef _HashIterator<K, V, KeyOfValue, HFun> iterator;
template <class K, class V, class KeyOfValue, class HFun> friend struct _HashIterator;
iterator begin() { for (size_t i = 0; i < _ht.size(); ++i) { if (_ht[i]) { return iterator(_ht[i], this); } } return iterator(nullptr, this); }
iterator end() { return iterator(nullptr, this); }
HashTable(size_t N = 10) { _ht.resize(N); _size = 0; }
size_t hashIndex(const K& key, size_t sz) { HFun hf; return hf(key) % sz; }
std::pair<iterator, bool> insert(const V& data) { CheckCapacity(); KeyOfValue kov; int index = hashIndex(kov(data), _ht.size());
pNode cur = _ht[index]; while (cur) { if (kov(cur->_data) == kov(data)) { return std::make_pair(iterator(cur, this), false); } cur = cur->_next; }
cur = new Node(data); cur->_next = _ht[index]; _ht[index] = cur; ++_size; return std::make_pair(iterator(cur, this), true); }
size_t getNextPrime(size_t sz) { const int PRIMECOUNT = 28; const static size_t primeList[PRIMECOUNT] = { 53ul, 97ul, 193ul, 389ul, 769ul, 1543ul, 3079ul, 6151ul, 12289ul, 24593ul, 49157ul, 98317ul, 196613ul, 393241ul, 786433ul, 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul, 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul, 1610612741ul, 3221225473ul, 4294967291ul }; for (int i = 0; i < PRIMECOUNT; ++i) { if (primeList[i] > sz) { return primeList[i]; } } return primeList[PRIMECOUNT - 1]; }
void CheckCapacity() { if (_size == _ht.size()) { size_t newC = getNextPrime(_ht.size()); std::vector<pNode> newHt; newHt.resize(newC); KeyOfValue kov; for (size_t i = 0; i < _ht.size(); ++i) { pNode cur = _ht[i]; while (cur) { pNode next = cur->_next; int index = hashIndex(kov(cur->_data), newHt.size()); cur->_next = newHt[index]; newHt[index] = cur; cur = next; } _ht[i] = nullptr; } std::swap(_ht, newHt); } }
pNode Find(const K& key) { int index = key % _ht.size(); pNode cur = _ht[index]; KeyOfValue kov; while (cur) { if (kov(cur->_data) == key) { return cur; } cur = cur->_next; } return nullptr; }
bool Erase(const K& key) { int index = hashIndex(key, _ht.size()); pNode cur = _ht[index]; pNode parent = nullptr; KeyOfValue kov; while (cur) { if (kov(cur->_data) == key) { if (parent == nullptr) { _ht[index] = cur->_next; } else { parent->_next = cur->_next; } delete cur; --_size; return true; } parent = cur; cur = cur->_next; } return false; } private: std::vector<pNode> _ht; size_t _size; };
template <class K, class V, class KeyOfValue, class HFun> struct _HashIterator { typedef HashNode<V> Node; typedef Node* pNode;
typedef _HashIterator<K, V, KeyOfValue, HFun> Self; typedef HashTable<K, V, KeyOfValue, HFun> HTable;
_HashIterator(pNode node, HTable* pht) : _node(node) , _pht(pht) {}
V& operator*() { return _node->_data; }
V* operator->() { return &_node->_data; }
bool operator!=(const Self& it) { return _node != it._node; }
Self& operator++() { if (_node->_next) { _node = _node->_next; } else { KeyOfValue kov; size_t index = _pht->hashIndex(kov(_node->_data), _pht->_ht.size()); ++index; while (index < _pht->_ht.size()) { if (_pht->_ht[index]) { _node = _pht->_ht[index]; break; } ++index; } if (index == _pht->_ht.size()) { _node = nullptr; } } return *this; } private: pNode _node; HTable* _pht; };
|