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