Guest User

Untitled

a guest
Jan 30th, 2016
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.25 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package sidescrollertest.compactgrid;
  8.  
  9. import java.io.IOException;
  10. import java.io.ObjectInputStream;
  11. import java.io.Serializable;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14.  
  15. /**
  16.  * A compacting 2D array-like thing that only
  17.  * stores Objects that are not equal. Objects
  18.  * that are equal according to their equals()
  19.  * method are only stored once. Objects that
  20.  * were not accessed in a specified time can
  21.  * be flushed to disk.
  22.  *
  23.  * @author Jonathan
  24.  */
  25. public class CompactGrid<T> implements Serializable {
  26.   private final ArrayList<T> knownObjects;
  27.   private transient HashMap<T, Integer> objectIDs;
  28.   private final Chunk[][] chunks;
  29.   private transient CompactGridChunkStore<T> chunkStore;
  30.   private final int szx, szy, chunkSize;
  31.   private final int storeTimeout;
  32.   private transient long lastStoreTimestamp;
  33.  
  34.  
  35.   /**
  36.    * Constructs a new Compact grid with the given
  37.    * size, chunk size, chunk store and store timeout.
  38.    *
  39.    * @param sx           The x coordinate of the grid size.
  40.    * @param sy           The y coordinate of the grid size.
  41.    * @param chunkSize    The chunk size (both width and height)
  42.    * @param store        The CompactGridChunkStore used for loading, storing and generating chunks.
  43.    * @param storeTimeout The time, in milliseconds, after which an unused Chunk may be flushed to disk.
  44.    */
  45.   public CompactGrid(int sx, int sy, int chunkSize, CompactGridChunkStore<T> store, int storeTimeout) {
  46.     this(sx, sy, chunkSize, new ArrayList(), store, storeTimeout);
  47.   }
  48.  
  49.   protected CompactGrid(int sx, int sy, int chunkSize, ArrayList<T> knownObjects, CompactGridChunkStore<T> store, int storeTimeout) {
  50.     this.chunkStore = store;
  51.     this.knownObjects = knownObjects;
  52.     this.szx = sx;
  53.     this.szy = sy;
  54.     this.chunkSize = chunkSize;
  55.     this.storeTimeout = storeTimeout;
  56.     this.setStoreTimestamp();
  57.    
  58.     int chunksX = (sx + chunkSize - 1) / chunkSize;
  59.     int chunksY = (sy + chunkSize - 1) / chunkSize;
  60.    
  61.     this.chunks = new Chunk[chunksX][chunksY];
  62.     for (int x = 0; x < chunksX; x++) {
  63.       for (int y = 0; y < chunksY; y++) {
  64.         this.chunks[x][y] = new Chunk(chunkSize, this, x, y);
  65.       }
  66.     }
  67.   }
  68.  
  69.   private void checkAndBuildIDs() {
  70.     if (this.objectIDs != null) {
  71.       return;
  72.     }
  73.     this.objectIDs = new HashMap();
  74.     for (int i = 0; i < this.knownObjects.size(); i++) {
  75.       this.objectIDs.put(this.knownObjects.get(i), i);
  76.     }
  77.   }
  78.  
  79.   private void boundsCheck(int x, int y) {
  80.     if ((x < 0) || (y < 0) || (x >= this.szx) || (y >= this.szy)) {
  81.       throw new ArrayIndexOutOfBoundsException("CompactGrid index: " + x + ", " + y);
  82.     }
  83.   }
  84.  
  85.   private Chunk getChunk(int x, int y) {
  86.     this.boundsCheck(x, y);
  87.     int cx = x / this.chunkSize;
  88.     int cy = y / this.chunkSize;
  89.     return this.chunks[cx][cy];
  90.   }
  91.  
  92.   private int getRefByValue(T value) {
  93.     this.checkAndBuildIDs();
  94.     Integer i = this.objectIDs.get(value);
  95.     if (i != null) {
  96.       return i;
  97.     }
  98.     this.knownObjects.add(value);
  99.     return this.knownObjects.size() - 1;
  100.   }
  101.  
  102.   /**
  103.    * Stores a value in this grid at the specified position.
  104.    *
  105.    * @param x     The x coordinate of the grid position.
  106.    * @param y     The y coordinate of the grid position.
  107.    * @param value The value to store.
  108.    */
  109.   public void set(int x, int y, T value) {
  110.     Chunk c = this.getChunk(x, y);
  111.     c.set(x % this.chunkSize, y % this.chunkSize, this.getRefByValue(value));
  112.     this.cleanUpIfNecessary();
  113.   }
  114.  
  115.   /**
  116.    * Returns the value stored at the specified grid position.
  117.    *
  118.    * @param x The x coordinate of the grid position.
  119.    * @param y The y coordinate of the grid position.
  120.    * @return  The value at this position.
  121.    */
  122.   public T get(int x, int y) {
  123.     Chunk c = this.getChunk(x, y);
  124.     int i = c.get(x % this.chunkSize, y % this.chunkSize);
  125.     this.cleanUpIfNecessary();
  126.     return this.knownObjects.get(i);
  127.   }
  128.  
  129.   private void populateChunk(Chunk c, T[][] values, int chunkSize) {
  130.     for (int x = 0; x < chunkSize; x++) {
  131.       for (int y = 0; y < chunkSize; y++) {
  132.         c.set(x, y, this.getRefByValue(values[x][y]));
  133.       }
  134.     }
  135.   }
  136.  
  137.   private void setStoreTimestamp() {
  138.     this.lastStoreTimestamp = System.currentTimeMillis();
  139.   }
  140.  
  141.   private void cleanUpIfNecessary() {
  142.     if (System.currentTimeMillis() - this.lastStoreTimestamp < this.storeTimeout / 2) {
  143.       return;
  144.     }
  145.     for (Chunk[] carr: this.chunks) {
  146.       for (Chunk c: carr) {
  147.         c.storeAwayIfNeeded();
  148.       }
  149.     }
  150.     this.setStoreTimestamp();
  151.   }
  152.  
  153.  
  154.   private void readObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
  155.     inputStream.defaultReadObject();
  156.     for (Chunk[] carr: this.chunks) {
  157.       for (Chunk c: carr) {
  158.         c.reInitAfterDeserialize(this);
  159.       }
  160.     }
  161.     this.setStoreTimestamp();
  162.   }
  163.  
  164.   public void reInitAfterDeserialize(CompactGridChunkStore<T> store) {
  165.     if (this.chunkStore != null) {
  166.       throw new IllegalStateException("CompactGrid.reInitAfterDeserialize on already initialized grid");
  167.     }
  168.     this.chunkStore = store;
  169.   }
  170.  
  171.  
  172.   private static class Chunk implements Serializable {
  173.     private final int chunkSize;
  174.     private int[][] objRefs;
  175.     private final int x, y;
  176.     private transient CompactGrid grid;
  177.     private transient long lastAccessTimeStamp;
  178.    
  179.     public Chunk(int chunkSize, CompactGrid grid, int x, int y) {
  180.       this.chunkSize = chunkSize;
  181.       this.grid = grid;
  182.       this.x = x;
  183.       this.y = y;
  184.     }
  185.    
  186.     private void touch() {
  187.       this.lastAccessTimeStamp = System.currentTimeMillis();
  188.     }
  189.    
  190.     public void reInitAfterDeserialize(CompactGrid grid) {
  191.       assert this.grid == null : "Chunk.setGrid(): Already set!";
  192.       this.grid = grid;
  193.       this.touch();
  194.     }
  195.    
  196.     private void store() {
  197.       assert this.objRefs != null : "Chunk.store(): Array null!";
  198.       this.grid.chunkStore.storeChunk(this.objRefs, this.x, this.y);
  199.       this.objRefs = null;
  200.     }
  201.    
  202.     public void storeAwayIfNeeded() {
  203.       if (this.objRefs == null) {
  204.         return;
  205.       }
  206.       if (System.currentTimeMillis() - this.lastAccessTimeStamp > this.grid.storeTimeout) {
  207.         this.store();
  208.       }
  209.     }
  210.    
  211.     private void load() {
  212.       assert this.objRefs == null : "Chunk.load(): Array not null!";
  213.       this.objRefs = this.grid.chunkStore.tryLoadChunk(this.x, this.y);
  214.       if (this.objRefs == null) {
  215.         this.objRefs = new int[this.chunkSize][this.chunkSize];
  216.         this.grid.populateChunk(this, this.grid.chunkStore.createChunk(this.x, this.y, this.chunkSize), this.chunkSize);
  217.       }
  218.     }
  219.    
  220.     private void loadIfNecessary() {
  221.       if (this.objRefs == null) {
  222.         this.load();
  223.       }
  224.     }
  225.    
  226.     public void set(int x, int y, int ref) {
  227.       this.touch();
  228.       this.loadIfNecessary();
  229.       this.objRefs[x][y] = ref;
  230.     }
  231.    
  232.     public int get(int x, int y) {
  233.       this.touch();
  234.       this.loadIfNecessary();
  235.       return this.objRefs[x][y];
  236.     }
  237.    
  238.   }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment