HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
small_map.hpp
1// Copyright Take Vos 2019-2022.
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 "../utility/utility.hpp"
8#include "../macros.hpp"
9#include <array>
10#include <utility>
11#include <optional>
12#include <type_traits>
13
14
15
16namespace hi::inline v1 {
17
18template<typename K, typename V, int N>
19class small_map {
20public:
21 using key_type = K;
22 using value_type = V;
23 struct item_type {
24 K key;
25 V value;
26 };
27 constexpr static int capacity = N;
29
30private:
31 typename array_type::iterator _end;
32 array_type items = {};
33
34public:
35 small_map()
36 {
37 _end = items.begin();
38 }
39
40 small_map(small_map const &other)
41 {
42 hi_axiom(this != &other);
43 _end = items.begin();
44 for (hilet &other_item : other) {
45 auto &this_item = *(_end++);
46 this_item = other_item;
47 }
48 }
49
50 small_map(small_map &&other)
51 {
52 hi_axiom(this != &other);
53 using std::swap;
54
55 _end = items.begin();
56 for (auto &other_item : other) {
57 auto &this_item = *(_end++);
58 swap(this_item, other_item);
59 }
60 // All items in other are valid, no reason to set other._end.
61 }
62
63 small_map &operator=(small_map const &other)
64 {
65 hi_return_on_self_assignment(other);
66 _end = items.begin();
67 for (hilet &other_item : other) {
68 auto &this_item = *(_end++);
69 this_item = other_item;
70 }
71 return *this;
72 }
73
74 small_map &operator=(small_map &&other)
75 {
76 hi_return_on_self_assignment(other);
77 _end = items.begin();
78 for (hilet &other_item : other) {
79 auto &this_item = *(_end++);
80 this_item = std::move(other_item);
81 }
82 other._end = other.begin();
83 return *this;
84 }
85
86 std::size_t size() const
87 {
88 return _end - items.begin();
89 }
90
91 decltype(auto) begin() const
92 {
93 return items.begin();
94 }
95 decltype(auto) begin()
96 {
97 return items.begin();
98 }
99 decltype(auto) end() const
100 {
101 return _end;
102 }
103 decltype(auto) end()
104 {
105 return _end;
106 }
107
108 std::optional<V> get(K const &key) const noexcept
109 {
110 for (auto i = begin(); i != end(); ++i) {
111 if (i->key == key) {
112 return i->value;
113 }
114 }
115 return {};
116 }
117
118 V get(K const &key, V const &default_value) const noexcept
119 {
120 if (hilet &value = get(key)) {
121 return *value;
122 } else {
123 return default_value;
124 }
125 }
126
127 template<typename KK, typename VV>
128 bool set(KK &&key, VV &&value) noexcept
129 {
130 auto i = begin();
131 for (; i != end(); ++i) {
132 if (i->key == key) {
133 i->value = std::forward<VV>(value);
134 return true;
135 }
136 }
137 if (i != items.end()) {
138 _end = i + 1;
139 i->key = std::forward<KK>(key);
140 i->value = std::forward<VV>(value);
141 return true;
142 }
143
144 return false;
145 }
146
147 V increment(K const &key) noexcept
148 {
149 static_assert(std::is_arithmetic_v<V>, "Only increment on a artihmatic value");
150 auto i = begin();
151 for (; i != end(); ++i) {
152 if (i->key == key) {
153 return ++(i->value);
154 }
155 }
156 if (i != items.end()) {
157 _end = i + 1;
158 i->key = key;
159 return i->value = V{1};
160 }
161
162 return 0;
163 }
164};
165
166} // namespace hi::inline v1
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Definition small_map.hpp:19
Definition small_map.hpp:23
T begin(T... args)
T end(T... args)
T move(T... args)
T swap(T... args)