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 sllnode<_Tp> * 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;
291 void insertBefore(sllnode<_Tp> * node, _Tp val) {
292 sllnode<_Tp> *tmp = new sllnode<_Tp>();
295 tmp->prev = node->prev;
297 if (tmp->prev == NULL) {
300 tmp->prev->next = tmp;
305 sllnode<_Tp> * erase(sllnode<_Tp> * node) {
309 node->prev->next = node->next;
315 node->next->prev = node->prev;
318 sllnode<_Tp> *next = node->next;
324 sllnode<_Tp> * begin() {
328 sllnode<_Tp> * end() {
354 #define VECTOR_DEFCAP 8
357 template<typename type>
360 ModelVector(uint _capacity = VECTOR_DEFCAP) :
363 array((type *) model_malloc(sizeof(type) * _capacity)) {
366 ModelVector(uint _capacity, type *_array) :
369 array((type *) model_malloc(sizeof(type) * _capacity)) {
370 memcpy(array, _array, capacity * sizeof(type));
377 return array[_size - 1];
380 void resize(uint psize) {
381 if (psize <= _size) {
384 } else if (psize > capacity) {
385 array = (type *)model_realloc(array, (psize << 1) * sizeof(type));
386 capacity = psize << 1;
388 bzero(&array[_size], (psize - _size) * sizeof(type));
392 void push_back(type item) {
393 if (_size >= capacity) {
394 uint newcap = capacity << 1;
395 array = (type *)model_realloc(array, newcap * sizeof(type));
398 array[_size++] = item;
401 type operator[](int index) const {
405 type & operator[](int index) {
413 type & at(uint index) const {
417 void setExpand(uint index, type item) {
423 void set(uint index, type item) {
427 void insertAt(uint index, type item) {
429 for (uint i = _size - 1;i > index;i--) {
435 void removeAt(uint index) {
436 for (uint i = index;(i + 1) < _size;i++) {
442 inline uint size() const {
462 template<typename type>
465 SnapVector(uint _capacity = VECTOR_DEFCAP) :
468 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
471 SnapVector(uint _capacity, type *_array) :
474 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
475 memcpy(array, _array, capacity * sizeof(type));
482 return array[_size - 1];
485 void resize(uint psize) {
486 if (psize <= _size) {
489 } else if (psize > capacity) {
490 array = (type *)snapshot_realloc(array, (psize <<1 )* sizeof(type));
491 capacity = psize << 1;
493 bzero(&array[_size], (psize - _size) * sizeof(type));
497 void push_back(type item) {
498 if (_size >= capacity) {
499 uint newcap = capacity << 1;
500 array = (type *)snapshot_realloc(array, newcap * sizeof(type));
503 array[_size++] = item;
506 type operator[](int index) const {
510 type & operator[](int index) {
518 type & at(uint index) const {
522 void setExpand(uint index, type item) {
528 void set(uint index, type item) {
532 void insertAt(uint index, type item) {
534 for (uint i = _size - 1;i > index;i--) {
540 void removeAt(uint index) {
541 for (uint i = index;(i + 1) < _size;i++) {
547 inline uint size() const {
552 snapshot_free(array);
566 #endif /* __STL_MODEL_H__ */