summaryrefslogtreecommitdiffstats
path: root/core/array.cpp
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2017-11-21 13:14:08 +0100
committerGitHub <noreply@github.com>2017-11-21 13:14:08 +0100
commit1c2782a7c7456b4a473e519ebb40968c521911e7 (patch)
tree715af6d88652fa9c2ba4a9c22ed539908ad0b157 /core/array.cpp
parent5a23136d1bdc88589ee7082098dadf74afef53c2 (diff)
parentd6e54de50239e8ac1de645bd411eeddbf627e4dc (diff)
downloadredot-engine-1c2782a7c7456b4a473e519ebb40968c521911e7.tar.gz
Merge pull request #12590 from poke1024/bsearch
Add bsearch and bsearch_custom to Array
Diffstat (limited to 'core/array.cpp')
-rw-r--r--core/array.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/core/array.cpp b/core/array.cpp
index 171c11776c..b7d4ae413a 100644
--- a/core/array.cpp
+++ b/core/array.cpp
@@ -265,6 +265,49 @@ Array &Array::sort_custom(Object *p_obj, const StringName &p_function) {
return *this;
}
+template <typename Less>
+_FORCE_INLINE_ int bisect(const Vector<Variant> &p_array, const Variant &p_value, bool p_before, const Less &p_less) {
+
+ int lo = 0;
+ int hi = p_array.size();
+ if (p_before) {
+ while (lo < hi) {
+ const int mid = (lo + hi) / 2;
+ if (p_less(p_array.get(mid), p_value)) {
+ lo = mid + 1;
+ } else {
+ hi = mid;
+ }
+ }
+ } else {
+ while (lo < hi) {
+ const int mid = (lo + hi) / 2;
+ if (p_less(p_value, p_array.get(mid))) {
+ hi = mid;
+ } else {
+ lo = mid + 1;
+ }
+ }
+ }
+ return lo;
+}
+
+int Array::bsearch(const Variant &p_value, bool p_before) {
+
+ return bisect(_p->array, p_value, p_before, _ArrayVariantSort());
+}
+
+int Array::bsearch_custom(const Variant &p_value, Object *p_obj, const StringName &p_function, bool p_before) {
+
+ ERR_FAIL_NULL_V(p_obj, 0);
+
+ _ArrayVariantSortCustom less;
+ less.obj = p_obj;
+ less.func = p_function;
+
+ return bisect(_p->array, p_value, p_before, less);
+}
+
Array &Array::invert() {
_p->array.invert();