/usr/include/ticcutils/Trie.h is in libticcutils2-dev 0.7-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | /*
$Id: Trie.h 16817 2014-01-06 10:07:03Z sloot $
$URL: https://ilk.uvt.nl/svn/sources/libticcutils/trunk/include/ticcutils/Trie.h $
Copyright (c) 1998 - 2014
ILK - Tilburg University
CLiPS - University of Antwerp
This file is part of ticcutils
ticcutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
ticcutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
For questions and suggestions, see:
http://ilk.uvt.nl/software.html
or send mail to:
timbl@uvt.nl
*/
#ifndef TICC_TRIE_H
#define TICC_TRIE_H
#if defined __GNUC__ || __IBMCPP__
#define LTGT <>
#else
#define LTGT
#endif
namespace Tries {
// A node in the generic trie.
template <class Info> class TrieNode;
template <class Info> std::ostream& operator<< ( std::ostream&,
const TrieNode<Info> * );
template <class Info> class TrieNode {
friend std::ostream& operator<< LTGT ( std::ostream&,
const TrieNode<Info> * );
public:
TrieNode( char );
~TrieNode();
Info *add_to_tree( Info *, const std::string& );
Info *scan_tree( const std::string& ) const;
void Iterate( void(*)( Info *, void * ), void * );
void Iterate( void(*)( Info * ) );
private:
char label; // the label.
Info *the_info; // The information at this pnt.
TrieNode *next_node; // Pointer to the next node.
TrieNode *sub_node; // Pointer to the sub node.
TrieNode( const TrieNode& );
TrieNode& operator=( const TrieNode& );
Info *add_to_tree( Info *, const char * );
Info *scan_tree( const char * ) const;
};
template <class Info>
inline Info *TrieNode<Info>::scan_tree( const char *name ) const {
//
// returns the info where it is found in the tree or NULL.
//
TrieNode *subtree = sub_node; // top node has empty label!
while ( subtree ) {
if ( subtree->label == *name ){
if ( *(++name) == '\0' )
return subtree->the_info;
else {
subtree = subtree->sub_node;
}
}
else if ( subtree->label > *name )
return NULL;
else
subtree = subtree->next_node;
}
return NULL;
}
template <class Info>
inline Info *TrieNode<Info>::scan_tree( const std::string& name ) const {
return scan_tree( name.c_str() );
}
template <class Info>
inline std::ostream& operator << ( std::ostream& os,
const TrieNode<Info> *tree ){
//
// print an TrieNode sorted on Info
//
if ( tree ){
os << tree->sub_node;
if ( tree->the_info )
os << tree->the_info << std::endl;
os << tree->next_node;
}
return os;
}
template <class Info>
inline void TrieNode<Info>::Iterate( void F( Info * ) ){
//
// Do F on each entry in the Trie
//
if ( the_info )
#pragma omp critical(trie_mod)
{
F( the_info );
}
if ( sub_node )
sub_node->Iterate( F );
if ( next_node )
next_node->Iterate( F );
}
template <class Info>
inline void TrieNode<Info>::Iterate( void F( Info *, void * ),
void *arg ){
//
// Do F on each entry in the Trie
//
if ( the_info )
#pragma omp critical(trie_mod)
{
F( the_info, arg );
}
if ( sub_node )
sub_node->Iterate( F, arg );
if ( next_node )
next_node->Iterate( F, arg );
}
template <class Info>
inline TrieNode<Info>::TrieNode( char lab ):
label(lab),
the_info(NULL),
next_node(NULL),
sub_node(NULL)
{
}
template <class Info>
inline TrieNode<Info>::~TrieNode(){
delete the_info;
delete sub_node;
delete next_node;
}
template <class Info>
inline Info *TrieNode<Info>::add_to_tree( Info *info,
const char *lab ){
// If the lab string is empty, we are at the bottom, and
// we can store the info.
//
if ( lab[0] == '\0') {
if ( !the_info )
the_info = info;
return the_info;
}
else {
// Search all the nodes in the node_list for a
// fitting sub node. If found, continue with it.
//
TrieNode<Info> **subNodePtr = &sub_node; // First one.
while (*subNodePtr != NULL) {
if ( (*subNodePtr)->label == lab[0] ) {
return (*subNodePtr)->add_to_tree( info, &lab[1] );
}
else if ( (*subNodePtr)->label > lab[0] ) {
TrieNode<Info> *tmp = *subNodePtr;
*subNodePtr = new TrieNode<Info>( lab[0] );
(*subNodePtr)->next_node = tmp;
return (*subNodePtr)->add_to_tree( info, &lab[1] );
}
subNodePtr = &((*subNodePtr)->next_node);
}
// We don't, so we create a new one, and continue.
//
*subNodePtr = new TrieNode<Info>( lab[0] );
return (*subNodePtr)->add_to_tree( info, &lab[1] );
}
}
template <class Info>
inline Info *TrieNode<Info>::add_to_tree( Info *info,
const std::string& lab ){
return add_to_tree( info, lab.c_str() );
}
// a generic trie.
template <class Info> class Trie;
template <class Info> std::ostream &operator << ( std::ostream &,
const Trie<Info> * );
template <class Info> class Trie{
friend std::ostream &operator << LTGT ( std::ostream &,
const Trie<Info> * );
public:
Trie():
Tree( new TrieNode<Info>( '\0' ) )
{
};
~Trie() {
delete Tree;
};
Info *Store( const std::string& str, Info *info ) {
return Tree->add_to_tree( info, str );
};
Info *Retrieve( const std::string& str ) const{
return Tree->scan_tree( str ); };
void ForEachDo( void F( Info *, void * ), void *arg ){
if ( Tree ) Tree->Iterate( F, arg ); };
void ForEachDo( void F( Info * ) ) {
if ( Tree ) Tree->Iterate( F ); };
protected:
TrieNode<Info> *Tree;
Trie( const Trie& );
Trie& operator=( const Trie& );
};
template <class Info>
inline std::ostream &operator << ( std::ostream &os,
const Trie<Info> *T ){
if ( T )
os << T->Tree;
return os;
}
}
#endif
|