Debian Codegraph @

core/templates/bin_sorted_array.h

raw symbols SCIP
1 /**************************************************************************/
2 /* bin_sorted_array.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 #pragma once
32
33 #include "core/templates/local_vector.h"
34 #include "core/templates/paged_array.h"
35
36 template <typename T>
37 class BinSortedArray {
38 PagedArray<T> array;
39 LocalVector<uint64_t> bin_limits;
40
41 // Implement if elements need to keep track of their own index in the array.
42 _FORCE_INLINE_ virtual void _update_idx(T &r_element, uint64_t p_idx) {}
43
44 _FORCE_INLINE_ void _swap(uint64_t p_a, uint64_t p_b) {
45 SWAP(array[p_a], array[p_b]);
46 _update_idx(array[p_a], p_a);
47 _update_idx(array[p_b], p_b);
48 }
49
50 public:
51 uint64_t insert(T &p_element, uint64_t p_bin) {
52 array.push_back(p_element);
53 uint64_t new_idx = array.size() - 1;
54 _update_idx(p_element, new_idx);
55 bin_limits[0] = new_idx;
56 if (p_bin != 0) {
57 new_idx = move(new_idx, p_bin);
58 }
59 return new_idx;
60 }
61
62 uint64_t move(uint64_t p_idx, uint64_t p_bin) {
63 ERR_FAIL_UNSIGNED_INDEX_V(p_idx, array.size(), -1);
64
65 uint64_t current_bin = bin_limits.size() - 1;
66 while (p_idx > bin_limits[current_bin]) {
67 current_bin--;
68 }
69
70 if (p_bin == current_bin) {
71 return p_idx;
72 }
73
74 uint64_t current_idx = p_idx;
75 if (p_bin > current_bin) {
76 while (p_bin > current_bin) {
77 uint64_t swap_idx = 0;
78
79 if (current_bin == bin_limits.size() - 1) {
80 bin_limits.push_back(0);
81 } else {
82 bin_limits[current_bin + 1]++;
83 swap_idx = bin_limits[current_bin + 1];
84 }
85
86 if (current_idx != swap_idx) {
87 _swap(current_idx, swap_idx);
88 current_idx = swap_idx;
89 }
90
91 current_bin++;
92 }
93 } else {
94 while (p_bin < current_bin) {
95 uint64_t swap_idx = bin_limits[current_bin];
96
97 if (current_idx != swap_idx) {
98 _swap(current_idx, swap_idx);
99 }
100
101 if (current_bin == bin_limits.size() - 1 && bin_limits[current_bin] == 0) {
102 bin_limits.resize(bin_limits.size() - 1);
103 } else {
104 bin_limits[current_bin]--;
105 }
106 current_idx = swap_idx;
107 current_bin--;
108 }
109 }
110
111 return current_idx;
112 }
113
114 void remove_at(uint64_t p_idx) {
115 ERR_FAIL_UNSIGNED_INDEX(p_idx, array.size());
116 uint64_t new_idx = move(p_idx, 0);
117 uint64_t swap_idx = array.size() - 1;
118
119 if (new_idx != swap_idx) {
120 _swap(new_idx, swap_idx);
121 }
122
123 if (bin_limits[0] > 0) {
124 bin_limits[0]--;
125 }
126
127 array.pop_back();
128 }
129
130 void set_page_pool(PagedArrayPool<T> *p_page_pool) {
131 array.set_page_pool(p_page_pool);
132 }
133
134 _FORCE_INLINE_ const T &operator[](uint64_t p_index) const {
135 return array[p_index];
136 }
137
138 _FORCE_INLINE_ T &operator[](uint64_t p_index) {
139 return array[p_index];
140 }
141
142 int get_bin_count() {
143 if (array.size() == 0) {
144 return 0;
145 }
146 return bin_limits.size();
147 }
148
149 int get_bin_start(int p_bin) {
150 ERR_FAIL_COND_V(p_bin >= get_bin_count(), ~0U);
151 if ((unsigned int)p_bin == bin_limits.size() - 1) {
152 return 0;
153 }
154 return bin_limits[p_bin + 1] + 1;
155 }
156
157 int get_bin_size(int p_bin) {
158 ERR_FAIL_COND_V(p_bin >= get_bin_count(), 0);
159 if ((unsigned int)p_bin == bin_limits.size() - 1) {
160 return bin_limits[p_bin] + 1;
161 }
162 return bin_limits[p_bin] - bin_limits[p_bin + 1];
163 }
164
165 void reset() {
166 array.reset();
167 bin_limits.clear();
168 bin_limits.push_back(0);
169 }
170
171 BinSortedArray() {
172 bin_limits.push_back(0);
173 }
174
175 virtual ~BinSortedArray() {
176 reset();
177 }
178 };