comp.lang.ada
 help / color / mirror / Atom feed
* Ada Hash
@ 2007-04-24 15:37 markp
  2007-04-24 18:12 ` Randy Brukardt
  2007-04-29 18:33 ` Matthew Heaney
  0 siblings, 2 replies; 4+ messages in thread
From: markp @ 2007-04-24 15:37 UTC (permalink / raw)


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?




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ada Hash
  2007-04-24 15:37 Ada Hash markp
@ 2007-04-24 18:12 ` Randy Brukardt
  2007-04-29 18:33 ` Matthew Heaney
  1 sibling, 0 replies; 4+ messages in thread
From: Randy Brukardt @ 2007-04-24 18:12 UTC (permalink / raw)


"markp" <markwork66@yahoo.com> wrote in message
news:1177429033.247571.9090@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.






^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ada Hash
  2007-04-24 15:37 Ada Hash markp
  2007-04-24 18:12 ` Randy Brukardt
@ 2007-04-29 18:33 ` Matthew Heaney
  2007-04-29 22:55   ` Robert A Duff
  1 sibling, 1 reply; 4+ messages in thread
From: Matthew Heaney @ 2007-04-29 18:33 UTC (permalink / raw)


markp <markwork66@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.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ada Hash
  2007-04-29 18:33 ` Matthew Heaney
@ 2007-04-29 22:55   ` Robert A Duff
  0 siblings, 0 replies; 4+ messages in thread
From: Robert A Duff @ 2007-04-29 22:55 UTC (permalink / raw)


Matthew Heaney <matthewjheaney@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



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-04-29 22:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-24 15:37 Ada Hash markp
2007-04-24 18:12 ` Randy Brukardt
2007-04-29 18:33 ` Matthew Heaney
2007-04-29 22:55   ` Robert A Duff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox