1 #ifndef __STL_MODEL_H__
2 #define __STL_MODEL_H__
6 typedef unsigned int uint;
12 _Tp getVal() {return val;}
13 mllnode<_Tp> * getNext() {return next;}
14 mllnode<_Tp> * getPrev() {return prev;}
23 friend class ModelList;
26 template<typename _Tp>
30 ModelList() : head(NULL),
31 tail(NULL), _size(0) {
34 void push_front(_Tp val) {
35 mllnode<_Tp> * tmp = new mllnode<_Tp>();
47 void push_back(_Tp val) {
48 mllnode<_Tp> * tmp = new mllnode<_Tp>();
54 else tail->next = tmp;
60 mllnode<_Tp> *tmp = head;
71 mllnode<_Tp> *tmp = tail;
83 mllnode<_Tp> *tmp=head->next;
91 void insertAfter(mllnode<_Tp> * node, _Tp val) {
92 mllnode<_Tp> *tmp = new mllnode<_Tp>();
95 tmp->next = node->next;
97 if (tmp->next == NULL) {
100 tmp->next->prev = tmp;
105 void insertBefore(mllnode<_Tp> * node, _Tp val) {
106 mllnode<_Tp> *tmp = new mllnode<_Tp>();
109 tmp->prev = node->prev;
111 if (tmp->prev == NULL) {
114 tmp->prev->next = tmp;
119 mllnode<_Tp> * erase(mllnode<_Tp> * node) {
123 node->prev->next = node->next;
129 node->next->prev = node->prev;
131 mllnode<_Tp> *next = node->next;
137 mllnode<_Tp> * begin() {
141 mllnode<_Tp> * end() {
168 template<typename _Tp>
171 _Tp getVal() {return val;}
172 sllnode<_Tp> * getNext() {return next;}
173 sllnode<_Tp> * getPrev() {return prev;}
181 friend class SnapList;
184 template<typename _Tp>
188 SnapList() : head(NULL),
189 tail(NULL), _size(0) {
192 void push_front(_Tp val) {
193 sllnode<_Tp> * tmp = new sllnode<_Tp>();
205 void push_back(_Tp val) {
206 sllnode<_Tp> * tmp = new sllnode<_Tp>();
212 else tail->next = tmp;
217 sllnode<_Tp>add_front(_Tp val) {
218 sllnode<_Tp> * tmp = new sllnode<_Tp>();
231 sllnode<_Tp> * add_back(_Tp val) {
232 sllnode<_Tp> * tmp = new sllnode<_Tp>();
238 else tail->next = tmp;
245 sllnode<_Tp> *tmp = head;
256 sllnode<_Tp> *tmp = tail;
267 while(head != NULL) {
268 sllnode<_Tp> *tmp=head->next;
276 void insertAfter(sllnode<_Tp> * node, _Tp val) {
277 sllnode<_Tp> *tmp = new sllnode<_Tp>();
280 tmp->next = node->next;
282 if (tmp->next == NULL) {
285 tmp->next->prev = tmp;
290 void insertBefore(sllnode<_Tp> * node, _Tp val) {
291 sllnode<_Tp> *tmp = new sllnode<_Tp>();
294 tmp->prev = node->prev;
296 if (tmp->prev == NULL) {
299 tmp->prev->next = tmp;
304 sllnode<_Tp> * erase(sllnode<_Tp> * node) {
308 node->prev->next = node->next;
314 node->next->prev = node->prev;
317 sllnode<_Tp> *next = node->next;
323 sllnode<_Tp> * begin() {
327 sllnode<_Tp> * end() {
353 #define VECTOR_DEFCAP 8
356 template<typename type>
359 ModelVector(uint _capacity = VECTOR_DEFCAP) :
362 array((type *) model_malloc(sizeof(type) * _capacity)) {
365 ModelVector(uint _capacity, type *_array) :
368 array((type *) model_malloc(sizeof(type) * _capacity)) {
369 memcpy(array, _array, capacity * sizeof(type));
376 return array[_size - 1];
379 void resize(uint psize) {
380 if (psize <= _size) {
383 } else if (psize > capacity) {
384 array = (type *)model_realloc(array, (psize << 1) * sizeof(type));
385 capacity = psize << 1;
387 bzero(&array[_size], (psize - _size) * sizeof(type));
391 void push_back(type item) {
392 if (_size >= capacity) {
393 uint newcap = capacity << 1;
394 array = (type *)model_realloc(array, newcap * sizeof(type));
397 array[_size++] = item;
400 type operator[](int index) const {
404 type & operator[](int index) {
412 type & at(uint index) const {
416 void setExpand(uint index, type item) {
422 void set(uint index, type item) {
426 void insertAt(uint index, type item) {
428 for (uint i = _size - 1;i > index;i--) {
434 void removeAt(uint index) {
435 for (uint i = index;(i + 1) < _size;i++) {
441 inline uint size() const {
461 template<typename type>
464 SnapVector(uint _capacity = VECTOR_DEFCAP) :
467 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
470 SnapVector(uint _capacity, type *_array) :
473 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
474 memcpy(array, _array, capacity * sizeof(type));
481 return array[_size - 1];
484 void resize(uint psize) {
485 if (psize <= _size) {
488 } else if (psize > capacity) {
489 array = (type *)snapshot_realloc(array, (psize <<1 )* sizeof(type));
490 capacity = psize << 1;
492 bzero(&array[_size], (psize - _size) * sizeof(type));
496 void push_back(type item) {
497 if (_size >= capacity) {
498 uint newcap = capacity << 1;
499 array = (type *)snapshot_realloc(array, newcap * sizeof(type));
502 array[_size++] = item;
505 type operator[](int index) const {
509 type & operator[](int index) {
517 type & at(uint index) const {
521 void setExpand(uint index, type item) {
527 void set(uint index, type item) {
531 void insertAt(uint index, type item) {
533 for (uint i = _size - 1;i > index;i--) {
539 void removeAt(uint index) {
540 for (uint i = index;(i + 1) < _size;i++) {
546 inline uint size() const {
551 snapshot_free(array);
565 #endif /* __STL_MODEL_H__ */