| 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 |
#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 |
|
| 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 |
}; |