OSDN Git Service

Update tests
[mikumikustudio/libgdx-mikumikustudio.git] / extensions / gdx-bullet / jni / src / custom / gdx / list.h
1 /*
2  *  A generic template list class.
3  *  Fairly typical of the list example you would
4  *  find in any c++ book.
5  */
6 #ifndef GENERIC_LIST_H
7 #define GENERIC_LIST_H
8
9 #include <assert.h>
10 #include <stdio.h>
11
12 template <class Type> class List {
13         public:
14                                 List(int s=0);
15                                 ~List();
16                 void    allocate(int s);
17                 void    SetSize(int s);
18                 void    Pack();
19                 void    Add(Type);
20                 void    AddUnique(Type);
21                 int     Contains(Type);
22                 void    Remove(Type);
23                 void    DelIndex(int i);
24                 Type *  element;
25                 int             num;
26                 int             array_size;
27                 Type    &operator[](int i){assert(i>=0 && i<num); return element[i];}
28 };
29
30
31 template <class Type>
32 List<Type>::List(int s){
33         num=0;
34         array_size = 0;
35         element = NULL;
36         if(s) {
37                 allocate(s);
38         }
39 }
40
41 template <class Type>
42 List<Type>::~List(){
43         delete element;
44 }
45
46 template <class Type>
47 void List<Type>::allocate(int s){
48         assert(s>0);
49         assert(s>=num);
50         Type *old = element;
51         array_size =s;
52         element = new Type[array_size];
53         assert(element);
54         for(int i=0;i<num;i++){
55                 element[i]=old[i];
56         }
57         if(old) delete old;
58 }
59 template <class Type>
60 void List<Type>::SetSize(int s){
61         if(s==0) { if(element) delete element;}
62         else {  allocate(s); }
63         num=s;
64 }
65 template <class Type>
66 void List<Type>::Pack(){
67         allocate(num);
68 }
69
70 template <class Type>
71 void List<Type>::Add(Type t){
72         assert(num<=array_size);
73         if(num==array_size) {
74                 allocate((array_size)?array_size *2:16);
75         }
76         //int i;
77         //for(i=0;i<num;i++) {
78                 // dissallow duplicates
79         //      assert(element[i] != t);
80         //}
81         element[num++] = t;
82 }
83
84 template <class Type>
85 int List<Type>::Contains(Type t){
86         int i;
87         int count=0;
88         for(i=0;i<num;i++) {
89                 if(element[i] == t) count++;
90         }
91         return count;
92 }
93
94 template <class Type>
95 void List<Type>::AddUnique(Type t){
96         if(!Contains(t)) Add(t);
97 }
98
99
100 template <class Type>
101 void List<Type>::DelIndex(int i){
102         assert(i<num);
103         num--;
104         while(i<num){
105                 element[i] = element[i+1];
106                 i++;
107         }
108 }
109
110 template <class Type>
111 void List<Type>::Remove(Type t){
112         int i;
113         for(i=0;i<num;i++) {
114                 if(element[i] == t) {
115                         break;
116                 }
117         }
118         DelIndex(i);
119         for(i=0;i<num;i++) {
120                 assert(element[i] != t);
121         }
122 }
123
124
125
126
127
128 #endif