summaryrefslogtreecommitdiffstats
path: root/tdehtml/misc/multimap.h
blob: 125e8e07d298db492ef11997a94b72ea9c0d11c0 (plain)
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
/*
 * This file is part of the DOM implementation for KDE.
 *
 * Copyright (C) 2006 Allan Sandfeld Jensen (kde@carewolf.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#ifndef _MultiMap_h_
#define _MultiMap_h_

#include <tqptrdict.h>
#include <tqptrlist.h>
#include <assert.h>
#include <stdlib.h>

template<class T> class MultiMapPtrList;

// KMultiMap is an implementaition of a Map with multiple entries per key.
// It is originally designed to work like a shell for TQPtrDict<TQPtrList>, but
// TQPtrList have been replaced with a much faster hash set.
template<class T>
class KMultiMap {
public:
    KMultiMap() : dict(257) { dict.setAutoDelete(true); }
    ~KMultiMap() {};

    typedef MultiMapPtrList<T> List;

    void append(void* key, T* element) {
        List *list = dict.find(key);
        if (!list){
             list = new List(8);
             dict.insert(key, list);
        }
        list->append(element);
    }
    void remove(void* key, T* element) {
        List *list = dict.find(key);
        if (list) {
            list->remove(element);
            if (list->isEmpty()) dict.remove(key);
        }
    }
    void remove(void* key) {
        dict.remove(key);
    }
    List* find(void* key) {
        return dict.find(key);
    }
private:
    TQPtrDict<List> dict;

};

static inline unsigned int stupidHash(void* ptr)
{
    unsigned long val = (unsigned long)ptr;
    // remove alignment and multiply by a prime unlikely to be a factor of size
    val = (val >> 4) * 1237;
    return val;
}

#define START_PTRLIST_SIZE 4
#define MAX_PTRLIST_SIZE 27

class PtrListEntry {
public:
    PtrListEntry(unsigned int log_size) : count(0), log_size(log_size), search(log_size), next(0) {
//         entry = new T* [size];
        assert(log_size < MAX_PTRLIST_SIZE);
        entry = (void**)calloc ((1<<log_size), sizeof(void*));
    }
    ~PtrListEntry() {
//         delete[] entry;
        free(entry);
    }
    bool insert(void* e) {
        unsigned int t_size = size();
        if (count == t_size) return false;
        unsigned int hash = stupidHash(e);
        void** firstFree = 0;
        // Only let elements be placed 'search' spots from their hash
        for(unsigned int j=0; j<search; j++) {
            unsigned int i = (hash + j) & (t_size-1); // modulo size
            // We need check to all hashes in 'search' to garuantee uniqueness
            if (entry[i] == 0) {
                if (!firstFree)
                    firstFree = entry + i;
            } else
            if (entry[i] == e)
                return true;
        }
        if (firstFree) {
            *firstFree = e;
            count++;
            return true;
        }
        // We had more than 'search' collisions
        if (count < (t_size/3)*2) {
            // only 2/3 full => increase search
            unsigned int s = search *2;
            if (s >= t_size) s = t_size;
            search = s;
            return insert(e);
        }
        return false;
    }
    // Insert another smaller set into this one
    // Is only garuantied to succede when this PtrList is new
    void insert(PtrListEntry* listEntry) {
        assert(size() >= listEntry->count * 2);
        unsigned int old_size = 1U << listEntry->log_size;
        for(unsigned int i = 0; i < old_size; i++) {
            bool s = true;
            void *e = listEntry->entry[i];
            if (e) s = insert(e);
            assert(s);
        }
    }
    bool remove(void* e) {
        if (count == 0) return false;
        unsigned int size = (1U<<log_size);
        unsigned int hash = stupidHash(e);
        // Elements are at most placed 'search' spots from their hash
        for(unsigned int j=0; j<search; j++) {
            unsigned int i = (hash + j) & (size-1); // modulo size
            if (entry[i] == e) {
                entry[i] = 0;
                count--;
                return true;
            }
        }
        return false;
    }
    bool contains(void* e) {
        if (count == 0) return false;
        unsigned int t_size = size();
        unsigned int hash = stupidHash(e);
        // Elements are at most placed 'search' spots from their hash
        for(unsigned int j=0; j<search; j++) {
            unsigned int i = (hash + j) & (t_size-1); // modulo size
            if (entry[i] == e) return true;
        }
        return false;
    }
    void* at(unsigned int i) const {
        assert (i < (1U<<log_size));
        return entry[i];
    }
    bool isEmpty() const {
        return count == 0;
    }
    bool isFull() const {
        return count == size();
    }
    unsigned int size() const {
        return (1U << log_size);
    }

    unsigned int count;
    const unsigned short log_size;
    unsigned short search;
    PtrListEntry *next;
    void** entry;
};

// An unsorted and unique PtrList that is implement as a linked list of hash-sets
// Optimized for fast insert and fast lookup
template<class T>
class MultiMapPtrList {
public:
    MultiMapPtrList(unsigned int init_size= 16) : m_first(0), m_current(0), m_pos(0) {
        assert(init_size > 0);
        unsigned int s = init_size - 1;
        unsigned int log_size = 0;
        while (s > 0) {
            log_size++;
            s = s >> 1;
        }
        m_first = new PtrListEntry(log_size);
    }
    MultiMapPtrList(const MultiMapPtrList& ptrList) : m_first(0), m_current(0), m_pos(0) {
        unsigned int t_count = ptrList.count();
        unsigned int log_size = 0;
        while (t_count > 0) {
            log_size++;
            t_count = t_count >> 1;
        }
        // At least as large as the largest ptrListEntry in the original
        if (t_count < ptrList.m_first->log_size) log_size = ptrList.m_first->log_size;
        m_first = new PtrListEntry(log_size);

        PtrListEntry *t_current = ptrList.m_first;
        while (t_current) {
            unsigned int t_size = t_current->size();
            for(unsigned int i=0; i < t_size; i++) {
                void* e = t_current->at(i);
                if (e != 0) {
                    bool t = m_first->insert(e);
                    if (!t) {
                        // Make a new, but keep the size
                        PtrListEntry *t_new = new PtrListEntry(log_size);
                        t_new->insert(e);
                        t_new->next = m_first;
                        m_first = t_new;
                    }
                }
            }
            t_current = t_current->next;
        }
    }
    ~MultiMapPtrList() {
        PtrListEntry *t_next, *t_current = m_first;
        while (t_current) {
            t_next = t_current->next;
            delete t_current;
            t_current = t_next;
        }
    }
    void append(T* e) {
        PtrListEntry *t_last = 0, *t_current = m_first;
        int count = 0;
        while (t_current) {
            if (t_current->insert(e)) return;
            t_last = t_current;
            t_current = t_current->next;
            count++;
        }
        // Create new hash-set
        unsigned int newsize = m_first->log_size+1;
        if (newsize > MAX_PTRLIST_SIZE) newsize = MAX_PTRLIST_SIZE;
        t_current = new PtrListEntry(newsize);
        bool t = t_current->insert(e);
        assert(t);
        // Prepend it to the list, for insert effeciency
        t_current->next = m_first;
        m_first = t_current;
        // ### rehash some of the smaller sets
        /*
        if (count > 4) {
            // rehash the last in the new
            t_current->insert(t_last);
        }*/
    }
    void remove(T* e) {
        PtrListEntry *t_next, *t_last = 0, *t_current = m_first;
        // Remove has to check every PtrEntry.
        while (t_current) {
            t_next = t_current->next;
            if (t_current->remove(e) && t_current->isEmpty()) {
                if (t_last) {
                    t_last->next = t_current->next;
                }
                else {
                    assert (m_first == t_current);
                    m_first = t_current->next;
                }
                delete t_current;
            } else {
                t_last = t_current;
            }
            t_current = t_next;
        }
    }
    bool contains(T* e) {
        PtrListEntry *t_current = m_first;
        while (t_current) {
            if (t_current->contains(e)) return true;
            t_current = t_current->next;
        }
        return false;
    }
    bool isEmpty() {
        if (!m_first) return true;
        PtrListEntry *t_current = m_first;
        while (t_current) {
            if (!t_current->isEmpty()) return false;
            t_current = t_current->next;
        }
        return true;
    }
    unsigned int count() const {
        unsigned int t_count = 0;
        PtrListEntry *t_current = m_first;
        while (t_current) {
            t_count += t_current->count;
            t_current = t_current->next;
        }
        return t_count;
    }
// Iterator functions:
    T* first() {
        m_current = m_first;
        m_pos = 0;
        // skip holes
        if (m_current && !m_current->at(m_pos))
            return next();
        else
            return current();
    }
    T* current() {
        if (!m_current)
            return (T*)0;
        else
            return (T*)m_current->at(m_pos);
    }
    T* next() {
        if (!m_current) return (T*)0;
        m_pos++;
        if (m_pos >= m_current->size()) {
            m_current = m_current->next;
            m_pos = 0;
        }
        // skip holes
        if (m_current && !m_current->at(m_pos))
            return next();
        else
            return current();
    }
private:
    PtrListEntry *m_first;
// iteration:
    PtrListEntry *m_current;
    unsigned int m_pos;
};

#undef START_PTRLIST_SIZE
#undef MAX_PTRLIST_SIZE

#endif