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

1  
// differences to AbstractList.subList / ArrayList.subList:
2  
// -probably doesn't handle modCount the same way
3  
//
4  
// made from AbstractList.subList
5  
sclass SubList<E> extends AbstractList<E> implements ISubList<E> {
6  
  L<E> root;
7  
  SubList<E> parent;
8  
  int offset;
9  
  int size;
10  
11  
  /**
12  
   * Constructs a sublist of an arbitrary AbstractList, which is
13  
   * not a SubList itself.
14  
   */
15  
  public SubList(L<E> root, int fromIndex, int toIndex) {
16  
    if (root cast SubList) {
17  
      this.parent = root;
18  
      this.root = root.root;
19  
      this.offset = root.offset + fromIndex;
20  
    } else {
21  
      this.parent = null;
22  
      this.root = root;
23  
      this.offset = fromIndex;
24  
    }
25  
    this.size = toIndex - fromIndex;
26  
  }
27  
28  
  public E set(int index, E element) {
29  
      Objects.checkIndex(index, size);
30  
      checkForComodification();
31  
      return root.set(offset + index, element);
32  
  }
33  
34  
  public E get(int index) {
35  
      Objects.checkIndex(index, size);
36  
      checkForComodification();
37  
      return root.get(offset + index);
38  
  }
39  
40  
  public int size() {
41  
      checkForComodification();
42  
      return size;
43  
  }
44  
45  
  public void add(int index, E element) {
46  
      rangeCheckForAdd(index);
47  
      checkForComodification();
48  
      root.add(offset + index, element);
49  
      updateSizeAndModCount(1);
50  
  }
51  
52  
  public E remove(int index) {
53  
      Objects.checkIndex(index, size);
54  
      checkForComodification();
55  
      E result = root.remove(offset + index);
56  
      updateSizeAndModCount(-1);
57  
      return result;
58  
  }
59  
60  
  protected void removeRange(int fromIndex, int toIndex) {
61  
      checkForComodification();
62  
      root.subList(offset + fromIndex, offset + toIndex).clear();
63  
      updateSizeAndModCount(fromIndex - toIndex);
64  
  }
65  
66  
  public boolean addAll(Collection<? extends E> c) {
67  
      return addAll(size, c);
68  
  }
69  
70  
  public boolean addAll(int index, Collection<? extends E> c) {
71  
      rangeCheckForAdd(index);
72  
      int cSize = c.size();
73  
      if (cSize==0)
74  
          return false;
75  
      checkForComodification();
76  
      root.addAll(offset + index, c);
77  
      updateSizeAndModCount(cSize);
78  
      return true;
79  
  }
80  
81  
  public Iterator<E> iterator() {
82  
      return listIterator();
83  
  }
84  
85  
  public ListIterator<E> listIterator(int index) {
86  
      checkForComodification();
87  
      rangeCheckForAdd(index);
88  
89  
      return new ListIterator<E>() {
90  
          private final ListIterator<E> i =
91  
                  root.listIterator(offset + index);
92  
93  
          public boolean hasNext() {
94  
              return nextIndex() < size;
95  
          }
96  
97  
          public E next() {
98  
              if (hasNext())
99  
                  return i.next();
100  
              else
101  
                  throw new NoSuchElementException();
102  
          }
103  
104  
          public boolean hasPrevious() {
105  
              return previousIndex() >= 0;
106  
          }
107  
108  
          public E previous() {
109  
              if (hasPrevious())
110  
                  return i.previous();
111  
              else
112  
                  throw new NoSuchElementException();
113  
          }
114  
115  
          public int nextIndex() {
116  
              return i.nextIndex() - offset;
117  
          }
118  
119  
          public int previousIndex() {
120  
              return i.previousIndex() - offset;
121  
          }
122  
123  
          public void remove() {
124  
              i.remove();
125  
              updateSizeAndModCount(-1);
126  
          }
127  
128  
          public void set(E e) {
129  
              i.set(e);
130  
          }
131  
132  
          public void add(E e) {
133  
              i.add(e);
134  
              updateSizeAndModCount(1);
135  
          }
136  
      };
137  
  }
138  
139  
  public List<E> subList(int fromIndex, int toIndex) {
140  
      _subListRangeCheck(fromIndex, toIndex, size);
141  
      return new SubList<>(this, fromIndex, toIndex);
142  
  }
143  
144  
  private void rangeCheckForAdd(int index) {
145  
      if (index < 0 || index > size)
146  
          throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
147  
  }
148  
149  
  private String outOfBoundsMsg(int index) {
150  
      return "Index: "+index+", Size: "+size;
151  
  }
152  
153  
  private void checkForComodification() {}
154  
155  
  private void updateSizeAndModCount(int sizeChange) {
156  
    SubList<E> slist = this;
157  
    do {
158  
      slist.size += sizeChange;
159  
      slist = slist.parent;
160  
    } while (slist != null);
161  
  }
162  
  
163  
  public L<E> rootList() { ret root; }
164  
  public L<E> parentList() { ret parent; }
165  
  public int subListOffset() { ret offset; }
166  
}

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: 193 / 372
Version history: 6 change(s)
Referenced in: [show references]