]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/libTrie/Trie.cc
Merged from trunk
[thirdparty/squid.git] / lib / libTrie / Trie.cc
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 #include "squid.h"
21 #include "Trie.h"
22 #if HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25 #include "TrieCharTransform.h"
26 #include "TrieNode.h"
27
28 #if !_USE_INLINE_
29 #include "Trie.cci"
30 #endif
31
32 Trie::Trie(TrieCharTransform *aTransform) : head(0) , transform(aTransform)
33 {}
34
35 extern "C" void *TrieCreate()
36 {
37 return new Trie;
38 }
39
40 Trie::~Trie()
41 {
42 delete head;
43 delete transform;
44 }
45
46 extern "C" void TrieDestroy(void *aTrie)
47 {
48 delete (Trie *)aTrie;
49 }
50
51 extern "C" void *TrieFind(void *aTrie, char const *aString, size_t theLength)
52 {
53 return ((Trie *)aTrie)->find(aString, theLength);
54 }
55
56 bool
57 Trie::add(char const *aString, size_t theLength, void *privatedata)
58 {
59 if (!privatedata)
60 return false;
61
62 if (head) {
63 if (find(aString, theLength))
64 return false;
65
66 return head->add(aString, theLength, privatedata, transform);
67 }
68
69 head = new TrieNode;
70
71 return head->add(aString, theLength, privatedata, transform);
72 }
73
74 extern "C" int TrieAdd(void *aTrie, char const *aString, size_t theLength, void *privatedata)
75 {
76
77 return ((Trie *)aTrie)->add(aString, theLength, privatedata);
78 }