Line | |
---|
1 | #ifndef __CONTAINER_H__
|
---|
2 | #define __CONTAINER_H__
|
---|
3 |
|
---|
4 | template <class T>
|
---|
5 | class ObjList
|
---|
6 | {
|
---|
7 | public:
|
---|
8 | ObjList(void){ clear(); }
|
---|
9 |
|
---|
10 | ~ObjList(void){
|
---|
11 | if (data)
|
---|
12 | free(data);
|
---|
13 | data_size=0;
|
---|
14 | capacity=0;
|
---|
15 | }
|
---|
16 |
|
---|
17 | ObjList(const ObjList &l){
|
---|
18 | this->operator =(l);
|
---|
19 | }
|
---|
20 |
|
---|
21 | void push_back(T i){
|
---|
22 | if (data_size>=capacity)
|
---|
23 | {
|
---|
24 | capacity *= 2;
|
---|
25 | data = (T*)realloc(data,capacity*sizeof(T));
|
---|
26 | }
|
---|
27 | data[data_size] = i;
|
---|
28 | data_size++;
|
---|
29 | }
|
---|
30 |
|
---|
31 | T & operator[](unsigned int i){ assert(i<data_size); return data[i]; }
|
---|
32 | const T & operator[](unsigned int i) const { assert(i<data_size); return data[i]; }
|
---|
33 |
|
---|
34 | const ObjList & operator=(const ObjList &l){
|
---|
35 | /* capacity=l.capacity;
|
---|
36 | data_size=l.data_size;
|
---|
37 | data=(T*)malloc(capacity*sizeof(T));
|
---|
38 | memcpy(data,l.data,data_size*sizeof(T));*/
|
---|
39 | clear();
|
---|
40 | for (unsigned int i=0; i<l.size(); i++)
|
---|
41 | push_back(l[i]);
|
---|
42 | return l;
|
---|
43 | }
|
---|
44 |
|
---|
45 | unsigned int size(void) const { return data_size; }
|
---|
46 |
|
---|
47 | void clear(void){
|
---|
48 | capacity=16;
|
---|
49 | data=(T*)malloc(capacity*sizeof(T));
|
---|
50 | data_size=0;
|
---|
51 | }
|
---|
52 |
|
---|
53 | void eraseAtPos(unsigned int i){
|
---|
54 | if (i!=data_size-1)
|
---|
55 | memmove(data+i,data+i+1,(data_size-i-1)*sizeof(T));
|
---|
56 | data_size--;
|
---|
57 | }
|
---|
58 |
|
---|
59 | private:
|
---|
60 | T *data;
|
---|
61 | unsigned int data_size;
|
---|
62 | unsigned int capacity;
|
---|
63 | };
|
---|
64 |
|
---|
65 | #endif
|
---|
Note: See
TracBrowser
for help on using the repository browser.