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).

1  
// persistable and synchronized
2  
sclass MRUAndAllTimeTop_naive<A> {
3  
  Map<A, Bool> mru = syncMRUCache(10); // most recently used
4  
  new MultiSet<A> allTimeTop;
5  
  int size;
6  
  
7  
  // increase a's score in all time top and move to front int MRU
8  
  void add(A a) {
9  
    mru.remove(a); // force re-insertion
10  
    mru.put(a, true);
11  
    allTimeTop.add(a);
12  
  }
13  
  
14  
  void addAll(Iterable<A> l) { fOr (A a : l) add(a); }
15  
  
16  
  // may count elements twice
17  
  int size() {
18  
    ret l(mru) + allTimeTop.uniqueSize();
19  
  }
20  
  
21  
  // interleave latest and most often accessed elements
22  
  ItIt<A> mixedIterator() {
23  
    ret uniqueIterator(roundRobinCombinedIterator(
24  
      reversedIterator(keysList(mru)),
25  
      iterator(allTimeTop.highestFirst())));
26  
  }
27  
}

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: 157 / 209
Referenced in: [show references]