1 #ifndef __STL_MODEL_H__
2 #define __STL_MODEL_H__
8 class ModelList : public std::list<_Tp, ModelAlloc<_Tp> >
11 typedef std::list< _Tp, ModelAlloc<_Tp> > list;
17 ModelList(size_t n, const _Tp& val = _Tp()) :
24 template<typename _Tp>
25 class SnapList : public std::list<_Tp, SnapshotAlloc<_Tp> >
28 typedef std::list<_Tp, SnapshotAlloc<_Tp> > list;
34 SnapList(size_t n, const _Tp& val = _Tp()) :
41 #define VECTOR_DEFCAP 8
43 typedef unsigned int uint;
45 template<typename type>
48 ModelVector(uint _capacity = VECTOR_DEFCAP) :
51 array((type *) model_malloc(sizeof(type) * _capacity)) {
54 ModelVector(uint _capacity, type *_array) :
57 array((type *) model_malloc(sizeof(type) * _capacity)) {
58 memcpy(array, _array, capacity * sizeof(type));
65 return array[size - 1];
68 void resize(uint psize) {
72 } else if (psize > capacity) {
73 array = (type *)model_realloc(array, (psize << 1) * sizeof(type));
74 capacity = psize << 1;
76 bzero(&array[_size], (psize - _size) * sizeof(type));
80 void push_back(type item) {
81 if (_size >= capacity) {
82 uint newcap = capacity << 1;
83 array = (type *)model_realloc(array, newcap * sizeof(type));
86 array[_size++] = item;
89 type operator[](uint index) const {
93 type & operator[](uint index) {
101 type & at(uint index) const {
105 void setExpand(uint index, type item) {
111 void set(uint index, type item) {
115 void insertAt(uint index, type item) {
117 for (uint i = _size - 1;i > index;i--) {
123 void removeAt(uint index) {
124 for (uint i = index;(i + 1) < _size;i++) {
130 inline uint size() const {
150 template<typename type>
153 SnapVector(uint _capacity = VECTOR_DEFCAP) :
156 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
159 SnapVector(uint _capacity, type *_array) :
162 array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
163 memcpy(array, _array, capacity * sizeof(type));
170 return array[_size - 1];
173 void resize(uint psize) {
174 if (psize <= _size) {
177 } else if (psize > capacity) {
178 array = (type *)snapshot_realloc(array, (psize <<1 )* sizeof(type));
179 capacity = psize << 1;
181 bzero(&array[_size], (psize - _size) * sizeof(type));
185 void push_back(type item) {
186 if (_size >= capacity) {
187 uint newcap = capacity << 1;
188 array = (type *)snapshot_realloc(array, newcap * sizeof(type));
191 array[_size++] = item;
194 type & operator[](uint index) {
198 type operator[](uint index) const {
206 type & at(uint index) const {
210 void setExpand(uint index, type item) {
216 void set(uint index, type item) {
220 void insertAt(uint index, type item) {
222 for (uint i = _size - 1;i > index;i--) {
228 void removeAt(uint index) {
229 for (uint i = index;(i + 1) < _size;i++) {
235 inline uint size() const {
240 snapshot_free(array);
254 #endif /* __STL_MODEL_H__ */