Raised This Month: $32 Target: $400
 8% 

[INC] hash.inc/hashx.inc


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Zefir
Member
Join Date: Oct 2007
Location: Kiev, Ukraine
Old 08-06-2010 , 09:50   [INC] hash.inc/hashx.inc
Reply With Quote #1

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
(handlekey[]) {
    new 
value
    HashXGetCell
(HashX:handlekeyvalue)
    
server_print("Key: %s, Value: %s"keyvalue)
  }

  
HashXForEach(trie_cells"my_func"
Example with data type:
PHP Code:
  new HaxhX:trie

  my_func
(idkey[], type) {
    new 
valuestring[512], Array:arr

    
switch (type) {
      case 
HASH_X_TYPE_NONE: {
        
server_print("Key: %s is EMPTY"key)
      }
      case 
HASH_X_TYPE_CELL: {
        
HashXGetCell(triekeyvalue)
        
server_print("Key: %s, Value: %d"keyvalue)
      }
      case 
HASH_X_TYPE_STRING: {
        
HashXGetString(triekeystring)
        
server_print("Key: %s, Value: %s"keystring)
      }
      default: {
        
HashXGetArray(triekeyarr)
        
server_print("Key: %s, ArraySize: %d"keytype)
      }
    }
  }

  
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
Attached Files
File Type: inc hash.inc (3.4 KB, 424 views)
File Type: inc hashx.inc (5.8 KB, 279 views)
__________________

Last edited by Zefir; 11-17-2010 at 06:41.
Zefir is offline
Send a message via ICQ to Zefir
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 08-06-2010 , 09:53   Re: [INC] hash.inc/hashx.inc
Reply With Quote #2

PHP Code:
    #define HashKeyExists(%1, %2)
    #define HashDeleteKey(%1, %2) 
I think you should remove spaces between %1 and %2.
__________________
hleV is offline
Zefir
Member
Join Date: Oct 2007
Location: Kiev, Ukraine
Old 08-06-2010 , 10:01   Re: [INC] hash.inc/hashx.inc
Reply With Quote #3

Oooo, wtf?

Tnx, fixed...
__________________
Zefir is offline
Send a message via ICQ to Zefir
joropito
AlliedModders Donor
Join Date: Mar 2009
Location: pfnAddToFullPack
Old 08-06-2010 , 10:09   Re: [INC] hash.inc/hashx.inc
Reply With Quote #4

Quote:
Originally Posted by Zefir View Post
Anyone who often use dynamic data arrays Array and Trie knows that TrieSetArray() is bugged in current version.
Can you explain more about that but? I didn't knew that.
__________________

Divide et vinces
approved plugins | steam account

I don't accept PM for support. Just ask on forums.
If you're looking for private work, PM me.
joropito is offline
Send a message via MSN to joropito
Zefir
Member
Join Date: Oct 2007
Location: Kiev, Ukraine
Old 08-06-2010 , 10:15   Re: [INC] hash.inc/hashx.inc
Reply With Quote #5

Quote:
Originally Posted by joropito View Post
Can you explain more about that but? I didn't knew that.
https://bugs.alliedmods.net/show_bug.cgi?id=3816

And first variant:
http://forums.alliedmods.net/showthr...10#post1020310
__________________
Zefir is offline
Send a message via ICQ to Zefir
Javivi
AlliedModders Donor
Join Date: Dec 2008
Old 08-06-2010 , 10:16   Re: [INC] hash.inc/hashx.inc
Reply With Quote #6

Quote:
Originally Posted by joropito View Post
Can you explain more about that but? I didn't knew that.
https://bugs.alliedmods.net/show_bug.cgi?id=3816
__________________
Javivi is offline
KORD_12.7
Senior Member
Join Date: Aug 2009
Location: Russia, Vladivostok
Old 08-06-2010 , 10:31   Re: [INC] hash.inc/hashx.inc
Reply With Quote #7

Good job, tnx
__________________

Vi Veri Veniversum Vivus Vici
Russian Half-Life and Adrenaline Gamer community
KORD_12.7 is offline
Send a message via ICQ to KORD_12.7
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-06-2010 , 12:54   Re: [INC] hash.inc/hashx.inc
Reply With Quote #8

There is a way to solve the TrieSetArray bug with orpheu in waiting a new release. See this link : http://forums.alliedmods.net/showpos...&postcount=444
__________________

Last edited by Arkshine; 08-06-2010 at 13:16.
Arkshine is offline
Zefir
Member
Join Date: Oct 2007
Location: Kiev, Ukraine
Old 08-06-2010 , 13:00   Re: [INC] hash.inc/hashx.inc
Reply With Quote #9

Quote:
Originally Posted by Arkshine View Post
There is a way to solve the TrieSetArray bug with orpheu. See this link : http://forums.alliedmods.net/showpos...&postcount=444
Good

Can check fact of fixing inside hash.inc for auto-switch to native function?
__________________
Zefir is offline
Send a message via ICQ to Zefir
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:11.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode