]> git.ipfire.org Git - thirdparty/squid.git/blame - lib/libTrie/TrieNode.cc
SourceFormat Enforcement
[thirdparty/squid.git] / lib / libTrie / TrieNode.cc
CommitLineData
43ae1d95 1/*
dc4eb86a 2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
43ae1d95 3 *
dc4eb86a
AJ
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.
43ae1d95 7 */
8
99742866 9#include "squid.h"
924f73bc 10#include "TrieCharTransform.h"
602d9612 11#include "TrieNode.h"
32d002cb 12#if HAVE_UNISTD_H
43ae1d95 13#include <unistd.h>
14#endif
43ae1d95 15
6ca34f6f 16TrieNode::TrieNode() : _privateData(NULL)
43ae1d95 17{
18 for (int i = 0; i < 256; ++i)
19 internal[i] = NULL;
20}
21
6ca34f6f 22TrieNode::~TrieNode()
43ae1d95 23{
24 for (int i = 0; i < 256; ++i)
25 delete internal[i];
26}
27
28/* as for find */
29bool
6ca34f6f 30TrieNode::add(char const *aString, size_t theLength, void *privatedata, TrieCharTransform *transform)
43ae1d95 31{
32 /* We trust that privatedata and existant keys have already been checked */
33
34 if (theLength) {
6ca34f6f 35 int index = transform ? (*transform)(*aString): *aString;
43ae1d95 36
924f73bc 37 if (!internal[index])
43ae1d95 38 internal[index] = new TrieNode;
43ae1d95 39
6ca34f6f 40 return internal[index]->add(aString + 1, theLength - 1, privatedata, transform);
43ae1d95 41 } else {
42 /* terminal node */
43
44 if (_privateData)
45 return false;
46
47 _privateData = privatedata;
48
49 return true;
50 }
51}
f53969cc 52