]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/libTrie/TrieNode.cc
SourceFormat Enforcement
[thirdparty/squid.git] / lib / libTrie / TrieNode.cc
1 /*
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #include "squid.h"
10 #include "TrieCharTransform.h"
11 #include "TrieNode.h"
12 #if HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15
16 TrieNode::TrieNode() : _privateData(NULL)
17 {
18 for (int i = 0; i < 256; ++i)
19 internal[i] = NULL;
20 }
21
22 TrieNode::~TrieNode()
23 {
24 for (int i = 0; i < 256; ++i)
25 delete internal[i];
26 }
27
28 /* as for find */
29 bool
30 TrieNode::add(char const *aString, size_t theLength, void *privatedata, TrieCharTransform *transform)
31 {
32 /* We trust that privatedata and existant keys have already been checked */
33
34 if (theLength) {
35 int index = transform ? (*transform)(*aString): *aString;
36
37 if (!internal[index])
38 internal[index] = new TrieNode;
39
40 return internal[index]->add(aString + 1, theLength - 1, privatedata, transform);
41 } else {
42 /* terminal node */
43
44 if (_privateData)
45 return false;
46
47 _privateData = privatedata;
48
49 return true;
50 }
51 }
52