// synchronized except for iterator
persistable sclass MRUAndAllTimeTop_optimized {
Map mru = syncMRUCache(10); // most recently used
new OptimizedMultiSet allTimeTop;
int allTimeMaxSize = 100;
// increase a's score in all time top and move to front int MRU
synchronized void add(A a) {
mru.remove(a); // force re-insertion
mru.put(a, true);
allTimeTop.add(a);
allTimeTop.truncate(allTimeMaxSize);
}
synchronized void addAll(Iterable l) { fOr (A a : l) add(a); }
// may count elements twice
synchronized int size() {
ret l(mru) + allTimeTop.uniqueSize();
}
// interleave latest and most often accessed elements
// iterator not synchronized
synchronized ItIt mixedIterator() {
ret uniqueIterator(roundRobinCombinedIterator(
reversedIterator(keysList(mru)),
iterator(allTimeTop.highestFirst())));
}
toString {
ret shortClassName(this) + " (" + n2(size()) + ")";
}
// MRU elements in last access order
// (These aren't that optimized, really. Should replace
// LinkedHashMap.)
Cl recent() {
ret reversedKeysList(mru);
}
A mostRecent() {
ret last(keys(mru));
}
}