HaniiPuppy

FileBackedRegionBitMatrix

Aug 28th, 2016
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.72 KB | None | 0 0
  1. public class FileBackedRegionBitMatrix extends BitMatrix
  2. {
  3.     public FileBackedRegionBitMatrix(File folder, int regionSize, int regionsToCache)
  4.     {
  5.         this.folder = folder;
  6.         this.regionSize = regionSize;
  7.         this.regions = new CacheMap<Point, BitMatrix>(regionsToCache);
  8.        
  9.         //regions.registerDecacheListener((ItemDecachedListener<Point, BitMatrix>) (Point key, BitMatrix value)
  10.         //                                  -> saveRegion(value, key.x, key.y));
  11.         regions.registerDecacheListener(new CacheMap.ItemDecachedListener<Point, BitMatrix>()
  12.         {
  13.             @Override
  14.             public void onDecached(Point key, BitMatrix value)
  15.             { saveRegion(value, key.x, key.y); }
  16.         });
  17.     }
  18.    
  19.     File folder;
  20.     protected final int regionSize; // 512
  21.     CacheMap<Point, BitMatrix> regions; // 400
  22.    
  23.     public Point getRegionCoOrds(int x, int y)
  24.     {
  25.         int regionX = ((x < 0) ? (x / regionSize - 1) : (x / regionSize));
  26.         int regionY = ((y < 0) ? (y / regionSize - 1) : (y / regionSize));
  27.        
  28.         return new Point(regionX, regionY);
  29.     }
  30.    
  31.     public Point getInRegionCoOrds(int x, int y)
  32.     {
  33.         int inRegionX = ((x < 0) ? -(x + 1) : x) % regionSize;
  34.         int inRegionY = ((y < 0) ? -(y + 1) : y) % regionSize;
  35.         return new Point(inRegionX, inRegionY);
  36.     }
  37.    
  38.     @Override
  39.     protected List<BitSet> getRelevantBitList(int x, int y)
  40.     { throw new UnsupportedOperationException("FileBackedBitMatrix doesn't use bit lists."); }
  41.    
  42.     protected BitMatrix getRegionAt(int x, int y)
  43.     {
  44.         Point regionCoOrds = getRegionCoOrds(x, y);
  45.         BitMatrix region = regions.get(regionCoOrds);
  46.        
  47.         if(region != null)
  48.             return region;
  49.        
  50.         region = loadRegion(regionCoOrds.x, regionCoOrds.y);
  51.        
  52.         if(region == null)
  53.             region = createRegion(regionCoOrds.x, regionCoOrds.y);
  54.        
  55.         regions.put(regionCoOrds, region);
  56.         return region;
  57.     }
  58.    
  59.     @Override
  60.     public boolean get(int x, int y)
  61.     {
  62.         Point inRegionCoOrds = getInRegionCoOrds(x, y);
  63.         return getRegionAt(x, y).get(inRegionCoOrds.x, inRegionCoOrds.y);
  64.     }
  65.    
  66.     @Override
  67.     public void set(int x, int y, boolean newVal)
  68.     {
  69.         Point inRegionCoOrds = getInRegionCoOrds(x, y);
  70.         getRegionAt(x, y).set(inRegionCoOrds.x, inRegionCoOrds.y, newVal);
  71.     }
  72.    
  73.     protected void saveRegion(BitMatrix region, int regionX, int regionY)
  74.     {
  75.         folder.mkdirs();
  76.         String fileName = "RoadRegion_" + regionX + "_" + regionY + ".bin";
  77.         File file = new File(folder, fileName);
  78.         FileOutputStream output;
  79.        
  80.         try
  81.         { if(file.exists()) Files.delete(file.toPath()); }
  82.         catch(IOException e)
  83.         { throw new RuntimeException("File \"" + fileName + "\" could not be deleted.", e); }
  84.            
  85.         try
  86.         { file.createNewFile(); }
  87.         catch(IOException e)
  88.         { throw new RuntimeException("File \"" + fileName + "\" could not be created. Did it already exist?", e); }
  89.        
  90.         try
  91.         { output = new FileOutputStream(file); }
  92.         catch(FileNotFoundException e)
  93.         { throw new IllegalArgumentException("File passed to loadRegionFromFile doesn't exist.", e); }
  94.        
  95.         try
  96.         {
  97.             for(int y = 0; y < regionSize; y++)
  98.             {
  99.                 for(int x = 0; x < regionSize; x += 8)
  100.                 {
  101.                     int b = 0;
  102.                    
  103.                     for(int i = Math.min(x + 7, regionSize - 1); i >= x; i--)
  104.                         b = (b << 1) | (region.get(i, y) ? 1 : 0);
  105.                    
  106.                     output.write(b);
  107.                 }
  108.             }
  109.            
  110.             output.close();
  111.         }
  112.         catch(IOException e)
  113.         { throw new RuntimeException("Error while writing binary file in saveRegion.", e); }
  114.     }
  115.    
  116.     public void save()
  117.     {
  118.         for(Map.Entry<Point, BitMatrix> entry : regions.entrySet())
  119.             saveRegion(entry.getValue(), entry.getKey().x, entry.getKey().y);
  120.     }
  121.    
  122.     protected BitMatrix loadRegion(int regionX, int regionY)
  123.     {
  124.         String expectedFileName = "RoadRegion_" + regionX + "_" + regionY + ".bin";
  125.         folder.mkdirs();
  126.        
  127.         for(File i : folder.listFiles())
  128.             if(i.getName().equals(expectedFileName))
  129.                 return loadRegionFromFile(i);
  130.        
  131.         return null;
  132.     }
  133.    
  134.     protected BitMatrix loadRegionFromFile(File file)
  135.     {
  136.         BitMatrix region = new BitMatrix();
  137.         InputStream input;
  138.        
  139.         try
  140.         { input =  new BufferedInputStream(new FileInputStream(file)); }
  141.         catch(FileNotFoundException e)
  142.         { throw new IllegalArgumentException("File passed to loadRegionFromFile doesn't exist.", e); }
  143.        
  144.         try
  145.         {
  146.             for(int y = 0; y < regionSize; y++)
  147.             {
  148.                 for(int x = 0; x < regionSize; x += 8)
  149.                 {
  150.                     int b = input.read();
  151.  
  152.                     if(b < 0)
  153.                         throw new EOFException();
  154.  
  155.                     for(int i = x; i < x + 8 && i < regionSize; i++)
  156.                     {
  157.                         region.set(i, y, (b & 1) != 0);
  158.                         b >>>= 1;
  159.                     }
  160.                 }
  161.             }
  162.         }
  163.         catch(IOException e)
  164.         { throw new RuntimeException("Error while parsing binary file passed to loadRegionFromFile.", e); }
  165.        
  166.         return region;
  167.     }
  168.    
  169.     protected BitMatrix createRegion(int regionX, int regionY)
  170.     { return new BitMatrix(); }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment