BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
CDFootPrint.cxx
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// Package: DChain
4// Module: CDFootPrint
5//
6// Description: keep record of which building-block a CDCandidate uses.
7//
8// Implimentation:
9// <Notes on implimentation>
10//
11// Author: Simon Patton
12// Created: Wed Oct 30 13:29:34 EST 1996
13// $Id: CDFootPrint.cxx,v 1.3 2009/10/14 07:18:50 hujf Exp $
14//
15// Revision history
16//
17// $Log: CDFootPrint.cxx,v $
18// Revision 1.3 2009/10/14 07:18:50 hujf
19// see ChangeLog
20//
21// Revision 1.2 2009/09/22 08:24:41 hujf
22// see ChangeLog
23//
24// Revision 1.1.1.1 2009/03/03 06:05:56 maqm
25// first import of BesDChain
26//
27// Revision 1.1 2001/04/11 13:19:01 urner
28// transition to files with CD prefix. Addition of new files
29//
30// Revision 1.2 2001/03/30 20:13:08 cdj
31// CDFootPrint now resets its m_numberIssued when the last CDFootPrint is deleted
32//
33// Revision 1.1.1.1 2000/12/18 22:17:25 cdj
34// imported CleoDChain
35//
36// Revision 1.13 1998/04/17 18:55:50 sjp
37// Modified to use latest types
38//
39// Revision 1.12 1997/09/03 14:58:34 sjp
40// Use new report.h and KTKinematicData
41//
42// Revision 1.11 1997/08/29 17:00:36 sjp
43// Modified to handle new Cairn Templated classes
44//
45// Revision 1.10 1997/08/19 23:02:48 sjp
46// Restructured package to be independent of CleoDChain
47//
48// Revision 1.9 1997/08/19 20:40:23 sjp
49// Updated to use <package>/<file>.h include structure.
50// (Note: This version of the code has not been compiled)
51//
52// Revision 1.8 1997/01/21 20:36:02 sjp
53// Changed CPP flags and include because of library reorganization
54//
55// Revision 1.7 1996/12/20 21:26:55 sjp
56// major overhaul, and fixed memoery bug
57//
58// Revision 1.6 1996/11/04 16:48:35 sjp
59// Variable length storage is implemented
60//
61// Revision 1.3 1996/04/06 02:49:28 sjp
62// Added equality check and `contains' function
63//
64// Revision 1.2 1996/02/20 00:36:00 sjp
65// Changed 'fresh' to return CDFootPrint, made other return values 'const'
66//
67// Revision 1.1 1995/11/09 20:01:57 sjp
68// New class to keep track of object that have already been `used'.
69//
70
71// system include files
72#include <iostream>
73#include <stdlib.h> // For 'exit'
74
75// user include files
76#include "BesDChain/CDFootPrint.h"
77
78//
79// constants, enums and typedefs
80//
81
82//
83// static data member definitions
84//
85uint32_t CDFootPrint::m_numberIssued = 0;
86uint32_t CDFootPrint::m_numberFootprints = 0;
87
88std::ostream& operator<<( std::ostream& os, const CDFootPrint& obj ) {
89 os << "0x" << std::hex;
90 for ( int i = obj.m_size - 1; i >= 0; i-- ) { os << obj.m_array[i]; }
91 os << std::dec;
92
93 return os;
94}
95// friend classses and functions
96
97//
98// constructors and destructor
99//
100
101//------ default constructor -----
102//
103CDFootPrint::CDFootPrint() : m_size( 0 ), m_array( 0 ) { ++m_numberFootprints; }
104
105//------ copy constructor -----
106//
107CDFootPrint::CDFootPrint( const CDFootPrint& aOtherPrint ) : m_size( 0 ), m_array( 0 ) {
108 //
109 // book the memory and copy contents of aOtherPrint
110 //
111 resize( aOtherPrint.m_size );
112 for ( uint32_t index = 0; index != m_size; ++index )
113 { m_array[index] = aOtherPrint.m_array[index]; }
114 ++m_numberFootprints;
115}
116
117//------ Destructor -----
118//
120 //
121 // delete memory
122 //
123 delete[] m_array;
124 if ( 0 == --m_numberFootprints ) { reset(); }
125}
126
127//
128// assignment operators
129//
130
131const CDFootPrint& CDFootPrint::operator=( const CDFootPrint& aOtherPrint ) {
132 if ( this == &aOtherPrint )
133 {
134 //
135 // if Footprint is assigned to itself do nothing.
136 //
137 return ( *this );
138 }
139 //
140 // book the memory and copy contents of aOtherPrint
141 //
142 resize( aOtherPrint.m_size );
143 for ( uint32_t index = 0; index != m_size; ++index )
144 { m_array[index] = aOtherPrint.m_array[index]; }
145 return ( *this );
146}
147
148const CDFootPrint& CDFootPrint::operator+=( const CDFootPrint& aOtherPrint ) {
149 if ( this == &aOtherPrint )
150 {
151 //
152 // if Footprint is added and assigned to itself do nothing.
153 //
154 return ( *this );
155 }
156 if ( m_size >= aOtherPrint.m_size )
157 {
158 //
159 // if this Footprint is larger or equal than to aOtherPrint,
160 // only `or' with the contents of aOtherPoint
161 //
162 for ( uint32_t index = 0; index != aOtherPrint.m_size; ++index )
163 { m_array[index] |= aOtherPrint.m_array[index]; }
164 }
165 else
166 {
167 //
168 // if this Footprint is smaller than to aOtherPrint book new memory
169 // Note: can not use resize, as this thorws away old memory
170 //
171 uint32_t* tmp_ptr = new uint32_t[aOtherPrint.m_size];
172 if ( 0 == tmp_ptr )
173 {
174 std::cerr << "No memory to allocate another kinematicData" << std::endl;
175 exit( 1 );
176 }
177 //
178 // for the length of the old memory, fill the new memory with
179 // `or' of old and aOtherPrint
180 //
181 for ( uint32_t index_1 = 0; index_1 != m_size; ++index_1 )
182 { tmp_ptr[index_1] = m_array[index_1] | aOtherPrint.m_array[index_1]; }
183 //
184 // for the rest of length of the new memory, fill with aOtherPrint
185 //
186 for ( uint32_t index_2 = m_size; index_2 != aOtherPrint.m_size; ++index_2 )
187 { tmp_ptr[index_2] = aOtherPrint.m_array[index_2]; }
188 //
189 // delete old memory
190 //
191 delete[] m_array;
192 //
193 // update member data elements
194 //
195 m_size = aOtherPrint.m_size;
196 m_array = tmp_ptr;
197 }
198 return ( *this );
199}
200
201//
202// member functions
203//
204
205//------ fresh -----
206// assign a fresh footprint to this object
207//
209 if ( m_size != 0 )
210 {
211 //
212 // if already assigned a value do nothing
213 //
214 return ( *this );
215 }
216
217 const uint32_t kBitsInByte = 8;
218 //
219 // Take a number and increase the number of CDFootPrints issued
220 //
221 uint32_t freshNumber = m_numberIssued++;
222 //
223 // calculate which bit, and which element to set
224 //
225 uint32_t element = freshNumber / ( kBitsInByte * sizeof( uint32_t ) );
226 uint32_t offsetInElement = freshNumber % ( kBitsInByte * sizeof( uint32_t ) );
227 //
228 // book enough memory
229 //
230 resize( element + 1 );
231 //
232 // fill all but the last part of the memory with zeros
233 //
234 for ( uint32_t index = 0; index < element; ++index ) { m_array[index] = uint32_t( 0 ); }
235 //
236 // fill in last part of memory with correct bit set
237 //
238 m_array[element] = uint32_t( 1 ) << offsetInElement;
239 return ( *this );
240}
241
242//------ resize -----
243// resize the arry for the footprint
244// Note: This routine does NOT copy the current contents into the
245// new memory
246//
247void CDFootPrint::resize( const uint32_t aNewSize ) {
248 if ( aNewSize == m_size )
249 {
250 //
251 // if Footprint is already the right size do nothing.
252 //
253 return;
254 }
255 //
256 // allocate a new section of memory, and delete old section
257 //
258 uint32_t* tmp_ptr = new uint32_t[aNewSize];
259 if ( 0 == tmp_ptr )
260 {
261 std::cerr << "No memory to allocate another kinematicData" << std::endl;
262 exit( 1 );
263 }
264 delete[] m_array;
265 //
266 // update member data elements
267 //
268 m_size = aNewSize;
269 m_array = tmp_ptr;
270 //
271 return;
272}
273
274//
275// const member functions
276//
277
278//------ equality -----
279// test to see if two footprints are identical
280//
281bool CDFootPrint::operator==( const CDFootPrint& aOtherPrint ) const {
282 if ( this == &aOtherPrint )
283 {
284 //
285 // if being compare with itself return `true'
286 //
287 return ( !false );
288 }
289 //
290 // find the shortest length to compare the two memory sections
291 //
292 uint32_t shorterSize;
293 if ( m_size > aOtherPrint.m_size ) { shorterSize = aOtherPrint.m_size; }
294 else { shorterSize = m_size; }
295 //
296 // check shorter CDFootPrint matches the section longer CDFootPrint
297 //
298 uint32_t index = 0;
299 while ( ( index != shorterSize ) && ( m_array[index] == aOtherPrint.m_array[index] ) )
300 { ++index; }
301 //
302 // if check finished before the shorter CDFootPrint was covered,
303 // then two CDFootPrints are unequal
304 //
305 if ( index != shorterSize ) { return ( false ); }
306 //
307 // check that the rest of the longer footprint is zero
308 //
309 if ( m_size >= aOtherPrint.m_size )
310 {
311 while ( ( index != m_size ) && ( m_array[index] == uint32_t( 0 ) ) ) { ++index; }
312 return ( m_size == index );
313 }
314 else
315 {
316 while ( ( index != aOtherPrint.m_size ) &&
317 ( aOtherPrint.m_array[index] == uint32_t( 0 ) ) )
318 { ++index; }
319 return ( aOtherPrint.m_size == index );
320 }
321}
322
323//------ non-equality -----
324// test to see if two footprints are not idential
325//
326bool CDFootPrint::operator!=( const CDFootPrint& aOtherPrint ) const {
327 return ( !operator==( aOtherPrint ) );
328}
329
330//------ addition -----
331// combine two CDFootPrints to create a third
332//
333CDFootPrint CDFootPrint::operator+( const CDFootPrint& aOtherPrint ) const {
334 CDFootPrint result( *this );
335 result += aOtherPrint;
336 return ( result );
337}
338
339//------ overlap -----
340// true if this CDFootPrint overlaps with the other one
341//
342bool CDFootPrint::overlap( const CDFootPrint& aOtherPrint ) const {
343 if ( this == &aOtherPrint )
344 {
345 //
346 // if being tested with itself return `true'
347 //
348 return ( !false );
349 }
350 //
351 // find the shortest length to compare the two memoery sections
352 //
353 uint32_t shorterSize;
354 if ( m_size > aOtherPrint.m_size ) { shorterSize = aOtherPrint.m_size; }
355 else { shorterSize = m_size; }
356 //
357 // check shorter CDFootPrint matches the section longer CDFootPrint
358 //
359 uint32_t index = 0;
360 while ( ( index != shorterSize ) &&
361 ( 0 == ( m_array[index] & aOtherPrint.m_array[index] ) ) )
362 { ++index; }
363 //
364 // if check finished before the shorter CDFootPrint was covered,
365 // then two CDFootPrints have at least one bit in common
366 //
367 return ( index != shorterSize );
368}
369
370//------ contains -----
371// true if this CDFootPrint contains the other one
372//
373bool CDFootPrint::contains( const CDFootPrint& aOtherPrint ) const {
374 if ( this == &aOtherPrint )
375 {
376 //
377 // if being tested with itself return `true'
378 //
379 return ( !false );
380 }
381 //
382 // find the shortest length to compare the two memoery sections
383 //
384 uint32_t shorterSize;
385 if ( m_size > aOtherPrint.m_size ) { shorterSize = aOtherPrint.m_size; }
386 else { shorterSize = m_size; }
387
388 //
389 // for shorter CDFootPrint check aOtherPrint is within this one
390 //
391 uint32_t index = 0;
392 while ( ( index != shorterSize ) &&
393 ( m_array[index] == ( m_array[index] | aOtherPrint.m_array[index] ) ) )
394 { ++index; }
395 //
396 // if check finished before the shorter CDFootPrint was covered,
397 // then aOtherPrint has at least one bit outside this CDFootPrint
398 //
399 if ( index != shorterSize ) { return ( false ); }
400 //
401 // if this CDFootPrint is the longer then aOtherPrint is totally contained
402 //
403 if ( m_size > aOtherPrint.m_size ) { return ( !false ); }
404 //
405 // as aOtherPrint is longer need to check rest of it is zero
406 //
407 while ( ( index != aOtherPrint.m_size ) && ( aOtherPrint.m_array[index] == uint32_t( 0 ) ) )
408 { ++index; }
409 //
410 // if check finished before aOtherPrint was finished
411 // then aOtherPrint has at least one bit in the rest of it
412 //
413 return ( aOtherPrint.m_size == index );
414}
415
416//
417// static member functions
418//
419
420//------ reset (a Static Function) -----
421// set number issued to zero
422//
423void CDFootPrint::reset() { m_numberIssued = 0; }
std::ostream & operator<<(std::ostream &os, const CDFootPrint &obj)
static void reset()
CDFootPrint operator+(const CDFootPrint &aOtherPrint) const
bool operator!=(const CDFootPrint &aOtherPrint) const
bool contains(const CDFootPrint &aOtherPrint) const
bool overlap(const CDFootPrint &aOtherPrint) const
const CDFootPrint & operator=(const CDFootPrint &)
const CDFootPrint & operator+=(const CDFootPrint &aOtherPrint)
bool operator==(const CDFootPrint &aOtherPrint) const
virtual ~CDFootPrint()
CDFootPrint & fresh()