Thread: TrieSnapshots?
View Single Post
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-20-2017 , 04:48   Re: TrieSnapshots?
Reply With Quote #3

It's from the dev version.

You have actually two ways to iterate over a trie:

- Snapshot: A basic API which duplicates the whole set of keys (only) into a list. Any changes to the original list will not affect the snapshot. It's an expensive operation but can be stored.
- Iterator: A contrario, Iterators are designed to be short-lived and not stored, and creating them is very cheap. Reading data from an iterator is just as fast as reading directly from the map. Any changes are forbidden (except updating value of existing key).

Snapshot:
Code:
#include <amxmodx>   public plugin_init() {     new Trie:list = TrieCreate();     {         TrieSetString(list, "adventure", "time!");         TrieSetString(list, "butterflies", "bees");         TrieSetString(list, "egg", "egg");     }       new Snapshot:keys = TrieSnapshotCreate(list);     {         new const length = TrieSnapshotLength(keys);         new buffer[32];           for (new const i = 0; i < length; ++i)         {             TrieSnapshotGetKey(keys, i, buffer, charmax(buffer));             server_print("buffer = %s", buffer);         }     }     TrieSnapshotDestroy(keys); }

Iterator:

Code:
#include <amxmodx> public plugin_init() {     new Trie:list = TrieCreate();     {         TrieSetString(list, "adventure", "time!");         TrieSetString(list, "butterflies", "bees");         TrieSetString(list, "hello", "world!");     }     new TrieIter:iter = TrieIterCreate(list);     {         new key[32], key_length;         new value[32], value_length;         while (!TrieIterEnded(iter))         {             key_length = TrieIterGetKey(iter, key, charsmax(key));             TrieIterGetString(iter, value, charsmax(value), value_length);             server_print("%s (%d) -> %s (%d)", key, key_length, value, value_length);             // Should throw an error             // TrieSetString(t, "monkey", "island");             // TrieDeleteKey(t, "full");             // TrieClear(t);             TrieIterNext(iter);         }     }     TrieIterDestroy(iter);     TrieDestroy(list); }
__________________
Arkshine is offline