Nebula
Toggle main menu visibility
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
//------------------------------------------------------------------------------
31
namespace
Util
32
{
33
template
<
class
KEYTYPE>
class
Set
34
{
35
public
:
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
;
64
const
Array<KEYTYPE>
&
KeysAsArray
()
const
;
66
template
<
class
RETURNTYPE> RETURNTYPE
KeysAs
()
const
;
67
68
protected
:
70
void
SortIfDirty
()
const
;
71
72
Array<KEYTYPE>
values
;
73
};
74
75
//------------------------------------------------------------------------------
78
template
<
class
KEYTYPE>
79
Set<KEYTYPE>::Set
()
80
{
81
// empty
82
}
83
84
//------------------------------------------------------------------------------
87
template
<
class
KEYTYPE>
88
Set<KEYTYPE>::Set
(
const
Set<KEYTYPE>
& rhs) :
89
values
(rhs.
values
)
90
{
91
// empty
92
}
93
94
//------------------------------------------------------------------------------
97
template
<
class
KEYTYPE>
void
98
Set<KEYTYPE>::operator=
(
const
Set<KEYTYPE>
& rhs)
99
{
100
this->
values
= rhs.
values
;
101
}
102
103
//------------------------------------------------------------------------------
106
template
<
class
KEYTYPE>
void
107
Set<KEYTYPE>::Clear
()
108
{
109
this->
values
.Clear();
110
}
111
112
//------------------------------------------------------------------------------
115
template
<
class
KEYTYPE>
SizeT
116
Set<KEYTYPE>::Size
()
const
117
{
118
return
this->
values
.Size();
119
}
120
121
//------------------------------------------------------------------------------
124
template
<
class
KEYTYPE>
bool
125
Set<KEYTYPE>::IsEmpty
()
const
126
{
127
return
(0 == this->
values
.Size());
128
}
129
130
//------------------------------------------------------------------------------
133
template
<
class
KEYTYPE>
void
134
Set<KEYTYPE>::Reserve
(
SizeT
numElements)
135
{
136
this->
values
.Reserve(numElements);
137
}
138
139
//------------------------------------------------------------------------------
142
template
<
class
KEYTYPE>
void
143
Set<KEYTYPE>::Add
(
const
KEYTYPE& key)
144
{
145
if
(!this->
Contains
(key))
146
{
147
this->
values
.InsertSorted(key);
148
}
149
}
150
151
//------------------------------------------------------------------------------
154
template
<
class
KEYTYPE>
void
155
Set<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
//------------------------------------------------------------------------------
167
template
<
class
KEYTYPE>
void
168
Set<KEYTYPE>::EraseAtIndex
(
IndexT
index)
169
{
170
this->
values
.EraseIndex(index);
171
}
172
173
//------------------------------------------------------------------------------
176
template
<
class
KEYTYPE>
IndexT
177
Set<KEYTYPE>::FindIndex
(
const
KEYTYPE& key)
const
178
{
179
return
this->
values
.BinarySearchIndex(key);
180
}
181
182
//------------------------------------------------------------------------------
185
template
<
class
KEYTYPE>
bool
186
Set<KEYTYPE>::Contains
(
const
KEYTYPE& key)
const
187
{
188
return
(
InvalidIndex
!= this->
values
.BinarySearchIndex(key));
189
}
190
191
//------------------------------------------------------------------------------
194
template
<
class
KEYTYPE>
const
KEYTYPE&
195
Set<KEYTYPE>::KeyAtIndex
(
IndexT
index)
const
196
{
197
return
this->
values
[index];
198
}
199
200
//------------------------------------------------------------------------------
203
template
<
class
KEYTYPE>
204
template
<
class
RETURNTYPE>
205
RETURNTYPE
206
Set<KEYTYPE>::KeysAs
()
const
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
//------------------------------------------------------------------------------
220
template
<
class
KEYTYPE>
221
const
Array<KEYTYPE>
&
222
Set<KEYTYPE>::KeysAsArray
()
const
223
{
224
return
this->
values
;
225
}
226
227
}
// namespace Util
228
//------------------------------------------------------------------------------
array.h
Util::Array
Nebula's dynamic array class.
Definition
array.h:61
Util::Set::values
Array< KEYTYPE > values
Definition
set.h:72
Util::Set::Size
SizeT Size() const
return number of unique keys
Definition
set.h:116
Util::Set::IsEmpty
bool IsEmpty() const
return true if empty
Definition
set.h:125
Util::Set::Erase
void Erase(const KEYTYPE &key)
erase a key and its associated value
Definition
set.h:155
Util::Set::Contains
bool Contains(const KEYTYPE &key) const
return true if key exists in the array
Definition
set.h:186
Util::Set::Add
void Add(const KEYTYPE &value)
add a unique value to set, won't get added twice
Definition
set.h:143
Util::Set::Reserve
void Reserve(SizeT numElements)
reserve space (useful if number of elements is known beforehand)
Definition
set.h:134
Util::Set::FindIndex
IndexT FindIndex(const KEYTYPE &key) const
find index of key pair (InvalidIndex if doesn't exist)
Definition
set.h:177
Util::Set::SortIfDirty
void SortIfDirty() const
make sure the key value pair array is sorted
Util::Set::KeysAs
RETURNTYPE KeysAs() const
get all keys as (typically) an array
Definition
set.h:206
Util::Set::EraseAtIndex
void EraseAtIndex(IndexT index)
erase a key at index
Definition
set.h:168
Util::Set::Set
Set()
default constructor
Definition
set.h:79
Util::Set::Clear
void Clear()
clear the set
Definition
set.h:107
Util::Set::KeysAsArray
const Array< KEYTYPE > & KeysAsArray() const
get all keys as an Util::Array
Definition
set.h:222
Util::Set::KeyAtIndex
const KEYTYPE & KeyAtIndex(IndexT index) const
get a key at given index
Definition
set.h:195
Util::Set::operator=
void operator=(const Set< KEYTYPE > &rhs)
assignment operator
Definition
set.h:98
n_assert
#define n_assert(exp)
Definition
debug.h:50
keyvaluepair.h
Util
A quad tree designed to return regions of free 2D space.
Definition
String.cs:6
InvalidIndex
static const int InvalidIndex
Definition
types.h:47
SizeT
int SizeT
Definition
types.h:42
IndexT
int IndexT
Definition
types.h:41
code
foundation
util
set.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.