/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Event/BesDChain/BesDChain-00-00-14/src/CDFootPrint.cxx

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     DChain
00004 // Module:      CDFootPrint
00005 // 
00006 // Description: keep record of which building-block a CDCandidate uses.
00007 //
00008 // Implimentation:
00009 //     <Notes on implimentation>
00010 //
00011 // Author:      Simon Patton
00012 // Created:     Wed Oct 30 13:29:34 EST 1996
00013 // $Id: CDFootPrint.cxx,v 1.3 2009/10/14 07:18:50 hujf Exp $
00014 //
00015 // Revision history
00016 //
00017 // $Log: CDFootPrint.cxx,v $
00018 // Revision 1.3  2009/10/14 07:18:50  hujf
00019 // see ChangeLog
00020 //
00021 // Revision 1.2  2009/09/22 08:24:41  hujf
00022 // see ChangeLog
00023 //
00024 // Revision 1.1.1.1  2009/03/03 06:05:56  maqm
00025 // first import of BesDChain
00026 //
00027 // Revision 1.1  2001/04/11 13:19:01  urner
00028 // transition to files with CD prefix. Addition of new files
00029 //
00030 // Revision 1.2  2001/03/30 20:13:08  cdj
00031 // CDFootPrint now resets its m_numberIssued when the last CDFootPrint is deleted
00032 //
00033 // Revision 1.1.1.1  2000/12/18 22:17:25  cdj
00034 // imported CleoDChain
00035 //
00036 // Revision 1.13  1998/04/17 18:55:50  sjp
00037 // Modified to use latest types
00038 //
00039 // Revision 1.12  1997/09/03 14:58:34  sjp
00040 // Use new report.h and KTKinematicData
00041 //
00042 // Revision 1.11  1997/08/29 17:00:36  sjp
00043 // Modified to handle new Cairn Templated classes
00044 //
00045 // Revision 1.10  1997/08/19 23:02:48  sjp
00046 // Restructured package to be independent of CleoDChain
00047 //
00048 // Revision 1.9  1997/08/19 20:40:23  sjp
00049 // Updated to use <package>/<file>.h include structure.
00050 //   (Note: This version of the code has not been compiled)
00051 //
00052 // Revision 1.8  1997/01/21 20:36:02  sjp
00053 // Changed CPP flags and include because of library reorganization
00054 //
00055 // Revision 1.7  1996/12/20 21:26:55  sjp
00056 // major overhaul, and fixed memoery bug
00057 //
00058 // Revision 1.6  1996/11/04 16:48:35  sjp
00059 // Variable length storage is implemented
00060 //
00061 // Revision 1.3  1996/04/06 02:49:28  sjp
00062 // Added equality check and `contains' function
00063 //
00064 // Revision 1.2  1996/02/20 00:36:00  sjp
00065 // Changed 'fresh' to return CDFootPrint, made other return values 'const'
00066 //
00067 // Revision 1.1  1995/11/09 20:01:57  sjp
00068 // New class to keep track of object that have already been `used'.
00069 //
00070 
00071 // system include files
00072 #include <stdlib.h>  // For 'exit'
00073 #include <iostream>
00074 
00075 // user include files
00076 #include "BesDChain/CDFootPrint.h"
00077 
00078 //
00079 // constants, enums and typedefs
00080 //
00081 
00082 //
00083 // static data member definitions
00084 //
00085 uint32_t CDFootPrint::m_numberIssued = 0 ;
00086 uint32_t CDFootPrint::m_numberFootprints = 0 ;
00087 
00088 
00089 std::ostream& operator << ( std::ostream& os, const CDFootPrint& obj ) {
00090    os << "0x" << std::hex;
00091    for ( int i = obj.m_size-1; i >= 0; i-- ) {
00092       os << obj.m_array[i];
00093    }
00094    os << std::dec;
00095 
00096    return os;
00097 }
00098 // friend classses and functions
00099 
00100 //
00101 // constructors and destructor
00102 //
00103 
00104 //------ default constructor -----
00105 //
00106 CDFootPrint::CDFootPrint() :
00107     m_size(0),
00108     m_array(0)
00109 {
00110     ++m_numberFootprints;
00111 }
00112 
00113 //------ copy constructor -----
00114 //
00115 CDFootPrint::CDFootPrint( const CDFootPrint& aOtherPrint ) :
00116     m_size(0) ,
00117     m_array(0)
00118 {
00119     //
00120     // book the memory and copy contents of aOtherPrint
00121     //
00122     resize( aOtherPrint.m_size );
00123     for ( uint32_t index = 0 ;
00124             index != m_size ;
00125             ++index ) {
00126         m_array[ index ] = aOtherPrint.m_array[ index ] ;
00127     }
00128     ++m_numberFootprints;
00129 }
00130 
00131 //------ Destructor -----
00132 //
00133 CDFootPrint::~CDFootPrint()
00134 {
00135     //
00136     // delete memory
00137     //
00138     delete []  m_array ;
00139     if( 0 == --m_numberFootprints ) {
00140         reset();
00141     }
00142 }
00143 
00144 //
00145 // assignment operators
00146 //
00147 
00148 const CDFootPrint& CDFootPrint::operator=( const CDFootPrint& aOtherPrint )
00149 {
00150     if ( this == &aOtherPrint ) {
00151         //
00152         // if Footprint is assigned to itself do nothing.
00153         //
00154         return( *this ) ;
00155     }
00156     //
00157     // book the memory and copy contents of aOtherPrint
00158     //
00159     resize( aOtherPrint.m_size ) ;
00160     for ( uint32_t index = 0 ;
00161             index != m_size ;
00162             ++index ) {
00163         m_array[ index ] = aOtherPrint.m_array[ index ] ;
00164     }
00165     return ( *this ) ;
00166 }
00167 
00168 const CDFootPrint& CDFootPrint::operator+=( const CDFootPrint& aOtherPrint )
00169 {
00170     if ( this == &aOtherPrint ) {
00171         //
00172         // if Footprint is added and assigned to itself do nothing.
00173         //
00174         return(*this);
00175     }   
00176     if ( m_size >= aOtherPrint.m_size ) {
00177         //
00178         // if this Footprint is larger or equal than to aOtherPrint,
00179         //   only `or' with the contents of aOtherPoint
00180         //
00181         for ( uint32_t index = 0 ;
00182                 index != aOtherPrint.m_size ;
00183                 ++index) {
00184             m_array[ index ] |= aOtherPrint.m_array[ index ] ;
00185         }
00186     }
00187     else {
00188         //
00189         // if this Footprint is smaller than to aOtherPrint book new memory
00190         // Note: can not use resize, as this thorws away old memory
00191         //
00192         uint32_t* tmp_ptr = new uint32_t[ aOtherPrint.m_size ];
00193         if ( 0 == tmp_ptr ) {
00194             std::cerr << "No memory to allocate another kinematicData" << std::endl ;
00195             exit( 1 ) ;
00196         }
00197         //
00198         // for the length of the old memory, fill the new memory with
00199         //   `or' of old and aOtherPrint
00200         //
00201         for ( uint32_t index_1 = 0 ;
00202                 index_1 != m_size ;
00203                 ++index_1 ) {
00204             tmp_ptr[ index_1 ] = m_array[ index_1 ] | aOtherPrint.m_array[ index_1 ] ;
00205         }
00206         //
00207         // for the rest of length of the new memory, fill with aOtherPrint
00208         //
00209         for ( uint32_t index_2 = m_size ;
00210                 index_2 != aOtherPrint.m_size ;
00211                 ++index_2) {
00212             tmp_ptr[ index_2 ] = aOtherPrint.m_array[ index_2 ] ;
00213         }
00214         //
00215         // delete old memory
00216         //    
00217         delete [] m_array ;
00218         //
00219         // update member data elements
00220         //
00221         m_size = aOtherPrint.m_size;
00222         m_array = tmp_ptr ;
00223     }
00224     return ( *this ) ;   
00225 }
00226 
00227 //
00228 // member functions
00229 //
00230 
00231 //------ fresh -----
00232 // assign a fresh footprint to this object
00233 //
00234 CDFootPrint& CDFootPrint::fresh( void )
00235 {
00236     if ( m_size != 0 ) {
00237         //
00238         // if already assigned a value do nothing
00239         //
00240         return ( *this ) ;
00241     }
00242 
00243     const uint32_t kBitsInByte = 8 ;
00244     //
00245     // Take a number and increase the number of CDFootPrints issued
00246     //
00247     uint32_t freshNumber = m_numberIssued++ ;
00248     //
00249     // calculate which bit, and which element to set
00250     //
00251     uint32_t element = freshNumber / ( kBitsInByte * sizeof( uint32_t ) ) ;
00252     uint32_t offsetInElement = freshNumber % ( kBitsInByte * sizeof( uint32_t ) ) ;
00253     //
00254     // book enough memory
00255     //
00256     resize( element + 1 ) ;
00257     //
00258     // fill all but the last part of the memory with zeros
00259     //
00260     for ( uint32_t index = 0 ;
00261             index < element ;
00262             ++index ) {
00263         m_array[ index ] = uint32_t( 0 );
00264     }
00265     //
00266     // fill in last part of memory with correct bit set
00267     //
00268     m_array[ element ] = uint32_t(1) << offsetInElement ;
00269     return ( *this ) ;
00270 }
00271 
00272 //------ resize -----
00273 // resize the arry for the footprint
00274 // Note: This routine does NOT copy the current contents into the
00275 //         new memory
00276 //
00277 void CDFootPrint::resize( const uint32_t aNewSize )
00278 {
00279     if ( aNewSize == m_size ) {
00280         //
00281         // if Footprint is already the right size do nothing.
00282         //
00283         return ;
00284     }
00285     //
00286     // allocate a new section of memory, and delete old section
00287     //
00288     uint32_t* tmp_ptr = new uint32_t[ aNewSize ] ;
00289     if ( 0 == tmp_ptr ) {
00290         std::cerr << "No memory to allocate another kinematicData" << std::endl ;
00291         exit( 1 ) ;
00292     }
00293     delete [] m_array ;
00294     //
00295     // update member data elements
00296     //
00297     m_size = aNewSize ;
00298     m_array = tmp_ptr ;
00299     //
00300     return ;
00301 }
00302 
00303 //
00304 // const member functions
00305 //
00306 
00307 //------ equality -----
00308 // test to see if two footprints are identical
00309 //
00310 bool CDFootPrint::operator==(const  CDFootPrint& aOtherPrint ) const
00311 {
00312     if ( this == &aOtherPrint ) {
00313         //
00314         // if being compare with itself return `true'
00315         //
00316         return( !false ) ;
00317     }
00318     //
00319     // find the shortest length to compare the two memory sections
00320     //
00321     uint32_t shorterSize ;
00322     if ( m_size > aOtherPrint.m_size ) {
00323         shorterSize = aOtherPrint.m_size ;
00324     }
00325     else {
00326         shorterSize = m_size ;
00327     }
00328     //
00329     // check shorter CDFootPrint matches the section longer CDFootPrint
00330     //
00331     uint32_t index = 0;      
00332     while ( ( index != shorterSize ) &&
00333             ( m_array[ index ] == aOtherPrint.m_array[ index ] ) ) {
00334         ++index ;
00335     }
00336     //
00337     // if check finished before the shorter CDFootPrint was covered,
00338     //   then two CDFootPrints are unequal
00339     //
00340     if ( index != shorterSize ) {
00341         return ( false ) ;
00342     }
00343     //
00344     // check that the rest of the longer footprint is zero
00345     //
00346     if ( m_size >= aOtherPrint.m_size ) {
00347         while ( ( index != m_size ) &&
00348                 ( m_array[ index ] == uint32_t( 0 ) ) )  {
00349             ++index ;
00350         }
00351         return ( m_size == index );
00352     }
00353     else {
00354         while( ( index != aOtherPrint.m_size ) &&
00355                 (aOtherPrint.m_array[ index ] == uint32_t( 0 ) ) ) {
00356             ++index ;
00357         }
00358         return ( aOtherPrint.m_size == index );
00359     }
00360 }
00361 
00362 //------ non-equality -----
00363 // test to see if two footprints are not idential
00364 //
00365 bool CDFootPrint::operator!=( const CDFootPrint& aOtherPrint ) const
00366 {
00367     return ( ! operator == ( aOtherPrint ) ) ;
00368 }
00369 
00370 //------ addition -----
00371 // combine two CDFootPrints to create a third
00372 //
00373 CDFootPrint CDFootPrint::operator+( const CDFootPrint& aOtherPrint ) const
00374 {
00375     CDFootPrint result( *this ) ;
00376     result += aOtherPrint ;
00377     return ( result ) ;
00378 }
00379 
00380 
00381 //------ overlap -----
00382 // true if this CDFootPrint overlaps with the other one
00383 //
00384 bool CDFootPrint::overlap( const CDFootPrint& aOtherPrint) const
00385 {
00386     if (this == & aOtherPrint) {
00387         //
00388         // if being tested with itself return `true'
00389         //
00390         return( !false );
00391     } 
00392     //
00393     // find the shortest length to compare the two memoery sections
00394     //
00395     uint32_t shorterSize ;
00396     if ( m_size > aOtherPrint.m_size ) {
00397         shorterSize = aOtherPrint.m_size ;
00398     }
00399     else {
00400         shorterSize = m_size ;
00401     }
00402     //
00403     // check shorter CDFootPrint matches the section longer CDFootPrint
00404     //
00405     uint32_t index = 0;      
00406     while ( ( index != shorterSize ) &&
00407             ( 0 == ( m_array[ index ] & aOtherPrint.m_array[ index ] ) ) ) {
00408         ++index ;
00409     }
00410     //
00411     // if check finished before the shorter CDFootPrint was covered,
00412     //   then two CDFootPrints have at least one bit in common
00413     //
00414     return ( index != shorterSize ) ;
00415 }
00416 
00417 //------ contains -----
00418 // true if this CDFootPrint contains the other one
00419 //
00420 bool CDFootPrint::contains( const CDFootPrint& aOtherPrint) const
00421 {
00422     if (this == & aOtherPrint) {
00423         //
00424         // if being tested with itself return `true'
00425         //
00426         return(! false);
00427     } 
00428     //
00429     // find the shortest length to compare the two memoery sections
00430     //
00431     uint32_t shorterSize ;
00432     if ( m_size > aOtherPrint.m_size ) {
00433         shorterSize = aOtherPrint.m_size ;
00434     }
00435     else {
00436         shorterSize = m_size ;
00437     }
00438 
00439     //
00440     // for shorter CDFootPrint check aOtherPrint is within this one
00441     //
00442     uint32_t index = 0 ;      
00443     while ( ( index != shorterSize ) &&
00444             ( m_array[ index ] ==( m_array[index] | aOtherPrint.m_array[index] ) ) ) {
00445         ++index ;
00446     }
00447     //
00448     // if check finished before the shorter CDFootPrint was covered,
00449     //   then aOtherPrint has at least one bit outside this CDFootPrint
00450     //
00451     if ( index != shorterSize ) {
00452         return ( false ) ;
00453     }
00454     //
00455     // if this CDFootPrint is the longer then aOtherPrint is totally contained
00456     //
00457     if ( m_size > aOtherPrint.m_size ) {
00458         return ( !false ) ;
00459     }
00460     //
00461     // as aOtherPrint is longer need to check rest of it is zero
00462     //
00463     while( ( index != aOtherPrint.m_size ) &&
00464             (aOtherPrint.m_array[ index ] == uint32_t( 0 ) ) ) {
00465         ++index ;
00466     }
00467     //
00468     // if check finished before aOtherPrint was finished
00469     //   then aOtherPrint has at least one bit in the rest of it
00470     //
00471     return ( aOtherPrint.m_size == index ) ;
00472 }
00473 
00474 //
00475 // static member functions
00476 //
00477 
00478 //------ reset (a Static Function) -----
00479 // set number issued to zero
00480 //
00481 void CDFootPrint::reset()
00482 {
00483     m_numberIssued = 0 ;
00484 }
00485 

Generated on Tue Nov 29 22:58:19 2016 for BOSS_7.0.2 by  doxygen 1.4.7