swarm-support
[Top][All Lists]
Advanced

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

[Fwd: Re: Quaestion on the order of insertion on a Collection?]


From: Gian Piero Sala
Subject: [Fwd: Re: Quaestion on the order of insertion on a Collection?]
Date: Fri, 05 Feb 1999 15:22:38 +0100

Ooops! I made a typing mistake in compFunc with re-casted args.
The correct version is:

int compFunc ( id a, id b )
{
  return ( *((float*) a) < *((float*) b) ? -1 : 1 );
}
--- Begin Message --- Subject: Re: Quaestion on the order of insertion on a Collection? Date: Fri, 05 Feb 1999 14:09:47 +0100
>  I need to insert members (chromosomes of a
> G.A.) in a collection in a descending order
> estabilished by the fitness of the chromosome (a
> float value) .Since It must be possible in my
> simulation to have objects with the same fitness
> value (the same Key)
> , a Map is not suitable.The other possibility is
> to use a Set collection in which object with
> duplicate keys are accepted.What I've not
> understood if
> a) ordering of the members of collection is or
> is not automatic once the compare function is
> provided .
> b) how can be the kind of order (ascending or
> descending ) can be estabilished.
> c) I'd like to be able to use - at: aKey  Set
> method to get all the members matching the same
> key.The fact is that I'd like to use it also in
> an unordered list.Can I use Set without
> specifying an ordering among members?
>
> Hope I was clear, thanks in advance.
> Fabio Mascelloni.
>

A map collection is definitely the most suitable
method. Swarm Map collections automatically sort
new objs when you insert them, so your map is
always sorted (up to v1.3.1, I don't think v1.4
has changed this behaviour). To get it work, you
have to:

1. Make up your own compFunc (!) and plug it into
your map collection using the setCompareFunction
message;

2. Your brand new compFunc must follow a straight
schema:
int compFunc ( id a, id b )
{
  return ( a < b ? -1 : 1 );
}

3. Since id a and id are translated into void*'s,
the may be either objects containing your keys or
pointer themselves to your keys.
In the first case (a pure OOP approach) you'll
have:

int compFunc ( id a, id b )
{
  return ( [a getKeyValue] < [b getKeyValue] ? -1
: 1);
}

--getKeyValue is the message you provide to get
the key from the object container--

In the second case (runs faster but *should be
avoided for many good reasons*) you simply cast to
(id) your float keys and re-cast them once passed
as arguments:

int compFunc ( id a, id b )
{
  return ( *((float) a) < *((float) b) ? -1 : 1);

// returning (float) *a < (float) *b ? -1 : 1 may
work, but gcc may get crazy, too!!
}

Since compFunc doesn't return a different value
(normally 0) when you compare equal keys, a new
obj with an already defined key will be appended
just after the last obj with that key in you map.

To get objects sorted in reverse order in
*compile* time, you simply invert return values in
compFunc:

int compFunc( id a, id b )
{
  return( a < b ? 1 : -1 );
}

:-)


--- End Message ---

reply via email to

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