Introduce Trie Iterators (#413)

* TrieIter: Add possibility to obtain a new'd HashTable iterator

* TrieIter: Add CellTrieIter and storage

* TrieIter: Implement TrieIterCreate

* TrieIter: Implement TrieIterEnded

* TrieIter: Implement TrieIterMore

* TrieIter: Implement TrieIterGetKey

* TrieIter: Implement TrieIterGetSize

* TrieIter: Implement TrieIterGetCell

* TrieIter: Implement TrieIterGetString

* TrieIter: Implement TrieIterGetArray

* TrieIter: Implement TrieIterDestroy

* TrieIter: Invalidate any mutating change that is key addition or key removal

* TrieIter: Clean up the handles at map change

* TrieITer; Add iter tests to trietest.sma

* TrieIter: Fix linux compilation

* TrieIter: Rename TrieIterMore to TrieIterNext

* TrieIter: Adjust documentation

* TrieITer; Adjust trietest.sma

* TrieIter: Create a custom StringHashMap class instead

+ used a copy of |iterator| instead of dynamic allocation
+ initialized vars directly in constructor
+ added a nested iteration test
This commit is contained in:
Vincent Herbet
2017-08-01 15:05:27 +02:00
committed by GitHub
parent 304e992942
commit 1dc1f1b9c4
6 changed files with 743 additions and 5 deletions

View File

@ -27,6 +27,21 @@ enum Trie
Invalid_Trie = 0
};
/**
* Hash map iterator tag declaration
*
* @note The word "Trie" in this API is historical. As of AMX Mod X 1.8.3,
* tries have been internally replaced with hash tables, which have O(1)
* insertion time instead of O(n).
* @note Plugins are responsible for freeing all TrieIter handles they acquire.
* Failing to free handles will result in the plugin and AMXX leaking
* memory.
*/
enum TrieIter
{
Invalid_TrieIter = 0
}
/**
* Hash map snapshot tag declaration
*
@ -269,3 +284,129 @@ native TrieSnapshotGetKey(Snapshot:handle, index, buffer[], maxlength);
* @return 1 on success, 0 if an invalid handle was passed in
*/
native TrieSnapshotDestroy(&Snapshot:handle);
/**
* Creates an iterator for a map. It provides iterative read-only access to the
* maps contents.
*
* @note Removing or adding keys to the underlying map will invalidate all its
* iterators. Updating values of existing keys is allowed and the changes
* will be immediately reflected in the iterator.
* @note 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.
* @note Just like in snapshots the keys are not sorted.
*
* @return New iterator handle, which must be freed via TrieIterDestroy().
* @error Invalid Handle
*/
native TrieIter:TrieIterCreate(Trie:handle);
/**
* Returns if the iterator has reached its end and no more data can be read.
*
* @param handle Iterator handle
*
* @return True if iterator has reached the end, false otherwise
* @error Invalid Handle
* Iterator has been closed (underlying map destroyed)
* Iterator is outdated
*/
native bool:TrieIterEnded(TrieIter:handle);
/**
* Advances the iterator to the next key/value pair if one is available.
*
* @param handle Iterator handle
*
* @error Invalid Handle
* Iterator has been closed (underlying map destroyed)
* Iterator is outdated
*/
native TrieIterNext(TrieIter:handle);
/**
* Retrieves the key the iterator currently points to.
*
* @param handle Iterator handle.
* @param key Buffer to store the current key in.
* @param outputsize Maximum size of string buffer.
*
* @return Nnumber of bytes written to the buffer
* @error Invalid handle
* Iterator has been closed (underlying map destroyed)
* Iterator is outdated
*/
native TrieIterGetKey(TrieIter:handle, key[], outputsize);
/**
* Retrieves the number of elements in the underlying map.
*
* @note When used on a valid iterator this is exactly the same as calling TrieGetSize on the map directly.
*
* @param handle Iterator handle
*
* @return Number of elements in the map
* @error Invalid handle
* Iterator has been closed (underlying map destroyed)
* Iterator is outdated
*/
native TrieIterGetSize(TrieIter:handle);
/**
* Retrieves a value at the current position of the iterator.
*
* @param handle Iterator handle
* @param value Variable to store value in
*
* @return True on success, false if the iterator is empty or the current
* value is an array or a string.
* @error Invalid handle
* Iterator has been closed (underlying map destroyed)
* Iterator is outdated
*/
native bool:TrieIterGetCell(TrieIter:handle, &any:value);
/**
* Retrieves a string at the current position of the iterator.
*
* @param handle Iterator handle
* @param buffer Buffer to store the string in
* @param outputsize Maximum size of string buffer
* @param size Optional parameter to store the number of bytes written to the buffer.
*
* @return True on success, false if the iterator is empty or the current value
* is not a string.
* @error Invalid handle
* Invalid buffer size
* Iterator has been closed (underlying map destroyed)
* Iterator is outdated
*/
native bool:TrieIterGetString(TrieIter:handle, buffer[], outputsize, &size = 0);
/**
* Retrieves an array at the current position of the iterator.
*
* @param handle Iterator handle
* @param buffer Buffer to store the array
* @param outputsize Maximum size of buffer
* @param size Optional parameter to store the number of bytes written to the buffer
*
* @return True on success, false if the iterator is empty or the current
* value is not an array.
* @error Invalid handle
* Invalid buffer size
* Iterator has been closed (underlying map destroyed)
* Iterator is outdated
*/
native bool:TrieIterGetArray(TrieIter:handle, any:array[], outputsize, &size = 0);
/**
* Destroys an iterator handle.
*
* @param handle Iterator handle.
*
* @return True on success, false if the value was never set.
*/
native TrieIterDestroy(&TrieIter:handle);

View File

@ -197,7 +197,7 @@ public trietest()
server_print("entry K42K should not be an array or string"); ok = false;
}
if (!TrieSetArray(t, "K42K", data, 1))
{
{
server_print("couldn't set K42K to 1-entry array"); ok = false;
}
if (!TrieGetArray(t, "K42K", array, sizeof(array), value))
@ -262,7 +262,7 @@ public trietest()
fail("Exists/Delete");
ok = true;
TrieClear(t);
if (TrieGetSize(t))
@ -293,7 +293,7 @@ public trietest()
else if (strcmp(buffer, "egg") == 0) found[2] = true;
else { server_print("unexpected key: %s", buffer); ok = false; }
}
if (!found[0] || !found[1] || !found[2])
{
server_print("did not find all keys"); ok = false;
@ -302,13 +302,240 @@ public trietest()
TrieSnapshotDestroy(keys);
TrieDestroy(t);
if (ok)
pass("Snapshot");
else
fail("Snapshot");
ok = true;
t = TrieCreate();
TrieSetString(t, "full", "throttle");
TrieSetString(t, "brutal", "legend");
TrieSetString(t, "broken", "age");
new TrieIter:iter = TrieIterCreate(t);
{
if (TrieIterGetSize(iter) != 3)
{
server_print("Trie iterator size should be 3, is %d", TrieIterGetSize(iter));
ok = false;
}
if (TrieIterEnded(iter))
{
server_print("Trie iterator should have a next key/value pair at this point");
ok = false;
}
new key[32], value[32], bool:valid[4], klen, vlen;
if (!TrieIterGetKey(iter, key, charsmax(key)))
{
server_print("Trie iterator should not be empty at this point (no key retrieval)");
ok = false;
}
else
{
while (!TrieIterEnded(iter))
{
klen = TrieIterGetKey(iter, key, charsmax(key));
TrieIterGetString(iter, value, charsmax(value), vlen);
if (strcmp(key, "full") == 0 && strcmp(value, "throttle") == 0)
valid[0] = true;
else if (strcmp(key, "brutal") == 0 && strcmp(value, "legend") == 0)
valid[1] = true;
else if (strcmp(key, "broken") == 0 && strcmp(value, "age") == 0)
valid[2] = true;
if (strlen(key) != klen)
{
server_print("Key string length does not match. %d != %d", strlen(key), klen);
ok = false;
}
if (strlen(value) != vlen)
{
server_print("Value string length does not match. %d != %d", strlen(key), vlen);
ok = false;
}
// Should thrown an error
// TrieSetString(t, "monkey", "island");
// TrieDeleteKey(t, "full");
// TrieClear(t);
TrieIterNext(iter);
}
if (!valid[0] || !valid[1] || !valid[2])
{
server_print("Did not find all value pairs (1)");
ok = false;
}
}
TrieIterDestroy(iter);
if (iter)
{
server_print("Iterator handle should be null after being destroyed: %d", iter);
ok = false;
}
iter = TrieIterCreate(t);
{
TrieSetString(t, "full", "speed");
TrieSetString(t, "brutal", "uppercut");
TrieSetString(t, "broken", "sword");
arrayset(valid, false, sizeof(valid));
for (; !TrieIterEnded(iter); TrieIterNext(iter))
{
if (TrieIterGetCell(iter, value[0]) || TrieIterGetArray(iter, array, sizeof(array)))
{
server_print("Entries should not be an array or string");
ok = false;
}
else
{
TrieIterGetKey(iter, key, charsmax(key));
TrieIterGetString(iter, value, charsmax(value));
if (strcmp(key, "full") == 0)
TrieSetString(t, "full", "speed");
else if (strcmp(key, "brutal") == 0)
TrieSetString(t, "brutal", "uppercut");
else if (strcmp(key, "broken") == 0)
TrieSetString(t, "broken", "sword");
}
}
if (TrieGetString(t, "full", value, charsmax(value)) && strcmp(value, "speed") == 0)
valid[0] = true;
if (TrieGetString(t, "brutal", value, charsmax(value)) && strcmp(value, "uppercut") == 0)
valid[1] = true;
if (TrieGetString(t, "broken", value, charsmax(value)) && strcmp(value, "sword") == 0)
valid[2] = true;
if (!valid[0] || !valid[1] || !valid[2])
{
server_print("Did not set the new values (overwriting value is allowed)");
ok = false;
}
}
TrieIterDestroy(iter);
if (TrieIterDestroy(iter))
{
server_print("Iter should be null");
ok = false;
}
iter = TrieIterCreate(t);
{
TrieDestroy(t);
// Should throw an error
// TrieIterNext(iter)
}
if (!TrieIterDestroy(iter))
{
server_print("Iter should be valid");
ok = false;
}
t = TrieCreate();
TrieSetCell(t, "key_1", cellmin);
TrieSetArray(t, "key_2", data, sizeof data);
iter = TrieIterCreate(t);
if (TrieIterEnded(iter))
{
server_print("Iter should not be ended: %d", iter);
ok = false;
}
for (; !TrieIterEnded(iter); TrieIterNext(iter))
{
TrieIterGetKey(iter, key, charsmax(key));
if (strcmp(key, "key_1") == 0)
{
new val;
if (!TrieIterGetCell(iter, val) || val != cellmin)
{
server_print("Failed to retrieve value (%d)", val)
ok = false;
}
}
else if (strcmp(key, "key_2") == 0)
{
if (!TrieIterGetArray(iter, array, sizeof(array)))
{
server_print("Failed to retrieve array")
ok = false;
}
else
{
for (new i = 0; i < sizeof data; i++)
{
if (data[i] != array[i])
{
server_print("slot %d should be %d, got %d", i, data[i], array[i]);
ok = false;
}
}
}
}
}
TrieClear(t);
TrieSetCell(t, "1", 1);
TrieSetCell(t, "2", 2);
TrieSetCell(t, "3", 3);
new totalSum = 0;
new element1, element2;
new TrieIter:iter1, TrieIter:iter2;
iter1 = TrieIterCreate(t)
for (; !TrieIterEnded(iter1); TrieIterNext(iter1))
{
iter2 = TrieIterCreate(t);
for(; !TrieIterEnded(iter2); TrieIterNext(iter2))
{
TrieIterGetCell(iter1, element1);
TrieIterGetCell(iter2, element2);
totalSum += element1 * element2;
}
TrieIterDestroy(iter2);
}
TrieIterDestroy(iter1);
if (totalSum != 36)
{
server_print("Sum should be 36, got %d", totalSum);
ok = false;
}
TrieIterDestroy(iter);
TrieDestroy(t);
}
if (ok)
pass("Iterator");
else
fail("Iterator");
done();
}