swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Inconsistency in Map/KeyedCollection?


From: Benedikt Stefansson
Subject: Inconsistency in Map/KeyedCollection?
Date: Thu, 13 Jan 2000 23:36:48 -0700

--- Begin Message --- Subject: Inconsistency in Map/KeyedCollection? Date: Thu, 13 Jan 2000 21:35:57 -0700
>From what I can see a Map will not insert a duplicate key,
nor will it add a member if the key already exists or create
a collection to hold the duplicates.

This is in fact consistent with the Map documentation:

<quote>

- (BOOL)at: aKey insert: anObject

at:insert: inserts an entry into a Map containing the key
and member values given as its arguments. It returns true if
the key was not previously contained in the collection. An
attempt to insert a duplicate key is simply rejected and
false is returned.

</quote>

However the docs for theKeyedCollection protocol seems to
imply that a Map will accept more than one member at a key,
and will create a collection to hold the multiple members.
Actually this is only hinted at in the blurb for the at:
aKey method, which is an accessor method (the protocol
actually doesn't define any set or put methods).

>From KeyedCollection documentation.

<quote>

- at: aKey

The at: message returns the existing member of the
collection which matches the key value passed as its
argument, or nil if there is no key value in the collection
which matches. If duplicate entries for this key exist, the
entire collection of duplicate members created for the key
value is returned instead.

</quote>

If my understanding is correct this this text is misleading
and should be taken out, since none of the collections that
implement the KeyedCollection protocol actually creates a
collection for duplicate members.

The 'collection of duplicate members' feature would of
course be useful, but can be implemented fairly easily by
the user.

Also, since we are on this topic, I've recently learned the
hard way that using keys objects with double values is not a
very reliable way to create a sorted map. I.e. this
implementation of the compare: method is not reliable:

-(int) compare: o
{
  return ([o get_value] == value) ? 0 : (value > [o
get_value]) ? -1 : 1;
}

What you may end up with is that even though values are the
same up to insignificant digits Map continues to cheerily
insert members at this value.

I've thus turned to creating a string from the double and
implementing this compare: method:

<quote>
-(int) compare: o
{
  // Catch same value through string
  return (strcmp(str_value,[o getC]) == 0) ? 0 : (value > [o
get_value]) ? 1 : -1;
}
</quote>

The code which creates the string is implemented in the
following fashion:

<quote>
#define STR_FORMAT "%+7.4f"
#define STR_LENGTH 20

if(!str_value)
    {
      str_value = malloc(STR_LENGTH*sizeof(char));
    }
sprintf(str_value,STR_FORMAT,value);
</quote>

You can of course define STR_FORMAT and STR_LENGTH any which
way you want.

Benedikt

PS. Marcus has been advocating the use of Java collections
instead of Swarm collections. How do Java Maps fare on this
front?


--
Benedikt Stefansson      | address@hidden
CASA, Inc.               | Ph : (505) 988-8807 x101
Santa Fe, NM 87501       | Fax: (505) 988-3440




--- End Message ---

reply via email to

[Prev in Thread] Current Thread [Next in Thread]