1 /* $Id: Trie.h,v 1.15 2011/01/08 19:07:36 sarrazip Exp $
2 Trie.h - Tree structure for string storage
4 verbiste - French conjugation system
5 Copyright (C) 2003-2005 Pierre Sarrazin <http://sarrazip.com/>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
33 /** Tree structure for (wide character) string storage.
34 @param T type of the user data attached to the stored strings;
35 pointers to objects of type T will be stored in the
36 trie, but no T object will be created, copied,
37 assigned or destroyed by the trie.
44 /** Constructs an empty trie.
45 @param userDataFromNew determines if the destructor
46 must assume that all "user data"
47 pointers come from new and must
48 thus be destroyed with delete
50 Trie(bool userDataFromNew);
53 /** Destroys the trie and its contents.
58 /** Adds the given (wide character) key and associates it with
59 the given user data pointer.
60 @returns the user data previously associated with the key,
61 or NULL is no user data was associated
63 T *add(const std::wstring &key, T *userData);
66 /** Searches the trie with the given (wide character) key.
67 Invokes the virtual function onFoundPrefixWithUserData()
69 @param key wide character string to search for
70 @returns a pointer to the user data pointer
71 associated with 'key', or NULL if
74 T *get(const std::wstring &key) const;
77 T *getWithDefault(const std::wstring &key, T *deFault = NULL);
80 /** Obtains the address of the user data associated with 'key'
81 and adds an entry if necessary.
82 @returns a non-null pointer to the user data pointer
83 associated with 'key';
84 if a new entry was created, the T * is null.
86 T **getUserDataPointer(const std::wstring &key);
89 /** Callback invoked by the Trie<>::get() method.
90 This callback will be called for each prefix of the searched
91 string for which the trie has some user data.
92 This method does nothing if it is not overridden in a derived class.
93 @param key the searched string
94 @param index length of the prefix
95 @param userData user data that is associated with the prefix
97 virtual void onFoundPrefixWithUserData(const std::wstring &/*key*/,
98 std::wstring::size_type /*index*/,
99 const T * /*userData*/) const
104 /** Computes and returns the number of memory bytes consumed by
105 this object, excluding the size of the user data instances.
106 @returns number of bytes
108 size_t computeMemoryConsumption() const;
117 /** Constructs a descriptor that can point to an inferior row.
118 @param inferior pointer to the inferior row
120 this pointer must have been obtained
123 Descriptor(Row *inferior = NULL);
125 Descriptor(const Descriptor &d) : inferiorRow(d.inferiorRow), userData(d.userData) {}
126 Descriptor &operator = (const Descriptor &d)
128 if (&d != this) { inferiorRow = d.inferiorRow; userData = d.userData; }
132 /** Destroys this object and calls operator delete on the inferior row.
133 Does not call operator delete on userData.
137 /** Calls recursiveDelete() on *inferiorRow if inferiorRow is not NULL.
138 Then, calls operator delete that row and sets inferiorRow to NULL.
139 @param deleteUserData if true, operator delete is called
140 on userData (which may be NULL)
142 void recursiveDelete(bool deleteUserData);
144 /** Computes and returns the number of memory bytes consumed by
145 this object, excluding the size of the user data.
146 @returns number of bytes
148 size_t computeMemoryConsumption() const;
156 wchar_t unichar; // Unicode character code
159 CharDesc(wchar_t u) : unichar(u), desc() {}
161 /** Computes and returns the number of memory bytes consumed by
162 this object, excluding the size of the Descriptor's user data.
163 @returns number of bytes
165 size_t computeMemoryConsumption() const;
176 /** Calls recursiveDelete() on each Descriptor in this row.
177 Then empties this row.
178 @param deleteUserData if true, operator delete is called
179 on the userData field of the
182 void recursiveDelete(bool deleteUserData);
185 /** Finds an element of this row whose (wide) character field is
187 Returns NULL if no such element exists.
189 Descriptor *find(wchar_t unichar);
191 /** Finds or creates an element of this row whose char. field is 'unichar'.
192 If no such element exists, one is created using the
193 default constructor of the Descriptor class.
195 Descriptor &operator [] (wchar_t unichar);
197 /** Computes and returns the number of memory bytes consumed by
198 this object, excluding the size of the Descriptors' user data.
199 @returns number of bytes
201 size_t computeMemoryConsumption() const;
204 std::vector<CharDesc> elements; // average size should be about 1.4
208 Descriptor *getDesc(Row *row,
209 const std::wstring &key,
210 std::wstring::size_type index,
212 bool callFoundPrefixCallback);
215 T *lambda; // user data associated with the empty string key
216 Row *firstRow; // must be created by operator new
217 bool userDataFromNew;
220 // Forbidden operations:
222 Trie &operator = (const Trie &);
227 } // namespace verbiste