1 #ifndef __STL_MODEL_H__
2 #define __STL_MODEL_H__
6 typedef unsigned int uint;
11 _Tp getVal() {return val;}
12 mllnode<_Tp> * getNext() {return next;}
13 mllnode<_Tp> * getPrev() {return prev;}
22 friend class ModelList;
25 template<typename _Tp>
29 ModelList() : head(NULL),
30 tail(NULL), _size(0) {
33 void push_front(_Tp val) {
34 mllnode<_Tp> * tmp = new mllnode<_Tp>();
46 void push_back(_Tp val) {
47 mllnode<_Tp> * tmp = new mllnode<_Tp>();
53 else tail->next = tmp;
59 mllnode<_Tp> *tmp = head;
70 mllnode<_Tp> *tmp = tail;
82 mllnode<_Tp> *tmp=head->next;
90 void insertAfter(mllnode<_Tp> * node, _Tp val) {
91 mllnode<_Tp> *tmp = new mllnode<_Tp>();
94 tmp->next = node->next;
96 if (tmp->next == NULL) {
99 tmp->next->prev = tmp;
104 void insertBefore(mllnode<_Tp> * node, _Tp val) {
105 mllnode<_Tp> *tmp = new mllnode<_Tp>();
108 tmp->prev = node->prev;
110 if (tmp->prev == NULL) {
113 tmp->prev->next = tmp;
118 mllnode<_Tp> * erase(mllnode<_Tp> * node) {
122 node->prev->next = node->next;
128 node->next->prev = node->prev;
130 mllnode<_Tp> *next = node->next;
136 mllnode<_Tp> * begin() {
140 mllnode<_Tp> * end() {
169 template<typename _Tp>
172 _Tp getVal() {return val;}
173 sllnode<_Tp> * getNext() {return next;}
174 sllnode<_Tp> * getPrev() {return prev;}
182 friend class SnapList;
183 friend class actionlist;
186 template<typename _Tp>
190 SnapList() : head(NULL),
191 tail(NULL), _size(0) {
194 void push_front(_Tp val) {
195 sllnode<_Tp> * tmp = new sllnode<_Tp>();
207 void push_back(_Tp val) {
208 sllnode<_Tp> * tmp = new sllnode<_Tp>();
214 else tail->next = tmp;
219 sllnode<_Tp>* add_front(_Tp val) {
220 sllnode<_Tp> * tmp = new sllnode<_Tp>();
233 sllnode<_Tp> * add_back(_Tp val) {
234 sllnode<_Tp> * tmp = new sllnode<_Tp>();
240 else tail->next = tmp;
247 sllnode<_Tp> *tmp = head;
258 sllnode<_Tp> *tmp = tail;
269 while(head != NULL) {
270 sllnode<_Tp> *tmp=head->next;
278 sllnode<_Tp> * insertAfter(sllnode<_Tp> * node, _Tp val) {
279 sllnode<_Tp> *tmp = new sllnode<_Tp>();
282 tmp->next = node->next;
284 if (tmp->next == NULL) {
287 tmp->next->prev = tmp;
293 void insertBefore(sllnode<_Tp> * node, _Tp val) {
294 sllnode<_Tp> *tmp = new sllnode<_Tp>();
297 tmp->prev = node->prev;
299 if (tmp->prev == NULL) {
302 tmp->prev->next = tmp;
307 sllnode<_Tp> * erase(sllnode<_Tp> * node) {
311 node->prev->next = node->next;
317 node->next->prev = node->prev;
320 sllnode<_Tp> *next = node->next;
326 sllnode<_Tp> * begin() {
330 sllnode<_Tp> * end() {
356 #define VECTOR_DEFCAP 8
359 template<typename type>
362 ModelVector(uint _capacity = VECTOR_DEFCAP) :
365 array((type *) model_malloc(sizeof(type) * _capacity)) {
368 ModelVector(uint _capacity, type *_array) :
371 array((type *) model_malloc(sizeof(type) * _capacity)) {
372 memcpy(array, _array, capacity * sizeof(type));
379 return array[_size - 1];
382 void resize(uint psize) {
383 if (psize <= _size) {
386 } else if (psize > capacity) {
387 array = (type *)model_realloc(array, (psize << 1) * sizeof(type));
388 capacity = psize << 1;
390 bzero(&array[_size], (psize - _size) * sizeof(type));
394 void push_back(type item) {
395 if (_size >= capacity) {
396 uint newcap = capacity << 1;
397 array = (type *)model_realloc(array, newcap * sizeof(type));
400 array[_size++] = item;
403 type operator[](int index) const {
407 type & operator[](int index) {
415 type & at(uint index) const {
419 void setExpand(uint index, type item) {
425 void set(uint index, type item) {
429 void insertAt(uint index, type item) {
431 for (uint i = _size - 1;i > index;i--) {
437 void removeAt(uint index) {
438 for (uint i = index;(i + 1) < _size;i++) {
444 inline uint size() const {
464 template<typename type>
467 SnapVector(uint _capacity = VECTOR_DEFCAP) :
470 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
473 SnapVector(uint _capacity, type *_array) :
476 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
477 memcpy(array, _array, capacity * sizeof(type));
484 return array[_size - 1];
487 void resize(uint psize) {
488 if (psize <= _size) {
491 } else if (psize > capacity) {
492 array = (type *)snapshot_realloc(array, (psize <<1 )* sizeof(type));
493 capacity = psize << 1;
495 bzero(&array[_size], (psize - _size) * sizeof(type));
499 void push_back(type item) {
500 if (_size >= capacity) {
501 uint newcap = capacity << 1;
502 array = (type *)snapshot_realloc(array, newcap * sizeof(type));
505 array[_size++] = item;
508 type operator[](int index) const {
512 type & operator[](int index) {
520 type & at(uint index) const {
524 void setExpand(uint index, type item) {
530 void set(uint index, type item) {
534 void insertAt(uint index, type item) {
536 for (uint i = _size - 1;i > index;i--) {
542 void remove(type item) {
543 for(uint i = 0;i < _size;i++) {
551 void removeAt(uint index) {
552 for (uint i = index;(i + 1) < _size;i++) {
558 inline uint size() const {
563 snapshot_free(array);
577 #endif /* __STL_MODEL_H__ */