From: Matthew Heaney <mheaney@on2.com>
Subject: Re: How to use associative arrays in Ada 2005?
Date: Mon, 27 Nov 2006 17:22:46 -0500
Date: 2006-11-27T22:22:46+00:00 [thread overview]
Message-ID: <456b6536$0$1006$39cecf19@news.twtelecom.net> (raw)
In-Reply-To: <m2r6voa97l.fsf@grendel.local>
Simon Wright wrote:
>
> There's no reason I can see to use maps of maps *for this problem*. I
> would use a hash function (Nation x Family_Name x Name) => Hash_Type
> in a single map.
Yes, that's right, you could do it either way.
I prefer that OP's approach of using a map of maps of maps, since that
allows you to perform queries like "give me all the entries for this
nation", etc.
> Matt, I see (A.18.4(5)) that Ada.Containers doesn't allow hash
> collisions (which, if true, is demanding a lot of hash function
> designers).
A.18.4 (5) says:
5/2{AI95-00302-03} {node (of a map)} A map contains pairs of keys and
elements, called nodes. Map cursors designate nodes, but also can be
thought of as designating an element (the element contained in the node)
for consistency with the other containers. There exists an equivalence
relation on keys, whose definition is different for hashed maps and
ordered maps. A map never contains two or more nodes with equivalent
keys. The length of a map is the number of nodes it contains.{length (of
a map)}
The RM is here:
http://www.adaic.com/standards/05aarm/html/AA-A-18-4.html
You're misreading that paragraph. It does say that:
"A map never contains two or more nodes with equivalent keys."
but that's very different from saying "hash collisions aren't allowed."
That sentence is simply saying that this is a (unique-key) map, not a
multi-map.
Hash functions can map key values to the same hash value (that can
happen easily if your hash function is poor). That's not good if there
are many such collisions, but it's certainly allowed by the standard.
But the hash value is only one aspect of a key used to determine its
uniqueness: the other is equivalence. The hash value of a key
determines its bucket, and the equivalence relation is then used to
compare the key to keys already in that bucket.
So it doesn't matter whether multiple keys hash to the same value (it
would just mean they all get put in the same bucket), since the success
of an insertion (say) is ultimately decided by equivalence.
Take a look at the generic formal region of the hashed map:
generic
type Key_Type is private;
type Element_Type is private;
with function Hash (Key : Key_Type) return Hash_Type;
with function Equivalent_Keys (Left, Right : Key_Type)
return Boolean;
with function "=" (Left, Right : Element_Type)
return Boolean is <>;
package Ada.Containers.Hashed_Maps is
You have to supply both a hash function and an equivalence function.
Typically you pass "=" as the generic actual for Equivalent_Keys, but
not always. (The reason why the general formal is declared that way is
because equivalence is orthogonal to equality.)
> If collisions aren't allowed we could go for an ordered
> map based on ... or some likely-to-be-more-efficient order.
If you have as your key a record comprising the 3 strings (nation,
family name, given name), then "=" would be adequate for Equivalent_Keys.
next prev parent reply other threads:[~2006-11-27 22:22 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-11-21 10:11 How to use associative arrays in Ada 2005? snoopysalive
2006-11-21 11:49 ` Georg Bauhaus
2006-11-21 14:18 ` Matthew Heaney
2006-11-21 23:35 ` snoopysalive
2006-11-23 19:27 ` snoopysalive
2006-11-23 19:40 ` Georg Bauhaus
2006-11-24 0:33 ` Georg Bauhaus
2006-11-24 11:49 ` Matthew Heaney
2006-11-24 8:27 ` Dmitry A. Kazakov
2006-11-24 11:51 ` Matthew Heaney
2006-11-26 19:05 ` snoopysalive
2006-11-26 20:30 ` Matthew Heaney
2006-11-27 9:15 ` Dmitry A. Kazakov
2006-11-27 19:53 ` Matthew Heaney
2006-11-27 21:11 ` Dmitry A. Kazakov
2006-11-27 21:52 ` Matthew Heaney
2006-11-28 8:29 ` Alex R. Mosteo
2006-11-28 13:19 ` Matthew Heaney
2006-11-28 8:34 ` Dmitry A. Kazakov
2006-11-28 13:21 ` Matthew Heaney
2006-11-27 21:08 ` Simon Wright
2006-11-27 22:22 ` Matthew Heaney [this message]
2006-11-27 22:58 ` Simon Wright
2006-11-28 1:55 ` Matthew Heaney
2006-11-24 11:35 ` Matthew Heaney
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox