Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:20:04

0001 
0002 /***********************************************************************
0003 * Copyright 1998-2020 CERN for the benefit of the EvtGen authors       *
0004 *                                                                      *
0005 * This file is part of EvtGen.                                         *
0006 *                                                                      *
0007 * EvtGen is free software: you can redistribute it and/or modify       *
0008 * it under the terms of the GNU General Public License as published by *
0009 * the Free Software Foundation, either version 3 of the License, or    *
0010 * (at your option) any later version.                                  *
0011 *                                                                      *
0012 * EvtGen is distributed in the hope that it will be useful,            *
0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
0015 * GNU General Public License for more details.                         *
0016 *                                                                      *
0017 * You should have received a copy of the GNU General Public License    *
0018 * along with EvtGen.  If not, see <https://www.gnu.org/licenses/>.     *
0019 ***********************************************************************/
0020 
0021 #ifndef EVTSTRINGHASH_HH
0022 #define EVTSTRINGHASH_HH
0023 
0024 #include <string>
0025 
0026 template <class T>
0027 class EvtStringHash {
0028   public:
0029     inline EvtStringHash( int size );
0030     inline void add( const std::string& str, T* data );
0031     inline T* get( const std::string& str );
0032     inline ~EvtStringHash();
0033 
0034   private:
0035     EvtStringHash();
0036     int _size;
0037     inline int hash( const std::string& str );
0038     std::string*** _strings;
0039     T*** _data;
0040     int* _entries;
0041 };
0042 
0043 template <class T>
0044 EvtStringHash<T>::EvtStringHash( int size )
0045 {
0046     _size = size;
0047 
0048     typedef std::string** EvtStringPtrPtr;
0049     typedef T** TPtrPtr;
0050 
0051     _strings = new EvtStringPtrPtr[_size];
0052     _data = new TPtrPtr[_size];
0053     _entries = new int[_size];
0054 
0055     int i;
0056 
0057     for ( i = 0; i < _size; i++ ) {
0058         _entries[i] = 0;
0059     }
0060 }
0061 
0062 template <class T>
0063 EvtStringHash<T>::~EvtStringHash()
0064 {
0065     int i;
0066     for ( i = 0; i < _size; i++ ) {
0067         int j;
0068         for ( j = 0; j < _entries[i]; j++ ) {
0069             delete _strings[i][j];
0070         }
0071         if ( _entries[i] > 0 ) {
0072             delete[] _strings[i];
0073             delete[] _data[i];
0074         }
0075     }
0076 
0077     delete[] _strings;
0078     delete[] _data;
0079     delete[] _entries;
0080 }
0081 
0082 template <class T>
0083 void EvtStringHash<T>::add( const std::string& str, T* data )
0084 {
0085     int ihash = hash( str );
0086 
0087     typedef std::string* EvtStringPtr;
0088     typedef T* TPtr;
0089 
0090     std::string** newstrings = new EvtStringPtr[_entries[ihash] + 1];
0091     T** newdata = new TPtr[_entries[ihash] + 1];
0092 
0093     int i;
0094 
0095     for ( i = 0; i < _entries[ihash]; i++ ) {
0096         newstrings[i] = _strings[ihash][i];
0097         newdata[i] = _data[ihash][i];
0098     }
0099 
0100     newstrings[_entries[ihash]] = new std::string;
0101     *( newstrings[_entries[ihash]] ) = str;
0102     newdata[_entries[ihash]] = data;
0103 
0104     if ( _entries[ihash] != 0 ) {
0105         delete[] _strings[ihash];
0106         delete[] _data[ihash];
0107     }
0108 
0109     _entries[ihash]++;
0110 
0111     _strings[ihash] = newstrings;
0112     _data[ihash] = newdata;
0113 }
0114 
0115 template <class T>
0116 T* EvtStringHash<T>::get( const std::string& str )
0117 {
0118     int ihash = hash( str );
0119 
0120     int i;
0121 
0122     for ( i = 0; i < _entries[ihash]; i++ ) {
0123         if ( *( _strings[ihash][i] ) == str )
0124             return _data[ihash][i];
0125     }
0126 
0127     return 0;
0128 }
0129 
0130 template <class T>
0131 int EvtStringHash<T>::hash( const std::string& str )
0132 {
0133     const char* cstr = str.c_str();
0134 
0135     int i = 0;
0136 
0137     int value = 0;
0138 
0139     while ( cstr[i] != 0 ) {
0140         value += (int)cstr[i];
0141         i++;
0142     }
0143 
0144     return value % _size;
0145 }
0146 
0147 #endif