17.04.2015, 19:17
Introduction
A hash is a unique numerical representation of a string. I'm sure you all know numbers are easier to compare than strings, so this uses hashes to compare strings. Example:
Old code:
New code:
Download
This library is a part of YSI, which can be found here. Keep your eye on that topic and your server log for updates.
YSI Download Topic
Functions
There are three main functions (one function, two macros):
Update:
This library now includes bernstein, FNV1 and FNV1a hashes. If you get a collision (which the compiler will signal by a "duplicate case label" error), try a different hash. Bernstein is "b", FNV1 is "f" and FNV1a is "a". These letters are placed after the "_H" or "_I" with an "@":
Make sure your YHash and _H@ or _I@ match algorithm, otherwise the checks may fail even if they're true.
Important note: If your compiler hangs, consider using the old style on longer strings. This is not well suited to hashing long strings. For example:
That will not compile because the generated line will be too long (but don't worry, the lines get optimised well, it's just a problem in the intermediate stages). On the other hand this will compile and will give the same answer:
This way just doesn't look quite as nice.
Credits
I am reposting this include (made by ******) with the thought of what he said to several members of the community. Everything that could help someone should not be deleted from the forums, which is something I agree with since I learned a lot by reading the tutorials on here. He made a lot of things that are used by lots of servers and that knowledge should not be lost for present and future developers.
A hash is a unique numerical representation of a string. I'm sure you all know numbers are easier to compare than strings, so this uses hashes to compare strings. Example:
Old code:
Код:
if (!strcmp(str, "first")) { // Do first code. } else if (!strcmp(str, "second")) { // Do second code. } // etc...
Код:
switch (YHash(str)) { case _H<first>: { // Do first code. } case _H<second>: { // Do second code. } }
This library is a part of YSI, which can be found here. Keep your eye on that topic and your server log for updates.
YSI Download Topic
Functions
There are three main functions (one function, two macros):
- YHash - This takes a string, for example "cmdtext" in "OnPlayerCommandText" and converts it to a hash. This does runtime strings - that is, strings which are not known at compile time (things people type etc). This returns a value which can be compared to the compile time hashed strings.
Код:YHash(str[], bool:sensitive = true, e_HASH_TYPE:type = bernstein);
The third parameter is the algorithm used to hash the string. The default is, as before, bernstein. The other options (see below) are "fnv1" and "fnv1a". - _H - This is the macro to generate a compile time constant hash. This version is used for case sensitive comparisons. A compile time constant is something which is known when you compile the script. The old version of this had words in round brackets with every letter separated by commas. The new version uses angle brackets and no commas. The new version however is slightly less flexible - it doesn't allow quite as long strings and doesn't support spaces. If you have a space in your word only the first word will be hashed. Note that you can mix and match which version you use if you need longer words or spaces.
Код:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_
Код:printf("The hash of \"hello\" is: %d", _H<hello>);
- _I - This is the same as "_H" but gives case insensitive hashes. Note - you should use this whenever possible.
Код:
switch (YHash(cmd, false)) { case _I<ban>: // Do the "/ban" command. case _I<kick>: // Do the "/kick" command. case _I<spawn>: // Do the "/spawn" command. }
This library now includes bernstein, FNV1 and FNV1a hashes. If you get a collision (which the compiler will signal by a "duplicate case label" error), try a different hash. Bernstein is "b", FNV1 is "f" and FNV1a is "a". These letters are placed after the "_H" or "_I" with an "@":
Код:
switch (YHash(cmd, true, fnv1)) { case _H@f<ban>: // Do the "/ban" command. case _H@f<kick>: // Do the "/kick" command. case _H@f<spawn>: // Do the "/spawn" command. }
Important note: If your compiler hangs, consider using the old style on longer strings. This is not well suited to hashing long strings. For example:
Код:
case _I<color22>:
Код:
case _I(c,o,l,o,r,2,2):
Credits
I am reposting this include (made by ******) with the thought of what he said to several members of the community. Everything that could help someone should not be deleted from the forums, which is something I agree with since I learned a lot by reading the tutorials on here. He made a lot of things that are used by lots of servers and that knowledge should not be lost for present and future developers.