📄 linkimpl.java
字号:
SubList(LinkImpl<T> list, int fromIndex, int toIndex) {
if (fromIndex < 0) {
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
}
if (toIndex > list.size()) {
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
}
if (fromIndex > toIndex) {
throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
}
l = list;
offset = fromIndex;
size = toIndex - fromIndex;
}
public T set(int index, T element) {
rangeCheck(index);
return l.set(index+offset, element);
}
public T get(int index) {
rangeCheck(index);
return l.get(index+offset);
}
public int size() {
return size;
}
public void add(int index, T element) {
if (index<0 || index>size) {
throw new IndexOutOfBoundsException();
}
l.add(index+offset, element);
size++;
}
public T remove(int index) {
rangeCheck(index);
T result = l.remove(index+offset);
size--;
return result;
}
protected void removeRange(int fromIndex, int toIndex) {
l.removeRange(fromIndex+offset, toIndex+offset);
size -= (toIndex-fromIndex);
}
public boolean addAll(Collection<? extends T> c) {
return addAll(size, c);
}
public boolean addAll(int index, Collection<? extends T> c) {
if (index<0 || index>size) {
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
}
int cSize = c.size();
if (cSize==0) {
return false;
}
l.addAll(offset+index, c);
size += cSize;
return true;
}
public Iterator<T> iterator() {
return listIterator();
}
public ListIterator<T> listIterator(final int index) {
if (index<0 || index>size) {
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
}
return new ListIterator<T>() {
private ListIterator<T> i = l.listIterator(index+offset);
public boolean hasNext() {
return nextIndex() < size;
}
public T next() {
if (hasNext()) {
return i.next();
} else {
throw new NoSuchElementException();
}
}
public boolean hasPrevious() {
return previousIndex() >= 0;
}
public T 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();
size--;
}
public void set(T o) {
i.set(o);
}
public void add(T o) {
i.add(o);
size++;
}
};
}
public List<T> subList(int fromIndex, int toIndex) {
return new SubList<T>(l, offset+fromIndex, offset+toIndex);
}
private void rangeCheck(int index) {
if (index<0 || index>=size) {
throw new IndexOutOfBoundsException("Index: "+index+",Size: "+size);
}
}
}
static class LinkIterator<T extends IPersistent> implements PersistentIterator, ListIterator<T> {
private Link<T> link;
private int i;
private int last;
LinkIterator(Link<T> link, int index) {
this.link = link;
i = index;
last = -1;
}
public boolean hasNext() {
return i < link.size();
}
public T next() throws NoSuchElementException {
if (!hasNext()) {
throw new NoSuchElementException();
}
last = i;
return link.get(i++);
}
public int nextIndex() {
return i;
}
public boolean hasPrevious() {
return i > 0;
}
public T previous() throws NoSuchElementException {
if (!hasPrevious()) {
throw new NoSuchElementException();
}
return link.get(last = --i);
}
public int previousIndex() {
return i-1;
}
public int nextOid() throws NoSuchElementException {
if (!hasNext()) {
throw new NoSuchElementException();
}
return link.getRaw(i++).getOid();
}
public void remove() {
if (last < 0) {
throw new IllegalStateException();
}
link.removeObject(last);
if (last < i) {
i -= 1;
}
}
public void set(T o) {
if (last < 0) {
throw new IllegalStateException();
}
link.setObject(last, o);
}
public void add(T o) {
link.insert(i++, o);
last = -1;
}
}
public boolean remove(Object o) {
int i = indexOf(o);
if (i >= 0) {
remove(i);
return true;
}
return false;
}
public boolean containsAll(Collection<?> c) {
Iterator<?> e = c.iterator();
while (e.hasNext()) {
if(!contains(e.next())) {
return false;
}
}
return true;
}
public boolean addAll(Collection<? extends T> c) {
boolean modified = false;
Iterator<? extends T> e = c.iterator();
while (e.hasNext()) {
if (add(e.next())) {
modified = true;
}
}
return modified;
}
public boolean removeAll(Collection<?> c) {
boolean modified = false;
Iterator<?> e = iterator();
while (e.hasNext()) {
if (c.contains(e.next())) {
e.remove();
modified = true;
}
}
return modified;
}
public boolean retainAll(Collection<?> c) {
boolean modified = false;
Iterator<T> e = iterator();
while (e.hasNext()) {
if (!c.contains(e.next())) {
e.remove();
modified = true;
}
}
return modified;
}
public Iterator<T> iterator() {
return new LinkIterator<T>(this, 0);
}
public ListIterator<T> listIterator(int index) {
return new LinkIterator<T>(this, index);
}
public ListIterator<T> listIterator() {
return listIterator(0);
}
private final T loadElem(int i)
{
IPersistent elem = arr[i];
if (elem != null && elem.isRaw()) {
elem = ((StorageImpl)elem.getStorage()).lookupObject(elem.getOid(), null);
}
return (T)elem;
}
public Iterator<T> select(Class cls, String predicate) {
Query<T> query = new QueryImpl<T>(null);
return query.select(cls, iterator(), predicate);
}
LinkImpl() {}
LinkImpl(int initSize) {
arr = new IPersistent[initSize];
}
LinkImpl(T[] arr, IPersistent owner) {
this.arr = arr;
this.owner = owner;
used = arr.length;
}
IPersistent[] arr;
int used;
transient IPersistent owner;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -