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

sclass UniqueList<A> extends RandomAccessAbstractList<A> {
  new L<A> list;
  new Map<A, Int> index; // position of each element
  
  public A get(int i) { ret list.get(i); }
  public int size() { ret list.size(); }
  
  // couldn't come up with a better name here... add element or
  // get its index if it's already there
  int addOrGetIndex(A a) {
    Int i = index.get(a);
    if (i == null) {
      index.put(a, i = size());
      list.add(a);
    }
    ret i;
  }
  
  public bool add(A a) {
    int n = size();
    ret addOrGetIndex(a) == n;
  }
}

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: 156 / 425
Version history: 3 change(s)
Referenced in: [show references]