Hash tables

Hello rooters,
The documentation on hash collections is very scarce. Thanks a lot if you can answer some of the following questions:
(1) - What does it mean exactly “to choose a good hash function”? Does it simply means to call TMath::Hash(const void *txt, Int_t ntxt) with a suitable txt representing the object?
(2) - Is ntxt in TMath::Hash() the number of characters in txt?
(3) - When do I have to use THashList rather than THashTable?

Many thanks in advance.

Hi,

a good hash function is a function that is easy to compute and approximates a random function: for each input, every output should be equally likely. TMath::Hash() is such a function with for strings with up to 4 characters ideal hashing.

Yes, ntxt is the number of characters in the string or length in bytes of the address to be hashed.

Use a THashList when, in addtition to the lookup performance, the insert order of the objects in the collection should be maintained, i.e. if obj1 is inserted before obj2 then, when iterating forward over the collection, obj1 is returned before obj2.

Use a THashTable when the insert order does not have to be maintained, i.e. iterating returns the objects in a random order, but only the lookup speed matters. A THashTable uses a less than half the amount of space of a THashList (which combines a THashTable with a TList).

Cheers, Fons.