summaryrefslogtreecommitdiff
path: root/AK/HashTable.h
blob: 8a4b5f847d6027239e8e572509a33700475ed997 (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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#pragma once

#include "Assertions.h"
#include "DoublyLinkedList.h"
#include "Traits.h"
#include "StdLibExtras.h"
#include "kstdio.h"

//#define HASHTABLE_DEBUG

namespace AK {

template<typename T, typename = Traits<T>> class HashTable;

template<typename T, typename TraitsForT>
class HashTable {
private:
    struct Bucket {
        DoublyLinkedList<T> chain;
    };

public:
    HashTable() { }
    explicit HashTable(HashTable&& other)
        : m_buckets(other.m_buckets)
        , m_size(other.m_size)
        , m_capacity(other.m_capacity)
    {
        other.m_size = 0;
        other.m_capacity = 0;
        other.m_buckets = nullptr;
    }
    HashTable& operator=(HashTable&& other)
    {
        if (this != &other) {
            clear();
            m_buckets = other.m_buckets;
            m_size = other.m_size;
            m_capacity = other.m_capacity;
            other.m_size = 0;
            other.m_capacity = 0;
            other.m_buckets = nullptr;
        }
        return *this;
    }

    ~HashTable() { clear(); }
    bool is_empty() const { return !m_size; }
    int size() const { return m_size; }
    int capacity() const { return m_capacity; }

    void ensure_capacity(int capacity)
    {
        ASSERT(capacity >= size());
        rehash(capacity);
    }

    void set(const T&);
    void set(T&&);
    bool contains(const T&) const;
    void clear();

    void dump() const;

    class Iterator {
    public:
        bool operator!=(const Iterator& other) const
        {
            if (m_is_end && other.m_is_end)
                return false;
            return &m_table != &other.m_table
                || m_is_end != other.m_is_end
                || m_bucket_index != other.m_bucket_index
                || m_bucket_iterator != other.m_bucket_iterator;
        }
        bool operator==(const Iterator& other) const { return !(*this != other); }
        T& operator*()
        {
#ifdef HASHTABLE_DEBUG
            kprintf("retrieve { bucket_index: %u, is_end: %u }\n", m_bucket_index, m_is_end);
#endif
            return *m_bucket_iterator;
        }
        T* operator->() { return m_bucket_iterator.operator->(); }
        Iterator& operator++()
        {
            skip_to_next();
            return *this;
        }

        void skip_to_next()
        {
#ifdef HASHTABLE_DEBUG
            unsigned pass = 0;
#endif
            while (!m_is_end) {
#ifdef HASHTABLE_DEBUG
                ++pass;
                kprintf("skip_to_next pass %u, m_bucket_index=%u\n", pass, m_bucket_index);
#endif
                if (m_bucket_iterator.is_end()) {
                    ++m_bucket_index;
                    if (m_bucket_index >= m_table.capacity()) {
                        m_is_end = true;
                        return;
                    }
                    m_bucket_iterator = m_table.m_buckets[m_bucket_index].chain.begin();
                } else {
                    ++m_bucket_iterator;
                }
                if (!m_bucket_iterator.is_end())
                    return;
            }
        }
    private:
        friend class HashTable;
        explicit Iterator(HashTable& table, bool is_end, typename DoublyLinkedList<T>::Iterator bucket_iterator = DoublyLinkedList<T>::Iterator::universal_end(), int bucket_index = 0)
            : m_table(table)
            , m_bucket_index(bucket_index)
            , m_is_end(is_end)
            , m_bucket_iterator(bucket_iterator)
        {
            if (!is_end && !m_table.is_empty() && !(m_bucket_iterator != DoublyLinkedList<T>::Iterator::universal_end())) {
#ifdef HASHTABLE_DEBUG
                kprintf("bucket iterator init!\n");
#endif
                m_bucket_iterator = m_table.m_buckets[0].chain.begin();
                if (m_bucket_iterator.is_end())
                    skip_to_next();
            }
        }

        HashTable& m_table;
        int m_bucket_index { 0 };
        bool m_is_end { false };
        typename DoublyLinkedList<T>::Iterator m_bucket_iterator;
    };

    Iterator begin() { return Iterator(*this, is_empty()); }
    Iterator end() { return Iterator(*this, true); }

    class ConstIterator {
    public:
        bool operator!=(const ConstIterator& other) const
        {
            if (m_is_end && other.m_is_end)
                return false;
            return &m_table != &other.m_table
                || m_is_end != other.m_is_end
                || m_bucket_index != other.m_bucket_index
                || m_bucket_iterator != other.m_bucket_iterator;
        }
        bool operator==(const ConstIterator& other) const { return !(*this != other); }
        const T& operator*() const
        {
#ifdef HASHTABLE_DEBUG
            kprintf("retrieve { bucket_index: %u, is_end: %u }\n", m_bucket_index, m_is_end);
#endif
            return *m_bucket_iterator;
        }
        const T* operator->() const { return m_bucket_iterator.operator->(); }
        ConstIterator& operator++()
        {
            skip_to_next();
            return *this;
        }

        void skip_to_next()
        {
#ifdef HASHTABLE_DEBUG
            unsigned pass = 0;
#endif
            while (!m_is_end) {
#ifdef HASHTABLE_DEBUG
                ++pass;
                kprintf("skip_to_next pass %u, m_bucket_index=%u\n", pass, m_bucket_index);
#endif
                if (m_bucket_iterator.is_end()) {
                    ++m_bucket_index;
                    if (m_bucket_index >= m_table.capacity()) {
                        m_is_end = true;
                        return;
                    }
                    const DoublyLinkedList<T>& chain = m_table.m_buckets[m_bucket_index].chain;
                    m_bucket_iterator = chain.begin();
                } else {
                    ++m_bucket_iterator;
                }
                if (!m_bucket_iterator.is_end())
                    return;
            }
        }
    private:
        friend class HashTable;
        ConstIterator(const HashTable& table, bool is_end, typename DoublyLinkedList<T>::ConstIterator bucket_iterator = DoublyLinkedList<T>::ConstIterator::universal_end(), int bucket_index = 0)
            : m_table(table)
            , m_bucket_index(bucket_index)
            , m_is_end(is_end)
            , m_bucket_iterator(bucket_iterator)
        {
            if (!is_end && !m_table.is_empty() && !(m_bucket_iterator != DoublyLinkedList<T>::ConstIterator::universal_end())) {
#ifdef HASHTABLE_DEBUG
                kprintf("const bucket iterator init!\n");
#endif
                const DoublyLinkedList<T>& chain = m_table.m_buckets[0].chain;
                m_bucket_iterator = chain.begin();
                if (m_bucket_iterator.is_end())
                    skip_to_next();
            }
        }

        const HashTable& m_table;
        int m_bucket_index { 0 };
        bool m_is_end { false };
        typename DoublyLinkedList<T>::ConstIterator m_bucket_iterator;
    };

    ConstIterator begin() const { return ConstIterator(*this, is_empty()); }
    ConstIterator end() const { return ConstIterator(*this, true); }

    Iterator find(const T&);
    ConstIterator find(const T&) const;

    void remove(const T& value)
    {
        auto it = find(value);
        if (it != end())
            remove(it);
    }

    void remove(Iterator);

private:
    Bucket& lookup(const T&, int* bucket_index = nullptr);
    const Bucket& lookup(const T&, int* bucket_index = nullptr) const;
    void rehash(int capacity);
    void insert(const T&);
    void insert(T&&);

    Bucket* m_buckets { nullptr };

    int m_size { 0 };
    int m_capacity { 0 };
};

template<typename T, typename TraitsForT>
void HashTable<T, TraitsForT>::set(T&& value)
{
    if (!m_capacity)
        rehash(1);
    auto& bucket = lookup(value);
    for (auto& e : bucket.chain) {
        if (e == value) {
            e = move(value);
            return;
        }
    }
    if (size() >= capacity()) {
        rehash(size() + 1);
        insert(move(value));
    } else {
        bucket.chain.append(move(value));
    }
    m_size++;
}

template<typename T, typename TraitsForT>
void HashTable<T, TraitsForT>::set(const T& value)
{
    if (!m_capacity)
        rehash(1);
    auto& bucket = lookup(value);
    for (auto& e : bucket.chain) {
        if (e == value) {
            e = move(value);
            return;
        }
    }
    if (size() >= capacity()) {
        rehash(size() + 1);
        insert(value);
    } else {
        bucket.chain.append(value);
    }
    m_size++;
}


template<typename T, typename TraitsForT>
void HashTable<T, TraitsForT>::rehash(int new_capacity)
{
    new_capacity *= 2;
#ifdef HASHTABLE_DEBUG
    kprintf("rehash to %u buckets\n", new_capacity);
#endif
    auto* new_buckets = new Bucket[new_capacity];
    auto* old_buckets = m_buckets;
    int old_capacity = m_capacity;
    m_buckets = new_buckets;
    m_capacity = new_capacity;

#ifdef HASHTABLE_DEBUG
    kprintf("reinsert %u buckets\n", old_capacity);
#endif
    for (int i = 0; i < old_capacity; ++i) {
        for (auto& value : old_buckets[i].chain) {
            insert(move(value));
        }
    }

    delete [] old_buckets;
}

template<typename T, typename TraitsForT>
void HashTable<T, TraitsForT>::clear()
{
    if (m_buckets) {
        delete [] m_buckets;
        m_buckets = nullptr;
    }
    m_capacity = 0;
    m_size = 0;
}

template<typename T, typename TraitsForT>
void HashTable<T, TraitsForT>::insert(T&& value)
{
    auto& bucket = lookup(value);
    bucket.chain.append(move(value));
}

template<typename T, typename TraitsForT>
void HashTable<T, TraitsForT>::insert(const T& value)
{
    auto& bucket = lookup(value);
    bucket.chain.append(value);
}

template<typename T, typename TraitsForT>
bool HashTable<T, TraitsForT>::contains(const T& value) const
{
    if (is_empty())
        return false;
    auto& bucket = lookup(value);
    for (auto& e : bucket.chain) {
        if (e == value)
            return true;
    }
    return false;
}

template<typename T, typename TraitsForT>
auto HashTable<T, TraitsForT>::find(const T& value) -> Iterator
{
    if (is_empty())
        return end();
    int bucket_index;
    auto& bucket = lookup(value, &bucket_index);
    auto bucket_iterator = bucket.chain.find(value);
    if (bucket_iterator != bucket.chain.end())
        return Iterator(*this, false, bucket_iterator, bucket_index);
    return end();
}

template<typename T, typename TraitsForT>
auto HashTable<T, TraitsForT>::find(const T& value) const -> ConstIterator
{
    if (is_empty())
        return end();
    int bucket_index;
    auto& bucket = lookup(value, &bucket_index);
    auto bucket_iterator = bucket.chain.find(value);
    if (bucket_iterator != bucket.chain.end())
        return ConstIterator(*this, false, bucket_iterator, bucket_index);
    return end();
}

template<typename T, typename TraitsForT>
void HashTable<T, TraitsForT>::remove(Iterator it)
{
    ASSERT(!is_empty());
    m_buckets[it.m_bucket_index].chain.remove(it.m_bucket_iterator);
    --m_size;
}

template<typename T, typename TraitsForT>
typename HashTable<T, TraitsForT>::Bucket& HashTable<T, TraitsForT>::lookup(const T& value, int* bucket_index)
{
    unsigned hash = TraitsForT::hash(value);
#ifdef HASHTABLE_DEBUG
    kprintf("hash for ");
    TraitsForT::dump(value);
    kprintf(" is %u\n", hash);
#endif
    if (bucket_index)
        *bucket_index = hash % m_capacity;
    return m_buckets[hash % m_capacity];
}

template<typename T, typename TraitsForT>
const typename HashTable<T, TraitsForT>::Bucket& HashTable<T, TraitsForT>::lookup(const T& value, int* bucket_index) const
{
    unsigned hash = TraitsForT::hash(value);
#ifdef HASHTABLE_DEBUG
    kprintf("hash for ");
    TraitsForT::dump(value);
    kprintf(" is %u\n", hash);
#endif
    if (bucket_index)
        *bucket_index = hash % m_capacity;
    return m_buckets[hash % m_capacity];
}

template<typename T, typename TraitsForT>
void HashTable<T, TraitsForT>::dump() const
{
    kprintf("HashTable{%p} m_size=%u, m_capacity=%u, m_buckets=%p\n", this, m_size, m_capacity, m_buckets);
    for (int i = 0; i < m_capacity; ++i) {
        auto& bucket = m_buckets[i];
        kprintf("Bucket %u\n", i);
        for (auto& e : bucket.chain) {
            kprintf("  > ");
            TraitsForT::dump(e);
            kprintf("\n");
        }
    }
}

}

using AK::HashTable;