diff --git a/main.cpp b/main.cpp index 43b5e9dd..dd21dab9 100644 --- a/main.cpp +++ b/main.cpp @@ -143,21 +143,22 @@ public: T & operator [](const Key & key) { if (pih_content.isEmpty()) _rehash(1); - int i = _index(key); + uint k = piHash(key); + int i = _index(k); if (i < pih_content.size_s()) { PIVector & hv(pih_content[i]); if (hv.size_s() == 1) { - if (hv[0].key == key) + if (hv[0].key == k) return hv[0].value; } for (int j = 0; j < hv.size_s(); ++j) - if (hv[j].key == key) + if (hv[j].key == k) return hv[j].value; } - if (pih_content[i].size_s() >= 2) + if (pih_content[i].size_s() >= 3) _rehash(pih_content.size_s() * 2); - i = _index(key); - pih_content[i] << HashEntry(key); + i = _index(k); + pih_content[i] << HashEntry(k); return pih_content[i].back().value; } const T operator [](const Key & key) const {return value(key);} @@ -183,11 +184,12 @@ public: PIHash & reserve(size_t new_size) {_rehash(new_size); return *this;} PIHash & remove(const Key & key) { - int i = _index(key); + uint k = piHash(key); + int i = _index(k); if (i >= pih_content.size_s()) return *this; PIVector & hv(pih_content[i]); for (int j = 0; j < hv.size_s(); ++j) - if (hv[j].key == key) { + if (hv[j].key == k) { hv.remove(j); --j; } @@ -205,15 +207,12 @@ public: return *this; } const T value(const Key & key, const T & default_ = T()) const { - int i = _index(key); + uint k = piHash(key); + int i = _index(k); if (i >= pih_content.size_s()) return default_; - PIVector & hv(pih_content[i]); - if (hv.size_s() == 1) { - if (hv[0].key == key) - return hv[0].value; - } + const PIVector & hv(pih_content[i]); for (int j = 0; j < hv.size_s(); ++j) - if (hv[j].key == key) + if (hv[j].key == k) return hv[j].value; return default_; } @@ -224,7 +223,7 @@ public: ret << pih_content[i][j].value; return ret; } - Key key(const T & value_, const Key & default_ = Key()) const { + /*Key key(const T & value_, const Key & default_ = Key()) const { for (int i = 0; i < pih_content.size_s(); ++i) for (int j = 0; j < pih_content[i].size_s(); ++j) if (pih_content[i][j].value == value_) @@ -237,7 +236,7 @@ public: for (int j = 0; j < pih_content[i].size_s(); ++j) ret << pih_content[i][j].key; return ret; - } + }*/ /*void dump() { piCout << "PIHash" << size() << "entries" << PICoutManipulators::NewLine << "content:"; @@ -250,8 +249,8 @@ public: protected: struct HashEntry { - HashEntry(Key k = Key(), const T & v = T()): key(k), value(v) {;} - Key key; + HashEntry(uint k = 0, const T & v = T()): key(k), value(v) {;} + uint key; T value; bool operator ==(const HashEntry & s) const {return key == s.key;} bool operator !=(const HashEntry & s) const {return key != s.key;} @@ -278,8 +277,8 @@ protected: while (s_ >> t) ++t; return (1 << t); } - int _index(const Key & k) { - return piHash(k) % pih_content.size_s(); + int _index(const uint & k) const { + return k % pih_content.size_s(); } void _rehash(int ns) { ns = asize(ns); @@ -289,7 +288,7 @@ protected: for (int i = 0; i < pih_content.size_s(); ++i) { for (int j = 0; j < pih_content[i].size_s(); ++j) { HashEntry & e(pih_content[i][j]); - int ni = piHash(e.key) % ns; + int ni = e.key % ns; nhc[ni] << e; } } @@ -327,14 +326,42 @@ int main() { h["5"] = "123"; h["6"] = "321"; h["7"] = "aaa"; + h["7"] = "aaa!!!!!"; piCout << h.size() << h.capacity(); //h.reserve(64); - piCout << h.keys() << h.values(); + //piCout << h.keys() << h.values(); //piCout << (1 << 2); - /*PIHash v2; - for (int i=0; i<1000; ++i) { - v2 << PIString::fromNumber(randomi()); - }*/ - piCout << h; + PIHash h2; + PIMap m2; + PIString prefix = "1234567890"; + PITimeMeasurer tm; + double el = 0.; + + tm.reset(); + for (int i=0; i<10000; ++i) { + h2[prefix + PIString::fromNumber(i)+"1234567890"] = PIString::fromNumber(randomi()); + } + el = tm.elapsed_m(); piCout << el << h2.capacity(); + + tm.reset(); + for (int i=0; i<10000; ++i) { + m2[prefix + PIString::fromNumber(i)+"1234567890"] = PIString::fromNumber(randomi()); + } + el = tm.elapsed_m(); piCout << el; + piCout << "*********"; + + PIString _s; + tm.reset(); + for (int i=0; i<10000; ++i) { + _s = h2.value(prefix + PIString::fromNumber(i)+"1234567890"); + } + el = tm.elapsed_m(); piCout << el << h2.capacity(); + + tm.reset(); + for (int i=0; i<10000; ++i) { + _s = m2.value(prefix + PIString::fromNumber(i)+"1234567890"); + } + el = tm.elapsed_m(); piCout << el; + return 0; } diff --git a/src_main/core/pistring.cpp b/src_main/core/pistring.cpp index 02338221..8a50c485 100755 --- a/src_main/core/pistring.cpp +++ b/src_main/core/pistring.cpp @@ -419,7 +419,7 @@ const char * PIString::dataAscii() const { uint PIString::hash() const { - return piHashData((const uchar*)PIDeque::data(), size()); + return piHashData((const uchar*)PIDeque::data(), size() * sizeof(PIChar)); }