forked from teamhimeh/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_tpl.h
112 lines (91 loc) · 2.19 KB
/
array_tpl.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
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
/*
* This file is part of the Simutrans project under the Artistic License.
* (see LICENSE.txt)
*/
#ifndef TPL_ARRAY_TPL_H
#define TPL_ARRAY_TPL_H
#include <typeinfo>
#include "../simdebug.h"
#include "../simtypes.h"
/**
* A template class for bounds checked 1-dimensional arrays.
* This is kept as simple as possible. Does not use exceptions
* for error handling.
*/
template<class T> class array_tpl
{
public:
typedef const T* const_iterator;
typedef T* iterator;
typedef uint32 index;
explicit array_tpl() : data(NULL), size(0) {}
explicit array_tpl(index s) : data(new T[s]), size(s) {}
explicit array_tpl(index s, const T& value) : data(new T[s]), size(s)
{
for (index i = 0; i < size; i++) {
data[i] = value;
}
}
~array_tpl() { delete [] data; }
index get_count() const { return size; }
bool empty() const { return size == 0; }
void clear()
{
delete [] data;
data = 0;
size = 0;
}
void resize(index resize)
{
if (size < resize) {
// extend if needed
T* new_data = new T[resize];
for (index i = 0; i < size; i++) {
new_data[i] = data[i];
}
delete [] data;
data = new_data;
}
size = resize;
}
void resize(index resize, const T& value)
{
if (size < resize) {
T* new_data = new T[resize];
index i;
for (i = 0; i < size; i++) {
new_data[i] = data[i];
}
for (; i < resize; i++) {
new_data[i] = value;
}
delete [] data;
data = new_data;
size = resize;
}
}
T& operator [](index i)
{
if (i >= size) {
dbg->fatal("array_tpl<T>::[]", "index out of bounds: %d not in 0..%d, T=%s", i, size - 1, typeid(T).name());
}
return data[i];
}
const T& operator [](index i) const
{
if (i >= size) {
dbg->fatal("array_tpl<T>::[]", "index out of bounds: %d not in 0..%d, T=%s", i, size - 1, typeid(T).name());
}
return data[i];
}
iterator begin() { return data; }
iterator end() { return data + size; }
const_iterator begin() const { return data; }
const_iterator end() const { return data + size; }
private:
array_tpl(const array_tpl&);
array_tpl& operator=( array_tpl const& other );
T* data;
index size;
};
#endif