BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
EvtStringHash.hh
Go to the documentation of this file.
1
2//
3// Copyright Information: See EvtGen/COPYRIGHT
4//
5
6#ifndef EVTSTRINGHASH_HH
7#define EVTSTRINGHASH_HH
8
9#include <string>
10
11template <class T> class EvtStringHash {
12
13public:
14 inline EvtStringHash( int size );
15 inline void add( const std::string& str, T* data );
16 inline T* get( const std::string& str );
17 inline ~EvtStringHash();
18
19private:
21 int _size;
22 inline int hash( const std::string& str );
23 std::string*** _strings;
24 T*** _data;
25 int* _entries;
26};
27
28template <class T> EvtStringHash<T>::EvtStringHash( int size ) {
29
30 _size = size;
31
32 typedef std::string** EvtStringPtrPtr;
33 typedef T** TPtrPtr;
34
35 _strings = new EvtStringPtrPtr[_size];
36 _data = new TPtrPtr[_size];
37 _entries = new int[_size];
38
39 int i;
40
41 for ( i = 0; i < _size; i++ ) { _entries[i] = 0; }
42}
43
45
46 int i;
47 for ( i = 0; i < _size; i++ )
48 {
49 int j;
50 for ( j = 0; j < _entries[i]; j++ ) { delete _strings[i][j]; }
51 if ( _entries[i] > 0 )
52 {
53 delete[] _strings[i];
54 delete[] _data[i];
55 }
56 }
57
58 delete[] _strings;
59 delete[] _data;
60 delete[] _entries;
61}
62
63template <class T> void EvtStringHash<T>::add( const std::string& str, T* data ) {
64
65 int ihash = hash( str );
66
67 typedef std::string* EvtStringPtr;
68 typedef T* TPtr;
69
70 std::string** newstrings = new EvtStringPtr[_entries[ihash] + 1];
71 T** newdata = new TPtr[_entries[ihash] + 1];
72
73 int i;
74
75 for ( i = 0; i < _entries[ihash]; i++ )
76 {
77 newstrings[i] = _strings[ihash][i];
78 newdata[i] = _data[ihash][i];
79 }
80
81 newstrings[_entries[ihash]] = new std::string;
82 *( newstrings[_entries[ihash]] ) = str;
83 newdata[_entries[ihash]] = data;
84
85 if ( _entries[ihash] != 0 )
86 {
87 delete[] _strings[ihash];
88 delete[] _data[ihash];
89 }
90
91 _entries[ihash]++;
92
93 _strings[ihash] = newstrings;
94 _data[ihash] = newdata;
95}
96
97template <class T> T* EvtStringHash<T>::get( const std::string& str ) {
98
99 int ihash = hash( str );
100
101 int i;
102
103 for ( i = 0; i < _entries[ihash]; i++ )
104 {
105 if ( *( _strings[ihash][i] ) == str ) return _data[ihash][i];
106 }
107
108 return 0;
109}
110
111template <class T> int EvtStringHash<T>::hash( const std::string& str ) {
112
113 const char* cstr = str.c_str();
114
115 int i = 0;
116
117 int value = 0;
118
119 while ( cstr[i] != 0 )
120 {
121 value += (int)cstr[i];
122 i++;
123 }
124
125 return value % _size;
126}
127
128#endif
TTree * data
EvtStringHash(int size)
void add(const std::string &str, T *data)
T * get(const std::string &str)