Print

Print


On Jun 9, 2005, at 4:22 PM, glen e. p. ropella wrote:

> Does RandomSequence work for dynamic data structures?  It appears from
> the code:
[snip]
> that we're using a fixed size array.  And if that's the case, I'd have
> to recreate the RandomSequence at each cycle, thereby defeating the
> purpose.  But, perhaps I'm just not looking deep enough.  To resize an
> array, you'd have to replace the old Sequence instance variable with a
> new array (with the new size), right?

Right.  But remember that most of the utility Steppables (MultiStep,
Sequence, ParallelSequence, RandomSequence, WeakStep, etc.) are our
attempt to handle the 90% most common needs with some pre-built
Steppable classes that we found useful.  There's nothing special about
them.

If you want a resizable array, it's pretty easy to do if you're not
doing parallel threads, something like this:

public class BagSequence implements Steppable
        {
        public Bag steps;
        public Sequence(Bag steps)  // steps must be a Bag of Steppables only
                { this.steps = steps; }
        public void step(SimState state)
                {
                for(int x=0;x<steps.numObjs;x++)
                        {
                        Steppable s = ((Steppable)(steps.objs[x]);
                        if (s!=null) s.step(state);
                        }
                }
        }

The random shuffling version follows straightforwardly from there.
Note that you have to be careful here because if the Bag does not
contain Steppables, you'll get a ClassCastException.  Also, if you
change the Bag from within the step() method of something in the
BagSequence, it'd effect the for-loop of the step() method in
BagSequence in ways that you'd have to think carefully about.

Sean