]>
Commit | Line | Data |
---|---|---|
43ae1d95 | 1 | /* |
924f73bc | 2 | * Copyright (c) 2002,2003 Robert Collins <rbtcollins@hotmail.com> |
43ae1d95 | 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. | |
26ac0430 | 8 | * |
43ae1d95 | 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. | |
26ac0430 | 13 | * |
43ae1d95 | 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 "Trie.h" | |
21 | #ifdef HAVE_UNISTD_H | |
22 | #include <unistd.h> | |
23 | #endif | |
24 | #include "TrieNode.h" | |
924f73bc | 25 | #include "TrieCharTransform.h" |
43ae1d95 | 26 | |
924f73bc | 27 | Trie::Trie (TrieCharTransform *aTransform) : head (0) , transform (aTransform) |
43ae1d95 | 28 | {} |
29 | ||
30 | extern "C" void *TrieCreate () | |
31 | { | |
32 | return new Trie; | |
33 | } | |
34 | ||
35 | Trie::~Trie () | |
36 | { | |
37 | delete head; | |
924f73bc | 38 | delete transform; |
43ae1d95 | 39 | } |
40 | ||
41 | extern "C" void TrieDestroy (void *aTrie) | |
42 | { | |
43 | delete (Trie *)aTrie; | |
44 | } | |
45 | ||
46 | extern "C" void *TrieFind (void *aTrie, char const *aString, size_t theLength) | |
47 | { | |
48 | return ((Trie *)aTrie)->find (aString, theLength); | |
49 | } | |
50 | ||
51 | bool | |
52 | ||
53 | Trie::add | |
26ac0430 | 54 | (char const *aString, size_t theLength, void *privatedata) |
43ae1d95 | 55 | { |
56 | if (!privatedata) | |
57 | return false; | |
58 | ||
59 | if (head) { | |
60 | if (find (aString, theLength)) | |
61 | return false; | |
62 | ||
63 | return head->add | |
924f73bc | 64 | (aString, theLength, privatedata, transform); |
43ae1d95 | 65 | } |
66 | ||
67 | head = new TrieNode; | |
68 | ||
69 | return head->add | |
924f73bc | 70 | (aString, theLength, privatedata, transform); |
43ae1d95 | 71 | } |
72 | ||
73 | extern "C" int TrieAdd (void *aTrie, char const *aString, size_t theLength, void *privatedata) | |
74 | { | |
75 | ||
76 | return ((Trie *)aTrie)->add | |
77 | (aString, theLength, privatedata); | |
78 | } | |
86a2f789 | 79 | |
80 | #ifndef _USE_INLINE_ | |
81 | #include "Trie.cci" | |
82 | #endif |