Add TrieGetSize native.

This commit is contained in:
Arkshine 2014-05-03 16:09:31 +02:00
parent 12a8864532
commit f3bce9ecfd
3 changed files with 39 additions and 22 deletions

View File

@ -262,6 +262,20 @@ static cell AMX_NATIVE_CALL TrieDestroy(AMX *amx, cell *params)
return 0;
}
// native TrieGetSize(Trie:handle);
static cell AMX_NATIVE_CALL TrieGetSize(AMX *amx, cell *params)
{
CellTrie *t = g_TrieHandles.lookup(params[1]);
if (t == NULL)
{
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
return 0;
}
return t->map.elements();
}
AMX_NATIVE_INFO trie_Natives[] =
{
{ "TrieCreate", TrieCreate },
@ -279,6 +293,8 @@ AMX_NATIVE_INFO trie_Natives[] =
{ "TrieKeyExists", TrieKeyExists },
{ "TrieDestroy", TrieDestroy },
{ "TrieGetSize", TrieGetSize },
{ NULL, NULL }
};

View File

@ -142,3 +142,12 @@ native bool:TrieKeyExists(Trie:handle, const key[]);
*/
native TrieDestroy(&Trie:handle);
/**
* Retrieves the number of elements in a map.
*
* @param handle Map Handle.
*
* @return Number of elements in the trie.
* @error Invalid Handle.
*/
native TrieGetSize(Trie:handle);

View File

@ -1,10 +1,5 @@
#include <amxmodx>
// These natives are only available in a debug build of amxmodx
native TrieFreeCount();
native TrieMallocCount();
new failcount = 0;
new passcount = 0;
public plugin_init()
@ -29,16 +24,7 @@ stock done()
{
server_print("Finished. %d tests, %d failed", failcount + passcount, failcount);
}
stock check_frees()
{
if (TrieMallocCount() != TrieFreeCount())
fail("free count == malloc count");
else
pass("free count == malloc count");
server_print("malloc count: %d free count: %d", TrieMallocCount(), TrieFreeCount());
}
public trietest()
{
failcount = 0;
@ -53,7 +39,11 @@ public trietest()
for (new i = 0; i < 100; i++)
{
formatex(key, charsmax(key), "K%dK", i);
TrieSetCell(t, key, i);
if (!TrieSetCell(t, key, i))
{
server_print("TrieGetCell(%d, '%s', %d) failed", t, key, i);
ok = false;
}
}
for (new i = 0; i < 100; i++)
@ -65,17 +55,22 @@ public trietest()
server_print("TrieGetCell(%d, '%s', %d) failed", t, key, val);
ok = false;
}
else if (val != i)
{
server_print("val mismatch, expected: %d got: %d", i, val);
ok = false;
}
}
// Check size is 100.
if (TrieGetSize(t) != 100)
{
ok = false;
server_print("Map size mismatch, expected: %d got: %d", 100, TrieGetSize(t));
}
if (ok)
pass("Cell tests");
else
fail("Cell tests");
@ -126,8 +121,6 @@ public trietest()
TrieDestroy(t);
check_frees();
t = TrieCreate();
ok = true;
for (new i = 0; i < 1000; i++)
@ -159,10 +152,9 @@ public trietest()
else
fail("Exists/Delete");
check_frees();
TrieClear(t);
TrieDestroy(t);
check_frees();
done();
}