Expand/Shrink

hash

Definition: atom res = hash(object key, atom algo=0)
Description: Calculates a hash value from key

key : Any Phix object
algo : ignored except for HSIEH30, see notes.

Returns an unsigned 32-bit atom (0..4294967295 aka #FFFFFFFF), or in the HSIEH30 case a 30-bit integer (0..1073741823 aka #3FFFFFFF).
pwa/p2js: Not supported.
Notes: Uses a completely different algorithm to Euphoria and will not therefore yield the same results.
While Euphoria defines HSIEH30/HSIEH32/ADLER32/FLETCHER32/MD5/SHA256 (with the latter two unsupported?), Phix only defines HSIEH30, as things currently stand.

The HSIEH30 argument in algo is supported only insofar as it coerces the result into an integer.

Note this is not thread-safe and was not specifically written to be particularly fast - I would expect to get better performance using standard dictionaries instead, not that I have actually bothered to run any benchmarks, you understand.
Example:
?hash(0                                                     ) --  438581301
?hash(1                                                     ) -- 2050536409.0 (not an integer)
?hash({"some key",12345}                                    ) --  974147209
?hash("The quick brown fox jumps over the lazy dog."        ) --   96315063
?hash("The quick brown fox jumps over the lazy dog"         ) -- 2869980142.0 (not an integer)
?hash({1, {2,3, {4,5,6}, 7}, 8.9}                           ) --  378003995
?hash("some key",                                    HSIEH30) --  436021236
?hash({"some key"},                                  HSIEH30) --  878205692
?hash(12345,                                         HSIEH30) --  454441044
?hash("The quick brown fox jumps over the lazy dog", HSIEH30) --  722496496
?hash(123,                                           HSIEH30) --   62934336
?hash(1.23,                                          HSIEH30) --  432062202
Implementation: See builtins\hash.e (an autoinclude) for details of the actual implementation.
See Also: dict