BOSS 7.0.2
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 <stdlib.h> // For 'exit'
73#include <iostream>
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
88
89std::ostream& operator << ( std::ostream& os, const CDFootPrint& obj ) {
90 os << "0x" << std::hex;
91 for ( int i = obj.m_size-1; i >= 0; i-- ) {
92 os << obj.m_array[i];
93 }
94 os << std::dec;
95
96 return os;
97}
98// friend classses and functions
99
100//
101// constructors and destructor
102//
103
104//------ default constructor -----
105//
107 m_size(0),
108 m_array(0)
109{
110 ++m_numberFootprints;
111}
112
113//------ copy constructor -----
114//
116 m_size(0) ,
117 m_array(0)
118{
119 //
120 // book the memory and copy contents of aOtherPrint
121 //
122 resize( aOtherPrint.m_size );
123 for ( uint32_t index = 0 ;
124 index != m_size ;
125 ++index ) {
126 m_array[ index ] = aOtherPrint.m_array[ index ] ;
127 }
128 ++m_numberFootprints;
129}
130
131//------ Destructor -----
132//
134{
135 //
136 // delete memory
137 //
138 delete [] m_array ;
139 if( 0 == --m_numberFootprints ) {
140 reset();
141 }
142}
143
144//
145// assignment operators
146//
147
149{
150 if ( this == &aOtherPrint ) {
151 //
152 // if Footprint is assigned to itself do nothing.
153 //
154 return( *this ) ;
155 }
156 //
157 // book the memory and copy contents of aOtherPrint
158 //
159 resize( aOtherPrint.m_size ) ;
160 for ( uint32_t index = 0 ;
161 index != m_size ;
162 ++index ) {
163 m_array[ index ] = aOtherPrint.m_array[ index ] ;
164 }
165 return ( *this ) ;
166}
167
169{
170 if ( this == &aOtherPrint ) {
171 //
172 // if Footprint is added and assigned to itself do nothing.
173 //
174 return(*this);
175 }
176 if ( m_size >= aOtherPrint.m_size ) {
177 //
178 // if this Footprint is larger or equal than to aOtherPrint,
179 // only `or' with the contents of aOtherPoint
180 //
181 for ( uint32_t index = 0 ;
182 index != aOtherPrint.m_size ;
183 ++index) {
184 m_array[ index ] |= aOtherPrint.m_array[ index ] ;
185 }
186 }
187 else {
188 //
189 // if this Footprint is smaller than to aOtherPrint book new memory
190 // Note: can not use resize, as this thorws away old memory
191 //
192 uint32_t* tmp_ptr = new uint32_t[ aOtherPrint.m_size ];
193 if ( 0 == tmp_ptr ) {
194 std::cerr << "No memory to allocate another kinematicData" << std::endl ;
195 exit( 1 ) ;
196 }
197 //
198 // for the length of the old memory, fill the new memory with
199 // `or' of old and aOtherPrint
200 //
201 for ( uint32_t index_1 = 0 ;
202 index_1 != m_size ;
203 ++index_1 ) {
204 tmp_ptr[ index_1 ] = m_array[ index_1 ] | aOtherPrint.m_array[ index_1 ] ;
205 }
206 //
207 // for the rest of length of the new memory, fill with aOtherPrint
208 //
209 for ( uint32_t index_2 = m_size ;
210 index_2 != aOtherPrint.m_size ;
211 ++index_2) {
212 tmp_ptr[ index_2 ] = aOtherPrint.m_array[ index_2 ] ;
213 }
214 //
215 // delete old memory
216 //
217 delete [] m_array ;
218 //
219 // update member data elements
220 //
221 m_size = aOtherPrint.m_size;
222 m_array = tmp_ptr ;
223 }
224 return ( *this ) ;
225}
226
227//
228// member functions
229//
230
231//------ fresh -----
232// assign a fresh footprint to this object
233//
235{
236 if ( m_size != 0 ) {
237 //
238 // if already assigned a value do nothing
239 //
240 return ( *this ) ;
241 }
242
243 const uint32_t kBitsInByte = 8 ;
244 //
245 // Take a number and increase the number of CDFootPrints issued
246 //
247 uint32_t freshNumber = m_numberIssued++ ;
248 //
249 // calculate which bit, and which element to set
250 //
251 uint32_t element = freshNumber / ( kBitsInByte * sizeof( uint32_t ) ) ;
252 uint32_t offsetInElement = freshNumber % ( kBitsInByte * sizeof( uint32_t ) ) ;
253 //
254 // book enough memory
255 //
256 resize( element + 1 ) ;
257 //
258 // fill all but the last part of the memory with zeros
259 //
260 for ( uint32_t index = 0 ;
261 index < element ;
262 ++index ) {
263 m_array[ index ] = uint32_t( 0 );
264 }
265 //
266 // fill in last part of memory with correct bit set
267 //
268 m_array[ element ] = uint32_t(1) << offsetInElement ;
269 return ( *this ) ;
270}
271
272//------ resize -----
273// resize the arry for the footprint
274// Note: This routine does NOT copy the current contents into the
275// new memory
276//
277void CDFootPrint::resize( const uint32_t aNewSize )
278{
279 if ( aNewSize == m_size ) {
280 //
281 // if Footprint is already the right size do nothing.
282 //
283 return ;
284 }
285 //
286 // allocate a new section of memory, and delete old section
287 //
288 uint32_t* tmp_ptr = new uint32_t[ aNewSize ] ;
289 if ( 0 == tmp_ptr ) {
290 std::cerr << "No memory to allocate another kinematicData" << std::endl ;
291 exit( 1 ) ;
292 }
293 delete [] m_array ;
294 //
295 // update member data elements
296 //
297 m_size = aNewSize ;
298 m_array = tmp_ptr ;
299 //
300 return ;
301}
302
303//
304// const member functions
305//
306
307//------ equality -----
308// test to see if two footprints are identical
309//
310bool CDFootPrint::operator==(const CDFootPrint& aOtherPrint ) const
311{
312 if ( this == &aOtherPrint ) {
313 //
314 // if being compare with itself return `true'
315 //
316 return( !false ) ;
317 }
318 //
319 // find the shortest length to compare the two memory sections
320 //
321 uint32_t shorterSize ;
322 if ( m_size > aOtherPrint.m_size ) {
323 shorterSize = aOtherPrint.m_size ;
324 }
325 else {
326 shorterSize = m_size ;
327 }
328 //
329 // check shorter CDFootPrint matches the section longer CDFootPrint
330 //
331 uint32_t index = 0;
332 while ( ( index != shorterSize ) &&
333 ( m_array[ index ] == aOtherPrint.m_array[ index ] ) ) {
334 ++index ;
335 }
336 //
337 // if check finished before the shorter CDFootPrint was covered,
338 // then two CDFootPrints are unequal
339 //
340 if ( index != shorterSize ) {
341 return ( false ) ;
342 }
343 //
344 // check that the rest of the longer footprint is zero
345 //
346 if ( m_size >= aOtherPrint.m_size ) {
347 while ( ( index != m_size ) &&
348 ( m_array[ index ] == uint32_t( 0 ) ) ) {
349 ++index ;
350 }
351 return ( m_size == index );
352 }
353 else {
354 while( ( index != aOtherPrint.m_size ) &&
355 (aOtherPrint.m_array[ index ] == uint32_t( 0 ) ) ) {
356 ++index ;
357 }
358 return ( aOtherPrint.m_size == index );
359 }
360}
361
362//------ non-equality -----
363// test to see if two footprints are not idential
364//
365bool CDFootPrint::operator!=( const CDFootPrint& aOtherPrint ) const
366{
367 return ( ! operator == ( aOtherPrint ) ) ;
368}
369
370//------ addition -----
371// combine two CDFootPrints to create a third
372//
374{
375 CDFootPrint result( *this ) ;
376 result += aOtherPrint ;
377 return ( result ) ;
378}
379
380
381//------ overlap -----
382// true if this CDFootPrint overlaps with the other one
383//
384bool CDFootPrint::overlap( const CDFootPrint& aOtherPrint) const
385{
386 if (this == & aOtherPrint) {
387 //
388 // if being tested with itself return `true'
389 //
390 return( !false );
391 }
392 //
393 // find the shortest length to compare the two memoery sections
394 //
395 uint32_t shorterSize ;
396 if ( m_size > aOtherPrint.m_size ) {
397 shorterSize = aOtherPrint.m_size ;
398 }
399 else {
400 shorterSize = m_size ;
401 }
402 //
403 // check shorter CDFootPrint matches the section longer CDFootPrint
404 //
405 uint32_t index = 0;
406 while ( ( index != shorterSize ) &&
407 ( 0 == ( m_array[ index ] & aOtherPrint.m_array[ index ] ) ) ) {
408 ++index ;
409 }
410 //
411 // if check finished before the shorter CDFootPrint was covered,
412 // then two CDFootPrints have at least one bit in common
413 //
414 return ( index != shorterSize ) ;
415}
416
417//------ contains -----
418// true if this CDFootPrint contains the other one
419//
420bool CDFootPrint::contains( const CDFootPrint& aOtherPrint) const
421{
422 if (this == & aOtherPrint) {
423 //
424 // if being tested with itself return `true'
425 //
426 return(! false);
427 }
428 //
429 // find the shortest length to compare the two memoery sections
430 //
431 uint32_t shorterSize ;
432 if ( m_size > aOtherPrint.m_size ) {
433 shorterSize = aOtherPrint.m_size ;
434 }
435 else {
436 shorterSize = m_size ;
437 }
438
439 //
440 // for shorter CDFootPrint check aOtherPrint is within this one
441 //
442 uint32_t index = 0 ;
443 while ( ( index != shorterSize ) &&
444 ( m_array[ index ] ==( m_array[index] | aOtherPrint.m_array[index] ) ) ) {
445 ++index ;
446 }
447 //
448 // if check finished before the shorter CDFootPrint was covered,
449 // then aOtherPrint has at least one bit outside this CDFootPrint
450 //
451 if ( index != shorterSize ) {
452 return ( false ) ;
453 }
454 //
455 // if this CDFootPrint is the longer then aOtherPrint is totally contained
456 //
457 if ( m_size > aOtherPrint.m_size ) {
458 return ( !false ) ;
459 }
460 //
461 // as aOtherPrint is longer need to check rest of it is zero
462 //
463 while( ( index != aOtherPrint.m_size ) &&
464 (aOtherPrint.m_array[ index ] == uint32_t( 0 ) ) ) {
465 ++index ;
466 }
467 //
468 // if check finished before aOtherPrint was finished
469 // then aOtherPrint has at least one bit in the rest of it
470 //
471 return ( aOtherPrint.m_size == index ) ;
472}
473
474//
475// static member functions
476//
477
478//------ reset (a Static Function) -----
479// set number issued to zero
480//
482{
483 m_numberIssued = 0 ;
484}
485
std::ostream & operator<<(std::ostream &os, const CDFootPrint &obj)
Definition: CDFootPrint.cxx:89
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()