EvtGen 2.2.0
Monte Carlo generator of particle decays, in particular the weak decays of heavy flavour particles such as B mesons.
Loading...
Searching...
No Matches
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
26template <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:
36 int m_size;
37 inline int hash( const std::string& str );
38 std::string*** m_strings;
39 T*** m_data;
41};
42
43template <class T>
45{
46 m_size = size;
47
48 typedef std::string** EvtStringPtrPtr;
49 typedef T** TPtrPtr;
50
51 m_strings = new EvtStringPtrPtr[m_size];
52 m_data = new TPtrPtr[m_size];
53 m_entries = new int[m_size];
54
55 int i;
56
57 for ( i = 0; i < m_size; i++ ) {
58 m_entries[i] = 0;
59 }
60}
61
62template <class T>
64{
65 int i;
66 for ( i = 0; i < m_size; i++ ) {
67 int j;
68 for ( j = 0; j < m_entries[i]; j++ ) {
69 delete m_strings[i][j];
70 }
71 if ( m_entries[i] > 0 ) {
72 delete[] m_strings[i];
73 delete[] m_data[i];
74 }
75 }
76
77 delete[] m_strings;
78 delete[] m_data;
79 delete[] m_entries;
80}
81
82template <class T>
83void 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[m_entries[ihash] + 1];
91 T** newdata = new TPtr[m_entries[ihash] + 1];
92
93 int i;
94
95 for ( i = 0; i < m_entries[ihash]; i++ ) {
96 newstrings[i] = m_strings[ihash][i];
97 newdata[i] = m_data[ihash][i];
98 }
99
100 newstrings[m_entries[ihash]] = new std::string;
101 *( newstrings[m_entries[ihash]] ) = str;
102 newdata[m_entries[ihash]] = data;
103
104 if ( m_entries[ihash] != 0 ) {
105 delete[] m_strings[ihash];
106 delete[] m_data[ihash];
107 }
108
109 m_entries[ihash]++;
110
111 m_strings[ihash] = newstrings;
112 m_data[ihash] = newdata;
113}
114
115template <class T>
116T* EvtStringHash<T>::get( const std::string& str )
117{
118 int ihash = hash( str );
119
120 int i;
121
122 for ( i = 0; i < m_entries[ihash]; i++ ) {
123 if ( *( m_strings[ihash][i] ) == str )
124 return m_data[ihash][i];
125 }
126
127 return 0;
128}
129
130template <class T>
131int 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 % m_size;
145}
146
147#endif
std::string *** m_strings
EvtStringHash(int size)
int hash(const std::string &str)
void add(const std::string &str, T *data)
T * get(const std::string &str)