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;
218 sllnode<_Tp> *tmp = head;
229 sllnode<_Tp> *tmp = tail;
240 while(head != NULL) {
241 sllnode<_Tp> *tmp=head->next;
249 void insertAfter(sllnode<_Tp> * node, _Tp val) {
250 sllnode<_Tp> *tmp = new sllnode<_Tp>();
253 tmp->next = node->next;
255 if (tmp->next == NULL) {
258 tmp->next->prev = tmp;
263 void insertBefore(sllnode<_Tp> * node, _Tp val) {
264 sllnode<_Tp> *tmp = new sllnode<_Tp>();
267 tmp->prev = node->prev;
269 if (tmp->prev == NULL) {
272 tmp->prev->next = tmp;
277 sllnode<_Tp> * erase(sllnode<_Tp> * node) {
281 node->prev->next = node->next;
287 node->next->prev = node->prev;
290 sllnode<_Tp> *next = node->next;
296 sllnode<_Tp> * begin() {
300 sllnode<_Tp> * end() {
326 #define VECTOR_DEFCAP 8
329 template<typename type>
332 ModelVector(uint _capacity = VECTOR_DEFCAP) :
335 array((type *) model_malloc(sizeof(type) * _capacity)) {
338 ModelVector(uint _capacity, type *_array) :
341 array((type *) model_malloc(sizeof(type) * _capacity)) {
342 memcpy(array, _array, capacity * sizeof(type));
349 return array[_size - 1];
352 void resize(uint psize) {
353 if (psize <= _size) {
356 } else if (psize > capacity) {
357 array = (type *)model_realloc(array, (psize << 1) * sizeof(type));
358 capacity = psize << 1;
360 bzero(&array[_size], (psize - _size) * sizeof(type));
364 void push_back(type item) {
365 if (_size >= capacity) {
366 uint newcap = capacity << 1;
367 array = (type *)model_realloc(array, newcap * sizeof(type));
370 array[_size++] = item;
373 type operator[](int index) const {
377 type & operator[](int index) {
385 type & at(uint index) const {
389 void setExpand(uint index, type item) {
395 void set(uint index, type item) {
399 void insertAt(uint index, type item) {
401 for (uint i = _size - 1;i > index;i--) {
407 void removeAt(uint index) {
408 for (uint i = index;(i + 1) < _size;i++) {
414 inline uint size() const {
434 template<typename type>
437 SnapVector(uint _capacity = VECTOR_DEFCAP) :
440 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
443 SnapVector(uint _capacity, type *_array) :
446 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
447 memcpy(array, _array, capacity * sizeof(type));
454 return array[_size - 1];
457 void resize(uint psize) {
458 if (psize <= _size) {
461 } else if (psize > capacity) {
462 array = (type *)snapshot_realloc(array, (psize <<1 )* sizeof(type));
463 capacity = psize << 1;
465 bzero(&array[_size], (psize - _size) * sizeof(type));
469 void push_back(type item) {
470 if (_size >= capacity) {
471 uint newcap = capacity << 1;
472 array = (type *)snapshot_realloc(array, newcap * sizeof(type));
475 array[_size++] = item;
478 type operator[](int index) const {
482 type & operator[](int index) {
490 type & at(uint index) const {
494 void setExpand(uint index, type item) {
500 void set(uint index, type item) {
504 void insertAt(uint index, type item) {
506 for (uint i = _size - 1;i > index;i--) {
512 void removeAt(uint index) {
513 for (uint i = index;(i + 1) < _size;i++) {
519 inline uint size() const {
524 snapshot_free(array);
538 #endif /* __STL_MODEL_H__ */