From: markp on
I need to use a hash table to store data using an integer as the key.
I would like to use Ada.Containers.Hashed_Maps. Is there a default
hash function for an integer that I can supply to the instantiation
and could somebody provide a quick sample piece of code to do this?

From: Randy Brukardt on
"markp" <markwork66(a)yahoo.com> wrote in message
news:1177429033.247571.9090(a)s33g2000prh.googlegroups.com...
> I need to use a hash table to store data using an integer as the key.
> I would like to use Ada.Containers.Hashed_Maps. Is there a default
> hash function for an integer that I can supply to the instantiation
> and could somebody provide a quick sample piece of code to do this?

There isn't a default function for this; but generally you don't need one -
the identity function would work fine unless your integers are distributed
unusually.

In any case, writing hash functions is an art; I generally test as many as a
dozen possibilities on sample data sets to see which one(s) work best. What
works best depends totally on your data; there is no such thing as a good
predefined hash function. (OTOH, if you don't have much data, anything might
be good enough -- but if that is true, why use the hashed container form at
all? The Ordered forms are easier to use and cost about the same on small
data sets.)

Randy.



From: Matthew Heaney on
markp <markwork66(a)yahoo.com> writes:

> I need to use a hash table to store data using an integer as the key.
> I would like to use Ada.Containers.Hashed_Maps. Is there a default
> hash function for an integer that I can supply to the instantiation
> and could somebody provide a quick sample piece of code to do this?

You just need an identity function, as Randy pointed out:

function Hash (N : Natural) return Hash_Type is
begin
return Hash_Type (N);
end;

If your integer subtype has negative values, then you could do something like:

function Hash is
new Unchecked_Conversion (Integer, Hash_Type);

You could also use one of the ordered forms directly. Integer subtypes have a
less-than relational operator by default, so there's nothing else you would
need to do.
From: Robert A Duff on
Matthew Heaney <matthewjheaney(a)earthlink.net> writes:

> If your integer subtype has negative values, then you could do something like:
>
> function Hash is
> new Unchecked_Conversion (Integer, Hash_Type);

Or use Hash_Type'Mod.

- Bob