remove STL vector
[c11tester.git] / stl-model.h
1 #ifndef __STL_MODEL_H__
2 #define __STL_MODEL_H__
3
4 #include <list>
5 #include "mymemory.h"
6
7 template<typename _Tp>
8 class ModelList : public std::list<_Tp, ModelAlloc<_Tp> >
9 {
10 public:
11         typedef std::list< _Tp, ModelAlloc<_Tp> > list;
12
13         ModelList() :
14                 list()
15         { }
16
17         ModelList(size_t n, const _Tp& val = _Tp()) :
18                 list(n, val)
19         { }
20
21         MEMALLOC
22 };
23
24 template<typename _Tp>
25 class SnapList : public std::list<_Tp, SnapshotAlloc<_Tp> >
26 {
27 public:
28         typedef std::list<_Tp, SnapshotAlloc<_Tp> > list;
29
30         SnapList() :
31                 list()
32         { }
33
34         SnapList(size_t n, const _Tp& val = _Tp()) :
35                 list(n, val)
36         { }
37
38         SNAPSHOTALLOC
39 };
40
41 #define VECTOR_DEFCAP 8
42
43 typedef unsigned int uint;
44
45 template<typename type>
46 class ModelVector {
47 public:
48         ModelVector(uint _capacity = VECTOR_DEFCAP) :
49                 _size(0),
50                 capacity(_capacity),
51                 array((type *) model_malloc(sizeof(type) * _capacity)) {
52         }
53
54         ModelVector(uint _capacity, type *_array)  :
55                 _size(_capacity),
56                 capacity(_capacity),
57                 array((type *) model_malloc(sizeof(type) * _capacity)) {
58                 memcpy(array, _array, capacity * sizeof(type));
59         }
60         void pop_back() {
61                 _size--;
62         }
63
64         type back() const {
65                 return array[size - 1];
66         }
67
68         void resize(uint psize) {
69                 if (psize <= _size) {
70                         _size = psize;
71                         return;
72                 } else if (psize > capacity) {
73                         array = (type *)model_realloc(array, (psize << 1) * sizeof(type));
74                         capacity = psize << 1;
75                 }
76                 bzero(&array[_size], (psize - _size) * sizeof(type));
77                 _size = psize;
78         }
79
80         void push_back(type item) {
81                 if (_size >= capacity) {
82                         uint newcap = capacity << 1;
83                         array = (type *)model_realloc(array, newcap * sizeof(type));
84                         capacity = newcap;
85                 }
86                 array[_size++] = item;
87         }
88
89         type operator[](uint index) const {
90                 return array[index];
91         }
92
93         type & operator[](uint index) {
94                 return array[index];
95         }
96
97         bool empty() const {
98                 return _size == 0;
99         }
100
101         type & at(uint index) const {
102                 return array[index];
103         }
104
105         void setExpand(uint index, type item) {
106                 if (index >= _size)
107                         resize(index + 1);
108                 set(index, item);
109         }
110
111         void set(uint index, type item) {
112                 array[index] = item;
113         }
114
115         void insertAt(uint index, type item) {
116                 resize(_size + 1);
117                 for (uint i = _size - 1;i > index;i--) {
118                         set(i, at(i - 1));
119                 }
120                 array[index] = item;
121         }
122
123         void removeAt(uint index) {
124                 for (uint i = index;(i + 1) < _size;i++) {
125                         set(i, at(i + 1));
126                 }
127                 resize(_size - 1);
128         }
129
130         inline uint size() const {
131                 return _size;
132         }
133
134         ~ModelVector() {
135                 model_free(array);
136         }
137
138         void clear() {
139                 _size = 0;
140         }
141
142         MEMALLOC;
143 private:
144         uint _size;
145         uint capacity;
146         type *array;
147 };
148
149
150 template<typename type>
151 class SnapVector {
152 public:
153         SnapVector(uint _capacity = VECTOR_DEFCAP) :
154                 _size(0),
155                 capacity(_capacity),
156                 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
157         }
158
159         SnapVector(uint _capacity, type *_array)  :
160                 _size(_capacity),
161                 capacity(_capacity),
162                 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
163                 memcpy(array, _array, capacity * sizeof(type));
164         }
165         void pop_back() {
166                 _size--;
167         }
168
169         type back() const {
170                 return array[_size - 1];
171         }
172
173         void resize(uint psize) {
174                 if (psize <= _size) {
175                         _size = psize;
176                         return;
177                 } else if (psize > capacity) {
178                         array = (type *)snapshot_realloc(array, (psize <<1 )* sizeof(type));
179                         capacity = psize << 1;
180                 }
181                 bzero(&array[_size], (psize - _size) * sizeof(type));
182                 _size = psize;
183         }
184
185         void push_back(type item) {
186                 if (_size >= capacity) {
187                         uint newcap = capacity << 1;
188                         array = (type *)snapshot_realloc(array, newcap * sizeof(type));
189                         capacity = newcap;
190                 }
191                 array[_size++] = item;
192         }
193
194         type & operator[](uint index) {
195                 return array[index];
196         }
197
198         type operator[](uint index) const {
199                 return array[index];
200         }
201
202         bool empty() const {
203                 return _size == 0;
204         }
205
206         type & at(uint index) const {
207                 return array[index];
208         }
209
210         void setExpand(uint index, type item) {
211                 if (index >= _size)
212                         resize(index + 1);
213                 set(index, item);
214         }
215
216         void set(uint index, type item) {
217                 array[index] = item;
218         }
219
220         void insertAt(uint index, type item) {
221                 resize(_size + 1);
222                 for (uint i = _size - 1;i > index;i--) {
223                         set(i, at(i - 1));
224                 }
225                 array[index] = item;
226         }
227
228         void removeAt(uint index) {
229                 for (uint i = index;(i + 1) < _size;i++) {
230                         set(i, at(i + 1));
231                 }
232                 resize(_size - 1);
233         }
234
235         inline uint size() const {
236                 return _size;
237         }
238
239         ~SnapVector() {
240                 snapshot_free(array);
241         }
242
243         void clear() {
244                 _size = 0;
245         }
246
247         SNAPSHOTALLOC;
248 private:
249         uint _size;
250         uint capacity;
251         type *array;
252 };
253
254 #endif  /* __STL_MODEL_H__ */