swarm-support
[Top][All Lists]
Advanced

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

Sorting problem


From: Benedikt Stefansson
Subject: Sorting problem
Date: Fri, 07 Mar 1997 16:53:40 -0800

Hi there,

I've been trying understand how (sorted) Maps work in the collection
library, but there are a couple of things that I can't get to work.

Essentially I need to create a sorted list for every location on an X by
Y Discrete2d space where the key is a data wrapping object of my
creation and for each key there will be a list holding pointers to the
agents associated with this key.

My original strategy was to create the Maps as follows:

        for(x=0;x<worldXSize;x++) {
                for(y=0;y<worldYSize;y++) {
                  aMap = [Map create: [self getZone]] ;
                  [aMap setCompareFunction: compare]; <--- NOTE
                  [self putObject: aMap atX: x Y: y];
                }
        }       

The actual work of filling in the objects on the Maps would be done in
another method: (Note that c is a pointer to an agent).

      dataObject = [[PriceData create: [self getZone]] setPrice: price];
      theMap = [self getObjectAtX: i Y: j] ;
      if( (priceGroup = [theMap at: dataObject]) ){
        [priceGroup addLast: c];
      }
      else{
        priceGroup = [List create: [self getZone]] ;
        [priceGroup addLast: c] ;
        [theMap at: dataObject insert: priceGroup];
      }
    }
  }

OK, so far so good. Now the crucial part that is tripping me up is the
-setCompareFunction. In the PriceData object class i've defined these
two 
methods:

-getPrice {
        return price;
}

-(int) compare: o {
  int compare;

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

  return compare;
}

So the big question is: What is the correct syntax to set the compare
function in Map and how do I get this to work?

I'd appreciate some help on this problem since I would be in big trouble
if I can't get this to work.

Regards,
-Benedikt

------------------
Benedikt Stefansson                 address@hidden
Center for Computable Economics     Tel. (310) 825-1777
Department of Economics, UCLA       Fax. (310) 825-9528
Los Angeles, CA 90095-1477          http://cce.sscnet.ucla.edu


reply via email to

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