-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslotmap.cpp
44 lines (31 loc) · 1.18 KB
/
slotmap.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
#include <slotmap/slotmap.hpp>
#include <slotmap/filtered.hpp>
#include <iostream>
#include <string>
#include <cassert>
#include <vector>
using namespace std;
using namespace Twig::Container;
template<class T> using VectorAdapter = vector<T>;
using TSlotmap = Slotmap<string, VectorAdapter, 32, 16, SlotmapFlags::GROW | SlotmapFlags::SKIPFIELD>;
int main() {
auto slotmap = TSlotmap(10); // create a slotmap with 10 slots
auto& first = slotmap.alloc();
first = "Roel ";
slotmap.push("de ");
auto id = slotmap.push("de ");
auto jong = string("jong");
slotmap.push(std::move(jong));
auto element = slotmap.find(id); // lookup the slot containing the first "de "
auto removedByElement = slotmap.free(*element);
auto removedById = slotmap.free(id); // conveniently try to remove directly by id
assert(removedByElement && !removedById);
for (auto value : slotmap)
cout << value;
cout << '\n'; // Output: Roel de de Jong
for (auto value : make_filtered(slotmap))
cout << value;
cout << "\n"; // Output: Roel de Jong
cout << "Slotmap size: " << slotmap.size() << "\n";
cout << "Slotmap capacity: " << slotmap.capacity() << "\n"; // Initial capacity depends on vector implementation
}