Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/templates/vset.h
9973 views
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
#pragma once
32
33
#include "core/templates/vector.h"
34
#include "core/typedefs.h"
35
36
template <typename T>
37
class VSet {
38
Vector<T> _data;
39
40
protected:
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
int64_t pos = _data.span().bisect(p_val, true);
48
49
if (pos < _data.size() && !(p_val < _data[pos]) && !(_data[pos] < p_val)) {
50
r_exact = true;
51
}
52
return pos;
53
}
54
55
_FORCE_INLINE_ int _find_exact(const T &p_val) const {
56
if (_data.is_empty()) {
57
return -1;
58
}
59
60
int64_t pos = _data.span().bisect(p_val, true);
61
62
if (pos < _data.size() && !(p_val < _data[pos]) && !(_data[pos] < p_val)) {
63
return pos;
64
}
65
return -1;
66
}
67
68
public:
69
void insert(const T &p_val) {
70
bool exact;
71
int pos = _find(p_val, exact);
72
if (exact) {
73
return;
74
}
75
_data.insert(pos, p_val);
76
}
77
78
bool has(const T &p_val) const {
79
return _find_exact(p_val) != -1;
80
}
81
82
void erase(const T &p_val) {
83
int pos = _find_exact(p_val);
84
if (pos < 0) {
85
return;
86
}
87
_data.remove_at(pos);
88
}
89
90
int find(const T &p_val) const {
91
return _find_exact(p_val);
92
}
93
94
_FORCE_INLINE_ bool is_empty() const { return _data.is_empty(); }
95
96
_FORCE_INLINE_ int size() const { return _data.size(); }
97
98
inline T &operator[](int p_index) {
99
return _data.write[p_index];
100
}
101
102
inline const T &operator[](int p_index) const {
103
return _data[p_index];
104
}
105
106
_FORCE_INLINE_ VSet() {}
107
_FORCE_INLINE_ VSet(std::initializer_list<T> p_init) :
108
_data(p_init) {}
109
};
110
111