CGEM BOSS 6.6.5.f
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
11
12template<class T>
14
15public:
16 inline EvtStringHash(int size);
17 inline void add(const std::string& str,T* data);
18 inline T* get(const std::string& str);
19 inline ~EvtStringHash();
20
21private:
23 int _size;
24 inline int hash(const std::string& str);
25 std::string*** _strings;
26 T*** _data;
27 int* _entries;
28
29};
30
31
32template<class T>
34
35 _size=size;
36
37 typedef std::string** EvtStringPtrPtr;
38 typedef T** TPtrPtr;
39
40 _strings=new EvtStringPtrPtr[_size];
41 _data=new TPtrPtr[_size];
42 _entries=new int[_size];
43
44 int i;
45
46 for(i=0;i<_size;i++){
47 _entries[i]=0;
48 }
49
50}
51
52template<class T>
54
55 int i;
56 for(i=0;i<_size;i++){
57 int j;
58 for(j=0;j<_entries[i];j++){
59 delete _strings[i][j];
60 }
61 if (_entries[i]>0){
62 delete [] _strings[i];
63 delete [] _data[i];
64 }
65 }
66
67 delete [] _strings;
68 delete [] _data;
69 delete [] _entries;
70
71}
72
73template<class T>
74void EvtStringHash<T>::add(const std::string& str,T* data){
75
76 int ihash=hash(str);
77
78 typedef std::string* EvtStringPtr;
79 typedef T* TPtr;
80
81 std::string** newstrings=new EvtStringPtr[_entries[ihash]+1];
82 T** newdata=new TPtr[_entries[ihash]+1];
83
84 int i;
85
86 for(i=0;i<_entries[ihash];i++){
87 newstrings[i]=_strings[ihash][i];
88 newdata[i]=_data[ihash][i];
89 }
90
91 newstrings[_entries[ihash]]=new std::string;
92 *(newstrings[_entries[ihash]])=str;
93 newdata[_entries[ihash]]=data;
94
95
96 if(_entries[ihash]!=0){
97 delete [] _strings[ihash];
98 delete [] _data[ihash];
99 }
100
101 _entries[ihash]++;
102
103 _strings[ihash]=newstrings;
104 _data[ihash]=newdata;
105
106}
107
108template<class T>
109T* EvtStringHash<T>::get(const std::string& str){
110
111 int ihash=hash(str);
112
113 int i;
114
115 for (i=0;i<_entries[ihash];i++){
116 if (*(_strings[ihash][i])==str) return _data[ihash][i];
117 }
118
119 return 0;
120
121
122}
123
124
125template<class T>
126int EvtStringHash<T>::hash(const std::string& str){
127
128 const char* cstr=str.c_str();
129
130 int i=0;
131
132 int value=0;
133
134 while(cstr[i]!=0){
135 value+=(int)cstr[i];
136 i++;
137 }
138
139 return value%_size;
140
141}
142
143
144#endif
145
146
147
148
149
TTree * data
void add(const std::string &str, T *data)
T * get(const std::string &str)