3030#include " tscore/List.h"
3131#include " tscore/Diags.h"
3232
33+ class TrieImpl
34+ {
35+ protected:
36+ inline static DbgCtl dbg_ctl_insert{" Trie::Insert" };
37+ inline static DbgCtl dbg_ctl_search{" Trie::Search" };
38+ };
39+
3340// Note that you should provide the class to use here, but we'll store
3441// pointers to such objects internally.
35- template <typename T> class Trie
42+ template <typename T> class Trie : private TrieImpl
3643{
3744public:
3845 Trie () { m_root.Clear (); }
@@ -84,7 +91,7 @@ template <typename T> class Trie
8491 ink_zero (children);
8592 }
8693
87- void Print (const char *debug_tag ) const ;
94+ void Print (const DbgCtl &dbg_ctl ) const ;
8895 inline Node *
8996 GetChild (char index) const
9097 {
@@ -142,9 +149,9 @@ Trie<T>::Insert(const char *key, T *value, int rank, int key_len /* = -1 */)
142149 int i = 0 ;
143150
144151 while (true ) {
145- if (is_debug_tag_set ( " Trie::Insert " )) {
146- Debug ( " Trie::Insert " , " Visiting Node..." );
147- curr_node->Print (" Trie::Insert " );
152+ if (is_dbg_ctl_enabled (dbg_ctl_insert )) {
153+ Dbg (dbg_ctl_insert , " Visiting Node..." );
154+ curr_node->Print (dbg_ctl_insert );
148155 }
149156
150157 if (i == key_len) {
@@ -154,7 +161,7 @@ Trie<T>::Insert(const char *key, T *value, int rank, int key_len /* = -1 */)
154161 next_node = curr_node->GetChild (key[i]);
155162 if (!next_node) {
156163 while (i < key_len) {
157- Debug ( " Trie::Insert " , " Creating child node for char %c (%d)" , key[i], key[i]);
164+ Dbg (dbg_ctl_insert , " Creating child node for char %c (%d)" , key[i], key[i]);
158165 curr_node = curr_node->AllocateChild (key[i]);
159166 ++i;
160167 }
@@ -165,15 +172,15 @@ Trie<T>::Insert(const char *key, T *value, int rank, int key_len /* = -1 */)
165172 }
166173
167174 if (curr_node->occupied ) {
168- Debug ( " Trie::Insert " , " Cannot insert duplicate!" );
175+ Dbg (dbg_ctl_insert , " Cannot insert duplicate!" );
169176 return false ;
170177 }
171178
172179 curr_node->occupied = true ;
173180 curr_node->value = value;
174181 curr_node->rank = rank;
175182 m_value_list.enqueue (curr_node->value );
176- Debug ( " Trie::Insert " , " inserted new element!" );
183+ Dbg (dbg_ctl_insert , " inserted new element!" );
177184 return true ;
178185}
179186
@@ -188,9 +195,9 @@ Trie<T>::Search(const char *key, int key_len /* = -1 */) const
188195 int i = 0 ;
189196
190197 while (curr_node) {
191- if (is_debug_tag_set ( " Trie::Search " )) {
192- Debug ( " Trie::Search " , " Visiting node..." );
193- curr_node->Print (" Trie::Search " );
198+ if (is_dbg_ctl_enabled (dbg_ctl_search )) {
199+ DbgPrint (dbg_ctl_search , " Visiting node..." );
200+ curr_node->Print (dbg_ctl_search );
194201 }
195202 if (curr_node->occupied ) {
196203 if (!found_node || curr_node->rank <= found_node->rank ) {
@@ -205,7 +212,7 @@ Trie<T>::Search(const char *key, int key_len /* = -1 */) const
205212 }
206213
207214 if (found_node) {
208- Debug ( " Trie::Search " , " Returning element with rank %d" , found_node->rank );
215+ Dbg (dbg_ctl_search , " Returning element with rank %d" , found_node->rank );
209216 return found_node->value ;
210217 }
211218
@@ -250,18 +257,18 @@ Trie<T>::Print() const
250257
251258template <typename T>
252259void
253- Trie<T>::Node::Print(const char *debug_tag ) const
260+ Trie<T>::Node::Print(const DbgCtl &dbg_ctl ) const
254261{
255262 if (occupied) {
256- Debug (debug_tag , " Node is occupied" );
257- Debug (debug_tag , " Node has rank %d" , rank);
263+ Dbg (dbg_ctl , " Node is occupied" );
264+ Dbg (dbg_ctl , " Node has rank %d" , rank);
258265 } else {
259- Debug (debug_tag , " Node is not occupied" );
266+ Dbg (dbg_ctl , " Node is not occupied" );
260267 }
261268
262269 for (int i = 0 ; i < N_NODE_CHILDREN; ++i) {
263270 if (GetChild (i)) {
264- Debug (debug_tag , " Node has child for char %c" , static_cast <char >(i));
271+ Dbg (dbg_ctl , " Node has child for char %c" , static_cast <char >(i));
265272 }
266273 }
267274}
0 commit comments