StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
GtHash.cxx
1 #include "TMath.h"
2 #include "GtHash.h"
3 
4 class GtCradle : public TObject
5 {
6 public:
7  Int_t fNWords; // number of words in array
8  ULong_t *fArray; // array
9  void *fPointer; // user pointer
10 
11 GtCradle(int n);
12 ~GtCradle(){ if (fArray) delete [] fArray;};
13 
14 protected:
15  virtual Bool_t IsEqual(const TObject* obj) const;
16  virtual ULong_t Hash() const;
17 };
18 
19 GtCradle::GtCradle(int n)
20 {
21  fNWords = n;
22  fArray = new ULong_t[fNWords];
23  fPointer=0;
24 }
25 
26 Bool_t GtCradle::IsEqual(const TObject* obj) const
27 {
28  GtCradle *He = (GtCradle*)obj;
29  if (fNWords != He->fNWords) return 0;
30  ULong_t *me = fArray;
31  ULong_t *he = He->fArray;
32  if (me[0] != he[0]) return 0;
33  for (int i=1; i<fNWords; i++) if (me[i]!=he[i]) return 0;
34  return 1;
35 }
36 ULong_t GtCradle::Hash() const
37 {
38  ULong_t *me = (ULong_t *)fArray;
39  ULong_t ret = fNWords;
40 //VP for (int i=0; i<fNWords; i++) ret ^= me[i];
41  ret = TMath::Hash(me,fNWords*sizeof(ULong_t));
42 
43  return ret;
44 }
45 GtHash::~GtHash(){Delete(); if (fPoka) delete fPoka;};
46 
47 void *GtHash::GetPointer(void *array,Int_t narray)
48 {
49  if (!fPoka || fPoka->fNWords!=narray) {
50  if (fPoka) delete fPoka;
51  fPoka = new GtCradle(narray);}
52 
53  memcpy(fPoka->fArray,array,narray*sizeof(ULong_t));
54 
55  fFound = (GtCradle*)FindObject(fPoka);
56  if (!fFound) return 0;
57  return fFound->fPointer;
58 }
59 
60 void GtHash::SetPointer(void *ptr)
61 {
62  assert (ptr);
63  assert (!fFound);
64  assert (fPoka);
65  fPoka->fPointer = ptr;
66  Add(fPoka);
67  fPoka = 0;
68 }
69 
70 Int_t GtHash::GetNParams()
71 { return (fFound) ? fFound->fNWords : 0 ; }
72 
73 const void *GtHash::GetParams()
74 { return (fFound) ? fFound->fArray : 0 ; }
75 
76 
77 
78 
GtCradle * fFound
Cradle pointer.
Definition: GtHash.h:17