// synchronized except for iterator
persistable sclass MRUAndAllTimeTop_optimized {
Map mru = syncMRUCache(10); // most recently used
new OptimizedMultiSet allTimeTop;
int size;
// 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);
}
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) + ")";
}
}