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

23
LINES

< > BotCompany Repo | #1029067 // UniqueList - list where each element appears only once [mixture of ArrayList and LinkedHashSet]

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

Libraryless. Click here for Pure Java version (51L/1K).

1  
sclass UniqueList<A> extends RandomAccessAbstractList<A> {
2  
  new L<A> list;
3  
  new Map<A, Int> index; // position of each element
4  
  
5  
  public A get(int i) { ret list.get(i); }
6  
  public int size() { ret list.size(); }
7  
  
8  
  // couldn't come up with a better name here... add element or
9  
  // get its index if it's already there
10  
  int addOrGetIndex(A a) {
11  
    Int i = index.get(a);
12  
    if (i == null) {
13  
      index.put(a, i = size());
14  
      list.add(a);
15  
    }
16  
    ret i;
17  
  }
18  
  
19  
  public bool add(A a) {
20  
    int n = size();
21  
    ret addOrGetIndex(a) == n;
22  
  }
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: #1029067
Snippet name: UniqueList - list where each element appears only once [mixture of ArrayList and LinkedHashSet]
Eternal ID of this version: #1029067/4
Text MD5: 3bfe23a6d6a0749311a98e0dec77082f
Transpilation MD5: 2ceb0355fd50cd3aef3d0fe8e32283af
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-18 23:17:28
Source code size: 576 bytes / 23 lines
Pitched / IR pitched: No / No
Views / Downloads: 161 / 433
Version history: 3 change(s)
Referenced in: [show references]