00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef FTList_FLAG_
00010 #define FTList_FLAG_
00011
00012
00013 #include <stdlib.h>
00014
00015 template <class T>
00016 class FTList{
00017 public:
00018
00020 FTList(int length_to_alloc = 100);
00021
00023 FTList(const FTList<T> &);
00024
00026 ~FTList();
00027
00029 int append(const T& x);
00030
00032 int append(const FTList<T> &);
00033
00035 int remove(int &);
00036
00038 void remove2(int);
00039
00041 void replace(int i, T src);
00042
00044 int deleteObj(int &);
00045
00047 void removeLast(void);
00048
00050 void clear(void);
00051
00053 void removeAll(void);
00054
00056 void deleteAll(void);
00057
00059 void resize(void);
00060
00062 T & operator[] (unsigned i) const;
00063
00065 T & operator() (unsigned i) const;
00066
00068 T & first(void) const;
00069
00071 T* firstPtr(void) const;
00072
00074 T* lastPtr(void) const;
00075
00077 int length(void) const;
00078
00079 private:
00080 int _length;
00081 int _remain;
00082 int _length_to_alloc;
00083 T * _obj;
00084 };
00085
00086
00087 #ifdef FTList_NO_INLINE
00088 #define inline
00089 #else
00090 #undef inline
00091 #define FTList_INLINE_DEFINE_HERE
00092 #endif
00093
00094 #ifdef FTList_INLINE_DEFINE_HERE
00095
00096 template <class T>
00097 inline
00098 FTList<T>::FTList(int length_to_alloc)
00099 : _length(0),
00100 _remain(length_to_alloc),
00101 _length_to_alloc(length_to_alloc),
00102 _obj((T *) malloc(length_to_alloc * sizeof(T))){
00103 }
00104
00105 template <class T>
00106 FTList<T>::FTList(const FTList<T> & src)
00107 : _length(src._length),
00108 _remain(src._remain),
00109 _length_to_alloc(src._length_to_alloc)
00110 {
00111 _obj = (T *) malloc((_length + _remain) * sizeof(T));
00112 T * srcObj = src._obj;
00113 for (int i = 0; i < _length; i++){
00114 *(_obj+i) = *(srcObj+i);
00115 }
00116 }
00117
00118 template <class T>
00119 inline
00120 FTList<T>::~FTList(){
00121 free(_obj);
00122 }
00123
00124 template <class T>
00125 inline
00126 int
00127 FTList<T>::append(const T& src){
00128 if (!_remain){
00129 _obj = (T *) realloc(_obj,(_length+_length_to_alloc) * sizeof(T));
00130 _remain = _length_to_alloc;
00131 }
00132 *(_obj+(_length++)) = src;
00133 _remain--;
00134 return _length;
00135 }
00136
00137 template <class T>
00138 inline
00139 int
00140 FTList<T>::remove(int & iterator){
00141 *(_obj+(iterator--)) = *(_obj+(--_length));
00142 _remain++;
00143 return _length;
00144 }
00145
00146 template <class T>
00147 inline
00148 void
00149 FTList<T>::remove2(int iterator){
00150 *(_obj+iterator) = *(_obj+(--_length));
00151 _remain++;
00152 }
00153
00154 template <class T>
00155 inline
00156 void
00157 FTList<T>::replace(int i, T src){
00158 *(_obj+i) = src;
00159 }
00160
00161 template <class T>
00162 inline
00163 int
00164 FTList<T>::deleteObj(int & iterator){
00165 delete *(_obj+iterator);
00166 *(_obj+(iterator--)) = *(_obj+(--_length));
00167 _remain++;
00168 return _length;
00169 }
00170
00171 template <class T>
00172 inline
00173 void
00174 FTList<T>::removeLast(void){
00175 _length--;
00176 _remain++;
00177 }
00178
00179 template <class T>
00180 inline
00181 void
00182 FTList<T>::clear(void){
00183 _remain += _length;
00184 _length = 0;
00185 }
00186
00187 template <class T>
00188 inline
00189 void
00190 FTList<T>::removeAll(void){
00191 free(_obj);
00192 _remain = 0;
00193 _length = 0;
00194 _obj = NULL;
00195 }
00196
00197
00198 template <class T>
00199 inline
00200 void
00201 FTList<T>::resize(void){
00202 _obj = (T *) realloc(_obj,_length * sizeof(T));
00203 _remain = 0;
00204 _length_to_alloc = _length;
00205 }
00206
00207 template <class T>
00208 inline
00209 T &
00210 FTList<T>::operator[] (unsigned i) const{
00211 return *(_obj+i);
00212 }
00213
00214 template <class T>
00215 inline
00216 T &
00217 FTList<T>::operator() (unsigned i) const{
00218 return *(_obj+i);
00219 }
00220
00221 template <class T>
00222 inline
00223 T &
00224 FTList<T>::first(void) const{
00225 return *_obj;
00226 }
00227
00228 template <class T>
00229 inline
00230 T*
00231 FTList<T>::firstPtr(void) const{
00232 return _obj;
00233 }
00234
00235 template <class T>
00236 inline
00237 T*
00238 FTList<T>::lastPtr(void) const{
00239 return _obj + (_length - 1);
00240 }
00241
00242 template <class T>
00243 inline
00244 int
00245 FTList<T>::length(void) const{
00246 return _length;
00247 }
00248
00249 template <class T>
00250 int FTList<T>::append(const FTList<T> & src)
00251 {
00252 int srcLength = src._length;
00253 T * srcObj = src._obj;
00254 int i = 0;
00255 if (_remain < srcLength){
00256 _obj = (T *) realloc(_obj,(_length + _remain + srcLength) * sizeof(T));
00257 while(i^srcLength) *(_obj+(_length++)) = *(srcObj+(i++));
00258 }else{
00259 while(i^srcLength) *(_obj+(_length++)) = *(srcObj+(i++));
00260 _remain -= srcLength;
00261 }
00262 return _length;
00263 }
00264
00265 template <class T>
00266 void FTList<T>::deleteAll(void)
00267 {
00268 int i = _length;
00269 while (i) delete *(_obj + (--i));
00270 clear();
00271 }
00272
00273 #endif
00274
00275 #undef inline
00276
00277 #endif
00278