mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-30 01:26:15 +02:00
* Maintain the count field properly.
This commit is contained in:
parent
db0d865ec4
commit
b803fb95cb
1 changed files with 44 additions and 29 deletions
|
@ -23,7 +23,7 @@ private:
|
||||||
KeyValue * hashTable;
|
KeyValue * hashTable;
|
||||||
|
|
||||||
/* Current size of the hash table. */
|
/* Current size of the hash table. */
|
||||||
unsigned int size;
|
unsigned int capacity;
|
||||||
|
|
||||||
/* Number of elements in the hash table. */
|
/* Number of elements in the hash table. */
|
||||||
unsigned int count;
|
unsigned int count;
|
||||||
|
@ -49,6 +49,8 @@ public:
|
||||||
|
|
||||||
void remove(ATerm key);
|
void remove(ATerm key);
|
||||||
|
|
||||||
|
unsigned int size();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void init(unsigned int expectedCount);
|
void init(unsigned int expectedCount);
|
||||||
|
|
||||||
|
@ -56,7 +58,7 @@ private:
|
||||||
|
|
||||||
void resizeTable(unsigned int expectedCount);
|
void resizeTable(unsigned int expectedCount);
|
||||||
|
|
||||||
void copy(KeyValue * elements, unsigned int size);
|
void copy(KeyValue * elements, unsigned int capacity);
|
||||||
|
|
||||||
inline unsigned int hash1(ATerm key) const;
|
inline unsigned int hash1(ATerm key) const;
|
||||||
inline unsigned int hash2(ATerm key) const;
|
inline unsigned int hash2(ATerm key) const;
|
||||||
|
@ -72,7 +74,7 @@ ATermMap::ATermMap(unsigned int expectedCount)
|
||||||
ATermMap::ATermMap(const ATermMap & map)
|
ATermMap::ATermMap(const ATermMap & map)
|
||||||
{
|
{
|
||||||
init(map.maxCount);
|
init(map.maxCount);
|
||||||
copy(map.hashTable, map.size);
|
copy(map.hashTable, map.capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -81,7 +83,7 @@ ATermMap & ATermMap::operator = (const ATermMap & map)
|
||||||
if (this == &map) return *this;
|
if (this == &map) return *this;
|
||||||
free();
|
free();
|
||||||
init(map.maxCount);
|
init(map.maxCount);
|
||||||
copy(map.hashTable, map.size);
|
copy(map.hashTable, map.capacity);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,7 +97,7 @@ ATermMap::~ATermMap()
|
||||||
void ATermMap::init(unsigned int expectedCount)
|
void ATermMap::init(unsigned int expectedCount)
|
||||||
{
|
{
|
||||||
assert(sizeof(ATerm) * 2 == sizeof(KeyValue));
|
assert(sizeof(ATerm) * 2 == sizeof(KeyValue));
|
||||||
size = 0;
|
capacity = 0;
|
||||||
count = 0;
|
count = 0;
|
||||||
maxCount = 0;
|
maxCount = 0;
|
||||||
hashTable = 0;
|
hashTable = 0;
|
||||||
|
@ -133,20 +135,20 @@ void ATermMap::resizeTable(unsigned int expectedCount)
|
||||||
// cout << maxCount << " " << size << endl;
|
// cout << maxCount << " " << size << endl;
|
||||||
// cout << (double) size / maxCount << endl;
|
// cout << (double) size / maxCount << endl;
|
||||||
|
|
||||||
unsigned int oldSize = size;
|
unsigned int oldCapacity = capacity;
|
||||||
KeyValue * oldHashTable = hashTable;
|
KeyValue * oldHashTable = hashTable;
|
||||||
|
|
||||||
maxCount = expectedCount;
|
maxCount = expectedCount;
|
||||||
size = roundToPowerOf2(maxCount * maxLoadFactor);
|
capacity = roundToPowerOf2(maxCount * maxLoadFactor);
|
||||||
hashTable = (KeyValue *) calloc(sizeof(KeyValue), size);
|
hashTable = (KeyValue *) calloc(sizeof(KeyValue), capacity);
|
||||||
ATprotectArray((ATerm *) hashTable, size * 2);
|
ATprotectArray((ATerm *) hashTable, capacity * 2);
|
||||||
|
|
||||||
// cout << size << endl;
|
// cout << capacity << endl;
|
||||||
|
|
||||||
/* Re-hash the elements in the old table. */
|
/* Re-hash the elements in the old table. */
|
||||||
if (oldSize != 0) {
|
if (oldCapacity != 0) {
|
||||||
count = 0;
|
count = 0;
|
||||||
copy(oldHashTable, oldSize);
|
copy(oldHashTable, oldCapacity);
|
||||||
ATunprotectArray((ATerm *) oldHashTable);
|
ATunprotectArray((ATerm *) oldHashTable);
|
||||||
::free(oldHashTable);
|
::free(oldHashTable);
|
||||||
nrResizes++;
|
nrResizes++;
|
||||||
|
@ -154,9 +156,9 @@ void ATermMap::resizeTable(unsigned int expectedCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ATermMap::copy(KeyValue * elements, unsigned int size)
|
void ATermMap::copy(KeyValue * elements, unsigned int capacity)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < size; ++i)
|
for (unsigned int i = 0; i < capacity; ++i)
|
||||||
if (elements[i].value) /* i.e., non-empty, non-deleted element */
|
if (elements[i].value) /* i.e., non-empty, non-deleted element */
|
||||||
set(elements[i].key, elements[i].value);
|
set(elements[i].key, elements[i].value);
|
||||||
}
|
}
|
||||||
|
@ -174,10 +176,10 @@ unsigned int ATermMap::hash1(ATerm key) const
|
||||||
|
|
||||||
/* Approximately equal to:
|
/* Approximately equal to:
|
||||||
double d = key2 * 0.6180339887;
|
double d = key2 * 0.6180339887;
|
||||||
unsigned int h = (int) (size * (d - floor(d)));
|
unsigned int h = (int) (capacity * (d - floor(d)));
|
||||||
*/
|
*/
|
||||||
|
|
||||||
unsigned int h = (size * ((key2 * knuth) & ((1 << shift) - 1))) >> shift;
|
unsigned int h = (capacity * ((key2 * knuth) & ((1 << shift) - 1))) >> shift;
|
||||||
|
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
@ -186,9 +188,10 @@ unsigned int ATermMap::hash1(ATerm key) const
|
||||||
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
|
/* Note: the result must be relatively prime to `capacity' (which
|
||||||
power of 2), so we make sure that the result is always odd. */
|
is a power of 2), so we make sure that the result is always
|
||||||
unsigned int h = ((key2 * 134217689) & (size - 1)) | 1;
|
odd. */
|
||||||
|
unsigned int h = ((key2 * 134217689) & (capacity - 1)) | 1;
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,21 +202,21 @@ static unsigned int nrSetProbes = 0;
|
||||||
|
|
||||||
void ATermMap::set(ATerm key, ATerm value)
|
void ATermMap::set(ATerm key, ATerm value)
|
||||||
{
|
{
|
||||||
if (count == maxCount) resizeTable(size * 2 / maxLoadFactor);
|
if (count == maxCount) resizeTable(capacity * 2 / maxLoadFactor);
|
||||||
|
|
||||||
nrItemsSet++;
|
nrItemsSet++;
|
||||||
for (unsigned int i = 0, h = hash1(key); i < size;
|
for (unsigned int i = 0, h = hash1(key); i < capacity;
|
||||||
++i, h = (h + hash2(key)) & (size - 1))
|
++i, h = (h + hash2(key)) & (capacity - 1))
|
||||||
{
|
{
|
||||||
// assert(h < size);
|
// assert(h < capacity);
|
||||||
nrSetProbes++;
|
nrSetProbes++;
|
||||||
/* Note: to see whether a slot is free, we check
|
/* Note: to see whether a slot is free, we check
|
||||||
hashTable[h].value, not hashTable[h].key, since we use
|
hashTable[h].value, not hashTable[h].key, since we use
|
||||||
value == 0 to mark deleted slots. */
|
value == 0 to mark deleted slots. */
|
||||||
if (hashTable[h].value == 0 || hashTable[h].key == key) {
|
if (hashTable[h].value == 0 || hashTable[h].key == key) {
|
||||||
|
if (hashTable[h].value == 0) count++;
|
||||||
hashTable[h].key = key;
|
hashTable[h].key = key;
|
||||||
hashTable[h].value = value;
|
hashTable[h].value = value;
|
||||||
count++;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -229,8 +232,8 @@ static unsigned int nrGetProbes = 0;
|
||||||
ATerm ATermMap::get(ATerm key) const
|
ATerm ATermMap::get(ATerm key) const
|
||||||
{
|
{
|
||||||
nrItemsGet++;
|
nrItemsGet++;
|
||||||
for (unsigned int i = 0, h = hash1(key); i < size;
|
for (unsigned int i = 0, h = hash1(key); i < capacity;
|
||||||
++i, h = (h + hash2(key)) & (size - 1))
|
++i, h = (h + hash2(key)) & (capacity - 1))
|
||||||
{
|
{
|
||||||
nrGetProbes++;
|
nrGetProbes++;
|
||||||
if (hashTable[h].key == 0) return 0;
|
if (hashTable[h].key == 0) return 0;
|
||||||
|
@ -242,18 +245,27 @@ ATerm ATermMap::get(ATerm key) const
|
||||||
|
|
||||||
void ATermMap::remove(ATerm key)
|
void ATermMap::remove(ATerm key)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0, h = hash1(key); i < size;
|
for (unsigned int i = 0, h = hash1(key); i < capacity;
|
||||||
++i, h = (h + hash2(key)) & (size - 1))
|
++i, h = (h + hash2(key)) & (capacity - 1))
|
||||||
{
|
{
|
||||||
if (hashTable[h].key == 0) return;
|
if (hashTable[h].key == 0) return;
|
||||||
if (hashTable[h].key == key) {
|
if (hashTable[h].key == key) {
|
||||||
hashTable[h].value = 0;
|
if (hashTable[h].value != 0) {
|
||||||
|
hashTable[h].value = 0;
|
||||||
|
count--;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
unsigned int ATermMap::size()
|
||||||
|
{
|
||||||
|
return count; /* STL nomenclature */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char * * argv)
|
int main(int argc, char * * argv)
|
||||||
{
|
{
|
||||||
ATerm bottomOfStack;
|
ATerm bottomOfStack;
|
||||||
|
@ -295,8 +307,11 @@ int main(int argc, char * * argv)
|
||||||
map.set(keys[i], values[i]);
|
map.set(keys[i], values[i]);
|
||||||
// cerr << "INSERT: " << keys[i] << " " << values[i] << endl;
|
// cerr << "INSERT: " << keys[i] << " " << values[i] << endl;
|
||||||
}
|
}
|
||||||
|
unsigned int size = map.size();
|
||||||
|
assert(size <= n);
|
||||||
values[n - 1] = 0;
|
values[n - 1] = 0;
|
||||||
map.remove(keys[n - 1]);
|
map.remove(keys[n - 1]);
|
||||||
|
assert(map.size() == size - 1);
|
||||||
for (unsigned int i = 0; i < n; ++i) {
|
for (unsigned int i = 0; i < n; ++i) {
|
||||||
if (map.get(keys[i]) != values[i]) {
|
if (map.get(keys[i]) != values[i]) {
|
||||||
for (unsigned int j = i + 1; j < n; ++j)
|
for (unsigned int j = i + 1; j < n; ++j)
|
||||||
|
|
Loading…
Reference in a new issue