/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Reconstruction/KalFitAlg/KalFitAlg-00-07-55-p03/KalFitAlg/KalFitList.h

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

Generated on Tue Nov 29 23:13:22 2016 for BOSS_7.0.2 by  doxygen 1.4.7