Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

44
LINES

< > BotCompany Repo | #1031875 // MRUAndAllTimeTop_optimized - keep sorted top ten list

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (10127L/55K).

// synchronized except for iterator
persistable sclass MRUAndAllTimeTop_optimized<A> {
  Map<A, Bool> mru = syncMRUCache(10); // most recently used
  new OptimizedMultiSet<A> 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<A> 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<A> 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<A> recent() {
    ret reversedKeysList(mru);
  }
  
  A mostRecent() {
    ret last(keys(mru));
  }
}

Author comment

Began life as a copy of #1024856

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1031875
Snippet name: MRUAndAllTimeTop_optimized - keep sorted top ten list
Eternal ID of this version: #1031875/12
Text MD5: 36cde0f285098a16e614db74876b80de
Transpilation MD5: b14d5f5e062dba2d25680f21bea61d0d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-12 03:58:13
Source code size: 1238 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 217 / 452
Version history: 11 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)