forked from lix-project/lix
* New ATermMap, seems more-or-less finished.
This commit is contained in:
parent
052cefe1bd
commit
db0d865ec4
|
@ -48,32 +48,53 @@ public:
|
||||||
ATerm get(ATerm key) const;
|
ATerm get(ATerm key) const;
|
||||||
|
|
||||||
void remove(ATerm key);
|
void remove(ATerm key);
|
||||||
void remove(const string & key);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void init(unsigned int expectedCount);
|
void init(unsigned int expectedCount);
|
||||||
|
|
||||||
|
void free();
|
||||||
|
|
||||||
void resizeTable(unsigned int expectedCount);
|
void resizeTable(unsigned int expectedCount);
|
||||||
|
|
||||||
unsigned int hash1(ATerm key) const;
|
void copy(KeyValue * elements, unsigned int size);
|
||||||
unsigned int hash2(ATerm key) const;
|
|
||||||
|
inline unsigned int hash1(ATerm key) const;
|
||||||
|
inline unsigned int hash2(ATerm key) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
ATermMap::ATermMap(unsigned int expectedCount)
|
ATermMap::ATermMap(unsigned int expectedCount)
|
||||||
{
|
{
|
||||||
init(expectedCount);
|
init(expectedCount * 10 / 9); /* slight adjustment */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ATermMap::ATermMap(const ATermMap & map)
|
||||||
|
{
|
||||||
|
init(map.maxCount);
|
||||||
|
copy(map.hashTable, map.size);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ATermMap & ATermMap::operator = (const ATermMap & map)
|
||||||
|
{
|
||||||
|
if (this == &map) return *this;
|
||||||
|
free();
|
||||||
|
init(map.maxCount);
|
||||||
|
copy(map.hashTable, map.size);
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ATermMap::~ATermMap()
|
ATermMap::~ATermMap()
|
||||||
{
|
{
|
||||||
if (hashTable) free(hashTable);
|
free();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ATermMap::init(unsigned int expectedCount)
|
void ATermMap::init(unsigned int expectedCount)
|
||||||
{
|
{
|
||||||
|
assert(sizeof(ATerm) * 2 == sizeof(KeyValue));
|
||||||
size = 0;
|
size = 0;
|
||||||
count = 0;
|
count = 0;
|
||||||
maxCount = 0;
|
maxCount = 0;
|
||||||
|
@ -82,88 +103,127 @@ void ATermMap::init(unsigned int expectedCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void ATermMap::free()
|
||||||
|
{
|
||||||
|
if (hashTable) {
|
||||||
|
ATunprotectArray((ATerm *) hashTable);
|
||||||
|
::free(hashTable);
|
||||||
|
hashTable = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static unsigned int roundToPowerOf2(unsigned int x)
|
||||||
|
{
|
||||||
|
x--;
|
||||||
|
x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16;
|
||||||
|
x++;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const unsigned int maxLoadFactor = /* 1 / */ 3;
|
||||||
|
static unsigned int nrResizes = 0;
|
||||||
|
|
||||||
|
|
||||||
void ATermMap::resizeTable(unsigned int expectedCount)
|
void ATermMap::resizeTable(unsigned int expectedCount)
|
||||||
{
|
{
|
||||||
assert(size == 0);
|
if (expectedCount == 0) expectedCount = 1;
|
||||||
|
// cout << maxCount << " -> " << expectedCount << endl;
|
||||||
|
// cout << maxCount << " " << size << endl;
|
||||||
|
// cout << (double) size / maxCount << endl;
|
||||||
|
|
||||||
this->maxCount = expectedCount;
|
unsigned int oldSize = size;
|
||||||
|
KeyValue * oldHashTable = hashTable;
|
||||||
|
|
||||||
unsigned int newSize = 128;
|
maxCount = expectedCount;
|
||||||
|
size = roundToPowerOf2(maxCount * maxLoadFactor);
|
||||||
|
hashTable = (KeyValue *) calloc(sizeof(KeyValue), size);
|
||||||
|
ATprotectArray((ATerm *) hashTable, size * 2);
|
||||||
|
|
||||||
hashTable = (KeyValue *) calloc(sizeof(KeyValue), newSize);
|
// cout << size << endl;
|
||||||
|
|
||||||
size = newSize;
|
/* Re-hash the elements in the old table. */
|
||||||
|
if (oldSize != 0) {
|
||||||
|
count = 0;
|
||||||
|
copy(oldHashTable, oldSize);
|
||||||
|
ATunprotectArray((ATerm *) oldHashTable);
|
||||||
|
::free(oldHashTable);
|
||||||
|
nrResizes++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void ATermMap::copy(KeyValue * elements, unsigned int size)
|
||||||
|
{
|
||||||
|
for (unsigned int i = 0; i < size; ++i)
|
||||||
|
if (elements[i].value) /* i.e., non-empty, non-deleted element */
|
||||||
|
set(elements[i].key, elements[i].value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const unsigned int shift = 16;
|
||||||
|
static const unsigned int knuth = (unsigned int) (0.6180339887 * (1 << shift));
|
||||||
|
|
||||||
|
|
||||||
unsigned int ATermMap::hash1(ATerm key) const
|
unsigned int ATermMap::hash1(ATerm key) const
|
||||||
{
|
{
|
||||||
/* Don't care about the least significant bits of the ATerm
|
/* Don't care about the least significant bits of the ATerm
|
||||||
pointer since they're always 0. */
|
pointer since they're always 0. */
|
||||||
unsigned int key2 = ((unsigned int) key) >> 2;
|
unsigned int key2 = ((unsigned int) key) >> 2;
|
||||||
|
|
||||||
#if 0
|
/* Approximately equal to:
|
||||||
double d1 = key2 * 0.6180339887;
|
double d = key2 * 0.6180339887;
|
||||||
unsigned int h1 = (int) (size * (d1 - floor(d1)));
|
unsigned int h = (int) (size * (d - floor(d)));
|
||||||
#endif
|
*/
|
||||||
|
|
||||||
#if 0
|
unsigned int h = (size * ((key2 * knuth) & ((1 << shift) - 1))) >> shift;
|
||||||
unsigned int h1 = size * (key2 * 61803 % 100000);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
unsigned int h1 = (size * ((key2 * 40503) & 0xffff)) >> 16;
|
return h;
|
||||||
|
|
||||||
// cout << key2 << " " << h1 << endl;
|
|
||||||
|
|
||||||
// unsigned int h1 = (key2 * 134217689) & (size - 1);
|
|
||||||
|
|
||||||
return h1 % size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
unsigned int ATermMap::hash2(ATerm key) const
|
unsigned int ATermMap::hash2(ATerm key) const
|
||||||
{
|
{
|
||||||
unsigned int key2 = ((unsigned int) key) >> 2;
|
unsigned int key2 = ((unsigned int) key) >> 2;
|
||||||
|
/* Note: the result must be relatively prime to `size' (which is a
|
||||||
#if 0
|
power of 2), so we make sure that the result is always odd. */
|
||||||
double d2 = key2 * 0.6180339887;
|
unsigned int h = ((key2 * 134217689) & (size - 1)) | 1;
|
||||||
unsigned int h2 = 1 | (int) (size * (d2 - floor(d2)));
|
return h;
|
||||||
#endif
|
|
||||||
|
|
||||||
unsigned int h3 = ((key2 * 134217689) & (size - 1)) | 1;
|
|
||||||
return h3;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
unsigned int nrItemsSet = 0;
|
static unsigned int nrItemsSet = 0;
|
||||||
unsigned int nrSetProbes = 0;
|
static unsigned int nrSetProbes = 0;
|
||||||
unsigned int nrMaxProbes = 0;
|
|
||||||
|
|
||||||
|
|
||||||
void ATermMap::set(ATerm key, ATerm value)
|
void ATermMap::set(ATerm key, ATerm value)
|
||||||
{
|
{
|
||||||
unsigned int probes = 0;
|
if (count == maxCount) resizeTable(size * 2 / maxLoadFactor);
|
||||||
|
|
||||||
nrItemsSet++;
|
nrItemsSet++;
|
||||||
for (unsigned int i = 0, h = hash1(key); i < size;
|
for (unsigned int i = 0, h = hash1(key); i < size;
|
||||||
++i, h = (h + hash2(key)) & (size - 1))
|
++i, h = (h + hash2(key)) & (size - 1))
|
||||||
{
|
{
|
||||||
assert(h < size);
|
// assert(h < size);
|
||||||
probes++;
|
|
||||||
nrSetProbes++;
|
nrSetProbes++;
|
||||||
if (hashTable[h].key == 0) {
|
/* Note: to see whether a slot is free, we check
|
||||||
if (probes > nrMaxProbes) nrMaxProbes = probes;
|
hashTable[h].value, not hashTable[h].key, since we use
|
||||||
|
value == 0 to mark deleted slots. */
|
||||||
|
if (hashTable[h].value == 0 || hashTable[h].key == key) {
|
||||||
hashTable[h].key = key;
|
hashTable[h].key = key;
|
||||||
hashTable[h].value = value;
|
hashTable[h].value = value;
|
||||||
count++;
|
count++;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
unsigned int nrItemsGet = 0;
|
static unsigned int nrItemsGet = 0;
|
||||||
unsigned int nrGetProbes = 0;
|
static unsigned int nrGetProbes = 0;
|
||||||
|
|
||||||
|
|
||||||
ATerm ATermMap::get(ATerm key) const
|
ATerm ATermMap::get(ATerm key) const
|
||||||
|
@ -180,6 +240,20 @@ ATerm ATermMap::get(ATerm key) const
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void ATermMap::remove(ATerm key)
|
||||||
|
{
|
||||||
|
for (unsigned int i = 0, h = hash1(key); i < size;
|
||||||
|
++i, h = (h + hash2(key)) & (size - 1))
|
||||||
|
{
|
||||||
|
if (hashTable[h].key == 0) return;
|
||||||
|
if (hashTable[h].key == key) {
|
||||||
|
hashTable[h].value = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char * * argv)
|
int main(int argc, char * * argv)
|
||||||
{
|
{
|
||||||
ATerm bottomOfStack;
|
ATerm bottomOfStack;
|
||||||
|
@ -211,18 +285,37 @@ int main(int argc, char * * argv)
|
||||||
|
|
||||||
|
|
||||||
for (int test = 0; test < 100000; ++test) {
|
for (int test = 0; test < 100000; ++test) {
|
||||||
ATermMap map(100);
|
// cerr << test << endl;
|
||||||
for (int i = 0; i < 30; ++i)
|
unsigned int n = 300;
|
||||||
map.set(someTerm(), someTerm());
|
ATermMap map(300);
|
||||||
for (int i = 0; i < 100; ++i)
|
ATerm keys[n], values[n];
|
||||||
|
for (unsigned int i = 0; i < n; ++i) {
|
||||||
|
keys[i] = someTerm();
|
||||||
|
values[i] = someTerm();
|
||||||
|
map.set(keys[i], values[i]);
|
||||||
|
// cerr << "INSERT: " << keys[i] << " " << values[i] << endl;
|
||||||
|
}
|
||||||
|
values[n - 1] = 0;
|
||||||
|
map.remove(keys[n - 1]);
|
||||||
|
for (unsigned int i = 0; i < n; ++i) {
|
||||||
|
if (map.get(keys[i]) != values[i]) {
|
||||||
|
for (unsigned int j = i + 1; j < n; ++j)
|
||||||
|
if (keys[i] == keys[j]) goto x;
|
||||||
|
cerr << "MISMATCH: " << keys[i] << " " << values[i] << " " << map.get(keys[i]) << " " << i << endl;
|
||||||
|
abort();
|
||||||
|
x: ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (unsigned int i = 0; i < 100; ++i)
|
||||||
map.get(someTerm());
|
map.get(someTerm());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cout << "RESIZES: " << nrResizes << endl;
|
||||||
|
|
||||||
cout << "SET: "
|
cout << "SET: "
|
||||||
<< nrItemsSet << " "
|
<< nrItemsSet << " "
|
||||||
<< nrSetProbes << " "
|
<< nrSetProbes << " "
|
||||||
<< (double) nrSetProbes / nrItemsSet << " "
|
<< (double) nrSetProbes / nrItemsSet << endl;
|
||||||
<< nrMaxProbes << endl;
|
|
||||||
|
|
||||||
cout << "GET: "
|
cout << "GET: "
|
||||||
<< nrItemsGet << " "
|
<< nrItemsGet << " "
|
||||||
|
|
|
@ -17,3 +17,14 @@ GET: 10000000 13032311 1.30323
|
||||||
real 0m0.956s
|
real 0m0.956s
|
||||||
user 0m0.928s
|
user 0m0.928s
|
||||||
sys 0m0.024s
|
sys 0m0.024s
|
||||||
|
|
||||||
|
|
||||||
|
[eelco@tyros:~/Dev/nix/table]$ time ./a.out
|
||||||
|
testing...
|
||||||
|
RESIZES: 0
|
||||||
|
SET: 30000000 35420570 1.18069
|
||||||
|
GET: 40000000 49507966 1.2377
|
||||||
|
|
||||||
|
real 0m3.796s
|
||||||
|
user 0m3.700s
|
||||||
|
sys 0m0.016s
|
||||||
|
|
Loading…
Reference in a new issue