Nebula
Loading...
Searching...
No Matches
sparsetable.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
13#include "util/array.h"
14#include "util/fixedtable.h"
15#include "util/dictionary.h"
16#include "util/stringatom.h"
17
18//------------------------------------------------------------------------------
19namespace Util
20{
21template<class TYPE> class SparseTable
22{
23public:
26
28 void BeginSetup(const Array<StringAtom>& columnNames, const Array<StringAtom>& rowNames, SizeT numUnique=0);
30 void AddSingle(IndexT colIndex, IndexT rowIndex, const TYPE& elm);
32 void AddMultiple(IndexT colIndex, IndexT rowIndex, const TYPE* firstElm, SizeT numElements);
34 void AddReference(IndexT colIndex, IndexT rowIndex, IndexT refColIndex, IndexT refRowIndex);
36 void SetEntryDirect(IndexT colIndex, IndexT rowIndex, ushort startIndex, ushort numElements);
38 void EndSetup();
40 void Clear();
41
43 SizeT GetNumColumns() const;
45 SizeT GetNumRows() const;
47 bool HasColumn(const StringAtom& colName) const;
49 bool HasRow(const StringAtom& rowName) const;
51 IndexT GetColumnIndexByName(const StringAtom& colName) const;
53 IndexT GetRowIndexByName(const StringAtom& rowName) const;
56
58 const TYPE* GetElements(IndexT colIndex, IndexT rowIndex, SizeT& outNumElements) const;
60 const TYPE* LookupElements(const StringAtom& colName, const StringAtom& rowName, SizeT& outNumElements) const;
61
62private:
65 {
66 TableEntry() : startIndex(0xffff), numElements(0) {};
67
68 ushort startIndex; // index into uniqueElements array
69 ushort numElements; // number of elements in uniqueElements array
70 };
71
76 bool inSetup;
77};
78
79//------------------------------------------------------------------------------
82template<class TYPE>
84 inSetup(false)
85{
86 // empty
87}
88
89//------------------------------------------------------------------------------
92template<class TYPE> void
94{
95 n_assert(!this->inSetup);
96 this->uniqueElements.Clear();
97 this->tableEntries.SetSize(0, 0);
98 this->colIndexMap.Clear();
99 this->rowIndexMap.Clear();
100}
101
102//------------------------------------------------------------------------------
105template<class TYPE> void
106SparseTable<TYPE>::BeginSetup(const Array<StringAtom>& columnNames, const Array<StringAtom>& rowNames, SizeT numUnique)
107{
108 n_assert(!this->inSetup);
109 this->inSetup = true;
110 if (numUnique > 0)
111 {
112 this->uniqueElements.Reserve(numUnique);
113 }
114 this->tableEntries.SetSize(columnNames.Size(), rowNames.Size());
115 this->colIndexMap.Reserve(columnNames.Size());
116 this->rowIndexMap.Reserve(rowNames.Size());
117
118 this->colIndexMap.BeginBulkAdd();
119 IndexT i;
120 for (i = 0; i < columnNames.Size(); i++)
121 {
122 this->colIndexMap.Add(columnNames[i], i);
123 }
124 this->colIndexMap.EndBulkAdd();
125
126 this->rowIndexMap.BeginBulkAdd();
127 for (i = 0; i < rowNames.Size(); i++)
128 {
129 this->rowIndexMap.Add(rowNames[i], i);
130 }
131 this->rowIndexMap.EndBulkAdd();
132}
133
134//------------------------------------------------------------------------------
137template<class TYPE> void
138SparseTable<TYPE>::AddSingle(IndexT colIndex, IndexT rowIndex, const TYPE& elm)
139{
140 n_assert(this->inSetup);
141 TableEntry& entry = this->tableEntries.At(colIndex, rowIndex);
142 entry.startIndex = this->uniqueElements.Size();
143 entry.numElements = 1;
144 this->uniqueElements.Append(elm);
145}
146
147//------------------------------------------------------------------------------
150template<class TYPE> void
151SparseTable<TYPE>::AddMultiple(IndexT colIndex, IndexT rowIndex, const TYPE* firstElm, SizeT numElms)
152{
153 n_assert(this->inSetup);
154 TableEntry& entry = this->tableEntires.At(colIndex, rowIndex);
155 entry.startIndex = this->uniqueElements.Size();
156 entry.numElements = elms.Size();
157 IndexT i;
158 for (i = 0; i < numElms; i++)
159 {
160 this->uniqueElements.Append(firstElm[i]);
161 }
162}
163
164//------------------------------------------------------------------------------
168template<class TYPE> void
169SparseTable<TYPE>::AddReference(IndexT colIndex, IndexT rowIndex, IndexT refColIndex, IndexT refRowIndex)
170{
171 n_assert(this->inSetup);
172 n_assert((refColIndex <= colIndex) && (refRowIndex <= rowIndex));
173 TableEntry& entry = this->tableEntries.At(refColIndex, refRowIndex);
174 this->tableEntries.Set(colIndex, rowIndex, entry);
175}
176
177//------------------------------------------------------------------------------
180template<class TYPE> void
181SparseTable<TYPE>::SetEntryDirect(IndexT colIndex, IndexT rowIndex, ushort startIndex, ushort numElements)
182{
183 n_assert(this->inSetup);
184 TableEntry& entry = this->tableEntries.At(refColIndex, refRowIndex);
185 entry.startIndex = startIndex;
186 entry.numElements = numElements;
187}
188
189//------------------------------------------------------------------------------
192template<class TYPE> void
194{
195 n_assert(this->inSetup);
196 this->inSetup = false;
197}
198
199//------------------------------------------------------------------------------
202template<class TYPE> SizeT
204{
205 return this->colIndexMap.Size();
206}
207
208//------------------------------------------------------------------------------
211template<class TYPE> SizeT
213{
214 return this->rowIndexMap.Size();
215}
216
217//------------------------------------------------------------------------------
220template<class TYPE> bool
222{
223 return this->colIndexMap.Contains(colName);
224}
225
226//------------------------------------------------------------------------------
229template<class TYPE> bool
231{
232 return this->rowIndexMap.Contains(rowName);
233}
234
235//------------------------------------------------------------------------------
238template<class TYPE> IndexT
240{
241 return this->colIndexMap[colName];
242}
243
244//------------------------------------------------------------------------------
247template<class TYPE> IndexT
249{
250 return this->rowIndexMap[rowName];
251}
252
253//------------------------------------------------------------------------------
256template<class TYPE> const TYPE*
257SparseTable<TYPE>::GetElements(IndexT colIndex, IndexT rowIndex, SizeT& outNumElements) const
258{
259 const TableEntry& tableEntry = this->tableEntries.At(colIndex, rowIndex);
260 if (tableEntry.numElements > 0)
261 {
262 const TYPE* elm = &(this->uniqueElements[tableEntry.startIndex]);
263 outNumElements = tableEntry.numElements;
264 return elm;
265 }
266 else
267 {
268 outNumElements = 0;
269 return 0;
270 }
271}
272
273//------------------------------------------------------------------------------
276template<class TYPE> const TYPE*
277SparseTable<TYPE>::LookupElements(const StringAtom& colName, const StringAtom& rowName, SizeT& outNumElements) const
278{
279 IndexT colIndex = this->colIndexMap[colName];
280 IndexT rowIndex = this->rowIndexMap[rowName];
281 return this->GetElements(colIndex, rowIndex, outNumElements);
282}
283
284} // namespace Util
285//------------------------------------------------------------------------------
Nebula's dynamic array class.
Definition array.h:60
const SizeT Size() const
get number of elements in array
Definition array.h:880
A collection of key/value pairs with quick value retrieval by key at roughly O(log n).
Definition dictionary.h:34
A fixed-size 2-dimensional array.
Definition fixedtable.h:18
A 2D sparse table where many entries may be redundant and support for multiple entries per cell.
Definition sparsetable.h:22
SizeT GetNumRows() const
get number of rows in the sparse table
Definition sparsetable.h:212
bool inSetup
Definition sparsetable.h:76
bool HasColumn(const StringAtom &colName) const
return true if column exists
Definition sparsetable.h:221
const TYPE * LookupElements(const StringAtom &colName, const StringAtom &rowName, SizeT &outNumElements) const
lookup entry by row/column names
Definition sparsetable.h:277
Dictionary< StringAtom, IndexT > rowIndexMap
Definition sparsetable.h:75
SparseTable()
constructor
Definition sparsetable.h:83
Array< TYPE > uniqueElements
Definition sparsetable.h:72
void AddSingle(IndexT colIndex, IndexT rowIndex, const TYPE &elm)
add a single new unique entry
Definition sparsetable.h:138
Dictionary< StringAtom, IndexT > colIndexMap
Definition sparsetable.h:74
FixedTable< TableEntry > tableEntries
Definition sparsetable.h:73
void BeginSetup(const Array< StringAtom > &columnNames, const Array< StringAtom > &rowNames, SizeT numUnique=0)
setup the sparse table
Definition sparsetable.h:106
void AddMultiple(IndexT colIndex, IndexT rowIndex, const TYPE *firstElm, SizeT numElements)
add a new multiple entry
Definition sparsetable.h:151
IndexT GetRowIndexByName(const StringAtom &rowName) const
return row index by name
Definition sparsetable.h:248
void AddReference(IndexT colIndex, IndexT rowIndex, IndexT refColIndex, IndexT refRowIndex)
add a reference to another column/index
Definition sparsetable.h:169
void SetEntryDirect(IndexT colIndex, IndexT rowIndex, ushort startIndex, ushort numElements)
add a direct reference using an index into the unique element array
Definition sparsetable.h:181
bool HasRow(const StringAtom &rowName) const
return true if row exists
Definition sparsetable.h:230
IndexT GetColumnIndexByName(const StringAtom &colName) const
return column index by name
Definition sparsetable.h:239
void EndSetup()
finish setting up the sparse table
Definition sparsetable.h:193
const TYPE * GetElements(IndexT colIndex, IndexT rowIndex, SizeT &outNumElements) const
get entry at given index
Definition sparsetable.h:257
SizeT GetNumColumns() const
get number of columns in the sparse table
Definition sparsetable.h:203
void Clear()
clear object
Definition sparsetable.h:93
SizeT GetNumUniqueElements() const
get current number of unique elements
A StringAtom.
Definition stringatom.h:22
#define n_assert(exp)
Definition debug.h:50
A pinned array is an array which manages its own virtual memory.
Definition String.cs:6
a table entry in the sparse table
Definition sparsetable.h:65
ushort startIndex
Definition sparsetable.h:68
TableEntry()
Definition sparsetable.h:66
ushort numElements
Definition sparsetable.h:69
int SizeT
Definition types.h:49
unsigned short ushort
Definition types.h:32
int IndexT
Definition types.h:48