1/**************************************************************************/
2/* vmap.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef VMAP_H
32#define VMAP_H
33
34#include "core/templates/cowdata.h"
35#include "core/typedefs.h"
36
37template <class T, class V>
38class VMap {
39public:
40 struct Pair {
41 T key;
42 V value;
43
44 _FORCE_INLINE_ Pair() {}
45
46 _FORCE_INLINE_ Pair(const T &p_key, const V &p_value) {
47 key = p_key;
48 value = p_value;
49 }
50 };
51
52private:
53 CowData<Pair> _cowdata;
54
55 _FORCE_INLINE_ int _find(const T &p_val, bool &r_exact) const {
56 r_exact = false;
57 if (_cowdata.is_empty()) {
58 return 0;
59 }
60
61 int low = 0;
62 int high = _cowdata.size() - 1;
63 const Pair *a = _cowdata.ptr();
64 int middle = 0;
65
66#ifdef DEBUG_ENABLED
67 if (low > high) {
68 ERR_PRINT("low > high, this may be a bug");
69 }
70#endif
71 while (low <= high) {
72 middle = (low + high) / 2;
73
74 if (p_val < a[middle].key) {
75 high = middle - 1; //search low end of array
76 } else if (a[middle].key < p_val) {
77 low = middle + 1; //search high end of array
78 } else {
79 r_exact = true;
80 return middle;
81 }
82 }
83
84 //return the position where this would be inserted
85 if (a[middle].key < p_val) {
86 middle++;
87 }
88 return middle;
89 }
90
91 _FORCE_INLINE_ int _find_exact(const T &p_val) const {
92 if (_cowdata.is_empty()) {
93 return -1;
94 }
95
96 int low = 0;
97 int high = _cowdata.size() - 1;
98 int middle;
99 const Pair *a = _cowdata.ptr();
100
101 while (low <= high) {
102 middle = (low + high) / 2;
103
104 if (p_val < a[middle].key) {
105 high = middle - 1; //search low end of array
106 } else if (a[middle].key < p_val) {
107 low = middle + 1; //search high end of array
108 } else {
109 return middle;
110 }
111 }
112
113 return -1;
114 }
115
116public:
117 int insert(const T &p_key, const V &p_val) {
118 bool exact;
119 int pos = _find(p_key, exact);
120 if (exact) {
121 _cowdata.get_m(pos).value = p_val;
122 return pos;
123 }
124 _cowdata.insert(pos, Pair(p_key, p_val));
125 return pos;
126 }
127
128 bool has(const T &p_val) const {
129 return _find_exact(p_val) != -1;
130 }
131
132 void erase(const T &p_val) {
133 int pos = _find_exact(p_val);
134 if (pos < 0) {
135 return;
136 }
137 _cowdata.remove_at(pos);
138 }
139
140 int find(const T &p_val) const {
141 return _find_exact(p_val);
142 }
143
144 int find_nearest(const T &p_val) const {
145 if (_cowdata.is_empty()) {
146 return -1;
147 }
148 bool exact;
149 return _find(p_val, exact);
150 }
151
152 _FORCE_INLINE_ int size() const { return _cowdata.size(); }
153 _FORCE_INLINE_ bool is_empty() const { return _cowdata.is_empty(); }
154
155 const Pair *get_array() const {
156 return _cowdata.ptr();
157 }
158
159 Pair *get_array() {
160 return _cowdata.ptrw();
161 }
162
163 const V &getv(int p_index) const {
164 return _cowdata.get(p_index).value;
165 }
166
167 V &getv(int p_index) {
168 return _cowdata.get_m(p_index).value;
169 }
170
171 const T &getk(int p_index) const {
172 return _cowdata.get(p_index).key;
173 }
174
175 T &getk(int p_index) {
176 return _cowdata.get_m(p_index).key;
177 }
178
179 inline const V &operator[](const T &p_key) const {
180 int pos = _find_exact(p_key);
181
182 CRASH_COND(pos < 0);
183
184 return _cowdata.get(pos).value;
185 }
186
187 inline V &operator[](const T &p_key) {
188 int pos = _find_exact(p_key);
189 if (pos < 0) {
190 pos = insert(p_key, V());
191 }
192
193 return _cowdata.get_m(pos).value;
194 }
195
196 _FORCE_INLINE_ VMap() {}
197 _FORCE_INLINE_ VMap(const VMap &p_from) { _cowdata._ref(p_from._cowdata); }
198
199 inline void operator=(const VMap &p_from) {
200 _cowdata._ref(p_from._cowdata);
201 }
202};
203
204#endif // VMAP_H
205