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

166
LINES

< > BotCompany Repo | #1031884 // SubList - our own version of .subList which allows getting the original list and the range

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

Libraryless. Click here for Pure Java version (3472L/19K).

// differences to AbstractList.subList / ArrayList.subList:
// -probably doesn't handle modCount the same way
//
// made from AbstractList.subList
sclass SubList<E> extends AbstractList<E> implements ISubList<E> {
  L<E> root;
  SubList<E> parent;
  int offset;
  int size;

  /**
   * Constructs a sublist of an arbitrary AbstractList, which is
   * not a SubList itself.
   */
  public SubList(L<E> root, int fromIndex, int toIndex) {
    if (root cast SubList) {
      this.parent = root;
      this.root = root.root;
      this.offset = root.offset + fromIndex;
    } else {
      this.parent = null;
      this.root = root;
      this.offset = fromIndex;
    }
    this.size = toIndex - fromIndex;
  }

  public E set(int index, E element) {
      Objects.checkIndex(index, size);
      checkForComodification();
      return root.set(offset + index, element);
  }

  public E get(int index) {
      Objects.checkIndex(index, size);
      checkForComodification();
      return root.get(offset + index);
  }

  public int size() {
      checkForComodification();
      return size;
  }

  public void add(int index, E element) {
      rangeCheckForAdd(index);
      checkForComodification();
      root.add(offset + index, element);
      updateSizeAndModCount(1);
  }

  public E remove(int index) {
      Objects.checkIndex(index, size);
      checkForComodification();
      E result = root.remove(offset + index);
      updateSizeAndModCount(-1);
      return result;
  }

  protected void removeRange(int fromIndex, int toIndex) {
      checkForComodification();
      root.subList(offset + fromIndex, offset + toIndex).clear();
      updateSizeAndModCount(fromIndex - toIndex);
  }

  public boolean addAll(Collection<? extends E> c) {
      return addAll(size, c);
  }

  public boolean addAll(int index, Collection<? extends E> c) {
      rangeCheckForAdd(index);
      int cSize = c.size();
      if (cSize==0)
          return false;
      checkForComodification();
      root.addAll(offset + index, c);
      updateSizeAndModCount(cSize);
      return true;
  }

  public Iterator<E> iterator() {
      return listIterator();
  }

  public ListIterator<E> listIterator(int index) {
      checkForComodification();
      rangeCheckForAdd(index);

      return new ListIterator<E>() {
          private final ListIterator<E> i =
                  root.listIterator(offset + index);

          public boolean hasNext() {
              return nextIndex() < size;
          }

          public E next() {
              if (hasNext())
                  return i.next();
              else
                  throw new NoSuchElementException();
          }

          public boolean hasPrevious() {
              return previousIndex() >= 0;
          }

          public E previous() {
              if (hasPrevious())
                  return i.previous();
              else
                  throw new NoSuchElementException();
          }

          public int nextIndex() {
              return i.nextIndex() - offset;
          }

          public int previousIndex() {
              return i.previousIndex() - offset;
          }

          public void remove() {
              i.remove();
              updateSizeAndModCount(-1);
          }

          public void set(E e) {
              i.set(e);
          }

          public void add(E e) {
              i.add(e);
              updateSizeAndModCount(1);
          }
      };
  }

  public List<E> subList(int fromIndex, int toIndex) {
      _subListRangeCheck(fromIndex, toIndex, size);
      return new SubList<>(this, fromIndex, toIndex);
  }

  private void rangeCheckForAdd(int index) {
      if (index < 0 || index > size)
          throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
  }

  private String outOfBoundsMsg(int index) {
      return "Index: "+index+", Size: "+size;
  }

  private void checkForComodification() {}

  private void updateSizeAndModCount(int sizeChange) {
    SubList<E> slist = this;
    do {
      slist.size += sizeChange;
      slist = slist.parent;
    } while (slist != null);
  }
  
  public L<E> rootList() { ret root; }
  public L<E> parentList() { ret parent; }
  public int subListOffset() { ret offset; }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1031884
Snippet name: SubList - our own version of .subList which allows getting the original list and the range
Eternal ID of this version: #1031884/7
Text MD5: d19072dd68eb1b1d1a7fb05bee067020
Transpilation MD5: 28b5d9431540cc37cae07652a48df79e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-07-24 02:54:25
Source code size: 4400 bytes / 166 lines
Pitched / IR pitched: No / No
Views / Downloads: 192 / 371
Version history: 6 change(s)
Referenced in: [show references]