[go: up one dir, main page]

Menu

[ab7a0e]: / libgnuworld / MTrie.h  Maximize  Restore  History

Download this file

286 lines (242 with data), 7.4 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* MTrie.h
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
*
* This program 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; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*
* $Id: MTrie.h,v 1.13 2004/05/18 16:50:57 dan_karrels Exp $
*/
#ifndef __MTRIE_H
#define __MTRIE_H
#include <map>
#include <list>
#include <string>
#include <iostream>
#include "StringTokenizer.h"
using gnuworld::StringTokenizer ;
/**
* This class is used to store elements keyed by a decimalized string,
* for example a hostname like www.yahoo.com
* The key advantage is that this class is designed to support
* fast wildcard matching of these hosts.
* For example, clients of this class can search for all elements
* whose keys match 'ab*ef.foo.ba?.net'.
*/
template< typename _valueT >
class MTrie
{
public:
/**
* Initialize an MTrie.
*/
MTrie( const char delimiter = '.' ) ;
/**
* Destructor will deallocate all internally allocated
* memory, but will not touch data stored here on behalf
* the client.
*/
virtual ~MTrie() ;
/**
* The type being stored in this structure
*/
typedef _valueT data_type ;
/**
* The key/value pair type
*/
typedef std::pair< std::string, _valueT > value_type ;
/**
* The type used to represent the number of elements
* in this MTrie
*/
typedef size_t size_type ;
/**
* Return the current number of elements being stored
* in this MTrie.
*/
inline size_type size() const
{ return numElements ; }
/**
* Insert a key/value pair. The key must NOT have any
* wildcard characters.
*/
bool insert( const std::string& key, const data_type& value ) ;
/**
* Return a list of the key/value pairs that match the given
* wildcard key. The key may contain '?' and '*', but need
* not do so. In general, the most specific searches will
* be fastest.
*/
virtual std::list< value_type > find( const std::string& key ) const ;
/**
* Remove all values associated with key's that match the given
* key. The key may have wildcards '?' and '*', and follow
* the same semantics as find(). Be sure to deallocate the
* values if necessary before calling erase(), this method will
* not deallocate the items stored in this structure.
* The number of elements erased is returned.
*/
virtual size_type erase( const std::string& key ) ;
/**
* Produce output about for each node in the trie in the
* following format:
* <level number> <nodesmap size> <valueslist size>
* This method is used for statistical analysis.
*/
virtual void dumpDebug( std::ostream& ) const ;
/**
* Send the key of each node at the given level to the
* output stream. Also send the total number of values
* under each subtree at those nodes.
*/
virtual void levelDebug( std::ostream&, size_t searchLevel )
const ;
/**
* Return all keys that have at least as many tokens as
* minLength.
*/
virtual std::list< std::string > findMinLength( size_t minLength )
const ;
protected:
/**
* Recursive helper method to the public levelDebug()
*/
virtual void levelDebug( std::ostream&, size_t currentLevel,
size_t searchLevel,
const MTrie< data_type >* ) const ;
/**
* Return the total number of values under the given node
*/
virtual size_t value_size( const MTrie< data_type >* ) const ;
/**
* Recursive helper method to the public findMinLength()
*/
virtual void findMinLength( size_t minLength,
std::list< std::string >& retMe,
const MTrie< data_type >* ) const ;
/**
* Recursive find method that handles all the hard work
* matching across multiple levels.
*/
virtual void find( const MTrie< data_type >*,
const StringTokenizer&,
StringTokenizer::const_reverse_iterator )
const ;
/**
* Recursive erase method that handles all the hard work
* matching across multiple levels.
*/
virtual size_type erase( MTrie< data_type >*,
std::list< std::string >&,
const std::string& origKey,
const std::string& key ) ;
/**
* Convenience method that puts together a list of
* string tokens into a single string.
*/
std::string getBase() const ;
/**
* Recursive method used only for searching for '*' matched
* strings.
*/
virtual void recursiveFind( const MTrie< data_type >*,
bool blindRecursion = false )
const ;
/**
* Recursive method used only for erasing '*' matched
* strings.
*/
virtual size_type recursiveErase( MTrie< data_type >*,
std::list< std::string >& base,
const std::string& key ) ;
/**
* A recursive method used to output some debugging information.
*/
virtual void recursiveDebug( std::ostream&,
size_t levelNum,
const MTrie< data_type >* ) const ;
/**
* The number of elements stored in this MTrie
*/
size_type numElements ;
/**
* The parent node to this node (NULL if root)
*/
MTrie< data_type >* parent ;
/**
* The string delimiter.
*/
char delimiter ;
/**
* The type used to store values
*/
typedef std::list< data_type > valuesListType ;
/**
* An iterator for iterating the valuesList
*/
typedef typename valuesListType::iterator values_iterator ;
/**
* A const iterator for iterating the valuesList
*/
typedef typename valuesListType::const_iterator const_values_iterator ;
/**
* The structure used to store values
*/
valuesListType valuesList ;
/**
* The type used to store pointers to levels in the trie
*/
typedef std::map< std::string, MTrie< _valueT >* > nodesMapType ;
/**
* An iterator to nodes in the trie
*/
typedef typename nodesMapType::iterator nodes_iterator ;
/**
* A const iterator to nodes in the trie
*/
typedef typename nodesMapType::const_iterator const_nodes_iterator ;
/**
* The structure used to store levels in the trie
*/
nodesMapType nodesMap ;
/**
* This variable is used in find() and erase() to reduce the
* number of arguments passed to the internal recursive methods.
* It stores the current key associated with the node
* being examined.
*/
mutable std::list< std::string > base ;
/**
* This variable is used in find() and erase() to reduce the
* number of arguments passed to the internal recursive methods.
* It stores the values to be returned from the methods.
*/
mutable std::list< value_type > returnMe ;
/**
* This variable is used in find() and erase() to reduce the
* number of arguments passed to the internal recursive methods.
* It stores the key being searched for in the overall
* find/erase.
*/
mutable std::string origKey ;
/**
* The tokens object used in find() and erase().
*/
StringTokenizer tokens ;
} ;
// This is a template class, cannot compile, so include the source
// inline.
#include "MTrie.cc"
#endif // __MTRIE_H