This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: map key issue - please support


Wilson wrote:

> But the following implementation fails to search. It returns 'Found'
> for a set of value which is not in the map. Please support.

I think there's something wrong with your predicate: (sorry, I can't
remember the right mathematical word)

>         if ( k1.key1 <= k2.key1 )
>         {
>            if ( k1.key2 < k2.key2 )
>            {
>                return true;
>            }
>            return false;              // A
>        }
>        if ( k1.key2 <= k2.key2 )
>        {
>            if ( k1.key1 < k2.key1 )
>            {
>                return true;
>            }
>            return false;              // B
>        }
>        return false;

In this case,

    ltstr( (1,2), (2,2) ) = false     (point A above)
    ltstr( (2,2), (1,2) ) = false     (point B above)

Hence both

        (1,2) >= (2,2)
    and (2,2) >= (1,2)

and the only way this can be true is if

        (1,2) == (2,2)

hence the match.

Did you want to implement a lexical ordering? That would be:

    if (k1.key1 < k2.key1)
    {
        return true:
    }
    else if (k1.key1 == k2.key1)
    {
        if (k1.key2 < k2.key2)
        {
             return true;
        }
        else
        {
             return false;
        }
    }
    else
    {
        // k1.key1 > k2.key1
        return false;
    }

or just the single statement

    return (k1.key1 < k2.key1)
           || ((k1.key1 == k2.key1) && (k1.key2 < k2.key2));

Rup.




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]