-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
280 lines (236 loc) · 7.46 KB
/
main.cpp
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
#include <algorithm>
#include <vector>
enum ContainerState
{
Ready = 0,
Modified,
};
template <typename T, int BufferSize = 64>
class contiguous_set
{
protected:
enum BufferOperation
{
InsertItem,
DeleteItem
};
class BufferItem
{
public:
BufferOperation operation;
T value;
BufferItem()
{
}
BufferItem(BufferOperation operation, T value) : operation(operation), value(value)
{
}
bool operator<(const BufferItem& item) const
{
if(operation == item.operation)
return value < item.value;
else
return operation < item.operation;
}
};
BufferItem op_buffer[BufferSize];
int buffer_ops;
ContainerState state;
std::vector<T> items;
public:
contiguous_set() : buffer_ops(0), state(Ready)
{
}
contiguous_set(contiguous_set<T>& other)
{
other.make_ready();
items.resize(items.size());
std::copy(other.items.begin(), other.items.end(), items.begin());
}
typename std::vector<T>::iterator begin() noexcept
{
make_ready();
return items.begin();
}
typename std::vector<T>::iterator end() noexcept
{
make_ready();
return items.end();
}
void make_ready()
{
if(buffer_ops == 0)
{
state = Ready;
return;
}
/// Want to reorder the ops so that the effect is the same, but more efficient.
/// InsertItem/DeleteItem pairs of the same value are nuked.
BufferItem optimised_op_buffer[BufferSize];
int write_command_pos = 0;
bool handled[BufferSize];
std::fill(handled, handled + BufferSize, false);
for(int i = 0; i < buffer_ops; ++i)
{
if(handled[i])
continue;
handled[i] = true;
if(op_buffer[i].operation == InsertItem)
{
bool eventual_add = true;
for(int j = i + 1; j < buffer_ops; ++j)
{
if(op_buffer[i].value == op_buffer[j].value)
{
if(op_buffer[j].operation == InsertItem)
{
eventual_add = true;
handled[j] = true;
}
else if(op_buffer[j].operation == DeleteItem)
{
eventual_add = false;
handled[j] = true;
}
}
}
if(eventual_add && !std::binary_search(items.begin(), items.end(), op_buffer[i].value))
optimised_op_buffer[write_command_pos++] = op_buffer[i];
}
else if(op_buffer[i].operation == DeleteItem)
{
bool eventual_delete = true;
for(int j = i + 1; j < buffer_ops; ++j)
{
if(op_buffer[i].value == op_buffer[j].value)
{
if(op_buffer[j].operation == InsertItem)
{
eventual_delete = false;
handled[j] = true;
}
else if(op_buffer[j].operation == DeleteItem)
{
eventual_delete = true;
handled[j] = true;
}
}
}
if(eventual_delete && std::binary_search(items.begin(), items.end(), op_buffer[i].value))
optimised_op_buffer[write_command_pos++] = op_buffer[i];
}
}
std::sort(optimised_op_buffer, optimised_op_buffer + write_command_pos);
int inserts = 0;
for(; inserts < write_command_pos; ++inserts)
if(optimised_op_buffer[inserts].operation == DeleteItem)
break;
int old_size = items.size();
items.resize(old_size + inserts);
for(int i = 0; i < inserts; ++i)
items[old_size + i] = optimised_op_buffer[i].value;
std::inplace_merge(items.begin(), items.end() - inserts, items.end());
/// Now delete
int write_item_pos = 0;
int delete_pos = inserts;
for(int i = 0; i < int(items.size()); ++i)
{
if(delete_pos < write_command_pos && items[i] == optimised_op_buffer[delete_pos].value)
++delete_pos;
else
items[write_item_pos++] = items[i];
}
items.resize(write_item_pos);
buffer_ops = 0;
state = Ready;
}
bool empty() noexcept
{
make_ready();
return items.empty();
}
typename std::vector<T>::size_type size() noexcept
{
make_ready();
return items.size();
}
typename std::vector<T>::size_type max_size() const noexcept
{
return items.max_size();
}
void insert(const T& val)
{
if(buffer_ops == BufferSize)
make_ready();
op_buffer[buffer_ops++] = BufferItem(InsertItem, val);
}
template <class InputIterator>
void insert(InputIterator first, InputIterator last)
{
while(first != last)
{
insert(*first);
++first;
}
}
void erase(const T& val)
{
if(buffer_ops == BufferSize)
make_ready();
op_buffer[buffer_ops++] = BufferItem(DeleteItem, val);
}
void swap(contiguous_set<T>& x)
{
for(int i = 0; i < BufferSize; ++i)
swap(op_buffer[i], x.op_buffer[i]);
swap(buffer_ops, x.buffer_ops);
swap(state, x.state);
items.swap(x.items);
}
void clear() noexcept
{
items.clear();
buffer_ops = 0;
}
typename std::vector<T>::iterator find(const T& val)
{
make_ready();
typename std::vector<T>::size_type beg = 0;
typename std::vector<T>::size_type end = size();
while(beg < end)
{
typename std::vector<T>::size_type med = beg + (end - beg)/2;
if(items[med] == val)
return iterator(items.begin() + med);
else if(items[med] > val)
end = med;
else
beg = med;
}
return end();
}
typename std::vector<T>::size_type count(const T& val)
{
return find(val) != end();
}
typename std::vector<T>::iterator lower_bound(const T& val)
{
return std::lower_bound(items.begin(), items.end(), val);
}
typename std::vector<T>::iterator upper_bound(const T& val)
{
return std::upper_bound(items.begin(), items.end(), val);
}
typename std::vector<T>::iterator equal_range(const T& val)
{
return std::equal_range(items.begin(), items.end(), val);
}
};
int main(int argc, char** argv)
{
contiguous_set<int> s;
for(int i = 0; i < 20; ++i)
s.insert(i);
for(auto it = s.begin(); it != s.end(); ++it)
printf("%d\n", *it);
}