/**************************************************************************/1/* vset.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/templates/vector.h"33#include "core/typedefs.h"3435template <typename T>36class VSet {37Vector<T> _data;3839protected:40_FORCE_INLINE_ int _find(const T &p_val, bool &r_exact) const {41r_exact = false;42if (_data.is_empty()) {43return 0;44}4546int64_t pos = _data.span().bisect(p_val, true);4748if (pos < _data.size() && !(p_val < _data[pos]) && !(_data[pos] < p_val)) {49r_exact = true;50}51return pos;52}5354_FORCE_INLINE_ int _find_exact(const T &p_val) const {55if (_data.is_empty()) {56return -1;57}5859int64_t pos = _data.span().bisect(p_val, true);6061if (pos < _data.size() && !(p_val < _data[pos]) && !(_data[pos] < p_val)) {62return pos;63}64return -1;65}6667public:68void insert(const T &p_val) {69bool exact;70int pos = _find(p_val, exact);71if (exact) {72return;73}74_data.insert(pos, p_val);75}7677bool has(const T &p_val) const {78return _find_exact(p_val) != -1;79}8081void erase(const T &p_val) {82int pos = _find_exact(p_val);83if (pos < 0) {84return;85}86_data.remove_at(pos);87}8889int find(const T &p_val) const {90return _find_exact(p_val);91}9293_FORCE_INLINE_ bool is_empty() const { return _data.is_empty(); }9495_FORCE_INLINE_ int size() const { return _data.size(); }9697inline T &operator[](int p_index) {98return _data.write[p_index];99}100101inline const T &operator[](int p_index) const {102return _data[p_index];103}104105_FORCE_INLINE_ VSet() {}106_FORCE_INLINE_ VSet(std::initializer_list<T> p_init) :107_data(p_init) {}108};109110111