HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
small_map.hpp
1// Copyright Take Vos 2019-2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "required.hpp"
8#include <array>
9#include <utility>
10#include <optional>
11#include <type_traits>
12
13namespace tt {
14
15template<typename K, typename V, int N>
16class small_map {
17public:
18 using key_type = K;
19 using value_type = V;
20 struct item_type {
21 K key;
22 V value;
23 };
24 static constexpr int capacity = N;
26
27private:
28 typename array_type::iterator _end;
29 array_type items = {};
30
31public:
32 small_map() {
33 _end = items.begin();
34 }
35
36 small_map(small_map const &other) {
37 tt_axiom(this != &other);
38 _end = items.begin();
39 for (ttlet &other_item: other) {
40 auto &this_item = *(_end++);
41 this_item = other_item;
42 }
43 }
44
45 small_map(small_map &&other) {
46 tt_axiom(this != &other);
47 using std::swap;
48
49 _end = items.begin();
50 for (auto &other_item: other) {
51 auto &this_item = *(_end++);
52 swap(this_item, other_item);
53 }
54 // All items in other are valid, no reason to set other._end.
55 }
56
57 small_map &operator=(small_map const &other) {
58 tt_return_on_self_assignment(other);
59 _end = items.begin();
60 for (ttlet &other_item: other) {
61 auto &this_item = *(_end++);
62 this_item = other_item;
63 }
64 return *this;
65 }
66
67 small_map &operator=(small_map &&other) {
68 tt_return_on_self_assignment(other);
69 _end = items.begin();
70 for (ttlet &other_item: other) {
71 auto &this_item = *(_end++);
72 this_item = std::move(other_item);
73 }
74 other._end = other.begin();
75 return *this;
76 }
77
78 size_t size() const {
79 return _end - items.begin();
80 }
81
82 decltype(auto) begin() const { return items.begin(); }
83 decltype(auto) begin() { return items.begin(); }
84 decltype(auto) end() const { return _end; }
85 decltype(auto) end() { return _end; }
86
87
88
89 std::optional<V> get(K const &key) const noexcept {
90 for (auto i = begin(); i != end(); ++i) {
91 if (i->key == key) {
92 return i->value;
93 }
94 }
95 return {};
96 }
97
98 V get(K const &key, V const &default_value) const noexcept {
99 if (ttlet &value = get(key)) {
100 return *value;
101 } else {
102 return default_value;
103 }
104 }
105
106 template<typename KK, typename VV>
107 bool set(KK &&key, VV &&value) noexcept {
108 auto i = begin();
109 for (; i != end(); ++i) {
110 if (i->key == key) {
111 i->value = std::forward<VV>(value);
112 return true;
113 }
114 }
115 if (i != items.end()) {
116 _end = i + 1;
117 i->key = std::forward<KK>(key);
118 i->value = std::forward<VV>(value);
119 return true;
120 }
121
122 return false;
123 }
124
125 V increment(K const &key) noexcept {
126 static_assert(std::is_arithmetic_v<V>, "Only increment on a artihmatic value");
127 auto i = begin();
128 for (; i != end(); ++i) {
129 if (i->key == key) {
130 return ++(i->value);
131 }
132 }
133 if (i != items.end()) {
134 _end = i + 1;
135 i->key = key;
136 return i->value = V{1};
137 }
138
139 return 0;
140 }
141};
142
143}
144
145
Definition small_map.hpp:16
Definition small_map.hpp:20
T begin(T... args)
T end(T... args)
T move(T... args)
T swap(T... args)