swarm-support
[Top][All Lists]
Advanced

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

Re: Something for everyone! Questions about programming AND installation


From: Benedikt Stefansson
Subject: Re: Something for everyone! Questions about programming AND installation!
Date: Wed, 29 Oct 1997 15:26:19 -0800

Hi Paul and others,

Regarding your question on how to tally voters choices. I hope this doesn't
sound to banal, but wouldn't the easiest way be to use an Array of counter
objects? I assume that you can enumerate the choices a priori; then initialize
an array of "bins" for the votes and run through the list of voters.

I find that it is nice to "wrap" an object around an integer in this case,
since I've had problems with storing and reading back  pointers to ints in
Swarm Arrays. I've included the code for a stublet/object that would do the
trick, at the end of this message.

This stublet is just a little counter that responds to the method -addOne. Now
you can initialize an Array of these little critters, one for each choice:

id counter;

distribution=[Array create: [self getZone] setCount: numChoices];
for(j=0;j<[distribution getCount];j++) {
    counter=[CountHeads create:[self getZone]];
    [distribution atOffset: j put: [counter zeroOut]];
}

Then to tally the votes:

id index,aVoter;

index=[voterList begin:[self getZone]];
while(aVoter=[index next])
    [[distribution atOffset: [aVoter getChoice]] addOne];
[index drop];

Finally, the header and m-file for the CountHeads class:


@interface CountHeads: Swarm {
  int counter;
}
-zeroOut;
-addOne;
-addTo:(int) i;
-(int)getCount;
-(void)drop;

@end



@implementation CountHeads

-zeroOut {
  counter=0;
  return self;
}

-addOne {
  counter++;
  return self;
}

-addTo: (int) i {
  counter+=i;
  return self;
}

-(int)getCount {
  return counter;
}

-(void)drop {
  [super drop];
}

@end


--
----------------
            "I get email, therefore I exist."
                 - Descartes (modified)
Benedikt Stefansson                 address@hidden
Department of Economics, UCLA       Fax. (310) 825-9528
Los Angeles, CA 90095-1477          Tel. (310) 825-4126



                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.
                  ==================================


reply via email to

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