Add IntMap methodmap (#2018)

* Add new IntHashMap and IntMap natives

This is a continuation of #579.

[HashMap] Adds new IntHashMap and IntMap natives.

This patch makes the following changes
  * Refactors StringHashMap to a generic HashMap template
  * Adds the IntHashMap class
  * Adds new IntMap natives
  * Adds IntMap tests to the tries test suite

[HashMap] Reverted rename of CharsAndLength

[HashMap] Use more descriptive template names

[HashMap] Removed old-style natives

[HashMap] Removed IntHash class

[HashMap] Reverted some search & replace errors

Co-authored-by: Geoffrey McRae <[email protected]>

* Fix spelling mistake + include

* Fix tries test

* Update tests with clone + ContainsKey

---------

Co-authored-by: Geoffrey McRae <[email protected]>
Co-authored-by: Nicholas Hastings <[email protected]>
This commit is contained in:
Kit o' Rifty
2024-10-31 23:35:41 +00:00
committed by GitHub
co-authored by Geoffrey McRae Nicholas Hastings
parent 6c3486d732
commit 34e9605519
20 changed files with 880 additions and 42 deletions
+119
View File
@@ -167,6 +167,125 @@ methodmap StringMapSnapshot < Handle
public native int GetKey(int index, char[] buffer, int maxlength);
};
methodmap IntMap < Handle
{
// Creates a hash map. A hash map is a container that can map integers (called
// "keys") to arbitrary values (cells, arrays, or strings). Keys in a hash map
// are unique. That is, there is at most one entry in the map for a given key.
//
// Insertion, deletion, and lookup in a hash map are all considered to be fast
// operations, amortized to O(1), or constant time.
//
// The word "Trie" in this API is historical. As of SourceMod 1.6, tries have
// been internally replaced with hash tables, which have O(1) insertion time
// instead of O(n).
//
// The IntMap must be freed via delete or CloseHandle().
public native IntMap();
// Clones a hash map, returning a new handle with the same size and data.
// This should NOT be confused with CloneHandle. This is a completely new
// handle with the same data but no relation to the original. It should be
// closed when no longer needed with delete or CloseHandle().
//
// @return New handle to the cloned hash map
public native IntMap Clone();
// Sets a value in a hash map, either inserting a new entry or replacing an old one.
//
// @param key Key integer.
// @param value Value to store at this key.
// @param replace If false, operation will fail if the key is already set.
// @return True on success, false on failure.
public native bool SetValue(const int key, any value, bool replace=true);
// Sets an array value in a Map, either inserting a new entry or replacing an old one.
//
// @param key Key integer.
// @param array Array to store.
// @param num_items Number of items in the array.
// @param replace If false, operation will fail if the key is already set.
// @return True on success, false on failure.
public native bool SetArray(const int key, const any[] array, int num_items, bool replace=true);
// Sets a string value in a Map, either inserting a new entry or replacing an old one.
//
// @param key Key integer.
// @param value String to store.
// @param replace If false, operation will fail if the key is already set.
// @return True on success, false on failure.
public native bool SetString(const int key, const char[] value, bool replace=true);
// Retrieves a value in a Map.
//
// @param key Key integer.
// @param value Variable to store value.
// @return True on success. False if the key is not set, or the key is set
// as an array or string (not a value).
public native bool GetValue(const int key, any &value);
// Retrieves an array in a Map.
//
// @param key Key integer.
// @param array Buffer to store array.
// @param max_size Maximum size of array buffer.
// @param size Optional parameter to store the number of elements written to the buffer.
// @return True on success. False if the key is not set, or the key is set
// as a value or string (not an array).
public native bool GetArray(const int key, any[] array, int max_size, int &size=0);
// Retrieves a string in a Map.
//
// @param key Key integer.
// @param value Buffer to store value.
// @param max_size 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 key is not set, or the key is set
// as a value or array (not a string).
public native bool GetString(const int key, char[] value, int max_size, int &size=0);
// Checks whether a key is present in a Map.
//
// @param key Key integer.
// @return True if the key has been found, else false.
public native bool ContainsKey(const int key);
// Removes a key entry from a Map.
//
// @param key Key integer.
// @return True on success, false if the value was never set.
public native bool Remove(const int key);
// Clears all entries from a Map.
public native void Clear();
// Create a snapshot of the map's keys. See IntMapSnapshot.
public native IntMapSnapshot Snapshot();
// Retrieves the number of elements in a map.
property int Size {
public native get();
}
};
// A IntMapSnapshot is created via IntMap.Snapshot(). It captures the
// keys on a map so they can be read. Snapshots must be freed with delete or
// CloseHandle().
methodmap IntMapSnapshot < Handle
{
// Returns the number of keys in the map snapshot.
property int Length {
public native get();
}
// Retrieves the key integer of a given key in a map snapshot.
//
// @param index Key index (starting from 0).
// @return The key integer
// @error Index out of range.
public native int GetKey(int index);
};
/**
* Creates a hash map. A hash map is a container that can map strings (called
* "keys") to arbitrary values (cells, arrays, or strings). Keys in a hash map
+187
View File
@@ -13,6 +13,7 @@ public Plugin:myinfo =
public OnPluginStart()
{
RegServerCmd("test_maps", RunTests);
RegServerCmd("test_int_maps", RunIntTests);
}
public Action:RunTests(argc)
@@ -161,3 +162,189 @@ public Action:RunTests(argc)
return Plugin_Handled;
}
public Action:RunIntTests(argc)
{
IntMap map = new IntMap();
for (new i = 0; i < 64; i++) {
if (!map.SetValue(i, i))
ThrowError("set map to %d failed", i);
if (!map.ContainsKey(i))
ThrowError("map contains %d failed", i)
new value;
if (!map.GetValue(i, value))
ThrowError("get map %d", i);
if (value != i)
ThrowError("get map %d == %d", i, i);
}
// Setting 17 without replace should fail.
new value;
if (map.SetValue(17, 999, false))
ThrowError("set map 17 should fail");
if (!map.GetValue(17, value) || value != 17)
ThrowError("value at 17 not correct");
if (!map.SetValue(17, 999))
ThrowError("set map 17 = 999 should succeed");
if (!map.GetValue(17, value) || value != 999)
ThrowError("value at 17 not correct");
// Check size is 64.
if (map.Size != 64)
ThrowError("map size not 64");
// Check 100 is not found.
int array[64];
char string[64];
if (map.ContainsKey(100) ||
map.GetValue(100, value) ||
map.GetArray(100, array, sizeof(array)) ||
map.GetString(100, string, sizeof(string)))
{
ThrowError("map should not have 100");
}
// Check that 17 is not a string or array.
if (map.GetArray(17, array, sizeof(array)) ||
map.GetString(17, string, sizeof(string)))
{
ThrowError("entry 17 should not be an array or string");
}
// Strings.
if (!map.SetString(17, "hellokitty"))
ThrowError("17 should be string");
if (!map.GetString(17, string, sizeof(string)) ||
strcmp(string, "hellokitty") != 0)
{
ThrowError("17 should be hellokitty");
}
if (map.GetValue(17, value) ||
map.GetArray(17, array, sizeof(array)))
{
ThrowError("entry 17 should not be an array or string");
}
// Arrays.
new data[5] = { 93, 1, 2, 3, 4 };
if (!map.SetArray(17, data, 5))
ThrowError("couldn't set 17 to 5-entry array");
if (!map.GetArray(17, array, sizeof(array)))
ThrowError("couldn't fetch 5-entry array");
for (new i = 0; i < 5; i++) {
if (data[i] != array[i])
ThrowError("17 slot %d should be %d, got %d", i, data[i], array[i]);
}
if (map.GetValue(17, value) ||
map.GetString(17, string, sizeof(string)))
{
ThrowError("entry 17 should not be a value or string");
}
if (!map.SetArray(17, data, 1))
ThrowError("couldn't set 17 to 1-entry array");
// Check that we fixed an old bug where 1-entry arrays where cells
if (!map.GetArray(17, array, sizeof(array), value))
ThrowError("couldn't fetch 1-entry array");
if (value != 1)
ThrowError("array size mismatch (%d, expected %d)", value, 1);
// Check that we maintained backward compatibility.
if (!map.GetValue(17, value))
ThrowError("backwards compatibility failed");
if (value != data[0])
ThrowError("wrong value (%d, expected %d)", value, data[0]);
// Remove "17".
if (!map.Remove(17))
ThrowError("17 should have been removed");
if (map.Remove(17))
ThrowError("17 should not exist");
if (map.ContainsKey(17) ||
map.GetValue(17, value) ||
map.GetArray(17, array, sizeof(array)) ||
map.GetString(17, string, sizeof(string)))
{
ThrowError("map should not have a 17");
}
map.Clear();
if (map.Size)
ThrowError("size should be 0");
map.SetString(42, "time!");
map.SetString(84, "bees");
map.SetString(126, "egg");
IntMapSnapshot keys = map.Snapshot();
{
if (keys.Length != 3)
ThrowError("map snapshot length should be 3");
bool found[3];
for (new i = 0; i < keys.Length; i++) {
decl key = keys.GetKey(i);
if (key == 42)
found[0] = true;
else if (key == 84)
found[1] = true;
else if (key == 126)
found[2] = true;
else
ThrowError("unexpected key: %d", key);
}
if (!found[0] || !found[1] || !found[2])
ThrowError("did not find all keys");
}
delete keys;
map.SetValue(10240, 6744);
map.SetValue(8, 13);
new cloneData[5] = { 12, 23, 55, 1, 2 };
new cloneArr[5];
map.SetArray(9102, cloneData, 5);
IntMap clone = map.Clone();
if (clone.Size != map.Size)
ThrowError("cloned map size mismatch (%d, expected %d)", clone.Size, map.Size);
if (!clone.GetString(42, string, sizeof(string)))
ThrowError("cloned map entry 42 should be a string");
if (strcmp(string, "time!") != 0)
ThrowError("cloned map entry 42 should be \"time!\"");
if (!clone.GetString(84, string, sizeof(string)))
ThrowError("cloned map entry 84 should be a string");
if (strcmp(string, "bees") != 0)
ThrowError("cloned map entry 84 should be \"bees\"");
if (!clone.GetString(126, string, sizeof(string)))
ThrowError("cloned map entry 126 should be a string");
if (strcmp(string, "egg") != 0)
ThrowError("cloned map entry 126 should be \"egg\"");
if (!clone.GetValue(10240, value))
ThrowError("cloned map entry 10240 should be a value");
if (value != 6744)
ThrowError("cloned map entry 10240 should be 6744")
if (!clone.GetValue(8, value))
ThrowError("cloned map entry 8 should be a value");
if (value != 13)
ThrowError("cloned map entry 8 should be 13")
if (!clone.GetArray(9102, cloneArr, 5))
ThrowError("cloned map entry 9102 should be an array");
for (new i = 0; i < 5; i++) {
if (cloneData[i] != cloneArr[i])
ThrowError("cloned map entry 9102 slot %d should be %d, got %d", i, cloneData[i], cloneArr[i]);
}
delete clone;
PrintToServer("All tests passed!");
delete map;
return Plugin_Handled;
}