evtgen is hosted by Hepforge, IPPP Durham
EvtGen  2.0.0
Monte Carlo generator of particle decays, in particular the weak decays of heavy flavour particles such as B mesons.
EvtStringHash.hh
Go to the documentation of this file.
1 
2 /***********************************************************************
3 * Copyright 1998-2020 CERN for the benefit of the EvtGen authors *
4 * *
5 * This file is part of EvtGen. *
6 * *
7 * EvtGen is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * EvtGen is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with EvtGen. If not, see <https://www.gnu.org/licenses/>. *
19 ***********************************************************************/
20 
21 #ifndef EVTSTRINGHASH_HH
22 #define EVTSTRINGHASH_HH
23 
24 #include <string>
25 
26 template <class T>
28  public:
29  inline EvtStringHash( int size );
30  inline void add( const std::string& str, T* data );
31  inline T* get( const std::string& str );
32  inline ~EvtStringHash();
33 
34  private:
35  EvtStringHash();
36  int _size;
37  inline int hash( const std::string& str );
38  std::string*** _strings;
39  T*** _data;
40  int* _entries;
41 };
42 
43 template <class T>
45 {
46  _size = size;
47 
48  typedef std::string** EvtStringPtrPtr;
49  typedef T** TPtrPtr;
50 
51  _strings = new EvtStringPtrPtr[_size];
52  _data = new TPtrPtr[_size];
53  _entries = new int[_size];
54 
55  int i;
56 
57  for ( i = 0; i < _size; i++ ) {
58  _entries[i] = 0;
59  }
60 }
61 
62 template <class T>
64 {
65  int i;
66  for ( i = 0; i < _size; i++ ) {
67  int j;
68  for ( j = 0; j < _entries[i]; j++ ) {
69  delete _strings[i][j];
70  }
71  if ( _entries[i] > 0 ) {
72  delete[] _strings[i];
73  delete[] _data[i];
74  }
75  }
76 
77  delete[] _strings;
78  delete[] _data;
79  delete[] _entries;
80 }
81 
82 template <class T>
83 void EvtStringHash<T>::add( const std::string& str, T* data )
84 {
85  int ihash = hash( str );
86 
87  typedef std::string* EvtStringPtr;
88  typedef T* TPtr;
89 
90  std::string** newstrings = new EvtStringPtr[_entries[ihash] + 1];
91  T** newdata = new TPtr[_entries[ihash] + 1];
92 
93  int i;
94 
95  for ( i = 0; i < _entries[ihash]; i++ ) {
96  newstrings[i] = _strings[ihash][i];
97  newdata[i] = _data[ihash][i];
98  }
99 
100  newstrings[_entries[ihash]] = new std::string;
101  *( newstrings[_entries[ihash]] ) = str;
102  newdata[_entries[ihash]] = data;
103 
104  if ( _entries[ihash] != 0 ) {
105  delete[] _strings[ihash];
106  delete[] _data[ihash];
107  }
108 
109  _entries[ihash]++;
110 
111  _strings[ihash] = newstrings;
112  _data[ihash] = newdata;
113 }
114 
115 template <class T>
116 T* EvtStringHash<T>::get( const std::string& str )
117 {
118  int ihash = hash( str );
119 
120  int i;
121 
122  for ( i = 0; i < _entries[ihash]; i++ ) {
123  if ( *( _strings[ihash][i] ) == str )
124  return _data[ihash][i];
125  }
126 
127  return 0;
128 }
129 
130 template <class T>
131 int EvtStringHash<T>::hash( const std::string& str )
132 {
133  const char* cstr = str.c_str();
134 
135  int i = 0;
136 
137  int value = 0;
138 
139  while ( cstr[i] != 0 ) {
140  value += (int)cstr[i];
141  i++;
142  }
143 
144  return value % _size;
145 }
146 
147 #endif
int hash(const std::string &str)
void add(const std::string &str, T *data)
T * get(const std::string &str)
std::string *** _strings