2006-05-04 14:21:08 +02:00
|
|
|
#include "aterm-map.hh"
|
2006-05-03 19:29:48 +02:00
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2006-05-04 14:21:08 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2006-05-03 19:29:48 +02:00
|
|
|
|
2006-09-05 00:08:40 +02:00
|
|
|
#include <aterm2.h>
|
|
|
|
|
2006-05-03 19:29:48 +02:00
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
2006-05-04 10:32:30 +02:00
|
|
|
static const unsigned int maxLoadFactor = /* 1 / */ 3;
|
|
|
|
static unsigned int nrResizes = 0;
|
|
|
|
static unsigned int sizeTotalAlloc = 0;
|
|
|
|
static unsigned int sizeCurAlloc = 0;
|
|
|
|
static unsigned int sizeMaxAlloc = 0;
|
|
|
|
|
|
|
|
|
2006-05-03 19:29:48 +02:00
|
|
|
ATermMap::ATermMap(unsigned int expectedCount)
|
|
|
|
{
|
2006-05-04 14:21:08 +02:00
|
|
|
init(expectedCount);
|
2006-05-04 01:07:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATermMap::ATermMap(const ATermMap & map)
|
|
|
|
{
|
|
|
|
init(map.maxCount);
|
2006-05-04 01:17:42 +02:00
|
|
|
copy(map.hashTable, map.capacity);
|
2006-05-04 01:07:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATermMap & ATermMap::operator = (const ATermMap & map)
|
|
|
|
{
|
|
|
|
if (this == &map) return *this;
|
|
|
|
free();
|
|
|
|
init(map.maxCount);
|
2006-05-04 01:17:42 +02:00
|
|
|
copy(map.hashTable, map.capacity);
|
2006-05-04 01:07:38 +02:00
|
|
|
return *this;
|
2006-05-03 19:29:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATermMap::~ATermMap()
|
|
|
|
{
|
2006-05-04 01:07:38 +02:00
|
|
|
free();
|
2006-05-03 19:29:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ATermMap::init(unsigned int expectedCount)
|
|
|
|
{
|
2006-05-04 01:07:38 +02:00
|
|
|
assert(sizeof(ATerm) * 2 == sizeof(KeyValue));
|
2006-05-04 01:17:42 +02:00
|
|
|
capacity = 0;
|
2006-05-03 19:29:48 +02:00
|
|
|
count = 0;
|
|
|
|
maxCount = 0;
|
|
|
|
hashTable = 0;
|
|
|
|
resizeTable(expectedCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-04 01:07:38 +02:00
|
|
|
void ATermMap::free()
|
|
|
|
{
|
|
|
|
if (hashTable) {
|
|
|
|
ATunprotectArray((ATerm *) hashTable);
|
|
|
|
::free(hashTable);
|
2006-05-04 10:32:30 +02:00
|
|
|
sizeCurAlloc -= sizeof(KeyValue) * capacity;
|
2006-05-04 01:07:38 +02:00
|
|
|
hashTable = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned int roundToPowerOf2(unsigned int x)
|
2006-05-03 19:29:48 +02:00
|
|
|
{
|
2006-05-04 01:07:38 +02:00
|
|
|
x--;
|
|
|
|
x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16;
|
|
|
|
x++;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2006-05-03 19:29:48 +02:00
|
|
|
|
2006-05-04 01:07:38 +02:00
|
|
|
void ATermMap::resizeTable(unsigned int expectedCount)
|
|
|
|
{
|
|
|
|
if (expectedCount == 0) expectedCount = 1;
|
|
|
|
// cout << maxCount << " -> " << expectedCount << endl;
|
|
|
|
// cout << maxCount << " " << size << endl;
|
|
|
|
// cout << (double) size / maxCount << endl;
|
|
|
|
|
2006-05-04 01:17:42 +02:00
|
|
|
unsigned int oldCapacity = capacity;
|
2006-05-04 01:07:38 +02:00
|
|
|
KeyValue * oldHashTable = hashTable;
|
|
|
|
|
|
|
|
maxCount = expectedCount;
|
2006-05-04 01:17:42 +02:00
|
|
|
capacity = roundToPowerOf2(maxCount * maxLoadFactor);
|
|
|
|
hashTable = (KeyValue *) calloc(sizeof(KeyValue), capacity);
|
2006-05-04 10:32:30 +02:00
|
|
|
sizeTotalAlloc += sizeof(KeyValue) * capacity;
|
|
|
|
sizeCurAlloc += sizeof(KeyValue) * capacity;
|
|
|
|
if (sizeCurAlloc > sizeMaxAlloc) sizeMaxAlloc = sizeCurAlloc;
|
2006-05-04 01:17:42 +02:00
|
|
|
ATprotectArray((ATerm *) hashTable, capacity * 2);
|
2006-05-04 01:07:38 +02:00
|
|
|
|
2006-05-04 01:17:42 +02:00
|
|
|
// cout << capacity << endl;
|
2006-05-04 01:07:38 +02:00
|
|
|
|
|
|
|
/* Re-hash the elements in the old table. */
|
2006-05-04 01:17:42 +02:00
|
|
|
if (oldCapacity != 0) {
|
2006-05-04 01:07:38 +02:00
|
|
|
count = 0;
|
2006-05-04 01:17:42 +02:00
|
|
|
copy(oldHashTable, oldCapacity);
|
2006-05-04 01:07:38 +02:00
|
|
|
ATunprotectArray((ATerm *) oldHashTable);
|
|
|
|
::free(oldHashTable);
|
2006-05-04 10:32:30 +02:00
|
|
|
sizeCurAlloc -= sizeof(KeyValue) * oldCapacity;
|
2006-05-04 01:07:38 +02:00
|
|
|
nrResizes++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-03 19:29:48 +02:00
|
|
|
|
2006-05-04 01:17:42 +02:00
|
|
|
void ATermMap::copy(KeyValue * elements, unsigned int capacity)
|
2006-05-04 01:07:38 +02:00
|
|
|
{
|
2006-05-04 01:17:42 +02:00
|
|
|
for (unsigned int i = 0; i < capacity; ++i)
|
2006-05-04 01:07:38 +02:00
|
|
|
if (elements[i].value) /* i.e., non-empty, non-deleted element */
|
|
|
|
set(elements[i].key, elements[i].value);
|
2006-05-03 19:29:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-11 04:19:43 +02:00
|
|
|
/* !!! use a bigger shift for 64-bit platforms? */
|
2006-05-04 01:07:38 +02:00
|
|
|
static const unsigned int shift = 16;
|
2006-05-11 04:19:43 +02:00
|
|
|
static const unsigned long knuth = (unsigned long) (0.6180339887 * (1 << shift));
|
2006-05-04 01:07:38 +02:00
|
|
|
|
|
|
|
|
2006-05-11 04:19:43 +02:00
|
|
|
unsigned long ATermMap::hash1(ATerm key) const
|
2006-05-03 19:29:48 +02:00
|
|
|
{
|
|
|
|
/* Don't care about the least significant bits of the ATerm
|
|
|
|
pointer since they're always 0. */
|
2006-05-11 04:19:43 +02:00
|
|
|
unsigned long key2 = ((unsigned long) key) >> 2;
|
2006-05-03 19:29:48 +02:00
|
|
|
|
2006-05-04 01:07:38 +02:00
|
|
|
/* Approximately equal to:
|
|
|
|
double d = key2 * 0.6180339887;
|
2006-05-04 01:17:42 +02:00
|
|
|
unsigned int h = (int) (capacity * (d - floor(d)));
|
2006-05-04 01:07:38 +02:00
|
|
|
*/
|
|
|
|
|
2006-05-11 04:19:43 +02:00
|
|
|
unsigned long h = (capacity * ((key2 * knuth) & ((1 << shift) - 1))) >> shift;
|
2006-05-03 19:29:48 +02:00
|
|
|
|
2006-05-04 01:07:38 +02:00
|
|
|
return h;
|
2006-05-03 19:29:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-11 04:19:43 +02:00
|
|
|
unsigned long ATermMap::hash2(ATerm key) const
|
2006-05-03 19:29:48 +02:00
|
|
|
{
|
2006-05-11 04:19:43 +02:00
|
|
|
unsigned long key2 = ((unsigned long) key) >> 2;
|
2006-05-04 01:17:42 +02:00
|
|
|
/* Note: the result must be relatively prime to `capacity' (which
|
|
|
|
is a power of 2), so we make sure that the result is always
|
|
|
|
odd. */
|
2006-05-11 04:19:43 +02:00
|
|
|
unsigned long h = ((key2 * 134217689) & (capacity - 1)) | 1;
|
2006-05-04 01:07:38 +02:00
|
|
|
return h;
|
2006-05-03 19:29:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-04 01:07:38 +02:00
|
|
|
static unsigned int nrItemsSet = 0;
|
|
|
|
static unsigned int nrSetProbes = 0;
|
2006-05-03 19:29:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
void ATermMap::set(ATerm key, ATerm value)
|
|
|
|
{
|
2006-05-04 01:17:42 +02:00
|
|
|
if (count == maxCount) resizeTable(capacity * 2 / maxLoadFactor);
|
2006-05-04 01:07:38 +02:00
|
|
|
|
2006-05-03 19:29:48 +02:00
|
|
|
nrItemsSet++;
|
2006-05-04 01:17:42 +02:00
|
|
|
for (unsigned int i = 0, h = hash1(key); i < capacity;
|
|
|
|
++i, h = (h + hash2(key)) & (capacity - 1))
|
2006-05-03 19:29:48 +02:00
|
|
|
{
|
2006-05-04 01:17:42 +02:00
|
|
|
// assert(h < capacity);
|
2006-05-03 19:29:48 +02:00
|
|
|
nrSetProbes++;
|
2006-05-04 01:07:38 +02:00
|
|
|
/* Note: to see whether a slot is free, we check
|
|
|
|
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) {
|
2006-05-04 01:17:42 +02:00
|
|
|
if (hashTable[h].value == 0) count++;
|
2006-05-03 19:29:48 +02:00
|
|
|
hashTable[h].key = key;
|
|
|
|
hashTable[h].value = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2006-05-04 01:07:38 +02:00
|
|
|
|
2006-05-03 19:29:48 +02:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-04 01:07:38 +02:00
|
|
|
static unsigned int nrItemsGet = 0;
|
|
|
|
static unsigned int nrGetProbes = 0;
|
2006-05-03 19:29:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
ATerm ATermMap::get(ATerm key) const
|
|
|
|
{
|
|
|
|
nrItemsGet++;
|
2006-05-04 01:17:42 +02:00
|
|
|
for (unsigned int i = 0, h = hash1(key); i < capacity;
|
|
|
|
++i, h = (h + hash2(key)) & (capacity - 1))
|
2006-05-03 19:29:48 +02:00
|
|
|
{
|
|
|
|
nrGetProbes++;
|
|
|
|
if (hashTable[h].key == 0) return 0;
|
|
|
|
if (hashTable[h].key == key) return hashTable[h].value;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-04 01:07:38 +02:00
|
|
|
void ATermMap::remove(ATerm key)
|
|
|
|
{
|
2006-05-04 01:17:42 +02:00
|
|
|
for (unsigned int i = 0, h = hash1(key); i < capacity;
|
|
|
|
++i, h = (h + hash2(key)) & (capacity - 1))
|
2006-05-04 01:07:38 +02:00
|
|
|
{
|
|
|
|
if (hashTable[h].key == 0) return;
|
|
|
|
if (hashTable[h].key == key) {
|
2006-05-04 01:17:42 +02:00
|
|
|
if (hashTable[h].value != 0) {
|
|
|
|
hashTable[h].value = 0;
|
|
|
|
count--;
|
|
|
|
}
|
2006-05-04 01:07:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-04 01:17:42 +02:00
|
|
|
unsigned int ATermMap::size()
|
|
|
|
{
|
|
|
|
return count; /* STL nomenclature */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-08 12:00:37 +02:00
|
|
|
void printATermMapStats()
|
|
|
|
{
|
2006-09-04 23:06:23 +02:00
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
|
|
|
|
2006-05-08 14:52:47 +02:00
|
|
|
cerr << "RESIZES: " << nrResizes << " "
|
2006-05-08 12:00:37 +02:00
|
|
|
<< sizeTotalAlloc << " "
|
|
|
|
<< sizeCurAlloc << " "
|
|
|
|
<< sizeMaxAlloc << endl;
|
|
|
|
|
2006-05-08 14:52:47 +02:00
|
|
|
cerr << "SET: "
|
2006-05-08 12:00:37 +02:00
|
|
|
<< nrItemsSet << " "
|
|
|
|
<< nrSetProbes << " "
|
|
|
|
<< (double) nrSetProbes / nrItemsSet << endl;
|
|
|
|
|
2006-05-08 14:52:47 +02:00
|
|
|
cerr << "GET: "
|
2006-05-08 12:00:37 +02:00
|
|
|
<< nrItemsGet << " "
|
|
|
|
<< nrGetProbes << " "
|
|
|
|
<< (double) nrGetProbes / nrItemsGet << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-04 14:21:08 +02:00
|
|
|
#if 0
|
2006-05-03 19:29:48 +02:00
|
|
|
int main(int argc, char * * argv)
|
|
|
|
{
|
|
|
|
ATerm bottomOfStack;
|
|
|
|
ATinit(argc, argv, &bottomOfStack);
|
|
|
|
|
|
|
|
/* Make test terms. */
|
|
|
|
int nrTestTerms = 100000;
|
|
|
|
ATerm testTerms[nrTestTerms];
|
|
|
|
|
|
|
|
for (int i = 0; i < nrTestTerms; ++i) {
|
|
|
|
char name[10];
|
|
|
|
sprintf(name, "%d", (int) random() % 37);
|
|
|
|
|
|
|
|
int arity = i == 0 ? 0 : (random() % 37);
|
|
|
|
ATerm kids[arity];
|
|
|
|
for (int j = 0; j < arity; ++j)
|
|
|
|
kids[j] = testTerms[random() % i];
|
|
|
|
|
|
|
|
testTerms[i] = (ATerm) ATmakeApplArray(ATmakeAFun(name, arity, ATfalse), kids);
|
|
|
|
// ATwriteToSharedTextFile(testTerms[i], stdout);
|
|
|
|
// printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cout << "testing...\n";
|
|
|
|
|
|
|
|
|
|
|
|
#define someTerm() (testTerms[(int) random() % nrTestTerms])
|
|
|
|
|
|
|
|
|
|
|
|
for (int test = 0; test < 100000; ++test) {
|
2006-05-04 11:22:29 +02:00
|
|
|
//cerr << test << endl;
|
2006-05-04 01:07:38 +02:00
|
|
|
unsigned int n = 300;
|
|
|
|
ATermMap map(300);
|
|
|
|
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]);
|
2006-05-04 11:22:29 +02:00
|
|
|
//cerr << "INSERT: " << keys[i] << " " << values[i] << endl;
|
2006-05-04 01:07:38 +02:00
|
|
|
}
|
2006-05-04 11:22:29 +02:00
|
|
|
|
2006-05-04 01:17:42 +02:00
|
|
|
unsigned int size = map.size();
|
|
|
|
assert(size <= n);
|
2006-05-04 01:07:38 +02:00
|
|
|
values[n - 1] = 0;
|
|
|
|
map.remove(keys[n - 1]);
|
2006-05-04 01:17:42 +02:00
|
|
|
assert(map.size() == size - 1);
|
2006-05-04 11:22:29 +02:00
|
|
|
|
|
|
|
unsigned int checksum;
|
|
|
|
unsigned int count = 0;
|
|
|
|
for (ATermMap::const_iterator i = map.begin(); i != map.end(); ++i, ++count) {
|
|
|
|
assert(i->key);
|
|
|
|
assert(i->value);
|
|
|
|
checksum += (unsigned int) (*i).key;
|
|
|
|
checksum += (unsigned int) (*i).value;
|
|
|
|
// cout << (*i).key << " " << (*i).value << endl;
|
|
|
|
}
|
|
|
|
assert(count == size - 1);
|
|
|
|
|
2006-05-04 01:07:38 +02:00
|
|
|
for (unsigned int i = 0; i < n; ++i) {
|
2006-05-04 11:22:29 +02:00
|
|
|
for (unsigned int j = i + 1; j < n; ++j)
|
|
|
|
if (keys[i] == keys[j]) goto x;
|
2006-05-04 01:07:38 +02:00
|
|
|
if (map.get(keys[i]) != values[i]) {
|
|
|
|
cerr << "MISMATCH: " << keys[i] << " " << values[i] << " " << map.get(keys[i]) << " " << i << endl;
|
|
|
|
abort();
|
|
|
|
}
|
2006-05-04 11:22:29 +02:00
|
|
|
if (values[i] != 0) {
|
|
|
|
checksum -= (unsigned int) keys[i];
|
|
|
|
checksum -= (unsigned int) values[i];
|
|
|
|
}
|
|
|
|
x: ;
|
2006-05-04 01:07:38 +02:00
|
|
|
}
|
2006-05-04 11:22:29 +02:00
|
|
|
|
|
|
|
assert(checksum == 0);
|
|
|
|
|
2006-05-04 01:07:38 +02:00
|
|
|
for (unsigned int i = 0; i < 100; ++i)
|
2006-05-03 19:29:48 +02:00
|
|
|
map.get(someTerm());
|
2006-05-04 11:22:29 +02:00
|
|
|
|
2006-05-03 19:29:48 +02:00
|
|
|
}
|
|
|
|
|
2006-05-08 12:00:37 +02:00
|
|
|
printATermMapStats();
|
2006-05-03 19:29:48 +02:00
|
|
|
}
|
2006-05-04 14:21:08 +02:00
|
|
|
#endif
|
2006-09-04 23:06:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|