!include once #1027304 // Eclipse Collections // read-only, so far. not thread-safe final sclass BufferedDiskIntMemory implements IIntMemory, AutoCloseable { File file; int size; RandomAccessFile raf; bool bigEndian; int pageShift, pageSize, maxCachedPages; new IntObjectHashMap cache; CacheEntry newestCacheEntry, oldestCacheEntry; sclass CacheEntry { int page; int[] data; CacheEntry newer, older; // MRU list } *() { pageShift = highestOneBit(4096); pageSize = 1 << pageShift; maxCachedPages = (128*1024*1024) >> pageShift; } *(File *file) { this(); size = toInt_safe(fileSize(file)/4); raf = randomAccessFileForReading(file); } public void close { dispose raf; } public int get(int idx) ctex { rangeCheck(idx, size); int page = idx >> pageShift; CacheEntry e = cache.get(page); if (e == null) e = loadPage(page); else touchPage(e); ret e.data[idx & (pageSize-1)]; } void touchPage(CacheEntry e) { if (e == newestCacheEntry) ret; if (e.older != null) e.older.newer = e.newer; else oldestCacheEntry = e.newer; e.newer.older = e.older; e.newer = null; newestCacheEntry = e; } bool cacheFull() { ret cache.size() >= maxCachedPages; } void evictAPage { CacheEntry e = oldestCacheEntry; cache.remove(e.page); oldestCacheEntry = e.newer; if (oldestCacheEntry == null) newestCacheEntry = null; } void loadPage(int page) { if (cacheFull()) evictAPage(); raf.seek((((long) (page << pageShift)) << 4); byte[] buf = new[pageSize*4]; raf.read(buf); new CacheEntry e; e.page = page; e.data = bigEndian ? intArrayFromBytes(buf) : intArrayFromBytes_littleEndian(buf); e.older = newestCacheEntry; cache.put(page, e); newestCacheEntry = e; if (oldestCacheEntry == null) oldestCacheEntry = e; } public void set(int idx, int val) { fail("read-only"); } public int size() { ret size; } }