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

108
LINES

< > BotCompany Repo | #1004042 // IndexedList - use ContentsIndexedList instead

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

Transpiled version (4767L) is out of date.

static class IndexedList<A> extends AbstractList<A> {
  new ArrayList<A> l;
  MultiMap<A, Int> index;
  static bool debug;
  
  *() {}
  *(L<A> x) {
    l.ensureCapacity(l(x));
    addAll(x);
  }
  
  // required immutable list methods
  
  @Override
  public A get(int i) {
    ret l.get(i);
  }
  
  @Override
  public int size() {
    ret l.size();
  }
  
  // required mutable list methods
  
  @Override
  public A set(int i, A a) {
    unindex(i);
    A prev = l.get(i);
    l.set(i, a);
    index(i);
    ret prev;
  }
  
  @Override
  public void add(int i, A a) {
    if (i != l.size()) {
      l.add(i, a);
      index = null;
    } else {
      l.add(i, a);
      index(i);
    }
  }
  
  @Override
  public A remove(int i) {
    A a = l.get(i);
    l.remove(i);
    index = null;
    ret a;
  }
  
  // speed up methods

  @Override  
  protected void removeRange(int fromIndex, int toIndex) {
    l.subList(fromIndex, toIndex).clear();
    index = null;
  }
  
  public int indexOf(O a) {
    ensureIndexed();
    L<Int> positions = index.get((A) a);
    int n = l(positions);
    int min = -1;
    if (n != 0) {
      min = positions.get(0);
      for (int i = 1; i < n; i++) {
        int p = positions.get(i);
        if (p < min) min = p;
      }
    }
    if (debug) print("IndexedList.indexOf " + a + " => " + min);
    ret min;
  }
  
  @Override
  public bool contains(O a) {
    ensureIndexed();
    bool b = index.containsKey((A) a);
    if (debug) print("IndexedList.contains " + a + " => " + b);
    ret b;
  }
  
  // indexing methods
  
  void unindex(int i) {
    if (index != null)
      index.remove(l.get(i), i);
  }
  
  void index(int i) {
    if (index == null)
      ensureIndexed();
    else
      index.put(l.get(i), i);
  }
  
  void ensureIndexed() {
    if (index == null) {
      index = new MultiMap;
      int n = size();
      for (int i = 0; i < n; i++)
        index.put(l.get(i), i);
    }
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004042
Snippet name: IndexedList - use ContentsIndexedList instead
Eternal ID of this version: #1004042/2
Text MD5: 654743973be99cb1e16940a41e94da66
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-15 01:39:32
Source code size: 2049 bytes / 108 lines
Pitched / IR pitched: No / No
Views / Downloads: 599 / 1225
Version history: 1 change(s)
Referenced in: #1004045 - IndexedList2 - maintains only a multiset of all elements
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)