[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: hash locate_matching_entry()
From: |
John Darrington |
Subject: |
Re: hash locate_matching_entry() |
Date: |
Fri, 10 Oct 2008 10:20:24 +0800 |
User-agent: |
Mutt/1.5.13 (2006-08-11) |
On Wed, Oct 08, 2008 at 10:36:47AM -0400, Jason Stover wrote:
On Wed, Oct 08, 2008 at 10:25:19AM -0400, Jason Stover wrote:
> I'm having some trouble getting a matching entry in my
> hash table. It looks like the problem is here:
>
> /* Locates an entry matching TARGET. Returns the index for the
> entry, if found, or the index of an empty entry that indicates
> where TARGET should go, otherwise. */
> static inline unsigned
> locate_matching_entry (struct hsh_table *h, const void *target)
> {
> unsigned i = h->hash (target, h->aux);
>
> assert (h->hash_ordered);
> for (;;)
> {
> void *entry;
> i &= h->size - 1;
> entry = h->entries[i];
> if (entry == NULL || !h->compare (entry, target, h->aux))
> return i;
> i--;
> }
> }
>
> In my code, h->compare (entry, target, h->aux) returns 1 when
> entry and target match. So I would think locate_matching_entry()
> should return i, but it doesn't because of the !h->compare (...).
> Should this line be:
>
> if (entry == NULL || h->compare (entry, target, h->aux))
John just clarified over IRC that the return value here is supposed to be
strcmp-ish: 0 for a match.
This is why I always encourage people to write
if ( 0 == strcmp (...))
instead of
if ( !strcmp (...))
J'
--
PGP Public key ID: 1024D/2DE827B3
fingerprint = 8797 A26D 0854 2EAB 0285 A290 8A67 719C 2DE8 27B3
See http://pgp.mit.edu or any PGP keyserver for public key.
signature.asc
Description: Digital signature