forked from toptensoftware/simplelib
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeyedarray.h
More file actions
209 lines (174 loc) · 3.62 KB
/
keyedarray.h
File metadata and controls
209 lines (174 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#ifndef __simplelib_keyedarray_h__
#define __simplelib_keyedarray_h__
#include <assert.h>
#include <stdlib.h>
#include "semantics.h"
#include "vector.h"
namespace SimpleLib
{
template <typename TKey, typename TValue, typename TKeySem = SValue, typename TValueSem = SValue, typename TKeyArg = TKey >
class CKeyedArray
{
public:
// Constructor
CKeyedArray()
{
}
// Destructor
virtual ~CKeyedArray()
{
}
// Types
typedef CKeyedArray<TKey, TValue, TKeySem, TValueSem, TKeyArg> _CKeyedArray;
DEPRECATED("Use GetCount instead")
int GetSize() const { return GetCount(); }
// Get number of items
int GetCount() const
{
return m_Keys.GetCount();
}
// Check if empty
bool IsEmpty() const
{
return m_Keys.IsEmpty();
}
// Add an entry, assert if key already exists
void Add(const TKey& Key, const TValue& Value)
{
int iPos = IndexOf(Key);
assert(iPos < 0);
m_Keys.Add(Key);
m_Values.Add(Value);
}
// Add or replace entry
void SetValue(const TKey& Key, const TValue& Value)
{
int index = IndexOf(Key);
if (index < 0)
{
m_Keys.Add(Key);
m_Values.Add(Value);
}
else
{
m_Keys.ReplaceAt(index, Key);
m_Values.ReplaceAt(index, Value);
}
}
// Set the value at a position
void SetValueAt(int index, const TValue& Value)
{
m_Values.ReplaceAt(index, Value);
}
// Remove entry by key
void Remove(const TKeyArg& Key)
{
int index = IndexOf(Key);
if (index >= 0)
{
RemoveAt(index);
}
}
// Remove entry at index
void RemoveAt(int index)
{
m_Keys.RemoveAt(index);
m_Values.RemoveAt(index);
}
// Remove all
void RemoveAll()
{
m_Keys.RemoveAll();
m_Values.RemoveAll();
}
// Detach
TValue Detach(const TKeyArg& Key)
{
int index = IndexOf(Key);
return DetachAt(index);
}
// Detach at inex
TValue DetachAt(int index)
{
assert(index >= 0);
m_Keys.RemoveAt(index);
return m_Values.DetachAt(index);
}
// Get a key at index
const TKey& GetKeyAt(int index) const
{
return m_Keys.GetAt(index);
}
// Get a value at index
const TValue& GetValueAt(int index) const
{
return m_Values.GetAt(index);
}
// Get key and value at index
class CKeyPair;
CKeyPair GetAt(TKeyArg key) const
{
int index = IndexOf(key);
assert(index >= 0);
return CKeyPair(m_Keys[index], m_Values[index]);
}
// Get by key, assert if unknown key
TValue& GetValue(const TKeyArg& Key) const
{
int index = IndexOf(Key);
assert(index >= 0);
return m_Values[index];
}
// Shortcut to above
TValue& operator[](TKeyArg key) const
{
return GetValue(key);
}
// Get to get a value
bool TryGetValue(const TKeyArg& Key, TValue& Value) const
{
int index = IndexOf(Key);
if (index < 0)
return false;
Value = m_Values[index];
return true;
}
// Check if contains a key
bool ContainsKey(const TKeyArg& Key) const
{
return IndexOf(Key) >= 0;
}
// Find index of key
int IndexOf(const TKeyArg& Key) const
{
return m_Keys.IndexOf(Key);
}
// Type used as return value from GetAt
class CKeyPair
{
public:
CKeyPair(TKey& key, TValue& value) :
Key(key),
Value(value)
{
}
CKeyPair(const CKeyPair& Other) :
Key(Other.Key),
Value(Other.Value)
{
}
const TKey& Key;
TValue& Value;
private:
CKeyPair& operator=(const CKeyPair& Other);
};
protected:
CVector<TKey, TKeySem> m_Keys;
CVector<TValue, TValueSem> m_Values;
private:
// Unsupported
CKeyedArray(const CKeyedArray& Other);
CKeyedArray& operator=(const CKeyedArray& Other);
};
} // namespace
#endif // __simplelib_keyedarray_h__