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;}
21 friend class ModelList;
24 template<typename _Tp>
28 ModelList() : head(NULL),
32 void push_front(_Tp val) {
33 mllnode<_Tp> * tmp = new mllnode<_Tp>();
44 void push_back(_Tp val) {
45 mllnode<_Tp> * tmp = new mllnode<_Tp>();
51 else tail->next = tmp;
55 void insertAfter(mllnode<_Tp> * node, _Tp val) {
56 mllnode<_Tp> *tmp = new mllnode<_Tp>();
59 tmp->next = node->next;
61 if (tmp->next == NULL) {
64 tmp->next->prev = tmp;
68 void insertBefore(mllnode<_Tp> * node, _Tp val) {
69 mllnode<_Tp> *tmp = new mllnode<_Tp>();
72 tmp->prev = node->prev;
74 if (tmp->prev == NULL) {
77 tmp->prev->next = tmp;
81 mllnode<_Tp> * erase(mllnode<_Tp> * node) {
85 node->prev->next = node->next;
91 tail->next->prev = node->prev;
93 mllnode<_Tp> *next = node->next;
98 mllnode<_Tp> * begin() {
102 mllnode<_Tp> * end() {
121 template<typename _Tp>
124 _Tp getVal() {return val;}
125 sllnode<_Tp> * getNext() {return next;}
126 sllnode<_Tp> * getPrev() {return prev;}
134 friend class SnapList;
137 template<typename _Tp>
141 SnapList() : head(NULL),
145 void push_front(_Tp val) {
146 sllnode<_Tp> * tmp = new sllnode<_Tp>();
157 void push_back(_Tp val) {
158 sllnode<_Tp> * tmp = new sllnode<_Tp>();
164 else tail->next = tmp;
168 void insertAfter(sllnode<_Tp> * node, _Tp val) {
169 sllnode<_Tp> *tmp = new sllnode<_Tp>();
172 tmp->next = node->next;
174 if (tmp->next == NULL) {
177 tmp->next->prev = tmp;
181 void insertBefore(sllnode<_Tp> * node, _Tp val) {
182 sllnode<_Tp> *tmp = new sllnode<_Tp>();
185 tmp->prev = node->prev;
187 if (tmp->prev == NULL) {
190 tmp->prev->next = tmp;
194 sllnode<_Tp> * erase(sllnode<_Tp> * node) {
198 node->prev->next = node->next;
204 tail->next->prev = node->prev;
207 sllnode<_Tp> *next = node->next;
212 sllnode<_Tp> * begin() {
216 sllnode<_Tp> * end() {
236 #define VECTOR_DEFCAP 8
238 typedef unsigned int uint;
240 template<typename type>
243 ModelVector(uint _capacity = VECTOR_DEFCAP) :
246 array((type *) model_malloc(sizeof(type) * _capacity)) {
249 ModelVector(uint _capacity, type *_array) :
252 array((type *) model_malloc(sizeof(type) * _capacity)) {
253 memcpy(array, _array, capacity * sizeof(type));
260 return array[size - 1];
263 void resize(uint psize) {
264 if (psize <= _size) {
267 } else if (psize > capacity) {
268 array = (type *)model_realloc(array, (psize << 1) * sizeof(type));
269 capacity = psize << 1;
271 bzero(&array[_size], (psize - _size) * sizeof(type));
275 void push_back(type item) {
276 if (_size >= capacity) {
277 uint newcap = capacity << 1;
278 array = (type *)model_realloc(array, newcap * sizeof(type));
281 array[_size++] = item;
284 type operator[](uint index) const {
288 type & operator[](uint index) {
296 type & at(uint index) const {
300 void setExpand(uint index, type item) {
306 void set(uint index, type item) {
310 void insertAt(uint index, type item) {
312 for (uint i = _size - 1;i > index;i--) {
318 void removeAt(uint index) {
319 for (uint i = index;(i + 1) < _size;i++) {
325 inline uint size() const {
345 template<typename type>
348 SnapVector(uint _capacity = VECTOR_DEFCAP) :
351 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
354 SnapVector(uint _capacity, type *_array) :
357 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
358 memcpy(array, _array, capacity * sizeof(type));
365 return array[_size - 1];
368 void resize(uint psize) {
369 if (psize <= _size) {
372 } else if (psize > capacity) {
373 array = (type *)snapshot_realloc(array, (psize <<1 )* sizeof(type));
374 capacity = psize << 1;
376 bzero(&array[_size], (psize - _size) * sizeof(type));
380 void push_back(type item) {
381 if (_size >= capacity) {
382 uint newcap = capacity << 1;
383 array = (type *)snapshot_realloc(array, newcap * sizeof(type));
386 array[_size++] = item;
389 type & operator[](uint index) {
393 type operator[](uint index) const {
401 type & at(uint index) const {
405 void setExpand(uint index, type item) {
411 void set(uint index, type item) {
415 void insertAt(uint index, type item) {
417 for (uint i = _size - 1;i > index;i--) {
423 void removeAt(uint index) {
424 for (uint i = index;(i + 1) < _size;i++) {
430 inline uint size() const {
435 snapshot_free(array);
449 #endif /* __STL_MODEL_H__ */