Fixed requiring dependencies at the wrong time.
3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #ifndef _INCLUDE_SOURCEMOD_TEMPLATED_TRIE_H_
33 #define _INCLUDE_SOURCEMOD_TEMPLATED_TRIE_H_
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 */
48 * @brief Trie class for storing key/value pairs, based on double array tries.
51 * For full works cited and implementation overview, there is a big comment
52 * block at the bottom of this file.
61 * @brief Clears all set objects in the trie.
70 * @brief Removes a key from the trie.
72 * @param key Key to remove.
73 * @return True on success, false if key was never set.
75 bool remove(const char *key)
77 KTrieNode *node = internal_retrieve(key);
78 if (!node || !node->valset)
92 * @brief Retrieves a pointer to the object stored at a given key.
94 * @param key Key to retrieve.
95 * @return Pointer to object, or NULL if key was not found or not set.
97 K * retrieve(const char *key)
99 KTrieNode *node = internal_retrieve(key);
100 if (!node || !node->valset)
108 * @brief Inserts or updates the object stored at a key.
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.
114 bool replace(const char *key, const K & obj)
116 KTrieNode *prev_node = internal_retrieve(key);
119 return insert(key, obj);
122 if (prev_node->valset)
124 prev_node->value.~K();
127 new (&prev_node->value) K(obj);
133 * @brief Inserts an object at a key.
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.
140 bool insert(const char *key, const K & obj)
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 */
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.
156 if (m_empty != NULL && m_empty->valset)
163 m_empty = (KTrieNode *)malloc(sizeof(KTrieNode));
166 m_empty->valset = true;
167 new (&m_empty->value) K(obj);
174 /* Start traversing at the root node (1) */
177 /* Find where the next character is, then advance */
178 curidx = m_base[lastidx].idx;
179 basenode = &m_base[curidx];
180 curoffs = charval(*keyptr);
182 node = &m_base[curidx];
185 /* Check if this slot is supposed to be empty. If so, we need to handle CASES 1/2:
186 * Insertion without collisions
188 if ( (curidx > m_baseSize) || (node->mode == Node_Unused) )
190 if (curidx > m_baseSize)
196 node = &m_base[curidx];
198 node->parent = lastidx;
201 node->mode = Node_Arc;
205 node->idx = x_addstring(keyptr);
206 node->mode = Node_Term;
209 new (&node->value) K(obj);
215 else if (node->parent != lastidx)
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.
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.
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.
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;
242 if (outgoing_base + outgoing_limit > m_baseSize)
244 outgoing_limit = m_baseSize - outgoing_base;
247 for (unsigned int i=1; i<=outgoing_limit; i++,cur++)
249 if (cur->mode == Node_Unused || cur->parent != lastidx)
253 outgoing_list[outgoing_count++] = i;
255 outgoing_list[outgoing_count++] = curidx - outgoing_base;
257 /* Now we need to find all the arcs leaving our parent...
258 * Note: the inconsistency is the base of our parent.
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;
267 if (incoming_base + incoming_limit > m_baseSize)
269 incoming_limit = m_baseSize - incoming_base;
272 assert(incoming_limit > 0 && incoming_limit <= 255);
274 for (unsigned int i=1; i<=incoming_limit; i++,cur++)
276 if (cur->mode == Node_Arc || cur->mode == Node_Term)
278 if (cur->parent == node->parent)
280 incoming_list[incoming_count++] = i;
285 if (incoming_count < outgoing_count + 1)
287 unsigned int q = x_check_multi(incoming_list, incoming_count);
289 node = &m_base[curidx];
291 /* If we're incoming, we need to modify our parent */
292 m_base[node->parent].idx = q;
294 /* For each node in the "to move" list,
295 * Relocate the node's info to the new position.
297 unsigned int idx, newidx, oldidx;
298 for (unsigned int i=0; i<incoming_count; i++)
300 idx = incoming_list[i];
302 oldidx = incoming_base + idx;
303 if (oldidx == lastidx)
305 /* Important! Make sure we're not invalidating our sacred lastidx */
308 /* Fully copy the node */
309 memcpy(&m_base[newidx], &m_base[oldidx], sizeof(KTrieNode));
310 if (m_base[oldidx].valset)
312 new (&m_base[newidx].value) K(m_base[oldidx].value);
313 m_base[oldidx].value.~K();
315 assert(m_base[m_base[newidx].parent].mode == Node_Arc);
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)
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)
325 outgoing_limit = 255;
327 for (unsigned int j=1; j<=outgoing_limit; j++, check_base++)
329 if (check_base->parent == oldidx)
331 check_base->parent = newidx;
339 unsigned int q = x_check_multi(outgoing_list, outgoing_count);
341 node = &m_base[curidx];
343 /* If we're outgoing, we need to modify our own base */
344 m_base[lastidx].idx = q;
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.
351 /* For each node in the "to move" list,
352 * Relocate the node's info to the new position.
354 unsigned int idx, newidx, oldidx;
355 for (unsigned int i=0; i<outgoing_count; i++)
357 idx = outgoing_list[i];
359 oldidx = outgoing_base + idx;
360 if (oldidx == lastidx)
362 /* Important! Make sure we're not invalidating our sacred lastidx */
365 /* Fully copy the node */
366 memcpy(&m_base[newidx], &m_base[oldidx], sizeof(KTrieNode));
367 if (m_base[oldidx].valset)
369 new (&m_base[newidx].value) K(m_base[oldidx].value);
370 m_base[oldidx].value.~K();
372 assert(m_base[m_base[newidx].parent].mode == Node_Arc);
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)
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)
382 outgoing_limit = 255;
384 for (unsigned int j=1; j<=outgoing_limit; j++, check_base++)
386 if (check_base->parent == oldidx)
388 check_base->parent = newidx;
394 /* Take the invisible node and use it as our new node */
395 node = &m_base[q + outgoing_list[outgoing_count]];
398 /* We're finally done! */
399 node->parent = lastidx;
402 node->mode = Node_Arc;
406 node->idx = x_addstring(keyptr);
407 node->mode = Node_Term;
410 new (&node->value) K(obj);
418 /* See what's in the next node - special case if terminator! */
419 if (node->mode == Node_Term)
421 /* If we're a terminator, we need to handle CASE 3:
422 * Insertion when a terminating collision occurs
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)
431 new (&node->value) K(obj);
435 /* Same string. We can't insert. */
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.
446 bool oldvalset = node->valset;
449 oldvalue = node->value;
451 if (*term == *keyptr)
453 while (*term == *keyptr)
455 /* Find the next free slot in the check array.
456 * This is the "vector base" essentially
459 node = &m_base[curidx];
460 /* Point the node to the next new base */
462 node->mode = Node_Arc;
463 if (node->valset == true)
466 node->valset = false;
468 /* Advance the input stream and local variables */
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 */
480 else if (node->valset)
482 node->valset = false;
485 /* We're done inserting new pairs. If one of them is exhausted,
486 * we take special shortcuts.
488 if (*term == '\0') //EX: BADGERHOUSE added over B -> ADGER.
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.
494 node->valset = oldvalset;
497 new (&node->value) K(oldvalue);
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.
506 /* Get the new base and apply re-basing */
507 q = x_check(*keyptr);
508 node = &m_base[curidx];
511 node->mode = Node_Arc;
513 /* Finish the final node */
514 curidx = q + charval(*keyptr);
515 node = &m_base[curidx];
517 /* Optimize - don't add to string table if there's nothing more to eat */
520 node->mode = Node_Arc;
524 node->idx = x_addstring(keyptr);
525 node->mode = Node_Term;
527 node->parent = lastidx;
529 new (&node->value) K(obj);
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.
537 new (&node->value) K(obj);
539 /* Get the new base and apply re-basing */
541 node = &m_base[curidx];
544 node->mode = Node_Arc;
546 /* Finish the final node */
547 curidx = q + charval(*term);
548 node = &m_base[curidx];
550 /* Optimize - don't add to string table if there's nothing more to eat */
553 node->mode = Node_Arc;
557 node->idx = (term - m_stringtab); /* Already in the string table! */
558 node->mode = Node_Term;
560 node->parent = lastidx;
561 node->valset = oldvalset;
564 new (&node->value) K(oldvalue);
569 /* Finally, we have to create two new nodes instead of just one. */
570 node->mode = Node_Arc;
572 /* Get the new base and apply re-basing */
573 q = x_check2(*keyptr, *term);
574 node = &m_base[curidx];
579 /* Re-create the old terminated node */
580 curidx = q + charval(*term);
581 node = &m_base[curidx];
583 node->valset = oldvalset;
586 new (&node->value) K(oldvalue);
588 node->parent = lastidx;
591 node->mode = Node_Arc;
595 node->mode = Node_Term;
596 node->idx = (term - m_stringtab); /* Already in the string table! */
599 /* Create the new keyed input node */
600 curidx = q + charval(*keyptr);
601 node = &m_base[curidx];
604 new (&node->value) K(obj);
605 node->parent = lastidx;
608 node->mode = Node_Arc;
612 node->mode = Node_Term;
613 node->idx = x_addstring(keyptr);
624 assert(node->mode == Node_Arc);
628 } while (*keyptr != '\0');
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.
636 /* We have to be an Arc. If the last result was anything else, we would have returned a new
637 * production earlier.
639 assert(node->mode == Node_Arc);
644 new (&node->value) K(obj);
653 * @brief Iterates over the trie returning all known values.
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.
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.
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.
670 void bad_iterator(char *buffer,
673 void (*func)(KTrie *, const char *, K & obj, void *data))
675 bad_iterator_r(buffer, maxlength, 0, data, func, 1);
679 void bad_iterator_r(char *buffer,
683 void (*func)(KTrie *, const char *, K & obj, void *data),
687 unsigned int idx, limit, start;
690 start = m_base[root].idx;
692 /* Bound our limits */
693 if (start + limit > m_baseSize)
695 limit = m_baseSize - start;
698 /* Search for strings */
699 for (unsigned int i = 1; i <= limit; i++)
702 if (m_base[idx].mode == Node_Unused
703 || m_base[idx].parent != root)
708 if (m_base[idx].mode == Node_Arc)
710 if (buf_pos < maxlength - 1)
712 buffer[buf_pos++] = (char)i;
715 if (m_base[idx].valset)
717 buffer[buf_pos] = '\0';
718 func(this, buffer, m_base[idx].value, data);
721 bad_iterator_r(buffer,
730 else if (m_base[idx].mode == Node_Term
731 && m_base[idx].valset == true)
735 save_buf_pos = buf_pos;
736 if (buf_pos < maxlength - 1)
738 buffer[buf_pos++] = (char)i;
740 if (buf_pos < maxlength - 1)
744 term = &m_stringtab[m_base[idx].idx];
745 destlen = strlen(term);
747 j < destlen && j + buf_pos < maxlength - 1;
750 buffer[buf_pos + j] = term[j];
754 buffer[buf_pos] = '\0';
756 func(this, buffer, m_base[idx].value, data);
758 buf_pos = save_buf_pos;
765 m_base = (KTrieNode *)malloc(sizeof(KTrieNode) * (256 + 1));
766 m_stringtab = (char *)malloc(sizeof(char) * 256);
776 if (m_empty != NULL && m_empty->valset)
779 m_empty->valset = false;
786 void run_destructor(void (*dtor)(K * ptr))
788 for (size_t i = 0; i <= m_baseSize; i++)
790 if (m_base[i].valset)
792 dtor(&m_base[i].value);
793 m_base[i].valset = false;
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.
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.
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 */
820 KTrieNode *internal_retrieve(const char *key)
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 */
832 /* Start traversing at the root node */
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];
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)
847 else if (node->mode == Node_Term)
849 char *term = &m_stringtab[node->idx];
850 if (strcmp(keyptr, term) == 0)
860 } while (*keyptr != '\0');
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;
870 KTrieNode *new_base = (KTrieNode *)malloc((new_size + 1) * sizeof(KTrieNode));
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));
879 for (size_t i = 0; i <= m_baseSize; i++)
881 if (m_base[i].valset)
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();
891 m_baseSize = new_size;
895 inline unsigned char charval(char c)
897 return (unsigned char)c;
899 void internal_clear()
904 memset(m_base, 0, sizeof(KTrieNode) * (m_baseSize + 1));
905 memset(m_stringtab, 0, sizeof(char) * m_stSize);
907 /* Sentinel root node */
909 m_base[1].mode = Node_Arc;
910 m_base[1].parent = 1;
912 void run_destructors()
914 for (size_t i = 0; i <= m_baseSize; i++)
916 if (m_base[i].valset)
918 m_base[i].value.~K();
922 unsigned int x_addstring(const char *ptr)
924 size_t len = strlen(ptr) + 1;
926 if (m_tail + len >= m_stSize)
928 while (m_tail + len >= m_stSize)
932 m_stringtab = (char *)realloc(m_stringtab,m_stSize);
935 unsigned int tail = m_tail;
936 strcpy(&m_stringtab[tail], ptr);
941 unsigned int x_check(char c, unsigned int start=1)
943 unsigned char _c = charval(c);
944 unsigned int to_check = m_baseSize - _c;
945 for (unsigned int i=start; i<=to_check; i++)
947 if (m_base[i+_c].mode == Node_Unused)
955 return x_check(c, to_check+1);
957 unsigned int x_check2(char c1, char c2, unsigned int start=1)
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++)
964 if (m_base[i+_c1].mode == Node_Unused
965 && m_base[i+_c2].mode == Node_Unused)
973 return x_check2(c1, c2, to_check+1);
975 unsigned int x_check_multi(
976 unsigned int offsets[],
978 unsigned int start=1)
981 unsigned int to_check = m_baseSize;
982 unsigned int highest = 0;
984 for (unsigned int i=0; i<count; i++)
986 if (offsets[i] > highest)
988 highest = offsets[i];
994 for (unsigned int i=start; i<=to_check; i++)
997 for (unsigned int j=0; j<count; j++)
999 cur = &m_base[i+offsets[j]];
1000 if (cur->mode != Node_Unused)
1014 return x_check_multi(offsets, count, to_check+1);
1019 return (sizeof(KTrieNode) * (m_baseSize))
1021 + sizeof(KTrieNode);
1025 return m_numElements;
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 */
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)
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:
1046 * BAILOPAN, BAT, BACON, BACK
1047 * These transition as the follow production rules:
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.
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)
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:
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
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.
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).
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).
1102 #endif //_INCLUDE_SOURCEMOD_TEMPLATED_TRIE_H_