You could try populating your SparseGrid2D with either an AtomicDouble or a MutableDouble from commons-lang.
That way you can modify them without pulling them out of the grid and putting them back in.

I am quite surprised it doesn't throw a ConcurrentModification exception since you are effectively looping through a list of objects that you are also modifying (with remove() and setLocation()). I suppose MASON passes a safe copy in that bag.

On Mon, Jun 15, 2015 at 5:23 PM Sadat Chowdhury <[log in to unmask]> wrote:
Hi All,

When I increased the 2D world size of my system, there was a significant slowdown of the simulation. After I investigated using a profiler, it turned out to be due to methods that scan through 2D grids that in my system are defined as DoubleGrid2D.

I have started converting those DoubleGrid2D into SparseGrid2D — because it fits the description perfectly: the world can be potentially unbounded and most times there will be fewer actual objects in the world.

While it was relatively easy to convert all the parts that was setting and accessing objects in the grid — I am at a point where I have a lot of DoubleGrid2D’s that used full-scanning functions like .lowerBound() and .multiply(). Supposing I have a SparseGrid2D that has only one Double object per coordinate, what would be the most efficient (fast) way to implement a function similar to multiply() ?

This is what I have so far, but I am just wondering if it can be done more efficiently (faster)?

        final public void SparseGrid2DMultiply(SparseGrid2D theGrid,
                        double multiplier) {
                /*
                 * theGrid is assumed to contain one and only one Double in a given
                 * coordinate
                 */
                Bag objects = theGrid.getAllObjects();
                for (Object object : objects) {
                        double val = (Double) object;
                        if (val != 0.0) {
                                val = val * multiplier;
                                Int2D location = theGrid.getObjectLocation(object);
                                theGrid.remove(object);
                                theGrid.setObjectLocation(val, location);
                        }
                }

        }

Any thoughts/comments is greatly appreciated,
Sadat.