]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TEvtGen/EvtGenBase/EvtStringHash.hh
An effective FD corretion
[u/mrichter/AliRoot.git] / TEvtGen / EvtGenBase / EvtStringHash.hh
1 //--------------------------------------------------------------------------
2 //
3 // Environment:
4 //      This software is part of the EvtGen package developed jointly
5 //      for the BaBar and CLEO collaborations.  If you use all or part
6 //      of it, please give an appropriate acknowledgement.
7 //
8 // Copyright Information: See EvtGen/COPYRIGHT
9 //      Copyright (C) 1998      Caltech, UCSB
10 //
11 // Module: EvtGen/EvtSpinDensity.hh
12 //
13 // Description: Templated class to implement a hash table on string
14 //
15 // Modification history:
16 //
17 //    RYD     Aug 2, 2006         Module created
18 //
19 //------------------------------------------------------------------------
20
21
22 #ifndef EVTSTRINGHASH_HH
23 #define EVTSTRINGHASH_HH
24
25 #include <string>
26
27
28 template<class T> 
29 class EvtStringHash{
30
31 public:
32   inline EvtStringHash(int size);
33   inline void add(const std::string& str,T* data);
34   inline T* get(const std::string& str);
35   inline ~EvtStringHash();
36
37 private:
38   EvtStringHash();
39   int _size;
40   inline int hash(const std::string& str);
41   std::string*** _strings;
42   T*** _data;
43   int* _entries;
44
45 };
46
47
48 template<class T> 
49 EvtStringHash<T>::EvtStringHash(int size){
50
51   _size=size;
52
53   typedef std::string** EvtStringPtrPtr;
54   typedef T** TPtrPtr;
55
56   _strings=new EvtStringPtrPtr[_size];
57   _data=new TPtrPtr[_size];
58   _entries=new int[_size];
59
60   int i;
61
62   for(i=0;i<_size;i++){
63     _entries[i]=0;
64   }
65
66 }
67
68 template<class T> 
69 EvtStringHash<T>::~EvtStringHash(){
70   
71   int i;
72   for(i=0;i<_size;i++){
73     int j;
74     for(j=0;j<_entries[i];j++){
75       delete _strings[i][j];
76     }    
77     if (_entries[i]>0){
78       delete [] _strings[i];
79       delete [] _data[i];
80     }
81   }
82
83   delete [] _strings;
84   delete [] _data;
85   delete [] _entries;
86
87 }
88
89 template<class T> 
90 void EvtStringHash<T>::add(const std::string& str,T* data){
91
92   int ihash=hash(str);
93
94   typedef std::string* EvtStringPtr;
95   typedef T* TPtr;
96
97   std::string** newstrings=new EvtStringPtr[_entries[ihash]+1];
98   T** newdata=new TPtr[_entries[ihash]+1];
99                                 
100   int i;
101
102   for(i=0;i<_entries[ihash];i++){
103     newstrings[i]=_strings[ihash][i];
104     newdata[i]=_data[ihash][i];
105   }
106
107   newstrings[_entries[ihash]]=new std::string;
108   *(newstrings[_entries[ihash]])=str;
109   newdata[_entries[ihash]]=data;
110   
111
112   if(_entries[ihash]!=0){
113     delete [] _strings[ihash];
114     delete [] _data[ihash];
115   }
116   
117   _entries[ihash]++;
118
119   _strings[ihash]=newstrings;
120   _data[ihash]=newdata;
121
122 }
123
124 template<class T> 
125 T* EvtStringHash<T>::get(const std::string& str){
126
127   int ihash=hash(str);
128   
129   int i;
130
131   for (i=0;i<_entries[ihash];i++){
132     if (*(_strings[ihash][i])==str) return _data[ihash][i];
133   }
134   
135   return 0;
136
137
138 }
139
140
141 template<class T> 
142 int EvtStringHash<T>::hash(const std::string& str){
143
144   const char* cstr=str.c_str();
145
146   int i=0;
147
148   int value=0;
149
150   while(cstr[i]!=0){
151     value+=(int)cstr[i];
152     i++;
153   }
154
155   return value%_size;
156
157 }
158
159
160 #endif
161
162
163
164
165