hash.inc
Anyone who often use dynamic data arrays Array and Trie knows that TrieSetArray() is bugged in current version.
This library solves this problem by creating an extra array to store data.
Also you can migrate to native function simply by recompiling your plugins with uncommented #define, if the error will be fixed in future releases.
PHP Code:
// Default, builtin variant
//#define HASH_NATIVE
Functions:
Hash:HashCreate():
— Creates hash.
HashClear(Hash:handle):
— Clears hash.
HashDestroy(Hash:handle):
— Clears and destroys hash.
bool:HashKeyExists(Hash:handle, key[]):
— Checks if the key exists in the hash.
bool:HashDeleteKey(Hash:handle, key[]):
— Deletes key.
HashSetCell(Hash:handle, key[], value):
— Sets integer in the hash.
HashSetString(Hash:handle, key[], str[]):
— Sets string in the hash.
HashSetArray(Hash:handle, key[], Array:arr, size_of_array):
— Sets array in the hash.
bool:HashGetCell(Hash:handle, key[], value):
— Gets integer from the hash.
bool:HashGetString(Hash:handle, key[], str[], size_of_string):
— Gets string from the hash, but no more than size_of_string.
bool:HashGetArray(Hash:handle, key[], Array:arr, size_of_array):
— Gets array from the hash.
— size_of_array is dummy for backwards compatibility.
hashx.inc
hashX.inc as well as hash.inc, which is required for hashX, is a wrapper for standard Trie.
The difference is ability to traverse the list and to store type information.
Every hash creates additional array and 2 hashed for internal data, therefore it is not recommended for big data amounts.
Constants:
PHP Code:
// Key size of the hash.
#define HASH_KEY_SIZE 64
// Type of item contents
// Type means array size inside the item if it is higher than HASH_X_TYPE_ARRAY.
#define HASH_X_TYPE_NONE -3
#define HASH_X_TYPE_STRING -2
#define HASH_X_TYPE_CELL -1
#define HASH_X_TYPE_ARRAY 0
Functions:
HashX:HashXCreate():
— Creates hash.
HashXClear(HashX:handle):
— Clears hash.
HashXDestroy(HashX:handle):
— Clears and destroys hash.
bool:HashXKeyExists(HashX:handle, key[]):
— Checks if the key exists in the hash.
bool:HashXDeleteKey(HashX:handle, key[]):
— Deletes key.
HashXSetCell(HashX:handle, key[], value):
— Sets integer in the hash.
HashXSetString(HashX:handle, key[], str[]):
— Sets string in the hash.
HashXSetArray(HashX:handle, key[], Array:arr, size_of_array):
— Sets array in the hash.
bool:HashXGetCell(HashX:handle, key[], value):
— Gets integer from the hash.
bool:HashXGetString(HashX:handle, key[], str[], size_of_string):
— Gets string from the hash, but no more than size_of_string.
bool:HashXGetArray(HashX:handle, key[], Array:arr, size_of_array):
— Gets array from the hash.
— size_of_array is dummy for backwards compatibility.
HashXKeyType(HasX:handle, key[]) :
— Returns item type for key.
— Type means array size inside the item if it is higher than HASH_X_TYPE_ARRAY.
— HASH_X_TYPE_NONE - means that the item is empty.
Array:HashXGetKeys(HashX:handle):
— Returns array of keys of hash.
— It duplicates internal array so it is recommended to use HashXForEach() in case of big data amount.
HashXForEach({HashX,_}:id, HashX:handle, func[], type_info = 0) :
— Calls function func for each hash item.
— id, key and type will be passed to function. type_info is 0 if not specified.
—
Example:
PHP Code:
new HaxhX:trie_cells
my_func(handle, key[]) {
new value
HashXGetCell(HashX:handle, key, value)
server_print("Key: %s, Value: %s", key, value)
}
HashXForEach(trie_cells, "my_func")
—
Example with data type:
PHP Code:
new HaxhX:trie
my_func(id, key[], type) {
new value, string[512], Array:arr
switch (type) {
case HASH_X_TYPE_NONE: {
server_print("Key: %s is EMPTY", key)
}
case HASH_X_TYPE_CELL: {
HashXGetCell(trie, key, value)
server_print("Key: %s, Value: %d", key, value)
}
case HASH_X_TYPE_STRING: {
HashXGetString(trie, key, string)
server_print("Key: %s, Value: %s", key, string)
}
default: {
HashXGetArray(trie, key, arr)
server_print("Key: %s, ArraySize: %d", key, type)
}
}
}
HashXForEach(0, "my_func", 1)
— Hash handle is not passed to function because it was determined to have no use because handler function is written for specific hash. But player id is often needed, but you can pass any integer instead of id as well as hash handle. See first example.
Ps: Tnx Exolent for idea, and
Void.Less for testing, translate and optimization help.
Off site:
http://up.org.ua/_tag/hashx.git