Bdb packages | Design docs | Source docs | Guidelines | Recent releases

Search | Site Map .

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/BdbTransfer/BdbPTChecksumSum.h

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: BdbPTChecksumSum.h,v 1.5 1999/07/22 00:10:19 svarovsk Exp $
00004 //
00005 // Description:
00006 //      class of function set to calculate checksum in special way.
00007 //      resulting checksum of file is independent of number of blocks
00008 //      it was calculated for if subsums of this blocks are added together
00009 //      in a proper way.
00010 //      most functions made inline for speed
00011 //
00012 // Environment:
00013 //      Software developed for the BaBar Detector at the SLAC B-Factory
00014 //
00015 // Author:
00016 //      Gennadi S. Svarovski (svarovsk@slac.stanford.edu)
00017 //          original author
00018 //
00019 // Copyright Information:
00020 //      Copyright (C) 1999      Stanford Linear Accelerator Center
00021 //
00022 //------------------------------------------------------------------------------
00023 
00024 #ifndef BDBPTCHECKSUMSUM_H
00025 #define BDBPTCHECKSUMSUM_H
00026 
00027 #include <sys/types.h>
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <assert.h>
00031 
00032 // 32 bits should be enough for everyone
00033 typedef unsigned int sum_t;
00034 
00035 class BdbPTChecksumSum {
00036     private:
00037         sum_t sum;
00038 
00039     public:
00040         // default constructor
00041         inline BdbPTChecksumSum( sum_t c = 0 );
00042 
00043         // arithmetics
00044         inline const BdbPTChecksumSum  rotate( void );
00045                                   void rotate( int c );
00046         inline const BdbPTChecksumSum  operator + ( sum_t c ) const;
00047         inline       BdbPTChecksumSum& operator +=( sum_t c );
00048         inline       BdbPTChecksumSum& operator  =( sum_t c );
00049         inline const BdbPTChecksumSum  operator + ( const BdbPTChecksumSum& c ) const;
00050         inline       BdbPTChecksumSum& operator +=( const BdbPTChecksumSum& c );
00051         inline       BdbPTChecksumSum& operator  =( const BdbPTChecksumSum& c );
00052         // getting size
00053         inline static size_t size( void );
00054 
00055         // for output and arithmetics
00056         inline sum_t value( void ) const;
00057 
00058         // calculation of checksum of a given block
00059         static const BdbPTChecksumSum calculate( const void *buffer, 
00060                                       size_t len, unsigned* extrashiftp );
00061 };
00062 
00063 // default constructor
00064 inline BdbPTChecksumSum::BdbPTChecksumSum( sum_t c ) : sum( c ) {
00065 };
00066 
00067 // arithmetics
00068 
00069 // cyclic shift
00070 inline const BdbPTChecksumSum BdbPTChecksumSum::rotate( void ) {
00071     return BdbPTChecksumSum((sum >> 31) + (sum << 1));
00072 }
00073 
00074 // addition with carry
00075 // made inline for speed
00076 inline BdbPTChecksumSum& BdbPTChecksumSum::operator +=( sum_t c ) {
00077     long long lsum = (long long)this->sum + (long long)c;
00078     while( lsum & 0x100000000LL ) { // while there is carry to bit 32
00079         lsum = (lsum & 0xFFFFFFFF) + 1;
00080     }
00081     this->sum = (sum_t)lsum;
00082     return *this;
00083 }
00084 
00085 inline const BdbPTChecksumSum BdbPTChecksumSum::operator + ( sum_t c ) const {
00086     return BdbPTChecksumSum( *this ) += c;
00087 }
00088 
00089 inline BdbPTChecksumSum& BdbPTChecksumSum::operator  =( sum_t c ) {
00090     return ( sum = c, *this );
00091 }
00092 
00093 inline BdbPTChecksumSum& BdbPTChecksumSum::operator +=( const BdbPTChecksumSum& c ) {
00094     long long lsum = (long long)this->sum + (long long)c.sum;
00095     while( lsum & 0x100000000LL ) { // while there is carry to bit 32
00096         lsum = (lsum & 0xFFFFFFFF) + 1;
00097     }
00098     this->sum = (sum_t)lsum;
00099     return *this;
00100 }
00101 
00102 inline const BdbPTChecksumSum BdbPTChecksumSum::operator + ( const BdbPTChecksumSum& c ) const {
00103     return BdbPTChecksumSum( *this ) += c;
00104 }
00105 
00106 inline BdbPTChecksumSum& BdbPTChecksumSum::operator  =( const BdbPTChecksumSum& c ) {
00107     return ( sum = c.sum, *this );
00108 }
00109 
00110 // getting size
00111 inline size_t BdbPTChecksumSum::size( void ) {
00112     return sizeof( sum_t );
00113 }
00114 
00115 // for output and arithmetics
00116 inline sum_t BdbPTChecksumSum::value( void ) const {
00117     return sum;
00118 }
00119 
00120 #endif // BDBPTCHECKSUMSUM_H

 


BaBar Public Site | SLAC | News | Links | Who's Who | Contact Us

Page Owner: Jacek Becla
Last Update: October 04, 2002