about summary refs log tree commit diff
path: root/table/aterm-map.cc
blob: 53623d0880fa24b4c9d5189fcfd8f2bed4b0ab0d (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
#include <string>
#include <iostream>
#include <assert.h>
#include <math.h>
#include <aterm2.h>

using namespace std;


class ATermMap
{
private:

    struct KeyValue
    {
        ATerm key;
        ATerm value;
    };

    /* 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 size;

    /* 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);
    void remove(const string & key);

private:
    void init(unsigned int expectedCount);

    void resizeTable(unsigned int expectedCount);

    unsigned int hash1(ATerm key) const;
    unsigned int hash2(ATerm key) const;
};


ATermMap::ATermMap(unsigned int expectedCount)
{
    init(expectedCount);
}


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


void ATermMap::init(unsigned int expectedCount)
{
    size = 0;
    count = 0;
    maxCount = 0;
    hashTable = 0;
    resizeTable(expectedCount);
}


void ATermMap::resizeTable(unsigned int expectedCount)
{
    assert(size == 0);

    this->maxCount = expectedCount;

    unsigned int newSize = 128;

    hashTable = (KeyValue *) calloc(sizeof(KeyValue), newSize);

    size = newSize;
}


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;

#if 0
    double d1 = key2 * 0.6180339887;
    unsigned int h1 = (int) (size * (d1 - floor(d1)));
#endif

#if 0
    unsigned int h1 = size * (key2 * 61803 % 100000);
#endif

    unsigned int h1 = (size * ((key2 * 40503) & 0xffff)) >> 16;

//     cout << key2 << " " << h1 << endl;
    
//     unsigned int h1 = (key2 * 134217689) & (size - 1);

    return h1 % size;
}


unsigned int ATermMap::hash2(ATerm key) const
{
    unsigned int key2 = ((unsigned int) key) >> 2;

#if 0    
    double d2 = key2 * 0.6180339887;
    unsigned int h2 = 1 | (int) (size * (d2 - floor(d2)));
#endif

    unsigned int h3 = ((key2 * 134217689) & (size - 1)) | 1;
    return h3;
}


unsigned int nrItemsSet = 0;
unsigned int nrSetProbes = 0;
unsigned int nrMaxProbes = 0;


void ATermMap::set(ATerm key, ATerm value)
{
    unsigned int probes = 0;
    nrItemsSet++;
    for (unsigned int i = 0, h = hash1(key); i < size;
         ++i, h = (h + hash2(key)) & (size - 1))
    {
        assert(h < size);
        probes++;
        nrSetProbes++;
        if (hashTable[h].key == 0) {
            if (probes > nrMaxProbes) nrMaxProbes = probes;
            hashTable[h].key = key;
            hashTable[h].value = value;
            count++;
            return;
        }
    }
    abort();
}


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


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


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) {
        ATermMap map(100);
        for (int i = 0; i < 30; ++i) 
            map.set(someTerm(), someTerm());
        for (int i = 0; i < 100; ++i)
            map.get(someTerm());
    }

    cout << "SET: "
         << nrItemsSet << " "
         << nrSetProbes << " "
         << (double) nrSetProbes / nrItemsSet << " "
         << nrMaxProbes << endl;

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