BOSS 7.0.5
BESIII Offline Software System
Loading...
Searching...
No Matches
Reconstruction/KalFitAlg/KalFitAlg-00-07-55-p03/KalFitAlg/KalFitList.h
Go to the documentation of this file.
1//
2// Directly from fzisan, useful class created by Kakuno-san
3//
4#ifndef KalFitList_FLAG_
5#define KalFitList_FLAG_
6
7//#include <alloc.h>
8#include <stdlib.h>
9
10template <class T>
11class KalFitList{
12public:
13
14 /// default constructor
15 KalFitList(int length_to_alloc = 100);
16
17 /// copy constructor
19
20 /// destructor
22
23 /// append an object into the end of the list
24 int append(T x);
25
26 /// append objects into the end of the list
27 int append(const KalFitList<T> &);
28
29 /// remove objects by index and returns decremented index and length
30 int remove(int &);
31
32 /// remove objects by index
33 void remove2(int);
34
35 /// replace index-th object by the object src
36 void replace(int i, T src);
37
38 /// delete objects by index and returns decremented index and length
39 int deleteObj(int &);
40
41 /// remove just last objects of the list
42 void removeLast(void);
43
44 /// clear lists but the allocated memory remains same
45 void clear(void);
46
47 /// clear lists and free memory
48 void removeAll(void);
49
50 /// delete all object and clear(allocated memory remains same)
51 void deleteAll(void);
52
53 /// re-allocate memory to reduce size
54 void resize(void);
55
56 /// returns a object by index
57 T operator[] (unsigned i) const;
58
59 /// returns the reference of a object by index
60 T & operator() (unsigned i) const;
61
62 /// returns the first object in the list
63 T first(void) const;
64
65 /// returns the pointer of first object
66 T* firstPtr(void) const;
67
68 /// returns the pointer of last object
69 T* lastPtr(void) const;
70
71 /// returns the length of the list
72 int length(void) const;
73
74private: // private data members
75 int _length;
76 int _remain;
77 int _length_to_alloc;
78 T * _obj;
79};
80
81//----------------------------------------------
82#ifdef KalFitList_NO_INLINE
83#define inline
84#else
85#undef inline
86#define KalFitList_INLINE_DEFINE_HERE
87#endif
88
89#ifdef KalFitList_INLINE_DEFINE_HERE
90
91template <class T>
92inline
93KalFitList<T>::KalFitList(int length_to_alloc)
94 : _length(0),
95 _remain(length_to_alloc),
96 _length_to_alloc(length_to_alloc),
97 _obj((T *) malloc(length_to_alloc * sizeof(T))){
98}
99
100template <class T>
102 : _length(src._length),
103 _remain(src._remain),
104 _length_to_alloc(src._length_to_alloc)
105{
106 _obj = (T *) malloc((_length + _remain) * sizeof(T));
107 T * srcObj = src._obj;
108 for (int i = 0; i < _length; i++){
109 *(_obj+i) = *(srcObj+i);
110 }
111}
112
113template <class T>
114inline
116 free(_obj);
117}
118
119template <class T>
120inline
121int
123 if (!_remain){
124 _obj = (T *) realloc(_obj,(_length+_length_to_alloc) * sizeof(T));
125 _remain = _length_to_alloc;
126 }
127 *(_obj+(_length++)) = src;
128 _remain--;
129 return _length;
130}
131
132template <class T>
133inline
134int
135KalFitList<T>::remove(int & iterator){
136 *(_obj+(iterator--)) = *(_obj+(--_length));
137 _remain++;
138 return _length;
139}
140
141template <class T>
142inline
143void
144KalFitList<T>::remove2(int iterator){
145 *(_obj+iterator) = *(_obj+(--_length));
146 _remain++;
147}
148
149template <class T>
150inline
151void
152KalFitList<T>::replace(int i, T src){
153 *(_obj+i) = src;
154}
155
156template <class T>
157inline
158int
159KalFitList<T>::deleteObj(int & iterator){
160 delete *(_obj+iterator);
161 *(_obj+(iterator--)) = *(_obj+(--_length));
162 _remain++;
163 return _length;
164}
165
166template <class T>
167inline
168void
170 _length--;
171 _remain++;
172}
173
174template <class T>
175inline
176void
178 _remain += _length;
179 _length = 0;
180}
181
182template <class T>
183inline
184void
186 free(_obj);
187 _remain = 0;
188 _length = 0;
189 _obj = NULL;
190}
191
192
193template <class T>
194inline
195void
197 _obj = (T *) realloc(_obj,_length * sizeof(T));
198 _remain = 0;
199 _length_to_alloc = _length;
200}
201
202template <class T>
203inline
204T
205KalFitList<T>::operator[] (unsigned i) const{
206 return *(_obj+i);
207}
208
209template <class T>
210inline
211T &
212KalFitList<T>::operator() (unsigned i) const{
213 return *(_obj+i);
214}
215
216template <class T>
217inline
218T
219KalFitList<T>::first(void) const{
220 return *_obj;
221}
222
223template <class T>
224inline
225T*
226KalFitList<T>::firstPtr(void) const{
227 return _obj;
228}
229
230template <class T>
231inline
232T*
233KalFitList<T>::lastPtr(void) const{
234 return _obj + (_length - 1);
235}
236
237template <class T>
238inline
239int
240KalFitList<T>::length(void) const{
241 return _length;
242}
243
244template <class T>
246{
247 int srcLength = src._length;
248 T * srcObj = src._obj;
249 int i = 0;
250 if (_remain < srcLength){
251 _obj = (T *) realloc(_obj,(_length_to_alloc + srcLength) * sizeof(T));
252 while(i^srcLength) *(_obj+(_length++)) = *(srcObj+(i++));
253 }else{
254 while(i^srcLength) *(_obj+(_length++)) = *(srcObj+(i++));
255 _remain -= srcLength;
256 }
257 return _length;
258}
259
260template <class T>
262{
263 int i = _length;
264 while (i) delete *(_obj + (--i));
265 clear();
266}
267
268#endif
269
270#undef inline
271
272#endif /* KalFitList_FLAG_ */
KalFitList(const KalFitList< T > &)
copy constructor
int append(T x)
append an object into the end of the list
T * lastPtr(void) const
returns the pointer of last object
int length(void) const
returns the length of the list
int remove(int &)
remove objects by index and returns decremented index and length
void remove2(int)
remove objects by index
int append(const KalFitList< T > &)
append objects into the end of the list
T * firstPtr(void) const
returns the pointer of first object
T & operator()(unsigned i) const
returns the reference of a object by index
void clear(void)
clear lists but the allocated memory remains same
void replace(int i, T src)
replace index-th object by the object src
T first(void) const
returns the first object in the list
void deleteAll(void)
delete all object and clear(allocated memory remains same)
void resize(void)
re-allocate memory to reduce size
KalFitList(int length_to_alloc=100)
default constructor
~KalFitList()
destructor
void removeAll(void)
clear lists and free memory
T operator[](unsigned i) const
returns a object by index
int deleteObj(int &)
delete objects by index and returns decremented index and length
void removeLast(void)
remove just last objects of the list