Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package sidescrollertest.compactgrid;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.HashMap;
- /**
- * A compacting 2D array-like thing that only
- * stores Objects that are not equal. Objects
- * that are equal according to their equals()
- * method are only stored once. Objects that
- * were not accessed in a specified time can
- * be flushed to disk.
- *
- * @author Jonathan
- */
- public class CompactGrid<T> implements Serializable {
- private final ArrayList<T> knownObjects;
- private transient HashMap<T, Integer> objectIDs;
- private final Chunk[][] chunks;
- private transient CompactGridChunkStore<T> chunkStore;
- private final int szx, szy, chunkSize;
- private final int storeTimeout;
- private transient long lastStoreTimestamp;
- /**
- * Constructs a new Compact grid with the given
- * size, chunk size, chunk store and store timeout.
- *
- * @param sx The x coordinate of the grid size.
- * @param sy The y coordinate of the grid size.
- * @param chunkSize The chunk size (both width and height)
- * @param store The CompactGridChunkStore used for loading, storing and generating chunks.
- * @param storeTimeout The time, in milliseconds, after which an unused Chunk may be flushed to disk.
- */
- public CompactGrid(int sx, int sy, int chunkSize, CompactGridChunkStore<T> store, int storeTimeout) {
- this(sx, sy, chunkSize, new ArrayList(), store, storeTimeout);
- }
- protected CompactGrid(int sx, int sy, int chunkSize, ArrayList<T> knownObjects, CompactGridChunkStore<T> store, int storeTimeout) {
- this.chunkStore = store;
- this.knownObjects = knownObjects;
- this.szx = sx;
- this.szy = sy;
- this.chunkSize = chunkSize;
- this.storeTimeout = storeTimeout;
- this.setStoreTimestamp();
- int chunksX = (sx + chunkSize - 1) / chunkSize;
- int chunksY = (sy + chunkSize - 1) / chunkSize;
- this.chunks = new Chunk[chunksX][chunksY];
- for (int x = 0; x < chunksX; x++) {
- for (int y = 0; y < chunksY; y++) {
- this.chunks[x][y] = new Chunk(chunkSize, this, x, y);
- }
- }
- }
- private void checkAndBuildIDs() {
- if (this.objectIDs != null) {
- return;
- }
- this.objectIDs = new HashMap();
- for (int i = 0; i < this.knownObjects.size(); i++) {
- this.objectIDs.put(this.knownObjects.get(i), i);
- }
- }
- private void boundsCheck(int x, int y) {
- if ((x < 0) || (y < 0) || (x >= this.szx) || (y >= this.szy)) {
- throw new ArrayIndexOutOfBoundsException("CompactGrid index: " + x + ", " + y);
- }
- }
- private Chunk getChunk(int x, int y) {
- this.boundsCheck(x, y);
- int cx = x / this.chunkSize;
- int cy = y / this.chunkSize;
- return this.chunks[cx][cy];
- }
- private int getRefByValue(T value) {
- this.checkAndBuildIDs();
- Integer i = this.objectIDs.get(value);
- if (i != null) {
- return i;
- }
- this.knownObjects.add(value);
- return this.knownObjects.size() - 1;
- }
- /**
- * Stores a value in this grid at the specified position.
- *
- * @param x The x coordinate of the grid position.
- * @param y The y coordinate of the grid position.
- * @param value The value to store.
- */
- public void set(int x, int y, T value) {
- Chunk c = this.getChunk(x, y);
- c.set(x % this.chunkSize, y % this.chunkSize, this.getRefByValue(value));
- this.cleanUpIfNecessary();
- }
- /**
- * Returns the value stored at the specified grid position.
- *
- * @param x The x coordinate of the grid position.
- * @param y The y coordinate of the grid position.
- * @return The value at this position.
- */
- public T get(int x, int y) {
- Chunk c = this.getChunk(x, y);
- int i = c.get(x % this.chunkSize, y % this.chunkSize);
- this.cleanUpIfNecessary();
- return this.knownObjects.get(i);
- }
- private void populateChunk(Chunk c, T[][] values, int chunkSize) {
- for (int x = 0; x < chunkSize; x++) {
- for (int y = 0; y < chunkSize; y++) {
- c.set(x, y, this.getRefByValue(values[x][y]));
- }
- }
- }
- private void setStoreTimestamp() {
- this.lastStoreTimestamp = System.currentTimeMillis();
- }
- private void cleanUpIfNecessary() {
- if (System.currentTimeMillis() - this.lastStoreTimestamp < this.storeTimeout / 2) {
- return;
- }
- for (Chunk[] carr: this.chunks) {
- for (Chunk c: carr) {
- c.storeAwayIfNeeded();
- }
- }
- this.setStoreTimestamp();
- }
- private void readObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
- inputStream.defaultReadObject();
- for (Chunk[] carr: this.chunks) {
- for (Chunk c: carr) {
- c.reInitAfterDeserialize(this);
- }
- }
- this.setStoreTimestamp();
- }
- public void reInitAfterDeserialize(CompactGridChunkStore<T> store) {
- if (this.chunkStore != null) {
- throw new IllegalStateException("CompactGrid.reInitAfterDeserialize on already initialized grid");
- }
- this.chunkStore = store;
- }
- private static class Chunk implements Serializable {
- private final int chunkSize;
- private int[][] objRefs;
- private final int x, y;
- private transient CompactGrid grid;
- private transient long lastAccessTimeStamp;
- public Chunk(int chunkSize, CompactGrid grid, int x, int y) {
- this.chunkSize = chunkSize;
- this.grid = grid;
- this.x = x;
- this.y = y;
- }
- private void touch() {
- this.lastAccessTimeStamp = System.currentTimeMillis();
- }
- public void reInitAfterDeserialize(CompactGrid grid) {
- assert this.grid == null : "Chunk.setGrid(): Already set!";
- this.grid = grid;
- this.touch();
- }
- private void store() {
- assert this.objRefs != null : "Chunk.store(): Array null!";
- this.grid.chunkStore.storeChunk(this.objRefs, this.x, this.y);
- this.objRefs = null;
- }
- public void storeAwayIfNeeded() {
- if (this.objRefs == null) {
- return;
- }
- if (System.currentTimeMillis() - this.lastAccessTimeStamp > this.grid.storeTimeout) {
- this.store();
- }
- }
- private void load() {
- assert this.objRefs == null : "Chunk.load(): Array not null!";
- this.objRefs = this.grid.chunkStore.tryLoadChunk(this.x, this.y);
- if (this.objRefs == null) {
- this.objRefs = new int[this.chunkSize][this.chunkSize];
- this.grid.populateChunk(this, this.grid.chunkStore.createChunk(this.x, this.y, this.chunkSize), this.chunkSize);
- }
- }
- private void loadIfNecessary() {
- if (this.objRefs == null) {
- this.load();
- }
- }
- public void set(int x, int y, int ref) {
- this.touch();
- this.loadIfNecessary();
- this.objRefs[x][y] = ref;
- }
- public int get(int x, int y) {
- this.touch();
- this.loadIfNecessary();
- return this.objRefs[x][y];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment