sclass MultiSetAndTopTen {
int maxSize = 100000, topTenTargetLength = 10;
Map ms;
L topTen;
*() {}
*(Map *ms, L *topTen) {}
void add(A a) {
long count = longMultiSet_add(ms, a);
insertToTopTen(a, count);
if (l(ms) >= maxSize)
longMultiSet_dropBottomPart(ms, iround(maxSize*0.1));
}
void insertToTopTen(A a, long count) {
int originalIndex = indexOf(topTen, a);
int i = originalIndex < 0 ? l(topTen) : originalIndex;
while (i > 0 && count >= toLong(ms.get(topTen.get(i-1))))
--i;
if (i >= topTenTargetLength || i == originalIndex) ret;
if (originalIndex >= 0) topTen.remove(originalIndex);
topTen.add(i, a);
truncateList(topTen, topTenTargetLength);
}
void printTopTen {
printAsciiHeading("TOP TEN (OF " + l(ms) + ")");
pnl(map(topTen, func(S s) { s + " - " + ms.get(s) }));
}
}