BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Reconstruction/KalFitAlg/include/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> class KalFitList {
11public:
12 /// default constructor
13 KalFitList( int length_to_alloc = 100 );
14
15 /// copy constructor
17
18 /// destructor
20
21 /// append an object into the end of the list
22 int append( T x );
23
24 /// append objects into the end of the list
25 int append( const KalFitList<T>& );
26
27 /// remove objects by index and returns decremented index and length
28 int remove( int& );
29
30 /// remove objects by index
31 void remove2( int );
32
33 /// replace index-th object by the object src
34 void replace( int i, T src );
35
36 /// delete objects by index and returns decremented index and length
37 int deleteObj( int& );
38
39 /// remove just last objects of the list
40 void removeLast( void );
41
42 /// clear lists but the allocated memory remains same
43 void clear( void );
44
45 /// clear lists and free memory
46 void removeAll( void );
47
48 /// delete all object and clear(allocated memory remains same)
49 void deleteAll( void );
50
51 /// re-allocate memory to reduce size
52 void resize( void );
53
54 /// returns a object by index
55 T operator[]( unsigned i ) const;
56
57 /// returns the reference of a object by index
58 T& operator()( unsigned i ) const;
59
60 /// returns the first object in the list
61 T first( void ) const;
62
63 /// returns the pointer of first object
64 T* firstPtr( void ) const;
65
66 /// returns the pointer of last object
67 T* lastPtr( void ) const;
68
69 /// returns the length of the list
70 int length( void ) const;
71
72private: // private data members
73 int _length;
74 int _remain;
75 int _length_to_alloc;
76 T* _obj;
77};
78
79//----------------------------------------------
80#ifdef KalFitList_NO_INLINE
81# define inline
82#else
83# undef inline
84# define KalFitList_INLINE_DEFINE_HERE
85#endif
86
87#ifdef KalFitList_INLINE_DEFINE_HERE
88
89template <class T>
90inline KalFitList<T>::KalFitList( int length_to_alloc )
91 : _length( 0 )
92 , _remain( length_to_alloc )
93 , _length_to_alloc( length_to_alloc )
94 , _obj( (T*)malloc( length_to_alloc * sizeof( T ) ) ) {}
95
96template <class T>
98 : _length( src._length )
99 , _remain( src._remain )
100 , _length_to_alloc( src._length_to_alloc ) {
101 _obj = (T*)malloc( ( _length + _remain ) * sizeof( T ) );
102 T* srcObj = src._obj;
103 for ( int i = 0; i < _length; i++ ) { *( _obj + i ) = *( srcObj + i ); }
104}
105
106template <class T> inline KalFitList<T>::~KalFitList() { free( _obj ); }
107
108template <class T> inline int KalFitList<T>::append( T src ) {
109 if ( !_remain )
110 {
111 _obj = (T*)realloc( _obj, ( _length + _length_to_alloc ) * sizeof( T ) );
112 _remain = _length_to_alloc;
113 }
114 *( _obj + ( _length++ ) ) = src;
115 _remain--;
116 return _length;
117}
118
119template <class T> inline int KalFitList<T>::remove( int& iterator ) {
120 *( _obj + ( iterator-- ) ) = *( _obj + ( --_length ) );
121 _remain++;
122 return _length;
123}
124
125template <class T> inline void KalFitList<T>::remove2( int iterator ) {
126 *( _obj + iterator ) = *( _obj + ( --_length ) );
127 _remain++;
128}
129
130template <class T> inline void KalFitList<T>::replace( int i, T src ) { *( _obj + i ) = src; }
131
132template <class T> inline int KalFitList<T>::deleteObj( int& iterator ) {
133 delete *( _obj + iterator );
134 *( _obj + ( iterator-- ) ) = *( _obj + ( --_length ) );
135 _remain++;
136 return _length;
137}
138
139template <class T> inline void KalFitList<T>::removeLast( void ) {
140 _length--;
141 _remain++;
142}
143
144template <class T> inline void KalFitList<T>::clear( void ) {
145 _remain += _length;
146 _length = 0;
147}
148
149template <class T> inline void KalFitList<T>::removeAll( void ) {
150 free( _obj );
151 _remain = 0;
152 _length = 0;
153 _obj = NULL;
154}
155
156template <class T> inline void KalFitList<T>::resize( void ) {
157 _obj = (T*)realloc( _obj, _length * sizeof( T ) );
158 _remain = 0;
159 _length_to_alloc = _length;
160}
161
162template <class T> inline T KalFitList<T>::operator[]( unsigned i ) const {
163 return *( _obj + i );
164}
165
166template <class T> inline T& KalFitList<T>::operator()( unsigned i ) const {
167 return *( _obj + i );
168}
169
170template <class T> inline T KalFitList<T>::first( void ) const { return *_obj; }
171
172template <class T> inline T* KalFitList<T>::firstPtr( void ) const { return _obj; }
173
174template <class T> inline T* KalFitList<T>::lastPtr( void ) const {
175 return _obj + ( _length - 1 );
176}
177
178template <class T> inline int KalFitList<T>::length( void ) const { return _length; }
179
180template <class T> int KalFitList<T>::append( const KalFitList<T>& src ) {
181 int srcLength = src._length;
182 T* srcObj = src._obj;
183 int i = 0;
184 if ( _remain < srcLength )
185 {
186 _obj = (T*)realloc( _obj, ( _length_to_alloc + srcLength ) * sizeof( T ) );
187 while ( i ^ srcLength ) *( _obj + ( _length++ ) ) = *( srcObj + ( i++ ) );
188 }
189 else
190 {
191 while ( i ^ srcLength ) *( _obj + ( _length++ ) ) = *( srcObj + ( i++ ) );
192 _remain -= srcLength;
193 }
194 return _length;
195}
196
197template <class T> void KalFitList<T>::deleteAll( void ) {
198 int i = _length;
199 while ( i ) delete *( _obj + ( --i ) );
200 clear();
201}
202
203#endif
204
205#undef inline
206
207#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