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

27
LINES

< > BotCompany Repo | #1035912 // MRUAndAllTimeTop_naive

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

Libraryless. Click here for Pure Java version (9799L/53K).

// persistable and synchronized
sclass MRUAndAllTimeTop_naive<A> {
  Map<A, Bool> mru = syncMRUCache(10); // most recently used
  new MultiSet<A> allTimeTop;
  int size;
  
  // increase a's score in all time top and move to front int MRU
  void add(A a) {
    mru.remove(a); // force re-insertion
    mru.put(a, true);
    allTimeTop.add(a);
  }
  
  void addAll(Iterable<A> l) { fOr (A a : l) add(a); }
  
  // may count elements twice
  int size() {
    ret l(mru) + allTimeTop.uniqueSize();
  }
  
  // interleave latest and most often accessed elements
  ItIt<A> mixedIterator() {
    ret uniqueIterator(roundRobinCombinedIterator(
      reversedIterator(keysList(mru)),
      iterator(allTimeTop.highestFirst())));
  }
}

Author comment

Began life as a copy of #1024856

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035912
Snippet name: MRUAndAllTimeTop_naive
Eternal ID of this version: #1035912/1
Text MD5: f7e9602e995fcad6b56eb40f179e7250
Transpilation MD5: 9f7d8398f1f5082c6c2cfaafee77bfd4
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:36:12
Source code size: 752 bytes / 27 lines
Pitched / IR pitched: No / No
Views / Downloads: 74 / 98
Referenced in: [show references]