Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / applications / utilities / surface / surfaceCoarsen / bunnylod / list.h
blob1c19f0d99c4405b4bfaee87e9659bb1517b7d23d
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
9 #include <assert.h>
10 #include <stdio.h>
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){
28 assert(i>=0 && i<num);
29 return element[i];}
33 template <class Type>
34 List<Type>::List(int s){
35 num=0;
36 array_size = 0;
37 element = NULL;
38 if(s) {
39 allocate(s);
43 template <class Type>
44 List<Type>::~List(){
45 delete element;
48 template <class Type>
49 void List<Type>::allocate(int s){
50 assert(s>0);
51 assert(s>=num);
52 Type *old = element;
53 array_size =s;
54 element = new Type[array_size];
55 assert(element);
56 for(int i=0;i<num;i++){
57 element[i]=old[i];
59 if(old) delete old;
61 template <class Type>
62 void List<Type>::SetSize(int s){
63 if(s==0) { if(element) delete element;}
64 else { allocate(s); }
65 num=s;
67 template <class Type>
68 void List<Type>::Pack(){
69 allocate(num);
72 template <class Type>
73 void List<Type>::Add(Type t){
74 assert(num<=array_size);
75 if(num==array_size) {
76 allocate((array_size)?array_size *2:16);
78 //int i;
79 //for(i=0;i<num;i++) {
80 // dissallow duplicates
81 // assert(element[i] != t);
82 //}
83 element[num++] = t;
86 template <class Type>
87 int List<Type>::Contains(Type t){
88 int i;
89 int count=0;
90 for(i=0;i<num;i++) {
91 if(element[i] == t) count++;
93 return count;
96 template <class Type>
97 void List<Type>::AddUnique(Type t){
98 if(!Contains(t)) Add(t);
102 template <class Type>
103 void List<Type>::DelIndex(int i){
104 assert(i<num);
105 num--;
106 while(i<num){
107 element[i] = element[i+1];
108 i++;
112 template <class Type>
113 void List<Type>::Remove(Type t){
114 int i;
115 for(i=0;i<num;i++) {
116 if(element[i] == t) {
117 break;
120 DelIndex(i);
121 for(i=0;i<num;i++) {
122 assert(element[i] != t);
130 #endif