forked from rethinkdb/rethinkdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_level_array.hpp
More file actions
144 lines (118 loc) · 3.9 KB
/
two_level_array.hpp
File metadata and controls
144 lines (118 loc) · 3.9 KB
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
// Copyright 2010-2013 RethinkDB, all rights reserved.
#ifndef CONTAINERS_TWO_LEVEL_ARRAY_HPP_
#define CONTAINERS_TWO_LEVEL_ARRAY_HPP_
#include <vector>
#include "errors.hpp"
/* two_level_array_t is a tree that always has exactly two levels. Its computational
complexity is similar to that of an array, but it neither allocates all of its memory
at once nor needs to realloc() as it grows. It also doesn't have a "size" -- it is
an infinite array. If a get() is called on an index in the array that set() has
never been called for, the result will be value_t().
It is parameterized on a value type, 'value_t'. It makes the following assumptions
about value_t:
1. value_t has a default constructor value_t() that has no side effects, and its
destructor has no side effects.
2. value_t has a good equality operator, where value_t() == value_t(), and
distinguishable values don't compare equal.
*/
template <class value_t>
class two_level_array_t {
private:
static const size_t CHUNK_SIZE = 1 << 16;
struct chunk_t {
chunk_t()
: count(0), values() // default-initialize each value in values
{ }
size_t count;
value_t values[CHUNK_SIZE];
};
std::vector<chunk_t *> chunks;
static size_t chunk_for_key(size_t key) {
size_t chunk_id = key / CHUNK_SIZE;
return chunk_id;
}
static size_t index_for_key(size_t key) {
return key % CHUNK_SIZE;
}
public:
two_level_array_t() { }
~two_level_array_t() {
for (auto it = chunks.begin(); it != chunks.end(); ++it) {
delete *it;
}
}
value_t get(size_t key) const {
size_t chunk_id = chunk_for_key(key);
if (chunk_id < chunks.size() && chunks[chunk_id] != NULL) {
return chunks[chunk_id]->values[index_for_key(key)];
} else {
return value_t();
}
}
void set(size_t key, value_t value) {
const size_t chunk_id = chunk_for_key(key);
if (chunk_id >= chunks.size() || chunks[chunk_id] == NULL) {
if (value == value_t()) {
return;
} else {
if (chunk_id >= chunks.size()) {
chunks.resize(chunk_id + 1, NULL);
}
chunks[chunk_id] = new chunk_t;
}
}
chunk_t *chunk = chunks[chunk_id];
const size_t index = index_for_key(key);
if (!(chunk->values[index] == value_t())) {
--chunk->count;
}
chunk->values[index] = value;
if (!(value == value_t())) {
++chunk->count;
}
if (chunk->count == 0) {
chunks[chunk_id] = NULL;
delete chunk;
while (!chunks.empty() && chunks.back() == NULL) {
chunks.pop_back();
}
}
}
};
template <class value_t>
class two_level_nevershrink_array_t {
private:
static const size_t CHUNK_SIZE = 1 << 16;
struct chunk_t {
chunk_t()
: values() // default-initialize each value in values
{ }
value_t values[CHUNK_SIZE];
};
std::vector<chunk_t *> chunks;
static size_t chunk_for_key(size_t key) {
size_t chunk_id = key / CHUNK_SIZE;
return chunk_id;
}
static size_t index_for_key(size_t key) {
return key % CHUNK_SIZE;
}
public:
two_level_nevershrink_array_t() { }
~two_level_nevershrink_array_t() {
for (auto it = chunks.begin(); it != chunks.end(); ++it) {
delete *it;
}
}
value_t &operator[](size_t key) {
const size_t chunk_id = chunk_for_key(key);
if (chunk_id >= chunks.size()) {
chunks.resize(chunk_id + 1, NULL);
}
if (chunks[chunk_id] == NULL) {
chunks[chunk_id] = new chunk_t;
}
return chunks[chunk_id]->values[index_for_key(key)];
}
};
#endif // CONTAINERS_TWO_LEVEL_ARRAY_HPP_