about summary refs log tree commit diff
path: root/table/aterm-map.cc
blob: b619ead3668aaa3d4dc7652fd967b387dc908e81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <string>
#include <iostream>
#include <assert.h>
#include <math.h>
#include <aterm2.h>

using namespace std;


class ATermMap
{
public:

    struct KeyValue
    {
        ATerm key;
        ATerm value;
    };

private:

    /* Hash table for the map.  We use open addressing, i.e., all
       key/value pairs are stored directly in the table, and there are
       no pointers.  Collisions are resolved through probing. */
    KeyValue * hashTable;

    /* Current size of the hash table. */
    unsigned int capacity;

    /* Number of elements in the hash table. */
    unsigned int count;

    /* Maximum number of elements in the hash table.  If `count'
       exceeds this number, the hash table is expanded. */
    unsigned int maxCount;
    
public:

    /* Create a map.  `expectedCount' is the number of elements the
       map is expected to hold. */
    ATermMap(unsigned int expectedCount);
    
    ATermMap(const ATermMap & map);
    
    ~ATermMap();

    ATermMap & operator = (const ATermMap & map);
        
    void set(ATerm key, ATerm value);

    ATerm get(ATerm key) const;

    void remove(ATerm key);

    unsigned int size();

    struct const_iterator
    {
        const ATermMap & map;
        unsigned int pos;
        const_iterator(const ATermMap & map, int pos) : map(map)
        {
            this->pos = pos;
        }
        bool operator !=(const const_iterator & i)
        {
            return pos != i.pos;
        }
        void operator ++()
        {
            if (pos == map.capacity) return;
            do { ++pos; 
            } while (pos < map.capacity && map.hashTable[pos].value == 0);
        }
        const KeyValue & operator *()
        {
            assert(pos < map.capacity);
            return map.hashTable[pos];
        }
        const KeyValue * operator ->()
        {
            assert(pos < map.capacity);
            return &map.hashTable[pos];
        }
    };

    const_iterator begin() const
    {
        unsigned int i = 0;
        while (i < capacity && hashTable[i].value == 0) ++i;
        return const_iterator(*this, i);
    }
    
    const_iterator end() const
    {
        return const_iterator(*this, capacity);
    }

private:
    
    void init(unsigned int expectedCount);

    void free();

    void resizeTable(unsigned int expectedCount);

    void copy(KeyValue * elements, unsigned int capacity);
    
    inline unsigned int hash1(ATerm key) const;
    inline unsigned int hash2(ATerm key) const;
};


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;


ATermMap::ATermMap(unsigned int expectedCount)
{
    init(expectedCount * 10 / 9); /* slight adjustment */
}


ATermMap::ATermMap(const ATermMap & map)
{
    init(map.maxCount);
    copy(map.hashTable, map.capacity);
}


ATermMap & ATermMap::operator = (const ATermMap & map)
{
    if (this == &map) return *this;
    free();
    init(map.maxCount);
    copy(map.hashTable, map.capacity);
    return *this;
}


ATermMap::~ATermMap()
{
    free();
}


void ATermMap::init(unsigned int expectedCount)
{
    assert(sizeof(ATerm) * 2 == sizeof(KeyValue));
    capacity = 0;
    count = 0;
    maxCount = 0;
    hashTable = 0;
    resizeTable(expectedCount);
}


