| 1 | /**************************************************************************/ |
| 2 | /* vset.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 VSET_H |
| 32 | #define VSET_H |
| 33 | |
| 34 | #include "core/templates/vector.h" |
| 35 | #include "core/typedefs.h" |
| 36 | |
| 37 | template <class T> |
| 38 | class VSet { |
| 39 | Vector<T> _data; |
| 40 | |
| 41 | _FORCE_INLINE_ int _find(const T &p_val, bool &r_exact) const { |
| 42 | r_exact = false; |
| 43 | if (_data.is_empty()) { |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | int low = 0; |
| 48 | int high = _data.size() - 1; |
| 49 | const T *a = &_data[0]; |
| 50 | int middle = 0; |
| 51 | |
| 52 | #ifdef DEBUG_ENABLED |
| 53 | if (low > high) { |
| 54 | ERR_PRINT("low > high, this may be a bug" ); |
| 55 | } |
| 56 | #endif |
| 57 | |
| 58 | while (low <= high) { |
| 59 | middle = (low + high) / 2; |
| 60 | |
| 61 | if (p_val < a[middle]) { |
| 62 | high = middle - 1; //search low end of array |
| 63 | } else if (a[middle] < p_val) { |
| 64 | low = middle + 1; //search high end of array |
| 65 | } else { |
| 66 | r_exact = true; |
| 67 | return middle; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | //return the position where this would be inserted |
| 72 | if (a[middle] < p_val) { |
| 73 | middle++; |
| 74 | } |
| 75 | return middle; |
| 76 | } |
| 77 | |
| 78 | _FORCE_INLINE_ int _find_exact(const T &p_val) const { |
| 79 | if (_data.is_empty()) { |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | int low = 0; |
| 84 | int high = _data.size() - 1; |
| 85 | int middle; |
| 86 | const T *a = &_data[0]; |
| 87 | |
| 88 | while (low <= high) { |
| 89 | middle = (low + high) / 2; |
| 90 | |
| 91 | if (p_val < a[middle]) { |
| 92 | high = middle - 1; //search low end of array |
| 93 | } else if (a[middle] < p_val) { |
| 94 | low = middle + 1; //search high end of array |
| 95 | } else { |
| 96 | return middle; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | public: |
| 104 | void insert(const T &p_val) { |
| 105 | bool exact; |
| 106 | int pos = _find(p_val, exact); |
| 107 | if (exact) { |
| 108 | return; |
| 109 | } |
| 110 | _data.insert(pos, p_val); |
| 111 | } |
| 112 | |
| 113 | bool has(const T &p_val) const { |
| 114 | return _find_exact(p_val) != -1; |
| 115 | } |
| 116 | |
| 117 | void erase(const T &p_val) { |
| 118 | int pos = _find_exact(p_val); |
| 119 | if (pos < 0) { |
| 120 | return; |
| 121 | } |
| 122 | _data.remove_at(pos); |
| 123 | } |
| 124 | |
| 125 | int find(const T &p_val) const { |
| 126 | return _find_exact(p_val); |
| 127 | } |
| 128 | |
| 129 | _FORCE_INLINE_ bool is_empty() const { return _data.is_empty(); } |
| 130 | |
| 131 | _FORCE_INLINE_ int size() const { return _data.size(); } |
| 132 | |
| 133 | inline T &operator[](int p_index) { |
| 134 | return _data.write[p_index]; |
| 135 | } |
| 136 | |
| 137 | inline const T &operator[](int p_index) const { |
| 138 | return _data[p_index]; |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | #endif // VSET_H |
| 143 | |