Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FileBackedRegionBitMatrix extends BitMatrix
- {
- public FileBackedRegionBitMatrix(File folder, int regionSize, int regionsToCache)
- {
- this.folder = folder;
- this.regionSize = regionSize;
- this.regions = new CacheMap<Point, BitMatrix>(regionsToCache);
- //regions.registerDecacheListener((ItemDecachedListener<Point, BitMatrix>) (Point key, BitMatrix value)
- // -> saveRegion(value, key.x, key.y));
- regions.registerDecacheListener(new CacheMap.ItemDecachedListener<Point, BitMatrix>()
- {
- @Override
- public void onDecached(Point key, BitMatrix value)
- { saveRegion(value, key.x, key.y); }
- });
- }
- File folder;
- protected final int regionSize; // 512
- CacheMap<Point, BitMatrix> regions; // 400
- public Point getRegionCoOrds(int x, int y)
- {
- int regionX = ((x < 0) ? (x / regionSize - 1) : (x / regionSize));
- int regionY = ((y < 0) ? (y / regionSize - 1) : (y / regionSize));
- return new Point(regionX, regionY);
- }
- public Point getInRegionCoOrds(int x, int y)
- {
- int inRegionX = ((x < 0) ? -(x + 1) : x) % regionSize;
- int inRegionY = ((y < 0) ? -(y + 1) : y) % regionSize;
- return new Point(inRegionX, inRegionY);
- }
- @Override
- protected List<BitSet> getRelevantBitList(int x, int y)
- { throw new UnsupportedOperationException("FileBackedBitMatrix doesn't use bit lists."); }
- protected BitMatrix getRegionAt(int x, int y)
- {
- Point regionCoOrds = getRegionCoOrds(x, y);
- BitMatrix region = regions.get(regionCoOrds);
- if(region != null)
- return region;
- region = loadRegion(regionCoOrds.x, regionCoOrds.y);
- if(region == null)
- region = createRegion(regionCoOrds.x, regionCoOrds.y);
- regions.put(regionCoOrds, region);
- return region;
- }
- @Override
- public boolean get(int x, int y)
- {
- Point inRegionCoOrds = getInRegionCoOrds(x, y);
- return getRegionAt(x, y).get(inRegionCoOrds.x, inRegionCoOrds.y);
- }
- @Override
- public void set(int x, int y, boolean newVal)
- {
- Point inRegionCoOrds = getInRegionCoOrds(x, y);
- getRegionAt(x, y).set(inRegionCoOrds.x, inRegionCoOrds.y, newVal);
- }
- protected void saveRegion(BitMatrix region, int regionX, int regionY)
- {
- folder.mkdirs();
- String fileName = "RoadRegion_" + regionX + "_" + regionY + ".bin";
- File file = new File(folder, fileName);
- FileOutputStream output;
- try
- { if(file.exists()) Files.delete(file.toPath()); }
- catch(IOException e)
- { throw new RuntimeException("File \"" + fileName + "\" could not be deleted.", e); }
- try
- { file.createNewFile(); }
- catch(IOException e)
- { throw new RuntimeException("File \"" + fileName + "\" could not be created. Did it already exist?", e); }
- try
- { output = new FileOutputStream(file); }
- catch(FileNotFoundException e)
- { throw new IllegalArgumentException("File passed to loadRegionFromFile doesn't exist.", e); }
- try
- {
- for(int y = 0; y < regionSize; y++)
- {
- for(int x = 0; x < regionSize; x += 8)
- {
- int b = 0;
- for(int i = Math.min(x + 7, regionSize - 1); i >= x; i--)
- b = (b << 1) | (region.get(i, y) ? 1 : 0);
- output.write(b);
- }
- }
- output.close();
- }
- catch(IOException e)
- { throw new RuntimeException("Error while writing binary file in saveRegion.", e); }
- }
- public void save()
- {
- for(Map.Entry<Point, BitMatrix> entry : regions.entrySet())
- saveRegion(entry.getValue(), entry.getKey().x, entry.getKey().y);
- }
- protected BitMatrix loadRegion(int regionX, int regionY)
- {
- String expectedFileName = "RoadRegion_" + regionX + "_" + regionY + ".bin";
- folder.mkdirs();
- for(File i : folder.listFiles())
- if(i.getName().equals(expectedFileName))
- return loadRegionFromFile(i);
- return null;
- }
- protected BitMatrix loadRegionFromFile(File file)
- {
- BitMatrix region = new BitMatrix();
- InputStream input;
- try
- { input = new BufferedInputStream(new FileInputStream(file)); }
- catch(FileNotFoundException e)
- { throw new IllegalArgumentException("File passed to loadRegionFromFile doesn't exist.", e); }
- try
- {
- for(int y = 0; y < regionSize; y++)
- {
- for(int x = 0; x < regionSize; x += 8)
- {
- int b = input.read();
- if(b < 0)
- throw new EOFException();
- for(int i = x; i < x + 8 && i < regionSize; i++)
- {
- region.set(i, y, (b & 1) != 0);
- b >>>= 1;
- }
- }
- }
- }
- catch(IOException e)
- { throw new RuntimeException("Error while parsing binary file passed to loadRegionFromFile.", e); }
- return region;
- }
- protected BitMatrix createRegion(int regionX, int regionY)
- { return new BitMatrix(); }
- }
Advertisement
Add Comment
Please, Sign In to add comment