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

23
LINES

< > BotCompany Repo | #1027477 // DynamicTopTen - not-so-optimized version using an ArrayList. Could use TreeSetWithDuplicates

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

Libraryless. Click here for Pure Java version (2557L/16K).

1  
sclass DynamicTopTen<A> {
2  
  new LPair<A, Double> list;
3  
  int n = 10;
4  
  
5  
  void add(A a, double score) {
6  
    put(a, score);
7  
  }
8  
  
9  
  void put(A a, double score) {
10  
    int i = l(list);
11  
    while (i > 0 && list.get(i-1).b < score)
12  
      --i;
13  
    if (i >= n) ret;
14  
    if (l(list) == n) removeLast(list);
15  
    list.add(i, pair(a, score));
16  
  }
17  
  
18  
  L<A> get() {
19  
    ret pairsA(list);
20  
  }
21  
  
22  
  LPair<A, Double> withScores() { ret list; }
23  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027477
Snippet name: DynamicTopTen - not-so-optimized version using an ArrayList. Could use TreeSetWithDuplicates
Eternal ID of this version: #1027477/6
Text MD5: 1c1ea8338bc25a0aa114ffc83c19d089
Transpilation MD5: 1114cb31d4e1b8ca46b2966706a4e0d3
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-04-18 12:27:03
Source code size: 453 bytes / 23 lines
Pitched / IR pitched: No / No
Views / Downloads: 242 / 585
Version history: 5 change(s)
Referenced in: [show references]