git-svn-id: svn://db.shs.com.ru/pip@846 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2019-08-15 07:44:28 +00:00
parent 0cbc989263
commit 3fddba0db0
2 changed files with 56 additions and 29 deletions

View File

@@ -143,21 +143,22 @@ public:
T & operator [](const Key & key) { T & operator [](const Key & key) {
if (pih_content.isEmpty()) _rehash(1); if (pih_content.isEmpty()) _rehash(1);
int i = _index(key); uint k = piHash(key);
int i = _index(k);
if (i < pih_content.size_s()) { if (i < pih_content.size_s()) {
PIVector<HashEntry> & hv(pih_content[i]); PIVector<HashEntry> & hv(pih_content[i]);
if (hv.size_s() == 1) { if (hv.size_s() == 1) {
if (hv[0].key == key) if (hv[0].key == k)
return hv[0].value; return hv[0].value;
} }
for (int j = 0; j < hv.size_s(); ++j) for (int j = 0; j < hv.size_s(); ++j)
if (hv[j].key == key) if (hv[j].key == k)
return hv[j].value; return hv[j].value;
} }
if (pih_content[i].size_s() >= 2) if (pih_content[i].size_s() >= 3)
_rehash(pih_content.size_s() * 2); _rehash(pih_content.size_s() * 2);
i = _index(key); i = _index(k);
pih_content[i] << HashEntry(key); pih_content[i] << HashEntry(k);
return pih_content[i].back().value; return pih_content[i].back().value;
} }
const T operator [](const Key & key) const {return value(key);} const T operator [](const Key & key) const {return value(key);}
@@ -183,11 +184,12 @@ public:
PIHash<Key, T> & reserve(size_t new_size) {_rehash(new_size); return *this;} PIHash<Key, T> & reserve(size_t new_size) {_rehash(new_size); return *this;}
PIHash<Key, T> & remove(const Key & key) { PIHash<Key, T> & remove(const Key & key) {
int i = _index(key); uint k = piHash(key);
int i = _index(k);
if (i >= pih_content.size_s()) return *this; if (i >= pih_content.size_s()) return *this;
PIVector<HashEntry> & hv(pih_content[i]); PIVector<HashEntry> & hv(pih_content[i]);
for (int j = 0; j < hv.size_s(); ++j) for (int j = 0; j < hv.size_s(); ++j)
if (hv[j].key == key) { if (hv[j].key == k) {
hv.remove(j); hv.remove(j);
--j; --j;
} }
@@ -205,15 +207,12 @@ public:
return *this; return *this;
} }
const T value(const Key & key, const T & default_ = T()) const { 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_; if (i >= pih_content.size_s()) return default_;
PIVector<HashEntry> & hv(pih_content[i]); const PIVector<HashEntry> & hv(pih_content[i]);
if (hv.size_s() == 1) {
if (hv[0].key == key)
return hv[0].value;
}
for (int j = 0; j < hv.size_s(); ++j) for (int j = 0; j < hv.size_s(); ++j)
if (hv[j].key == key) if (hv[j].key == k)
return hv[j].value; return hv[j].value;
return default_; return default_;
} }
@@ -224,7 +223,7 @@ public:
ret << pih_content[i][j].value; ret << pih_content[i][j].value;
return ret; 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 i = 0; i < pih_content.size_s(); ++i)
for (int j = 0; j < pih_content[i].size_s(); ++j) for (int j = 0; j < pih_content[i].size_s(); ++j)
if (pih_content[i][j].value == value_) if (pih_content[i][j].value == value_)
@@ -237,7 +236,7 @@ public:
for (int j = 0; j < pih_content[i].size_s(); ++j) for (int j = 0; j < pih_content[i].size_s(); ++j)
ret << pih_content[i][j].key; ret << pih_content[i][j].key;
return ret; return ret;
} }*/
/*void dump() { /*void dump() {
piCout << "PIHash" << size() << "entries" << PICoutManipulators::NewLine << "content:"; piCout << "PIHash" << size() << "entries" << PICoutManipulators::NewLine << "content:";
@@ -250,8 +249,8 @@ public:
protected: protected:
struct HashEntry { struct HashEntry {
HashEntry(Key k = Key(), const T & v = T()): key(k), value(v) {;} HashEntry(uint k = 0, const T & v = T()): key(k), value(v) {;}
Key key; uint key;
T value; T value;
bool operator ==(const HashEntry & s) const {return key == s.key;} bool operator ==(const HashEntry & s) const {return key == s.key;}
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; while (s_ >> t) ++t;
return (1 << t); return (1 << t);
} }
int _index(const Key & k) { int _index(const uint & k) const {
return piHash(k) % pih_content.size_s(); return k % pih_content.size_s();
} }
void _rehash(int ns) { void _rehash(int ns) {
ns = asize(ns); ns = asize(ns);
@@ -289,7 +288,7 @@ protected:
for (int i = 0; i < pih_content.size_s(); ++i) { for (int i = 0; i < pih_content.size_s(); ++i) {
for (int j = 0; j < pih_content[i].size_s(); ++j) { for (int j = 0; j < pih_content[i].size_s(); ++j) {
HashEntry & e(pih_content[i][j]); HashEntry & e(pih_content[i][j]);
int ni = piHash(e.key) % ns; int ni = e.key % ns;
nhc[ni] << e; nhc[ni] << e;
} }
} }
@@ -327,14 +326,42 @@ int main() {
h["5"] = "123"; h["5"] = "123";
h["6"] = "321"; h["6"] = "321";
h["7"] = "aaa"; h["7"] = "aaa";
h["7"] = "aaa!!!!!";
piCout << h.size() << h.capacity(); piCout << h.size() << h.capacity();
//h.reserve(64); //h.reserve(64);
piCout << h.keys() << h.values(); //piCout << h.keys() << h.values();
//piCout << (1 << 2); //piCout << (1 << 2);
/*PIHash<PIString> v2; PIHash<PIString, PIString> h2;
for (int i=0; i<1000; ++i) { PIMap<PIString, PIString> m2;
v2 << PIString::fromNumber(randomi()); PIString prefix = "1234567890";
}*/ PITimeMeasurer tm;
piCout << h; 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; return 0;
} }

View File

@@ -419,7 +419,7 @@ const char * PIString::dataAscii() const {
uint PIString::hash() const { uint PIString::hash() const {
return piHashData((const uchar*)PIDeque<PIChar>::data(), size()); return piHashData((const uchar*)PIDeque<PIChar>::data(), size() * sizeof(PIChar));
} }