1 #ifndef __STL_MODEL_H__
2 #define __STL_MODEL_H__
10 _Tp getVal() {return val;}
11 mllnode<_Tp> * getNext() {return next;}
12 mllnode<_Tp> * getPrev() {return prev;}
19 friend class ModelList;
22 template<typename _Tp>
26 ModelList() : head(NULL),
30 void push_front(_Tp val) {
31 mllnode<_Tp> * tmp = new mllnode<_Tp>();
42 void push_back(_Tp val) {
43 mllnode<_Tp> * tmp = new mllnode<_Tp>();
49 else tail->next = tmp;
53 void insertAfter(mllnode<_Tp> * node, _Tp val) {
54 mllnode<_Tp> *tmp = new mllnode<_Tp>();
57 tmp->next = node->next;
59 if (tmp->next == NULL) {
62 tmp->next->prev = tmp;
66 void insertBefore(mllnode<_Tp> * node, _Tp val) {
67 mllnode<_Tp> *tmp = new mllnode<_Tp>();
70 tmp->prev = node->prev;
72 if (tmp->prev == NULL) {
75 tmp->prev->next = tmp;
79 void erase(mllnode<_Tp> * node) {
83 node->prev->next = node->next;
89 tail->next->prev = node->prev;
95 mllnode<_Tp> begin() {
118 template<typename _Tp>
121 _Tp getVal() {return val;}
122 sllnode<_Tp> * getNext() {return next;}
123 sllnode<_Tp> * getPrev() {return prev;}
130 friend class SnapList;
133 template<typename _Tp>
137 SnapList() : head(NULL),
141 void push_front(_Tp val) {
142 sllnode<_Tp> * tmp = new sllnode<_Tp>();
153 void push_back(_Tp val) {
154 sllnode<_Tp> * tmp = new sllnode<_Tp>();
160 else tail->next = tmp;
164 void insertAfter(sllnode<_Tp> * node, _Tp val) {
165 sllnode<_Tp> *tmp = new sllnode<_Tp>();
168 tmp->next = node->next;
170 if (tmp->next == NULL) {
173 tmp->next->prev = tmp;
177 void insertBefore(sllnode<_Tp> * node, _Tp val) {
178 sllnode<_Tp> *tmp = new sllnode<_Tp>();
181 tmp->prev = node->prev;
183 if (tmp->prev == NULL) {
186 tmp->prev->next = tmp;
190 void erase(sllnode<_Tp> * node) {
194 node->prev->next = node->next;
200 tail->next->prev = node->prev;
206 sllnode<_Tp> begin() {
230 #define VECTOR_DEFCAP 8
232 typedef unsigned int uint;
234 template<typename type>
237 ModelVector(uint _capacity = VECTOR_DEFCAP) :
240 array((type *) model_malloc(sizeof(type) * _capacity)) {
243 ModelVector(uint _capacity, type *_array) :
246 array((type *) model_malloc(sizeof(type) * _capacity)) {
247 memcpy(array, _array, capacity * sizeof(type));
254 return array[size - 1];
257 void resize(uint psize) {
258 if (psize <= _size) {
261 } else if (psize > capacity) {
262 array = (type *)model_realloc(array, (psize << 1) * sizeof(type));
263 capacity = psize << 1;
265 bzero(&array[_size], (psize - _size) * sizeof(type));
269 void push_back(type item) {
270 if (_size >= capacity) {
271 uint newcap = capacity << 1;
272 array = (type *)model_realloc(array, newcap * sizeof(type));
275 array[_size++] = item;
278 type operator[](uint index) const {
282 type & operator[](uint index) {
290 type & at(uint index) const {
294 void setExpand(uint index, type item) {
300 void set(uint index, type item) {
304 void insertAt(uint index, type item) {
306 for (uint i = _size - 1;i > index;i--) {
312 void removeAt(uint index) {
313 for (uint i = index;(i + 1) < _size;i++) {
319 inline uint size() const {
339 template<typename type>
342 SnapVector(uint _capacity = VECTOR_DEFCAP) :
345 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
348 SnapVector(uint _capacity, type *_array) :
351 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
352 memcpy(array, _array, capacity * sizeof(type));
359 return array[_size - 1];
362 void resize(uint psize) {
363 if (psize <= _size) {
366 } else if (psize > capacity) {
367 array = (type *)snapshot_realloc(array, (psize <<1 )* sizeof(type));
368 capacity = psize << 1;
370 bzero(&array[_size], (psize - _size) * sizeof(type));
374 void push_back(type item) {
375 if (_size >= capacity) {
376 uint newcap = capacity << 1;
377 array = (type *)snapshot_realloc(array, newcap * sizeof(type));
380 array[_size++] = item;
383 type & operator[](uint index) {
387 type operator[](uint index) const {
395 type & at(uint index) const {
399 void setExpand(uint index, type item) {
405 void set(uint index, type item) {
409 void insertAt(uint index, type item) {
411 for (uint i = _size - 1;i > index;i--) {
417 void removeAt(uint index) {
418 for (uint i = index;(i + 1) < _size;i++) {
424 inline uint size() const {
429 snapshot_free(array);
443 #endif /* __STL_MODEL_H__ */