blob: 955c9817bc32ccb8d284f52d3b77395318475925 (
plain) (
blame)
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
|
/**
* @file
* @brief Functions for managing indexed tables of data attached to enums
**/
#ifndef DATAINDEX_H
#define DATAINDEX_H
template <typename TKey, typename TVal, int NMax>
class data_index
{
public:
data_index(const TVal* _pop) : pop(_pop), index_created(false)
{ }
const TVal* operator[](TKey key)
{
ASSERT_RANGE(key, 0, NMax);
check_index();
return &pop[index[key]];
}
bool contains(TKey key)
{
ASSERT_RANGE(key, 0, NMax);
check_index();
return index[key] >= 0;
}
protected:
const TVal* pop;
bool index_created;
int index[NMax];
virtual TKey map(const TVal* val) = 0;
void check_index()
{
if (!index_created)
{
memset(index, -1, sizeof(index));
int i = 0;
for (const TVal* p = pop; map(p); p++)
{
index[map(p)] = i;
i++;
}
index_created = true;
}
}
};
#endif
|