public/sm_trie_tpl.h
author Fyren <fyrenmoo@gmail.com>
Wed Apr 01 05:56:51 2009 -0700 (2009-04-01)
changeset 2692 126e2d41c2b3
parent 2471 194be299b700
child 4056 5d02b16b8d9e
permissions -rw-r--r--
Disable nextmap in Synergy and Dystopia (bug 3687, bug 3741; r=liam)
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_TEMPLATED_TRIE_H_
    33 #define _INCLUDE_SOURCEMOD_TEMPLATED_TRIE_H_
    34 
    35 #include <new>
    36 #include <string.h>
    37 #include <stdlib.h>
    38 #include <assert.h>
    39 
    40 enum NodeType
    41 {
    42 	Node_Unused = 0,		/* Node is not being used (sparse) */
    43 	Node_Arc,				/* Node is part of an arc and does not terminate */
    44 	Node_Term,				/* Node is a terminator */
    45 };
    46 
    47 /**
    48  * @brief Trie class for storing key/value pairs, based on double array tries.
    49  * @file sm_trie_tpl.h
    50  *
    51  * For full works cited and implementation overview, there is a big comment 
    52  * block at the bottom of this file.
    53  */
    54 
    55 template <typename K>
    56 class KTrie
    57 {
    58 	class KTrieNode;
    59 public:
    60 	/**
    61 	 * @brief Clears all set objects in the trie.
    62 	 */
    63 	void clear()
    64 	{
    65 		run_destructors();
    66 		internal_clear();
    67 	}
    68 
    69 	/**
    70 	 * @brief Removes a key from the trie.
    71 	 *
    72 	 * @param key		Key to remove.
    73 	 * @return			True on success, false if key was never set.
    74 	 */
    75 	bool remove(const char *key)
    76 	{
    77 		KTrieNode *node = internal_retrieve(key);
    78 		if (!node || !node->valset)
    79 		{
    80 			return false;
    81 		}
    82 
    83 		node->value.~K();
    84 		node->valset = false;
    85 
    86 		m_numElements--;
    87 
    88 		return true;
    89 	}
    90 
    91 	/**
    92 	 * @brief Retrieves a pointer to the object stored at a given key.
    93 	 *
    94 	 * @param key		Key to retrieve.
    95 	 * @return			Pointer to object, or NULL if key was not found or not set.
    96 	 */
    97 	K * retrieve(const char *key)
    98 	{
    99 		KTrieNode *node = internal_retrieve(key);
   100 		if (!node || !node->valset)
   101 		{
   102 			return NULL;
   103 		}
   104 		return &node->value;
   105 	}
   106 
   107 	/**
   108 	 * @brief Inserts or updates the object stored at a key.
   109 	 *
   110 	 * @param key		Key to update or insert.
   111 	 * @param obj		Object to store at the key.
   112 	 * @return			True on success, false on failure.
   113 	 */
   114 	bool replace(const char *key, const K & obj)
   115 	{
   116 		KTrieNode *prev_node = internal_retrieve(key);
   117 		if (!prev_node)
   118 		{
   119 			return insert(key, obj);
   120 		}
   121 
   122 		if (prev_node->valset)
   123 		{
   124 			prev_node->value.~K();
   125 		}
   126 
   127 		new (&prev_node->value) K(obj);
   128 
   129 		return true;
   130 	}
   131 
   132 	/**
   133 	 * @brief Inserts an object at a key.
   134 	 *
   135 	 * @param key		Key to insert at.
   136 	 * @param obj		Object to store at the key.
   137 	 * @return			True on success, false if the key is already set or 
   138 	 *					insertion otherwise failed.
   139 	 */
   140 	bool insert(const char *key, const K & obj)
   141 	{
   142 		unsigned int lastidx = 1;		/* the last node index */
   143 		unsigned int curidx;			/* current node index */
   144 		const char *keyptr = key;		/* input stream at current token */
   145 		KTrieNode *node = NULL;			/* current node being processed */
   146 		KTrieNode *basenode = NULL;		/* current base node being processed */
   147 		unsigned int q;					/* temporary var for x_check results */
   148 		unsigned int curoffs;			/* current offset */
   149 
   150 		/**
   151 		 * Empty strings are a special case, since there are no productions.  We could 
   152 		 * probably rework it to use BASE[0] but this hack is easier.
   153 		 */
   154 		if (*key == '\0')
   155 		{
   156 			if (m_empty != NULL && m_empty->valset)
   157 			{
   158 				return false;
   159 			}
   160 
   161 			if (m_empty == NULL)
   162 			{
   163 				m_empty = (KTrieNode *)malloc(sizeof(KTrieNode));
   164 			}
   165 
   166 			m_empty->valset = true;
   167 			new (&m_empty->value) K(obj);
   168 
   169 			m_numElements++;
   170 
   171 			return true;
   172 		}
   173 
   174 		/* Start traversing at the root node (1) */
   175 		do
   176 		{
   177 			/* Find where the next character is, then advance */
   178 			curidx = m_base[lastidx].idx;
   179 			basenode = &m_base[curidx];
   180 			curoffs = charval(*keyptr);
   181 			curidx += curoffs;
   182 			node = &m_base[curidx];
   183 			keyptr++;
   184 
   185 			/* Check if this slot is supposed to be empty.  If so, we need to handle CASES 1/2:
   186 			 * Insertion without collisions
   187 			 */
   188 			if ( (curidx > m_baseSize) || (node->mode == Node_Unused) )
   189 			{
   190 				if (curidx > m_baseSize)
   191 				{
   192 					if (!grow())
   193 					{
   194 						return false;
   195 					}
   196 					node = &m_base[curidx];
   197 				}
   198 				node->parent = lastidx;
   199 				if (*keyptr == '\0')
   200 				{
   201 					node->mode = Node_Arc;
   202 				}
   203 				else
   204 				{
   205 					node->idx = x_addstring(keyptr);
   206 					node->mode = Node_Term;
   207 				}
   208 				node->valset = true;
   209 				new (&node->value) K(obj);
   210 
   211 				m_numElements++;
   212 
   213 				return true;
   214 			}
   215 			else if (node->parent != lastidx)
   216 			{
   217 				/* Collision! We have to split up the tree here.  CASE 4:
   218 				 * Insertion when a new word is inserted with a collision.
   219 				 * NOTE: This is the hardest case to handle.  All below examples are based on:
   220 				 * BACHELOR, BADGE, inserting BABY.
   221 				 * The problematic production here is A -> B, where B is already being used.
   222 			     *
   223 				 * This process has to rotate one half of the 'A' arc.  We generate two lists:
   224 				 *  Outgoing Arcs - Anything leaving this 'A'
   225 				 *  Incoming Arcs - Anything going to this 'A'
   226 				 * Whichever list is smaller will be moved.  Note that this works because the intersection
   227 				 * affects both arc chains, and moving one will make the slot available to either.
   228 				 */
   229 				KTrieNode *cur;
   230 
   231 				/* Find every node arcing from the last node.
   232 				 * I.e. for BACHELOR, BADGE, BABY,
   233 				 * The arcs leaving A will be C and D, but our current node is B -> *.
   234 				 * Thus, we use the last index (A) to find the base for arcs leaving A.
   235 				 */
   236 				unsigned int outgoing_base = m_base[lastidx].idx;
   237 				unsigned int outgoing_list[256];
   238 				unsigned int outgoing_count = 0;	/* count the current index here */
   239 				cur = &m_base[outgoing_base] + 1;
   240 				unsigned int outgoing_limit = 255;
   241 
   242 				if (outgoing_base + outgoing_limit > m_baseSize)
   243 				{
   244 					outgoing_limit = m_baseSize - outgoing_base;
   245 				}
   246 
   247 				for (unsigned int i=1; i<=outgoing_limit; i++,cur++)
   248 				{
   249 					if (cur->mode == Node_Unused || cur->parent != lastidx)
   250 					{
   251 						continue;
   252 					}
   253 					outgoing_list[outgoing_count++] = i;
   254 				}
   255 				outgoing_list[outgoing_count++] = curidx - outgoing_base;
   256 
   257 				/* Now we need to find all the arcs leaving our parent...
   258 				 * Note: the inconsistency is the base of our parent.  
   259 				 */
   260 				assert(m_base[node->parent].mode == Node_Arc);
   261 				unsigned int incoming_list[256];
   262 				unsigned int incoming_base = m_base[node->parent].idx;
   263 				unsigned int incoming_count = 0;
   264 				unsigned int incoming_limit = 255;
   265 				cur = &m_base[incoming_base] + 1;
   266 
   267 				if (incoming_base + incoming_limit > m_baseSize)
   268 				{
   269 					incoming_limit = m_baseSize - incoming_base;
   270 				}
   271 
   272 				assert(incoming_limit > 0 && incoming_limit <= 255);
   273 
   274 				for (unsigned int i=1; i<=incoming_limit; i++,cur++)
   275 				{
   276 					if (cur->mode == Node_Arc || cur->mode == Node_Term)
   277 					{
   278 						if (cur->parent == node->parent)
   279 						{
   280 							incoming_list[incoming_count++] = i;
   281 						}
   282 					}
   283 				}
   284 
   285 				if (incoming_count < outgoing_count + 1)
   286 				{
   287 					unsigned int q = x_check_multi(incoming_list, incoming_count);
   288 
   289 					node = &m_base[curidx];
   290 
   291 					/* If we're incoming, we need to modify our parent */
   292 					m_base[node->parent].idx = q;
   293 
   294 					/* For each node in the "to move" list,
   295 					 * Relocate the node's info to the new position.
   296 					 */
   297 					unsigned int idx, newidx, oldidx;
   298 					for (unsigned int i=0; i<incoming_count; i++)
   299 					{
   300 						idx = incoming_list[i];
   301 						newidx = q + idx;
   302 						oldidx = incoming_base + idx;
   303 						if (oldidx == lastidx)
   304 						{
   305 							/* Important! Make sure we're not invalidating our sacred lastidx */
   306 							lastidx = newidx;
   307 						}
   308 						/* Fully copy the node */
   309 						memcpy(&m_base[newidx], &m_base[oldidx], sizeof(KTrieNode));
   310 						if (m_base[oldidx].valset)
   311 						{
   312 							new (&m_base[newidx].value) K(m_base[oldidx].value);
   313 							m_base[oldidx].value.~K();
   314 						}
   315 						assert(m_base[m_base[newidx].parent].mode == Node_Arc);
   316 						/* Erase old data */
   317 						memset(&m_base[oldidx], 0, sizeof(KTrieNode));
   318 						/* If we are not a terminator, we have children we must take care of */
   319 						if (m_base[newidx].mode == Node_Arc)
   320 						{
   321 							KTrieNode *check_base = &m_base[m_base[newidx].idx] + 1;
   322 							outgoing_limit = (m_base + m_baseSize + 1) - check_base;
   323 							if (outgoing_limit > 255)
   324 							{
   325 								outgoing_limit = 255;
   326 							}
   327 							for (unsigned int j=1; j<=outgoing_limit; j++, check_base++)
   328 							{
   329 								if (check_base->parent == oldidx)
   330 								{
   331 									check_base->parent = newidx;
   332 								}
   333 							}
   334 						}
   335 					}
   336 				}
   337 				else
   338 				{
   339 					unsigned int q = x_check_multi(outgoing_list, outgoing_count);
   340 
   341 					node = &m_base[curidx];
   342 
   343 					/* If we're outgoing, we need to modify our own base */
   344 					m_base[lastidx].idx = q;
   345 
   346 					/* Take the last index (curidx) out of the list.  Technically we are not moving this,
   347 					 * since it's already being used by something else.  
   348 					 */
   349 					outgoing_count--;
   350 
   351 					/* For each node in the "to move" list,
   352 					 * Relocate the node's info to the new position.
   353 					 */
   354 					unsigned int idx, newidx, oldidx;
   355 					for (unsigned int i=0; i<outgoing_count; i++)
   356 					{
   357 						idx = outgoing_list[i];
   358 						newidx = q + idx;
   359 						oldidx = outgoing_base + idx;
   360 						if (oldidx == lastidx)
   361 						{
   362 							/* Important! Make sure we're not invalidating our sacred lastidx */
   363 							lastidx = newidx;
   364 						}
   365 						/* Fully copy the node */
   366 						memcpy(&m_base[newidx], &m_base[oldidx], sizeof(KTrieNode));
   367 						if (m_base[oldidx].valset)
   368 						{
   369 							new (&m_base[newidx].value) K(m_base[oldidx].value);
   370 							m_base[oldidx].value.~K();
   371 						}
   372 						assert(m_base[m_base[newidx].parent].mode == Node_Arc);
   373 						/* Erase old data */
   374 						memset(&m_base[oldidx], 0, sizeof(KTrieNode));
   375 						/* If we are not a terminator, we have children we must take care of */
   376 						if (m_base[newidx].mode == Node_Arc)
   377 						{
   378 							KTrieNode *check_base = &m_base[m_base[newidx].idx] + 1;
   379 							outgoing_limit = (m_base + m_baseSize + 1) - check_base;
   380 							if (outgoing_limit > 255)
   381 							{
   382 								outgoing_limit = 255;
   383 							}
   384 							for (unsigned int j=1; j<=outgoing_limit; j++, check_base++)
   385 							{
   386 								if (check_base->parent == oldidx)
   387 								{
   388 									check_base->parent = newidx;
   389 								}
   390 							}
   391 						}
   392 					}
   393 
   394 					/* Take the invisible node and use it as our new node */
   395 					node = &m_base[q + outgoing_list[outgoing_count]];
   396 				}
   397 
   398 				/* We're finally done! */
   399 				node->parent = lastidx;
   400 				if (*keyptr == '\0')
   401 				{
   402 					node->mode = Node_Arc;
   403 				}
   404 				else
   405 				{
   406 					node->idx = x_addstring(keyptr);
   407 					node->mode = Node_Term;
   408 				}
   409 				node->valset = true;
   410 				new (&node->value) K(obj);
   411 
   412 				m_numElements++;
   413 
   414 				return true;
   415 			}
   416 			else
   417 			{
   418 				/* See what's in the next node - special case if terminator! */
   419 				if (node->mode == Node_Term)
   420 				{
   421 					/* If we're a terminator, we need to handle CASE 3:
   422 					 * Insertion when a terminating collision occurs
   423 					 */
   424 					char *term = &m_stringtab[node->idx];
   425 					/* Do an initial browsing to make sure they're not the same string */
   426 					if (strcmp(keyptr, term) == 0)
   427 					{
   428 						if (!node->valset)
   429 						{
   430 							node->valset = true;
   431 							new (&node->value) K(obj);
   432 							m_numElements++;
   433 							return true;
   434 						}
   435 						/* Same string.  We can't insert. */
   436 						return false;
   437 					}
   438 					/* For each matching character pair, we need to disband the terminator.
   439 					 * This splits the similar prefix into a single arc path.
   440 					 * First, save the old values so we can move them to a new node.
   441 					 * Next, for each loop:
   442 					 *  Take the current (invalid) node, and point it to the next arc base.
   443 					 *  Set the current node to the node at the next arc.
   444 					 */
   445 					K oldvalue;
   446 					bool oldvalset = node->valset;
   447 					if (oldvalset)
   448 					{
   449 						oldvalue = node->value;
   450 					}
   451 					if (*term == *keyptr)
   452 					{
   453 						while (*term == *keyptr)
   454 						{
   455 							/* Find the next free slot in the check array.
   456 							 * This is the "vector base" essentially
   457 							 */
   458 							q = x_check(*term);
   459 							node = &m_base[curidx];
   460 							/* Point the node to the next new base */
   461 							node->idx = q;
   462 							node->mode = Node_Arc;
   463 							if (node->valset == true)
   464 							{
   465 								node->value.~K();
   466 								node->valset = false;
   467 							}
   468 							/* Advance the input stream and local variables */
   469 							lastidx = curidx;
   470 							curidx = q + charval(*term);
   471 							node = &m_base[curidx];
   472 							/* Make sure the new current node has its parent set. */
   473 							node->parent = lastidx;
   474 							node->mode = Node_Arc;	/* Just in case we run x_check again */
   475 							*term = '\0';	/* Unmark the string table here */
   476 							term++;
   477 							keyptr++;
   478 						}
   479 					}
   480 					else if (node->valset)
   481 					{
   482 						node->valset = false;
   483 						node->value.~K();
   484 					}
   485 					/* We're done inserting new pairs.  If one of them is exhausted,
   486 					 * we take special shortcuts.
   487 					 */
   488 					if (*term == '\0')				//EX: BADGERHOUSE added over B -> ADGER.  
   489 					{
   490 						/* First backpatch the current node - it ends the newly split terminator.
   491 						 * In the example, this would mean the node is the production from R -> ?
   492 						 * This node ends the old BADGER, so we set it here.
   493 						 */
   494 						node->valset = oldvalset;
   495 						if (node->valset)
   496 						{
   497 							new (&node->value) K(oldvalue);
   498 						}
   499 
   500 						/* The terminator was split up, but pieces of keyptr remain.
   501 						 * We need to generate a new production, in this example, R -> H,
   502 						 * with H being a terminator to OUSE.  Thus we get:
   503 						 * B,A,D,G,E,R*,H*->OUSE (* = value set).
   504 						 * NOTE: parent was last set at the end of the while loop.
   505 						 */
   506 						/* Get the new base and apply re-basing */
   507 						q = x_check(*keyptr);
   508 						node = &m_base[curidx];
   509 
   510 						node->idx = q;
   511 						node->mode = Node_Arc;
   512 						lastidx = curidx;
   513 						/* Finish the final node */
   514 						curidx = q + charval(*keyptr);
   515 						node = &m_base[curidx];
   516 						keyptr++;
   517 						/* Optimize - don't add to string table if there's nothing more to eat */
   518 						if (*keyptr == '\0')
   519 						{
   520 							node->mode = Node_Arc;
   521 						}
   522 						else
   523 						{
   524 							node->idx = x_addstring(keyptr);
   525 							node->mode = Node_Term;
   526 						}
   527 						node->parent = lastidx;
   528 						node->valset = true;
   529 						new (&node->value) K(obj);
   530 					}
   531 					else if (*keyptr == '\0')
   532 					{	//EX: BADGER added over B -> ADGERHOUSE
   533 						/* First backpatch the current node - it ends newly split input string.
   534 						 * This is the exact opposite of the above procedure.
   535 						 */
   536 						node->valset = true;
   537 						new (&node->value) K(obj);
   538 
   539 						/* Get the new base and apply re-basing */
   540 						q = x_check(*term);
   541 						node = &m_base[curidx];
   542 
   543 						node->idx = q;
   544 						node->mode = Node_Arc;
   545 						lastidx = curidx;
   546 						/* Finish the final node */
   547 						curidx = q + charval(*term);
   548 						node = &m_base[curidx];
   549 						term++;
   550 						/* Optimize - don't add to string table if there's nothing more to eat */
   551 						if (*term == '\0')
   552 						{
   553 							node->mode = Node_Arc;
   554 						}
   555 						else
   556 						{
   557 							node->idx = (term - m_stringtab); /* Already in the string table! */
   558 							node->mode = Node_Term;
   559 						}
   560 						node->parent = lastidx;
   561 						node->valset = oldvalset;
   562 						if (node->valset)
   563 						{
   564 							new (&node->value) K(oldvalue);
   565 						}
   566 					}
   567 					else
   568 					{
   569 						/* Finally, we have to create two new nodes instead of just one. */
   570 						node->mode = Node_Arc;
   571 
   572 						/* Get the new base and apply re-basing */
   573 						q = x_check2(*keyptr, *term);
   574 						node = &m_base[curidx];
   575 
   576 						node->idx = q;
   577 						lastidx = curidx;
   578 
   579 						/* Re-create the old terminated node */
   580 						curidx = q + charval(*term);
   581 						node = &m_base[curidx];
   582 						term++;
   583 						node->valset = oldvalset;
   584 						if (node->valset)
   585 						{
   586 							new (&node->value) K(oldvalue);
   587 						}
   588 						node->parent = lastidx;
   589 						if (*term == '\0')
   590 						{
   591 							node->mode = Node_Arc;
   592 						}
   593 						else
   594 						{
   595 							node->mode = Node_Term;
   596 							node->idx = (term - m_stringtab); /* Already in the string table! */
   597 						}
   598 
   599 						/* Create the new keyed input node */
   600 						curidx = q + charval(*keyptr);
   601 						node = &m_base[curidx];
   602 						keyptr++;
   603 						node->valset = true;
   604 						new (&node->value) K(obj);
   605 						node->parent = lastidx;
   606 						if (*keyptr == '\0')
   607 						{
   608 							node->mode = Node_Arc;
   609 						}
   610 						else
   611 						{
   612 							node->mode = Node_Term;
   613 							node->idx = x_addstring(keyptr);
   614 						}
   615 					}
   616 
   617 					m_numElements++;
   618 
   619 					/* Phew! */
   620 					return true;
   621 				}
   622 				else
   623 				{
   624 					assert(node->mode == Node_Arc);
   625 				}
   626 			}
   627 			lastidx = curidx;
   628 		} while (*keyptr != '\0');
   629 
   630 		assert(node);
   631 
   632 		/* If we've exhausted the string and we have a valid reached node,
   633 		 * the production rule already existed.  Make sure it's valid to set first.
   634 		 */
   635 
   636 		/* We have to be an Arc.  If the last result was anything else, we would have returned a new 
   637 		 * production earlier.
   638 		 */
   639 		assert(node->mode == Node_Arc);
   640 
   641 		if (!node->valset)
   642 		{
   643 			node->valset = true;
   644 			new (&node->value) K(obj);
   645 			m_numElements++;
   646 			return true;
   647 		}
   648 
   649 		return false;
   650 	}
   651 
   652 	/** 
   653 	 * @brief Iterates over the trie returning all known values.  
   654 	 * 
   655 	 * Note: This function is for debugging.  Do not use it as a 
   656 	 * production iterator since it's inefficient.  Iteration is 
   657 	 * guaranteed to be sorted ascendingly.
   658 	 *
   659 	 * The callback function takes:
   660 	 *  (KTrie)			- Pointer to this Trie
   661 	 *  (const char *)	- String containing key name.
   662 	 *  (K &)			- By-reference object at the key.
   663 	 *  (data)			- User pointer.
   664 	 *
   665 	 * @param buffer			Buffer to use as a key name cache.
   666 	 * @param maxlength			Maximum length of the key name buffer.
   667 	 * @param data				User pointer for passing to the iterator.
   668 	 * @param func				Iterator callback function.
   669 	 */
   670 	void bad_iterator(char *buffer, 
   671 		size_t maxlength, 
   672 		void *data,
   673 		void (*func)(KTrie *, const char *, K & obj, void *data))
   674 	{
   675 		bad_iterator_r(buffer, maxlength, 0, data, func, 1);
   676 	}
   677 
   678 private:
   679 	void bad_iterator_r(char *buffer, 
   680 		size_t maxlength, 
   681 		size_t buf_pos,
   682 		void *data,
   683 		void (*func)(KTrie *, const char *, K & obj, void *data),
   684 		unsigned int root)
   685 	{
   686 		char *term;
   687 		unsigned int idx, limit, start;
   688 
   689 		limit = 255;
   690 		start = m_base[root].idx;
   691 
   692 		/* Bound our limits */
   693 		if (start + limit > m_baseSize)
   694 		{
   695 			limit = m_baseSize - start;
   696 		}
   697 
   698 		/* Search for strings */
   699 		for (unsigned int i = 1; i <= limit; i++)
   700 		{
   701 			idx = start + i;
   702 			if (m_base[idx].mode == Node_Unused
   703 				|| m_base[idx].parent != root)
   704 			{
   705 				continue;
   706 			}
   707 
   708 			if (m_base[idx].mode == Node_Arc)
   709 			{
   710 				if (buf_pos < maxlength - 1)
   711 				{
   712 					buffer[buf_pos++] = (char)i;
   713 				}
   714 
   715 				if (m_base[idx].valset)
   716 				{
   717 					buffer[buf_pos] = '\0';
   718 					func(this, buffer, m_base[idx].value, data);
   719 				}
   720 
   721 				bad_iterator_r(buffer,
   722 					maxlength,
   723 					buf_pos,
   724 					data,
   725 					func,
   726 					idx);
   727 
   728 				buf_pos--;
   729 			}
   730 			else if (m_base[idx].mode == Node_Term 
   731 					 && m_base[idx].valset == true)
   732 			{
   733 				size_t save_buf_pos;
   734 
   735 				save_buf_pos = buf_pos;
   736 				if (buf_pos < maxlength - 1)
   737 				{
   738 					buffer[buf_pos++] = (char)i;
   739 				}
   740 				if (buf_pos < maxlength - 1)
   741 				{
   742 					size_t destlen, j;
   743 
   744 					term = &m_stringtab[m_base[idx].idx];
   745 					destlen = strlen(term);
   746 					for (j = 0; 
   747 						 j < destlen && j + buf_pos < maxlength - 1;
   748 						 j++)
   749 					{
   750 						buffer[buf_pos + j] = term[j];
   751 					}
   752 					buf_pos += j;
   753 				}
   754 				buffer[buf_pos] = '\0';
   755 
   756 				func(this, buffer, m_base[idx].value, data);
   757 				
   758 				buf_pos = save_buf_pos;
   759 			}
   760 		}
   761 	}
   762 public:
   763 	KTrie()
   764 	{
   765 		m_base = (KTrieNode *)malloc(sizeof(KTrieNode) * (256 + 1));
   766 		m_stringtab = (char *)malloc(sizeof(char) * 256);
   767 		m_baseSize = 256;
   768 		m_stSize = 256;
   769 		m_empty = NULL;
   770 		m_numElements = 0;
   771 
   772 		internal_clear();
   773 	}
   774 	~KTrie()
   775 	{
   776 		if (m_empty != NULL && m_empty->valset)
   777 		{
   778 			m_empty->value.~K();
   779 			m_empty->valset = false;
   780 		}
   781 		free(m_empty);
   782 		run_destructors();
   783 		free(m_base);
   784 		free(m_stringtab);
   785 	}
   786 	void run_destructor(void (*dtor)(K * ptr))
   787 	{
   788 		for (size_t i = 0; i <= m_baseSize; i++)
   789 		{
   790 			if (m_base[i].valset)
   791 			{
   792 				dtor(&m_base[i].value);
   793 				m_base[i].valset = false;
   794 			}
   795 		}
   796 	}
   797 private:
   798 	class KTrieNode
   799 	{
   800 		friend class KTrie;
   801 	private:
   802 		/**
   803 		 * For Node_Arc, this index stores the 'base' offset to the next arc chain.
   804 		 *   I.e. to jump from this arc to character C, it will be at base[idx+C].
   805 		 * For Node_Term, this is an index into the string table.
   806 		 */
   807 		unsigned int idx;	
   808 
   809 		/**
   810 		 * This contains the prior arc that we must have come from.
   811 		 * For example, if arc 63 has a base jump of index 12, and we want to see if
   812 		 * there is a valid character C, the parent of 12+C must be 63.
   813 		 */
   814 		unsigned int parent;
   815 		K value;				/* Value associated with this node */
   816 		NodeType mode;			/* Current usage type of the node */
   817 		bool valset;			/* Whether or not a value is set */
   818 	};
   819 private:
   820 	KTrieNode *internal_retrieve(const char *key)
   821 	{
   822 		unsigned int lastidx = 1;		/* the last node index */
   823 		unsigned int curidx;			/* current node index */
   824 		const char *keyptr = key;		/* input stream at current token */
   825 		KTrieNode *node = NULL;			/* current node being processed */
   826 
   827 		if (!*key)
   828 		{
   829 			return m_empty;
   830 		}
   831 
   832 		/* Start traversing at the root node */
   833 		do
   834 		{
   835 			/* Find where the next character is, then advance */
   836 			curidx = m_base[lastidx].idx;
   837 			node = &m_base[curidx];
   838 			curidx += charval(*keyptr);
   839 			node = &m_base[curidx];
   840 			keyptr++;
   841 
   842 			/* Check if this slot is supposed to be empty or is a collision */
   843 			if ((curidx > m_baseSize) || node->mode == Node_Unused || node->parent != lastidx)
   844 			{
   845 				return NULL;
   846 			}
   847 			else if (node->mode == Node_Term)
   848 			{
   849 				char *term = &m_stringtab[node->idx];
   850 				if (strcmp(keyptr, term) == 0)
   851 				{
   852 					break;
   853 				}
   854 				else
   855 				{
   856 					return NULL;
   857 				}
   858 			}
   859 			lastidx = curidx;
   860 		} while (*keyptr != '\0');
   861 
   862 		return node;
   863 	}
   864 	bool grow()
   865 	{
   866 		/* The current # of nodes in the tree is trie->baseSize + 1 */
   867 		unsigned int cur_size = m_baseSize;
   868 		unsigned int new_size = cur_size * 2;
   869 
   870 		KTrieNode *new_base = (KTrieNode *)malloc((new_size + 1) * sizeof(KTrieNode));
   871 		if (!new_base)
   872 		{
   873 			return false;
   874 		}
   875 
   876 		memcpy(new_base, m_base, sizeof(KTrieNode) * (m_baseSize + 1));
   877 		memset(&new_base[cur_size + 1], 0, (new_size - cur_size) * sizeof(KTrieNode));
   878 
   879 		for (size_t i = 0; i <= m_baseSize; i++)
   880 		{
   881 			if (m_base[i].valset)
   882 			{
   883 				/* Placement construct+copy the object, then placement destroy the old. */
   884 				new (&new_base[i].value) K(m_base[i].value);
   885 				m_base[i].value.~K();
   886 			}
   887 		}
   888 
   889 		free(m_base);
   890 		m_base = new_base;
   891 		m_baseSize = new_size;
   892 
   893 		return true;
   894 	}
   895 	inline unsigned char charval(char c)
   896 	{
   897 		return (unsigned char)c;
   898 	}
   899 	void internal_clear()
   900 	{
   901 		m_tail = 0;
   902 		m_numElements = 0;
   903 
   904 		memset(m_base, 0, sizeof(KTrieNode) * (m_baseSize + 1));
   905 		memset(m_stringtab, 0, sizeof(char) * m_stSize);
   906 
   907 		/* Sentinel root node */
   908 		m_base[1].idx = 1;
   909 		m_base[1].mode = Node_Arc;
   910 		m_base[1].parent = 1;
   911 	}
   912 	void run_destructors()
   913 	{
   914 		for (size_t i = 0; i <= m_baseSize; i++)
   915 		{
   916 			if (m_base[i].valset)
   917 			{
   918 				m_base[i].value.~K();
   919 			}
   920 		}
   921 	}
   922 	unsigned int x_addstring(const char *ptr)
   923 	{
   924 		size_t len = strlen(ptr) + 1;
   925 
   926 		if (m_tail + len >= m_stSize)
   927 		{		
   928 			while (m_tail + len >= m_stSize)
   929 			{
   930 				m_stSize *= 2;
   931 			}
   932 			m_stringtab = (char *)realloc(m_stringtab,m_stSize);
   933 		}
   934 
   935 		unsigned int tail = m_tail;
   936 		strcpy(&m_stringtab[tail], ptr);
   937 		m_tail += len;
   938 
   939 		return tail;
   940 	}
   941 	unsigned int x_check(char c, unsigned int start=1)
   942 	{
   943 		unsigned char _c = charval(c);
   944 		unsigned int to_check = m_baseSize - _c;
   945 		for (unsigned int i=start; i<=to_check; i++)
   946 		{
   947 			if (m_base[i+_c].mode == Node_Unused)
   948 			{
   949 				return i;
   950 			}
   951 		}
   952 
   953 		grow();
   954 
   955 		return x_check(c, to_check+1);
   956 	}
   957 	unsigned int x_check2(char c1, char c2, unsigned int start=1)
   958 	{
   959 		unsigned char _c1 = charval(c1);
   960 		unsigned char _c2 = charval(c2);
   961 		unsigned int to_check = m_baseSize - (_c1 > _c2 ? _c1 : _c2);
   962 		for (unsigned int i=start; i<=to_check; i++)
   963 		{
   964 			if (m_base[i+_c1].mode == Node_Unused
   965 				&& m_base[i+_c2].mode == Node_Unused)
   966 			{
   967 				return i;
   968 			}
   969 		}
   970 
   971 		grow();
   972 
   973 		return x_check2(c1, c2, to_check+1);
   974 	}
   975 	unsigned int x_check_multi(
   976 		unsigned int offsets[], 
   977 		unsigned int count,
   978 		unsigned int start=1)
   979 	{
   980 		KTrieNode *cur;
   981 		unsigned int to_check = m_baseSize;
   982 		unsigned int highest = 0;
   983 
   984 		for (unsigned int i=0; i<count; i++)
   985 		{
   986 			if (offsets[i] > highest)
   987 			{
   988 				highest = offsets[i];
   989 			}
   990 		}
   991 
   992 		to_check -= highest;
   993 
   994 		for (unsigned int i=start; i<=to_check; i++)
   995 		{
   996 			bool okay = true;
   997 			for (unsigned int j=0; j<count; j++)
   998 			{
   999 				cur = &m_base[i+offsets[j]];
  1000 				if (cur->mode != Node_Unused)
  1001 				{
  1002 					okay = false;
  1003 					break;
  1004 				}
  1005 			}
  1006 			if (okay)
  1007 			{
  1008 				return i;
  1009 			}
  1010 		}
  1011 
  1012 		grow();
  1013 
  1014 		return x_check_multi(offsets, count, to_check+1);
  1015 	}
  1016 public:
  1017 	size_t mem_usage()
  1018 	{
  1019 		return (sizeof(KTrieNode) * (m_baseSize))
  1020 			+ m_stSize
  1021 			+ sizeof(KTrieNode);
  1022 	}
  1023 	size_t size()
  1024 	{
  1025 		return m_numElements;
  1026 	}
  1027 private:
  1028 	KTrieNode *m_base;			/* Base array for the sparse tables */
  1029 	KTrieNode *m_empty;			/* Special case for empty strings */
  1030 	char *m_stringtab;			/* String table pointer */
  1031 	unsigned int m_baseSize;	/* Size of the base array, in members */
  1032 	unsigned int m_stSize;		/* Size of the string table, in bytes */
  1033 	unsigned int m_tail;		/* Current unused offset into the string table */
  1034 	size_t m_numElements;		/* Number of elements in use */
  1035 };
  1036 
  1037 /**
  1038  * Double Array Trie algorithm, based on:
  1039  * An Efficient Implementation of Trie Structures, by
  1040  *  Jun-ichi Aoe and Katsushi Maromoto, and Takashi Sato
  1041  * from Software - Practice and Experience, Vol. 22(9), 695-721 (September 1992)
  1042  *
  1043  *  A Trie is a simple data structure which stores strings as DFAs, with each 
  1044  * transition state being a string entry.  For example, observe the following strings:
  1045  *
  1046  * BAILOPAN, BAT, BACON, BACK
  1047  *  These transition as the follow production rules:
  1048  *  B -> ...                  B
  1049  *       A -> ...             BA
  1050  *            I -> ...        BAI
  1051  *                 LOPAN      BAILOPAN
  1052  *            T -> ...        BAT
  1053  *            C ->            BAC
  1054  *                 O -> ...   BACO
  1055  *                      N     BACON
  1056  *                 K          BACK
  1057  *
  1058  *  The standard implementation for this - using lists - gives a slow linear lookup, somewhere between
  1059  * O(N+M) or O(log n).  A faster implementation is proposed in the paper above, which is based on compacting
  1060  * the transition states into two arrays.  In the paper's implementation, two arrays are used, and thus it is
  1061  * called the "Double Array" algorithm.  However, the CHECK array's size is maintained the same as BASE, 
  1062  * so they can be combined into one structure.  The array seems complex at first, but is very simple: it is a
  1063  * tree structure flattened out into a single vector.  I am calling this implementation the Flat Array Trie.
  1064  *
  1065  *  BASE[] is an array where each member is a node in the Trie.  The node can either be UNUSED (empty), an ARC
  1066  * (containing an offset to the next set of ARCs), or a TERMINATOR (contains the rest of a string).
  1067  * Each node has an index which must be interpreted based on the node type.  If the node is a TERMINATOR, then the
  1068  * index is an index into a string table, to find the rest of the string.  
  1069  *  If the node is an ARC, the index is another index into BASE.  For each possible token that can follow the
  1070  * current token, the value of those tokens can be added to the index given in the ARC.  Thus, given a current
  1071  * position and the next desired token, the current arc will jump to another arc which can contain either:
  1072  *   1) An invalid production (collision, no entry exists)
  1073  *   2) An empty production (no entry exists)
  1074  *   3) Another arc label (the string ends here or continues into more productions)
  1075  *   4) A TERMINATOR (the string ends here and contains an unused set of productions)
  1076  * 
  1077  *  So, given current offset N (starting at N=1), jumping to token C means the next offset will be:
  1078  *      offs = BASE[n] + C
  1079  *  Thus, the next node will be at:
  1080  *      BASE[BASE[n] + C]
  1081  * 
  1082  *  This allows each ARC to specify the base offset for any of its ARC children, like a tree.  Each node specifies
  1083  * its parent ARC -- so if an invalid offset is specified, the parent will not match, and thus no such derived 
  1084  * string exists.
  1085  *
  1086  *  This means that arrays can be laid out "sparsely," maximizing their usage.  Note that N need not be related to
  1087  * the range of tokens (1-256).  I.e., a base index does not have to be at 1, 256, 512, et cetera.  This is because
  1088  * insertion comes with a small deal of complexity.  To insert a new set of tokens T, the algorithm finds a new 
  1089  * BASE index N such that BASE[N+T[i]] is unused for each T[i].  Thus, indirection is not necessarily linear; 
  1090  * traversing a chain of ARC nodes can _and will_ jump around BASE.
  1091  *
  1092  *  Of course, given this level of flexibility in the array organization, there are collisions.  This is largely 
  1093  * where insertions become slow, as the old chain must be relocated before the new one is used.  Relocation means 
  1094  * finding one or more new base indexes, and this means traversing BASE until an acceptable index is found, such 
  1095  * that each offset is unused (see description in previous paragraph).
  1096  *
  1097  *  However, it is not insertion time we are concerned about.  The "trie" name comes from reTRIEval.  We are only
  1098  * concerned with lookup and deletion.  Both lookup and deletion are O(k), where k is relative to the length of the 
  1099  * input string.  Note that it is best case O(1) and worst case O(k).  Deleting the entire trie is always O(1).
  1100  */
  1101 
  1102 #endif //_INCLUDE_SOURCEMOD_TEMPLATED_TRIE_H_