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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
| #pragma once #include<iostream> #include<vector> using namespace std;
namespace BsyOpen { template<class K> struct GetWay { const K& operator()(const K& k) { return k; } };
template<> struct GetWay<string> { size_t operator()(const string& k) { size_t way = 0; for (size_t i = 0; i < k.size(); ++i) { way *= 131; way += k[i]; } return way; } };
template<class T> struct HashNode { T _data; HashNode<T>* _next;
HashNode(const T& data) :_data(data) ,_next(nullptr) {} };
template<class K, class T, class TheKey, class GetWayy> class HashTable;
template<class K,class T,class TheKey,class GetWayy> struct __HashIterator { typedef __HashIterator< K, T, TheKey, GetWayy> Self; typedef HashTable<K, T, TheKey, GetWayy> HT; typedef HashNode<T> Node; Node* _node; HT* _pht;
__HashIterator(Node* node,HT* pht) :_node(node) ,_pht(pht) {}
T& operator*() { return _node->_data; }
T* operator->() { return &_node->_data; }
Self operator++() { if (_node->_next) { _node = _node->_next; } else { TheKey thekey; GetWayy getway; size_t i = getway(thekey(_node->_data)) % _pht->_tables.size(); i++; for (; i < _pht->_tables.size(); ++i) { Node* cur = _pht->_tables[i]; if (cur) { _node = cur; return *this; } } _node = nullptr; } return *this; }
bool operator!=(const Self& s) { return _node != s._node; } };
template<class K,class T,class TheKey,class GetWayy> class HashTable { public: friend struct __HashIterator< K, T, TheKey, GetWayy>; typedef HashNode<T> Node; typedef __HashIterator< K, T, TheKey,GetWayy> iterator;
iterator begin() { for (size_t i = 0; i < _tables.size(); ++i) { if (_tables[i]) { return iterator(_tables[i],this); } } return end(); }
iterator end() { return iterator(nullptr,this); }
~HashTable() { Clear(); }
void Clear() { for (size_t i = 0; i < _tables.size(); ++i) { Node* cur = _tables[i]; while (cur) { Node* next = cur->_next; delete cur; cur = next; } _tables[i] = nullptr; } }
size_t GetNextPrime(size_t num) { const int primecount = 28; const size_t primelist[primecount] = { 53,97,193,389,769, 1543,3079,6151,12289,24593, 49157,98317,1996613,393241,786433, 1572869,3145739,6291469,12582917,25165843, 50331653,100663319,201326611,402653189,805306457, 1610612741,3221225473,4294967291 };
for (size_t i = 0; i < primecount; ++i) { if (primelist[i] > num) { return primelist[i]; } } return primelist[primecount - 1]; }
pair<iterator,bool> Insert(const T& data) { TheKey thekey; GetWayy getway;
if (_num == _tables.size()) { vector<Node*> newtable; size_t newsize = GetNextPrime(_num); newtable.resize(newsize); for (size_t i = 0; i < _tables.size(); ++i) { Node* cur = _tables[i]; while (cur) { Node* next = cur->_next; size_t index = getway(thekey(cur->_data)) % newtable.size(); cur->_next = newtable[index]; newtable[index] = cur; cur = next; } _tables[i] = nullptr; } _tables.swap(newtable); }
size_t index = getway(thekey(data)) % _tables.size();
Node* cur = _tables[index]; while (cur) { if (thekey(cur->_data) == thekey(data)) return make_pair(iterator(cur,this),false); else cur = cur->_next; }
Node* newnode = new Node(data); newnode->_next = _tables[index]; _tables[index] = newnode;
++_num; return make_pair(iterator(newnode, this), true); }
Node* Find(const K& k) { TheKey thekey; GetWayy getway; size_t index = getway(k) % _tables.size(); Node* cur = _tables[index]; while (cur) { if (thekey(cur->_data) == k) return cur; else cur = cur->_next; } return nullptr; }
bool Erase(const K& k) { TheKey thekey; GetWayy getway; size_t index = getway(k) % _tables.size(); Node* prev = nullptr; Node* cur = _tables[index]; while (cur) { if (thekey(cur->_data) == k) {
if (prev == nullptr) _tables[index] = cur->_next; else prev->_next = cur->_next; delete cur; return true; } else { prev = cur; cur = cur->_next; } } return false; }
private: vector<Node*> _tables; size_t _num = 0; }; }
|