-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.cpp
More file actions
100 lines (78 loc) · 2.1 KB
/
map.cpp
File metadata and controls
100 lines (78 loc) · 2.1 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
#include <chrono>
#include <iostream>
#include <list>
#include "map.hpp"
using namespace std::literals::string_literals;
using namespace std::literals::string_view_literals;
//////////////////////////////////////////////////////////////////////////////
void dump(auto n)
{
std::list<decltype(n)> q{n};
do
{
for (auto m(q.size()); m--;)
{
auto const n(q.front());
q.erase(q.begin());
if (n)
{
q.insert(q.end(), {n->l_, n->r_});
std::cout << '(' << n->kv_.first << ',' << n->kv_.second << ')';
}
else
{
std::cout << "(null)";
}
std::cout << ' ';
}
std::cout << std::endl;
}
while (q.size() && std::any_of(q.cbegin(), q.cend(),
[](auto const p) noexcept { return p; }));
}
//////////////////////////////////////////////////////////////////////////////
int main()
{
sg::map<std::string, int> st{
{"a", -1},
{"c", 1}
};
st.insert_or_assign("b"s, 0);
st.emplace("d"s, 2);
st.insert({"e", 3});
st["f"s] = 4;
dump(st.root());
std::cout << "height: " << sg::detail::height(st.root()) << std::endl;
std::cout << "size: " << st.size() << std::endl;
std::cout << (st == st) << std::endl;
st.erase(st.cbegin());
std::for_each(
st.crbegin(),
st.crend(),
[](auto&& p) noexcept
{
std::cout << "(" << p.first << " " << p.second << ")" << std::endl;
}
);
srand(time(nullptr));
using timer_t = std::chrono::high_resolution_clock;
auto t0(timer_t::now());
for (std::size_t i{}; 10000 != i; ++i)
{
st.emplace(std::string(rand() % 9 + 1, 48 + rand() % (123 - 48)), i);
}
std::cout << std::chrono::nanoseconds(timer_t::now() - t0).count() << std::endl;
auto S(st.size());
t0 = timer_t::now();
while (S)
{
st.erase(std::next(st.begin(), rand() % S--));
}
std::cout << std::chrono::nanoseconds(timer_t::now() - t0).count() << std::endl;
sg::map<std::string, std::unique_ptr<int>> ll;
ll["lalala"] = std::make_unique<int>(11);
erase(ll, "lalala"); // bad
erase(ll, "lalala"sv); // best
erase(ll, {"lalala"}); // convenience
return 0;
}