aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/stllike/hashtable.hpp
blob: 290f3e312d1231db3fee9f72d66aedbe4e40b2c4 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "hashtable.h"
#include <algorithm>

namespace vespalib {

namespace {

/// TODO Currently we require that you have atleast one element in _nodes to avoid one extra branch
/// However that means that empty unused hashtables are larger than necessary.
/// This we should probably reconsider.
template<typename Modulator>
uint32_t
computeModulo(size_t size) {
    return (size > 0) ? Modulator::selectHashTableSize(roundUp2inN(size) / 3) : 1;
}

template <typename NodeStore>
NodeStore
createStore(size_t size, uint32_t modulo) {
    size = (size > 0) ? roundUp2inN(std::max(size_t(modulo), roundUp2inN(size))) : 1;
    NodeStore store;
    store.reserve(size);
    store.resize(modulo);
    return store;
}

}
template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
void hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::swap(hashtable & rhs)
{
    std::swap(_modulator, rhs._modulator);
    std::swap(_count, rhs._count);
    _nodes.swap(rhs._nodes);
    std::swap(_hasher, rhs._hasher);
    std::swap(_equal, rhs._equal);
    std::swap(_keyExtractor, rhs._keyExtractor);
}

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::hashtable(size_t reservedSpace) :
    _modulator(computeModulo<Modulator>(reservedSpace)),
    _count(0),
    _nodes(createStore<NodeStore>(reservedSpace, _modulator.getTableSize()))
{ }

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::hashtable(size_t reservedSpace, const Hash & hasher, const Equal & equal) :
    _modulator(computeModulo<Modulator>(reservedSpace)),
    _count(0),
    _nodes(createStore<NodeStore>(reservedSpace, _modulator.getTableSize())),
    _hasher(hasher),
    _equal(equal)
{ }

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::hashtable(const hashtable &) = default;

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator> &
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::operator = (const hashtable &) = default;

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::~hashtable() = default;

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
typename hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::iterator
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::find(const Key & key) noexcept
{
    next_t h = hash(key);
    if (__builtin_expect(_nodes[h].valid(), true)) {
        do {
            if (__builtin_expect(_equal(_keyExtractor(_nodes[h].getValue()), key), true)) {
                return iterator(this, h);
            }
            h = _nodes[h].getNext();
        } while (h != Node::npos);
    }
    return end();
}

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
template< typename AltKey>
typename hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::iterator
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::find(const AltKey & key) noexcept
{
    next_t h = hash(key);
    if (__builtin_expect(_nodes[h].valid(), true)) {
        do {
            if (__builtin_expect(_equal(_keyExtractor(_nodes[h].getValue()), key), true)) {
                return iterator(this, h);
            }
            h = _nodes[h].getNext();
        } while (h != Node::npos);
    }
    return end();
}

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
void
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::erase(const Key & key) {
    const_iterator found(find(key));
    if (found != end()) {
        DefaultMoveHandler moveHandler;
        erase(moveHandler, hash(key), found);
    }
}

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
void
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::clear() {
    if (_count == 0) return; // Already empty and properly initialized

    _nodes.clear();
    _count = 0;
    _nodes.resize(getTableSize());
}

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
template< typename V >
typename hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::insert_result
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::insert_internal(V && node)
{
    const next_t h = hash(_keyExtractor(node));
    if ( ! _nodes[h].valid() ) [[likely]] {
        _nodes[h] = std::forward<V>(node);
        _count++;
        return insert_result(iterator(this, h), true);
    }
    return insert_internal_cold(std::forward<V>(node), h);
}

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
template< typename V >
typename hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::insert_result
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::insert_internal_cold(V && node, next_t h)
{
    for (next_t c(h); c != Node::npos; c = _nodes[c].getNext()) {
        if (_equal(_keyExtractor(_nodes[c].getValue()), _keyExtractor(node))) {
            return insert_result(iterator(this, c), false);
        }
    }
    if (_nodes.size() < _nodes.capacity()) {
        const next_t p(_nodes[h].getNext());
        const next_t newIdx(_nodes.size());
        _nodes[h].setNext(newIdx);
        _nodes.template emplace_back(std::forward<V>(node), p);
        _count++;
        return insert_result(iterator(this, newIdx), true);
    } else {
        resize(_nodes.capacity()*2);
        return insert_internal(std::forward<V>(node));
    }
}


template <typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator>
void
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::force_insert(Value && value) {
    const next_t h = hash(_keyExtractor(value));
    if (!_nodes[h].valid()) [[likely]] {
        _nodes[h] = std::move(value);
        _count++;
    } else if (_nodes.size() < _nodes.capacity()) [[likely]] {
        const next_t p(_nodes[h].getNext());
        const next_t newIdx(_nodes.size());
        _nodes[h].setNext(newIdx);
        _nodes.template emplace_back(std::move(value), p);
        _count++;
    } else {
        resize(_nodes.capacity()*2);
        force_insert(std::move(value));
    }
}

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
template<typename MoveHandler>
void
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::reclaim(MoveHandler & moveHandler, next_t node)
{
    size_t last(_nodes.size()-1);
    if (last >= getTableSize()) {
        if (last != node) {
            next_t h = hash(_keyExtractor(_nodes[last].getValue()));
            for (next_t n(_nodes[h].getNext()); n != last; n=_nodes[h].getNext()) {
                h = n;
            }
            move(moveHandler, last, node);
            _nodes[h].setNext(node);
        }
        _nodes.resize(last);
    }
}

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
template <typename Func>
void
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::for_each(Func func) const
{
    uint32_t i(0);
    for (; i < _modulator.getTableSize(); i++) {
        if (_nodes[i].valid()) {
            func(_nodes[i].getValue());
        }
    }
    for (; i < _nodes.size(); i++) {
        func(_nodes[i].getValue());
    }
}

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
template <typename MoveHandler>
void
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::erase(MoveHandler & moveHandler, next_t h, const const_iterator & it)
{
    next_t prev = Node::npos;
    do {
        if (h == it.getInternalIndex()) {
            if (prev != Node::npos) {
                _nodes[prev].setNext(_nodes[h].getNext());
                reclaim(moveHandler, h);
            } else {
                if (_nodes[h].hasNext()) {
                    next_t next = _nodes[h].getNext();
                    move(moveHandler, next, h);
                    reclaim(moveHandler, next);
                } else {
                    _nodes[h].invalidate();
                }
            }
            _count--;
            return;
        }
        prev = h;
        h = _nodes[h].getNext();
    } while (h != Node::npos);
}

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
void
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::resize(size_t newSize)
{
    next_t newModulo = computeModulo<Modulator>(newSize);
    NodeStore newStore = createStore<NodeStore>(newSize, newModulo);
    _modulator = Modulator(newModulo);
    _count = 0;
    _nodes.swap(newStore);
    move(std::move(newStore));
}

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
void
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::move(NodeStore && oldStore)
{
    for (auto & entry : oldStore) {
        if (entry.valid()) {
            force_insert(std::move(entry.getValue()));
        }
    }
}

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
size_t
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::getMemoryConsumption() const
{
    return sizeof(hashtable<Key, Value, Hash, Equal, KeyExtract>)
            + _nodes.capacity() * sizeof(Node);
}

template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
size_t
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::getMemoryUsed() const
{
    return sizeof(hashtable<Key, Value, Hash, Equal, KeyExtract>)
            + _nodes.size() * sizeof(Node);
}

}