00001
00007 #ifndef _SET_H_
00008 #define _SET_H_
00009
00010 #include <stdlib.h>
00011 #include <string.h>
00012
00013 template <class TYPE>
00014 class Set {
00015 public:
00016 Set(const unsigned int s = 0);
00017 virtual ~Set();
00018 TYPE *getArray() const;
00019 TYPE &operator [] (const unsigned int i) const;
00020 unsigned int getCount() const;
00021 unsigned int add(const TYPE&);
00022 void remove(const unsigned int i);
00023 void removeObject(const TYPE&);
00024 void clear();
00025 void reset();
00026
00027 private:
00028 unsigned int size, count;
00029 TYPE *list;
00030 };
00031
00032
00033
00034
00035 template <class TYPE>
00036 Set<TYPE>::Set(const unsigned int s) {
00037 count = 0;
00038 size = s;
00039 if (size > 0)
00040 list = (TYPE *)malloc(size*sizeof(TYPE));
00041 else
00042 list = NULL;
00043 }
00044
00045
00046
00047
00048 template <class TYPE>
00049 Set<TYPE>::~Set() {
00050 free(list);
00051 }
00052
00053
00054
00055
00056 template <class TYPE>
00057 TYPE *Set<TYPE>::getArray() const {
00058 return list;
00059 }
00060
00061
00062
00063
00064 template <class TYPE>
00065 TYPE &Set<TYPE>::operator [] (const unsigned int i) const {
00066 return list[i];
00067 }
00068
00069
00070
00071
00072 template <class TYPE>
00073 unsigned int Set<TYPE>::getCount() const {
00074 return count;
00075 }
00076
00077
00078
00079
00080 template <class TYPE>
00081 unsigned int Set<TYPE>::add(const TYPE& obj) {
00082 if (count >= size) {
00083 if (size)
00084 size += size;
00085 else
00086 size = 8;
00087
00088 list = (TYPE *)realloc(list, size*sizeof(TYPE));
00089 }
00090 list[count] = obj;
00091 return count++;
00092 }
00093
00094
00095
00096
00097 template <class TYPE>
00098 void Set<TYPE>::remove(const unsigned int i) {
00099 if (i < count) {
00100 count--;
00101 list[i] = list[count];
00102 }
00103 }
00104
00105
00106
00107
00108 template <class TYPE>
00109 void Set<TYPE>::removeObject(const TYPE& obj) {
00110 for (unsigned int i=0; i<count; i++) {
00111 if (obj == list[i]) {
00112 remove(i);
00113 return;
00114 }
00115 }
00116 }
00117
00118
00119
00120
00121 template <class TYPE>
00122 void Set<TYPE>::clear() {
00123 count = 0;
00124 }
00125
00126
00127
00128
00129 template <class TYPE>
00130 void Set<TYPE>::reset() {
00131 free(list);
00132 list = NULL;
00133 count = size = 0;
00134 }
00135
00136 #endif