Nebula
Loading...
Searching...
No Matches
set.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
27#include "util/array.h"
28#include "util/keyvaluepair.h"
29
30//------------------------------------------------------------------------------
31namespace Util
32{
33template<class KEYTYPE> class Set
34{
35public:
37 Set();
39 Set(const Set<KEYTYPE>& rhs);
41 void operator=(const Set<KEYTYPE>& rhs);
42
44 SizeT Size() const;
46 void Clear();
48 bool IsEmpty() const;
50 void Reserve(SizeT numElements);
52 void Add(const KEYTYPE& value);
54 void Erase(const KEYTYPE& key);
56 void EraseAtIndex(IndexT index);
58 IndexT FindIndex(const KEYTYPE& key) const;
60 bool Contains(const KEYTYPE& key) const;
62 const KEYTYPE& KeyAtIndex(IndexT index) const;
66 template<class RETURNTYPE> RETURNTYPE KeysAs() const;
67
68protected:
70 void SortIfDirty() const;
71
73};
74
75//------------------------------------------------------------------------------
78template<class KEYTYPE>
80{
81 // empty
82}
83
84//------------------------------------------------------------------------------
87template<class KEYTYPE>
89 values(rhs.values)
90{
91 // empty
92}
93
94//------------------------------------------------------------------------------
97template<class KEYTYPE> void
99{
100 this->values = rhs.values;
101}
102
103//------------------------------------------------------------------------------
106template<class KEYTYPE> void
108{
109 this->values.Clear();
110}
111
112//------------------------------------------------------------------------------
115template<class KEYTYPE> SizeT
117{
118 return this->values.Size();
119}
120
121//------------------------------------------------------------------------------
124template<class KEYTYPE> bool
126{
127 return (0 == this->values.Size());
128}
129
130//------------------------------------------------------------------------------
133template<class KEYTYPE> void
135{
136 this->values.Reserve(numElements);
137}
138
139//------------------------------------------------------------------------------
142template<class KEYTYPE> void
143Set<KEYTYPE>::Add(const KEYTYPE& key)
144{
145 if (!this->Contains(key))
146 {
147 this->values.InsertSorted(key);
148 }
149}
150
151//------------------------------------------------------------------------------
154template<class KEYTYPE> void
155Set<KEYTYPE>::Erase(const KEYTYPE& key)
156{
157 IndexT eraseIndex = this->values.BinarySearchIndex(key);
158 #if NEBULA_BOUNDSCHECKS
159 n_assert(InvalidIndex != eraseIndex);
160 #endif
161 this->values.EraseIndex(eraseIndex);
162}
163
164//------------------------------------------------------------------------------
167template<class KEYTYPE> void
169{
170 this->values.EraseIndex(index);
171}
172
173//------------------------------------------------------------------------------
176template<class KEYTYPE> IndexT
177Set<KEYTYPE>::FindIndex(const KEYTYPE& key) const
178{
179 return this->values.BinarySearchIndex(key);
180}
181
182//------------------------------------------------------------------------------
185template<class KEYTYPE> bool
186Set<KEYTYPE>::Contains(const KEYTYPE& key) const
187{
188 return (InvalidIndex != this->values.BinarySearchIndex(key));
189}
190
191//------------------------------------------------------------------------------
194template<class KEYTYPE> const KEYTYPE&
196{
197 return this->values[index];
198}
199
200//------------------------------------------------------------------------------
203template<class KEYTYPE>
204template<class RETURNTYPE>
205RETURNTYPE
207{
208 RETURNTYPE result(this->Size(), this->Size());
209 IndexT i;
210 for (i = 0; i < this->values.Size(); i++)
211 {
212 result.Append(this->values[i]);
213 }
214 return result;
215}
216
217//------------------------------------------------------------------------------
220template<class KEYTYPE>
221const Array<KEYTYPE>&
223{
224 return this->values;
225}
226
227} // namespace Util
228//------------------------------------------------------------------------------
Nebula's dynamic array class.
Definition array.h:60
void Clear()
clear array (calls destructors)
Definition array.h:1207
A collection of unique values with quick lookup.
Definition set.h:34
Array< KEYTYPE > values
Definition set.h:72
SizeT Size() const
return number of unique keys
Definition set.h:116
bool IsEmpty() const
return true if empty
Definition set.h:125
Set(const Set< KEYTYPE > &rhs)
copy constructor
Definition set.h:88
void Erase(const KEYTYPE &key)
erase a key and its associated value
Definition set.h:155
bool Contains(const KEYTYPE &key) const
return true if key exists in the array
Definition set.h:186
void Add(const KEYTYPE &value)
add a unique value to set, won't get added twice
Definition set.h:143
void Reserve(SizeT numElements)
reserve space (useful if number of elements is known beforehand)
Definition set.h:134
IndexT FindIndex(const KEYTYPE &key) const
find index of key pair (InvalidIndex if doesn't exist)
Definition set.h:177
void SortIfDirty() const
make sure the key value pair array is sorted
RETURNTYPE KeysAs() const
get all keys as (typically) an array
Definition set.h:206
void EraseAtIndex(IndexT index)
erase a key at index
Definition set.h:168
Set()
default constructor
Definition set.h:79
void Clear()
clear the set
Definition set.h:107
const Array< KEYTYPE > & KeysAsArray() const
get all keys as an Util::Array
Definition set.h:222
const KEYTYPE & KeyAtIndex(IndexT index) const
get a key at given index
Definition set.h:195
void operator=(const Set< KEYTYPE > &rhs)
assignment operator
Definition set.h:98
#define n_assert(exp)
Definition debug.h:50
A pinned array is an array which manages its own virtual memory.
Definition String.cs:6
static const int InvalidIndex
Definition types.h:54
int SizeT
Definition types.h:49
int IndexT
Definition types.h:48