swarm-support
[Top][All Lists]
Advanced

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

sorter for lists


From: Benedikt Stefansson
Subject: sorter for lists
Date: Fri, 04 Jul 1997 10:53:17 +0200

Rick Riolo wrote:


     "Has anyone written a little Sorter object to sort
      arbitrary lists?  Ie, one would pass the sorter
      the list, a method to get values to compare from the
      objects in the list, and a method to do the compare,"


Hi Rick,

I hacked up this class the other day to rank agents by some criterion.
It will in other words rank any list of objects, you only need to pass
the list and the name of the method that should be used to rank.

To change it into a "sorter" is trivial. In it's present form the class
is designed to return a list of the best objects (since there might be
more than one object that has the same rank we always assume that there
is a list returned, even if #N members is 1). Internally reference to
these lists are kept as a sorted map of pointers.  You could write a
simple routine that takes the internal ranked list and "flattens it
out", thereby creating the sorted list you need. E.g.:

        sortedList=[List create:[self getZone]];

        listIndex=[theList begin:[self getZone]];
        while((aGroup=[listList next])) {
              groupIndex=[aGroup begin:[self getZone]];
              while((aMember=[groupIndex next])) 
                    [sortedList addLast: aMember];
              [groupIndex drop];
       }
       [listIndex drop];

Note that theList is an instance variable in the class, sortedList would
be a new instance variable probably but listIndex,aGroup,groupIndex and
aMember should be method vars.

This should work, I think, but I haven't actual run it.

Regards,
-Benedikt


Attachement is the *.h and *.m file for RankedList. Please let me know
if there are problems with the attachements.
// By BenediktStefansson 1997
// Portions (C) Santa Fe Institute

// Object that ranks other objects according to some
// criterion and returns best objects as individuals or lists

#import <swarmobject.h>
#import <space.h>
#import <activity.h>
#import <collections.h>

@class RankedList;
@class RankedData;

@interface RankedList : Swarm {
        id theCollection;
        SEL theSelector;
        id theProbe;
        id theList;
}

-setCollection: (id) c;

-setProbedSelector: (SEL) s;

-createEnd;

-update;

-getBest;

-getList;

@end

@interface RankedData : Swarm {
        double data;
}

-setData: (double) d;

-(double) getData;

-(int) compare: o;

@end

// By BenediktStefansson 1997
// Portions (C) Santa Fe Institute

// Object that ranks other objects according to some
// criterion and returns best objects as individuals or lists

#import "RankedList.h"

@implementation RankedList

-setCollection: (id) c {
        theCollection=c;
        return self;
}

-setProbedSelector: (SEL) s {
        theSelector=s;
        return self;
}


-createEnd {
        theList=[Map create:[self getZone]];
        return self;
}


-update {
        id anIndex;
        id aMember;
        id aGroup;
        id theData;

        if(!theProbe) {
                theProbe=[MessageProbe createBegin: [self getZone]];
                [theProbe setProbedSelector: theSelector];
                theProbe=[theProbe createEnd];
        }

        [theList removeAll];

        anIndex=[theCollection begin:[self getZone]];

        while((aMember=[anIndex next])) {
                theData=[RankedData create: [self getZone]]; 
                [theData setData: (double)[theProbe doubleDynamicCallOn: 
aMember]];

                if((aGroup=[theList at: theData])){
                        [aGroup addLast: aMember];
                }
                else {
                        aGroup=[List create: [self getZone]];
                        [aGroup addLast: aMember];
                        [theList at: theData insert: aGroup];
                }
        }
        [anIndex drop];

        return self;
}



-getBest {
        if([theList getCount])
                return [theList getFirst];
        else
                return nil;
}       


-getList {
        return theList;
}

         
@end

@implementation RankedData 

-setData: (double) d {
  data=d;
  return self;
}

-(double) getData {
  return data;
}


-(int) compare: o {
  
  int compare;

  if(([o getData]==data)){
    compare=0;
  }
  else {
    if(([o getData]<data)) compare=-1;
    if(([o getData]>data)) compare=1;
  }

  return compare;
}

@end

reply via email to

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