Menu

[r135]: / src / stringset.h  Maximize  Restore  History

Download this file

220 lines (201 with data), 5.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright 2005-2007 Gerald Schmidt.
*
* This file is part of Xml Copy Editor.
*
* Xml Copy Editor is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* Xml Copy Editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xml Copy Editor; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef STRINGSET_H
#define STRINGSET_H
#include <stdint.h>
#include <cstdio>
#include <string>
#include <stdexcept>
#include <cmath>
#include <climits>
#include <cstring>
#ifdef __WXMSW__
#include <mem.h>
#endif
template<class T>
class StringSetNode
{
public:
StringSetNode ( T *keyParameter, size_t lenParameter )
{
next = NULL;
len = lenParameter;
key = new T[len];
memcpy ( key, keyParameter, len * sizeof ( T ) );
}
~StringSetNode()
{
delete[] key;
}
StringSetNode<T> *next;
T *key;
size_t len;
};
template<class T>
class StringSet
{
public:
StringSet (
int hashSizePower = 19
);
~StringSet();
StringSet ( const StringSet<T>& );
StringSet& operator= ( const StringSet<T>& );
inline bool empty();
inline int count();
StringSetNode<T> *insert ( std::basic_string<T> &s );
StringSetNode<T> *insert ( T *s, size_t len, uint32_t hash = UINT_MAX );
StringSetNode<T> *find ( std::basic_string<T> &s );
StringSetNode<T> *find ( T *s, size_t len, uint32_t hash = UINT_MAX );
void clear();
private:
bool emptyFlag;
uint32_t hashSize, hashMask, nodeCount;
StringSetNode<T> **table;
uint32_t hash ( const char *s, size_t len );
void allocateHashTable ( uint32_t hashSize );
};
template<class T>
StringSet<T>::StringSet ( int hashSizePower )
{
if ( hashSizePower < 1 )
throw std::runtime_error ( "StringSet: invalid parameter" );
hashSize = ( int ) pow ( ( double ) 2, ( double ) hashSizePower );
hashMask = hashSize - 1;
nodeCount = 0;
allocateHashTable ( hashSize );
}
template<class T>
StringSet<T>::~StringSet()
{
if ( !empty() )
clear();
delete[] table;
}
template<class T>
StringSet<T>::StringSet ( const StringSet<T>& d )
{
hashSize = d.hashSize;
hashMask = hashSize - 1;
nodeCount = 0;
allocateHashTable ( hashSize );
StringSetNode<T> *np;
for ( uint32_t i = 0; i < d.hashSize; ++i )
for ( np = d.table[i]; np != NULL; np = np->next )
insert ( np->key, np->len, i );
}
template<class T>
StringSet<T>& StringSet<T>::operator= ( const StringSet<T>& d )
{
if ( this != &d )
{
clear();
StringSetNode<T> *np;
for ( uint32_t i = 0; i < d.hashSize; ++i )
for ( np = d.table[i]; np != NULL; np = np->next )
insert ( np->key, np->len, i );
}
return *this;
}
// see 'one-at-a-time hash' (http://burtleburtle.net/bob/hash/doobs.html)
template<class T>
uint32_t StringSet<T>::hash ( const char *key, size_t len )
{
uint32_t hash;
for ( hash = 0; len--; ++key )
{
hash += *key;
hash += ( hash << 10 );
hash ^= ( hash >> 6 );
}
hash += ( hash << 3 );
hash ^= ( hash >> 11 );
hash += ( hash << 15 );
return ( hash & hashMask );
}
template<class T>
StringSetNode<T> *StringSet<T>::find ( std::basic_string<T> &s )
{
return find ( ( T * ) s.data(), s.size() );
}
template<class T>
StringSetNode<T> *StringSet<T>::find ( T *s, size_t len, uint32_t hashValue )
{
if ( hashValue == UINT_MAX )
hashValue = hash ( ( const char* ) s, len * sizeof ( T ) );
StringSetNode<T> *np;
for ( np = table[hashValue]; np != NULL; np = np->next )
if ( len == np->len && ( memcmp ( s, np->key, len ) == 0 ) )
return np;
return NULL;
}
template<class T>
StringSetNode<T> *StringSet<T>::insert ( std::basic_string<T> &s )
{
return insert ( ( T * ) s.data(), s.size() );
}
template<class T>
StringSetNode<T> *StringSet<T>::insert ( T *s, size_t len, uint32_t hashValue )
{
if ( hashValue == UINT_MAX )
hashValue = hash ( ( const char* ) s, len * sizeof ( T ) );
StringSetNode<T> *np;
if ( ( np = find ( s, len, hashValue ) ) == NULL )
{
np = new StringSetNode<T> ( s, len );
np->next = table[hashValue];
table[hashValue] = np;
++nodeCount;
}
return np;
}
template<class T>
void StringSet<T>::clear()
{
if ( !nodeCount )
return;
StringSetNode<T> *np, *memory;
for ( uint32_t i = 0; i < hashSize; ++i )
{
for ( np = table[i]; np != NULL; np = memory )
{
memory = np->next;
delete np;
}
table[i] = NULL;
}
nodeCount = 0;
}
template<class T>
int StringSet<T>::count()
{
return nodeCount;
}
template<class T>
bool StringSet<T>::empty()
{
return ( nodeCount ) ? false : true;
}
template<class T>
void StringSet<T>::allocateHashTable ( uint32_t hashSize )
{
table = new StringSetNode<T> *[hashSize];
memset ( table, 0, sizeof ( StringSetNode<T> * ) * hashSize );
}
#endif
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.