void ATermMap::free()
{
    if (hashTable) {
        ATunprotectArray((ATerm *) hashTable);
        ::free(hashTable);
        sizeCurAlloc -= sizeof(KeyValue) * capacity;
        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;
}


void ATermMap::resizeTable(unsigned int expectedCount)
{
    if (expectedCount == 0) expectedCount = 1;
//     cout << maxCount << " -> " << expectedCount << endl;
//     cout << maxCount << " " << size << endl;
//     cout << (double) size / maxCount << endl;

    unsigned int oldCapacity = capacity;
    KeyValue * oldHashTable = hashTable;

    maxCount = expectedCount;
    capacity = roundToPowerOf2(maxCount * maxLoadFactor);
    hashTable = (KeyValue *) calloc(sizeof(KeyValue), capacity);
    sizeTotalAlloc += sizeof(KeyValue) * capacity;
    sizeCurAlloc += sizeof(KeyValue) * capacity;
    if (sizeCurAlloc > sizeMaxAlloc) sizeMaxAlloc = sizeCurAlloc;
    ATprotectArray((ATerm *) hashTable, capacity * 2);
    
//     cout << capacity << endl;

    /* Re-hash the elements in the old table. */
    if (oldCapacity != 0) {
        count = 0;
        copy(oldHashTable, oldCapacity);
        ATunprotectArray((ATerm *) oldHashTable);
        ::free(oldHashTable);
        sizeCurAlloc -= sizeof(KeyValue) * oldCapacity;
        nrResizes++;
    }
}


void ATermMap::copy(KeyValue * elements, unsigned int capacity)
{
    for (unsigned int i = 0; i < capacity; ++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
{
    /* Don't care about the least significant bits of the ATerm
       pointer since they're always 0. */
    unsigned int key2 = ((unsigned int) key) >> 2;

    /* Approximately equal to:
    double d = key2 * 0.6180339887;
    unsigned int h = (int) (capacity * (d - floor(d)));
    */
 
    unsigned int h = (capacity * ((key2 * knuth) & ((1 << shift) - 1))) >> shift;

    return h;
}


unsigned int ATermMap::hash2(ATerm key) const
{
    unsigned int key2 = ((unsigned int) key) >> 2;
    /* 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. */
    unsigned int h = ((key2 * 134217689) & (capacity - 1)) | 1;
    return h;
}


static unsigned int nrItemsSet = 0;
static unsigned int nrSetProbes = 0;


void ATermMap::set(ATerm key, ATerm value)
{
    if (count == maxCount) resizeTable(capacity * 2 / maxLoadFactor);
    
    nrItemsSet++;
    for (unsigned int i = 0, h = hash1(key); i < capacity;
         ++i, h = (h + hash2(key)) & (capacity - 1))
    {
        // assert(h < capacity);
        nrSetProbes++;
        /* 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) {
            if (hashTable[h].value == 0) count++;
            hashTable[h].key = key;
            hashTable[h].value = value;
            return;
        }
    }
        
    abort();
}


static unsigned int nrItemsGet = 0;
static unsigned int nrGetProbes = 0;


ATerm ATermMap::get(ATerm key) const
{
    nrItemsGet++;
    for (unsigned int i = 0, h = hash1(key); i < capacity;
         ++i, h = (h + hash2(key)) & (capacity - 1))
    {
        nrGetProbes++;
        if (hashTable[h].key == 0) return 0;
        if (hashTable[h].key == key) return hashTable[h].value;
    }
    return 0;
}


void ATermMap::remove(ATerm key)
{
    for (unsigned int i = 0, h = hash1(key); i < capacity;
         ++i, h = (h + hash2(key)) & (capacity - 1))
    {
        if (hashTable[h].key == 0) return;
        if (hashTable[h].key == key) {
            if (hashTable[h].value != 0) {
                hashTable[h].value = 0;
                count--;
            }
            return;
        }
    }
}


unsigned int ATermMap::size()
{
    return count; /* STL nomenclature */
}


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) {
        //cerr << test << endl;
        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]);
            //cerr << "INSERT: " << keys[i] << " " << values[i] << endl;
        }

        unsigned int size = map.size();
        assert(size <= n);
        values[n - 1] = 0;
        map.remove(keys[n - 1]);
        assert(map.size() == size - 1);

        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);

        for (unsigned int i = 0; i < n; ++i) {
            for (unsigned int j = i + 1; j < n; ++j)
                if (keys[i] == keys[j]) goto x;
            if (map.get(keys[i]) != values[i]) {
                cerr << "MISMATCH: " << keys[i] << " " << values[i] << " " << map.get(keys[i]) << " " << i << endl;
                abort();
            }
            if (values[i] != 0) {
                checksum -= (unsigned int) keys[i];
                checksum -= (unsigned int) values[i];
            }
        x:  ;
        }

        assert(checksum == 0);
        
        for (unsigned int i = 0; i < 100; ++i)
            map.get(someTerm());
        
    }

    cout << "RESIZES: " << nrResizes << " "
         << sizeTotalAlloc << " "
         << sizeCurAlloc << " "
         << sizeMaxAlloc << endl;
        
    cout << "SET: "
         << nrItemsSet << " "
         << nrSetProbes << " "
         << (double) nrSetProbes / nrItemsSet << endl;

    cout << "GET: "
         << nrItemsGet << " "
         << nrGetProbes << " "
         << (double) nrGetProbes / nrItemsGet << endl;
}