-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtempzerovec.h
79 lines (63 loc) · 1.39 KB
/
tempzerovec.h
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
#ifndef JWUTIL_TEMPZEROVEC_H
#define JWUTIL_TEMPZEROVEC_H
#include <vector>
namespace jw_util
{
template <typename DataType>
class TempZeroVec
{
public:
static TempZeroVec alloc(unsigned int size)
{
#ifdef TEMPZEROVEC_ASSERT_UNLOCKED
assert(!locked);
locked = true;
#endif
if (data.size() < size)
{
data.resize(size, 0);
}
return TempZeroVec();
}
static void free(const TempZeroVec &vec)
{
#ifdef TEMPZEROVEC_ASSERT_UNLOCKED
assert(locked);
locked = false;
#endif
#ifdef TEMPZEROVEC_ASSERT_ZEROED
typename std::vector<DataType>::const_iterator i = data.cbegin();
while (i != data.cend())
{
assert(*i == static_cast<DataType>(0));
i++;
}
#endif
}
DataType &operator[](unsigned int i)
{
assert(i < data.size());
return data[i];
}
const DataType &operator[](unsigned int i) const
{
assert(i < data.size());
return data[i];
}
private:
TempZeroVec()
{
}
static std::vector<DataType> data;
#ifdef TEMPZEROVEC_ASSERT_UNLOCKED
static bool locked;
#endif
};
template <typename DataType>
std::vector<DataType> TempZeroVec<DataType>::data;
#ifdef TEMPZEROVEC_ASSERT_UNLOCKED
template <typename DataType>
bool TempZeroVec<DataType>::locked = false;
#endif
}
#endif // JWUTIL_TEMPZEROVEC_H