]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/libTrie/TrieNode.h
Sync with trunk rev.13542
[thirdparty/squid.git] / lib / libTrie / TrieNode.h
1 /*
2 * Copyright (c) 2002,2003 Robert Collins <rbtcollins@hotmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
17 *
18 */
19
20 #ifndef LIBTRIE_TRIENODE_H
21 #define LIBTRIE_TRIENODE_H
22
23 #include "TrieCharTransform.h"
24
25 #include <sys/types.h>
26 #include <utility>
27
28 /* TODO: parameterize this to be more generic -
29 * i.e. M-ary internal node sizes etc
30 */
31
32 class TrieNode
33 {
34
35 public:
36 TrieNode();
37 ~TrieNode();
38 TrieNode(TrieNode const &);
39 TrieNode &operator= (TrieNode const &);
40
41 /* Find a string.
42 * If found, return the private data.
43 * If not found, return NULL.
44 */
45 inline void *find (char const *, size_t, TrieCharTransform *, bool const prefix) const;
46
47 /* Add a string.
48 * returns false if the string is already
49 * present or can't be added.
50 */
51
52 bool add (char const *, size_t, void *, TrieCharTransform *);
53
54 private:
55 /* 256-way Trie */
56 /* The char index into internal is an
57 * 8-bit prefix to a string in the trie.
58 * internal[0] is the terminal node for
59 * a string and may not be used
60 */
61 TrieNode * internal[256];
62
63 /* If a string ends here, non NULL */
64 void *_privateData;
65 };
66
67 /* recursive. TODO? make iterative */
68 void *
69 TrieNode::find (char const *aString, size_t theLength, TrieCharTransform *transform, bool const prefix) const
70 {
71 if (theLength) {
72 int index = -1;
73 unsigned char pos = transform ? (*transform) (*aString) : *aString;
74
75 if (internal[pos])
76 index = pos;
77
78 if (index > -1) {
79 void *result;
80 result = internal[index]->find(aString + 1, theLength - 1, transform, prefix);
81
82 if (result)
83 return result;
84 }
85
86 if (prefix)
87 return _privateData;
88
89 return NULL;
90 } else {
91 /* terminal node */
92 return _privateData;
93 }
94 }
95 #endif /* LIBTRIE_TRIENODE_H */