!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 = true; bool debug; // pageSize is in ints int pageShift, pageSize, maxCachedPages; new IntObjectHashMap cache; CacheEntry newestCacheEntry, oldestCacheEntry; // stats int pageLoads, evictions; sclass CacheEntry { int page; int[] data; CacheEntry newer, older; // MRU list } *() { pageShift = highestOneBit(1024); 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) { 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 { ++evictions; CacheEntry e = oldestCacheEntry; if (debug) print("Evicting page " + e.page); cache.remove(e.page); oldestCacheEntry = e.newer; if (oldestCacheEntry == null) newestCacheEntry = null; } CacheEntry loadPage(int page) ctex { ++pageLoads; if (cacheFull()) evictAPage(); if (debug) print("Loading page " + page); raf.seek(((long) page) << (pageShift+2)); 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; ret e; } public void set(int idx, int val) { fail("read-only"); } public int size() { ret size; } }