Commit f9e448a5 authored by mey's avatar mey
Browse files

added Int2DCache.java

AbstractDynamicMap.java
    uses Int2DCache instead of its own private cache
parent 0e3e43c1
......@@ -5,8 +5,8 @@ import java.util.Iterator;
import java.util.Set;
import de.zmt.pathfinding.filter.ConvolveOp;
import sim.field.grid.ObjectGrid2D;
import sim.util.Int2D;
import sim.util.Int2DCache;
/**
* Abstract implementation of a {@link DynamicMap}. Locations can be marked
......@@ -23,9 +23,6 @@ import sim.util.Int2D;
abstract class AbstractDynamicMap extends BasicMapChangeNotifier implements NamedMap, DynamicMap {
private static final long serialVersionUID = 1L;
/** Cache of {@link Int2D} locations used in {@link #dirtySet}. */
private static ObjectGrid2D locationsCache = new ObjectGrid2D(0, 0);
/** Width of map. */
private final int width;
/** Height of map. */
......@@ -55,47 +52,7 @@ abstract class AbstractDynamicMap extends BasicMapChangeNotifier implements Name
this.height = height;
this.xExtend = xExtend;
this.yExtend = yExtend;
adjustCacheSize(width, height);
}
/**
* Adjust the cache if needed, to fit given dimensions.
*
* @param width
* @param height
*/
private static synchronized void adjustCacheSize(int width, int height) {
// locations cache is sufficient: do nothing
if (locationsCache != null && locationsCache.getWidth() >= width && locationsCache.getHeight() >= height) {
return;
}
// create new grid that fits requirements
ObjectGrid2D newCache = new ObjectGrid2D(Math.max(width, locationsCache.getWidth()),
Math.max(height, locationsCache.getHeight()));
for (int x = 0; x < newCache.getWidth(); x++) {
for (int y = 0; y < newCache.getHeight(); y++) {
Object location;
// already in old cache: copy reference
if (x < locationsCache.getWidth() && y < locationsCache.getHeight()) {
location = locationsCache.get(x, y);
}
// not in old cache: create new
else {
location = new Int2D(x, y);
}
newCache.set(x, y, location);
}
}
/*
* Assigning a reference is an atomic operation. There is no other write
* operation done on the cache. Concurrent read operations on shared
* data are safe which makes this class suitable for multithreading.
*/
locationsCache = newCache;
Int2DCache.adjustCacheSize(width, height);
}
/**
......@@ -118,21 +75,21 @@ abstract class AbstractDynamicMap extends BasicMapChangeNotifier implements Name
for (int i = xMin; i < xMax; i++) {
for (int j = yMin; j < yMax; j++) {
dirtySet.add((Int2D) locationsCache.get(i, j));
dirtySet.add(Int2DCache.get(i, j));
}
}
}
@Override
public void forceUpdate(int x, int y) {
updateCleanNotify((Int2D) locationsCache.get(x, y));
updateCleanNotify(Int2DCache.get(x, y));
}
@Override
public final void forceUpdateAll() {
for (int x = 0; x < getWidth(); x++) {
for (int y = 0; y < getHeight(); y++) {
Int2D location = (Int2D) locationsCache.get(x, y);
Int2D location = Int2DCache.get(x, y);
dirtySet.remove(location);
update(location.x, location.y);
}
......@@ -143,7 +100,7 @@ abstract class AbstractDynamicMap extends BasicMapChangeNotifier implements Name
@Override
public void updateIfDirty(int x, int y) {
// if requested value is dated: it needs to be updated
Int2D location = (Int2D) locationsCache.get(x, y);
Int2D location = Int2DCache.get(x, y);
if (dirtySet.contains(location)) {
updateCleanNotify(location);
}
......@@ -176,7 +133,7 @@ abstract class AbstractDynamicMap extends BasicMapChangeNotifier implements Name
* @return <code>true</code> if location is marked dirty
*/
boolean isDirty(int x, int y) {
return dirtySet.contains(locationsCache.get(x, y));
return dirtySet.contains(Int2DCache.get(x, y));
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment