From f3bce9ecfd05c3fb4166eb51e27b04031313341c Mon Sep 17 00:00:00 2001 From: Arkshine Date: Sat, 3 May 2014 16:09:31 +0200 Subject: [PATCH] Add TrieGetSize native. --- amxmodx/trie_natives.cpp | 16 +++++++++++++++ plugins/include/celltrie.inc | 9 +++++++++ plugins/testsuite/trietest.sma | 36 +++++++++++++--------------------- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/amxmodx/trie_natives.cpp b/amxmodx/trie_natives.cpp index e7a885eb..9b073c4b 100644 --- a/amxmodx/trie_natives.cpp +++ b/amxmodx/trie_natives.cpp @@ -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 } }; diff --git a/plugins/include/celltrie.inc b/plugins/include/celltrie.inc index 87b292ea..e89e7037 100644 --- a/plugins/include/celltrie.inc +++ b/plugins/include/celltrie.inc @@ -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); diff --git a/plugins/testsuite/trietest.sma b/plugins/testsuite/trietest.sma index 42f122ac..c51d4a7b 100644 --- a/plugins/testsuite/trietest.sma +++ b/plugins/testsuite/trietest.sma @@ -1,10 +1,5 @@ #include - -// 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(); }