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