algorithm - How to implement a hash function for a HashSet/HashMap -


if needed hash entire hashset<t> or hashmap<t, u>, t had hash algorithm implemented, how it? note not asking hashing elements of hash table, i'm talking hashing entire data structure itself. not difficult ordered set treeset, because order of elements of hash table not well-defined, more tricky. sorting elements infeasible in general case, algorithm should take no more o(n) time.

i'm looking general, language independent example, can provide code or links code language.

your options

  1. enforce order purposes of creating hash
  2. apply hash algorithm commutative (independent of order)

the first option may viable if number of elements relatively small. can sort hash elements e.g. hash value (of each element), apply well-known hash-combining techniques such multiplying each successive element's contribution hash (someprime)^n.

for second option, adding hash of each element in hash may provide suitable distribution, since hash of each element should distributed.