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) {
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<HashEntry> & 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<Key, T> & reserve(size_t new_size) {_rehash(new_size); return *this;}
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;
PIVector<HashEntry> & 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<HashEntry> & hv(pih_content[i]);
if (hv.size_s() == 1) {
if (hv[0].key == key)
return hv[0].value;
}
const PIVector<HashEntry> & 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<PIString> v2;
for (int i=0; i<1000; ++i) {
v2 << PIString::fromNumber(randomi());
}*/
piCout << h;
PIHash<PIString, PIString> h2;
PIMap<PIString, PIString> 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;
}