sclass MultiSetAndTopTen { int topTenTargetLength = 10; Map ms; L topTen; *() {} *(Map *ms, L *topTen) {} void add(A a) { insertToTopTen(a, longMultiSet_add(ms, a)); } void insertToTopTen(S 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"); pnl(map(topTen, func(S s) { s + " - " + map.get(s) })); } }