Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement subList() on primitive immutable lists #1155

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ public void appendString(
@Override
public ImmutableBooleanList subList(int fromIndex, int toIndex)
{
throw new UnsupportedOperationException("subList not yet implemented!");
return new ImmutableSubList<>(this, fromIndex, toIndex);
}

private Object writeReplace()
Expand Down Expand Up @@ -718,4 +718,111 @@ public boolean next()
return next;
}
}

protected static class ImmutableBooleanArraySubList
extends ImmutableBooleanArrayList
implements Serializable, RandomAccess //?
{
private final ImmutableBooleanArrayList original;
private final int offset;
private final int size;

protected ImmutableBooleanArraySubList(ImmutableBooleanArrayList 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 + ')');
}
this.original = list;
this.offset = fromIndex;
this.size = toIndex - fromIndex;
}

@Override
public T get(int index)
{
this.checkIfOutOfBounds(index);
return this.original.get(index + this.offset);
}

@Override
public int size()
{
return this.size;
}

@Override
public ImmutableBooleanList newWith(boolean element)
{
BitSet newItems = (BitSet) this.original.items.clone();
if (element)
{
newItems.set(this.size);
}
return new ImmutableBooleanArrayList(newItems, this.size + 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look like it uses the from offset.

}

protected Object writeReplace()
{
return Lists.immutable.withAll(this);
}

@Override
public Iterator<T> iterator()
{
return this.listIterator(0);
}

@Override
public ImmutableBooleanArraySubList subList(int fromIndex, int toIndex)
{
if (fromIndex < 0)
{
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
}
if (toIndex > this.size())
{
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
}
if (fromIndex > toIndex)
{
throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ')');
}

return new ImmutableBooleanArraySubList<>(this.original, this.offset + fromIndex, this.offset + toIndex);
}

@Override
public MutableStack<boolean> toStack()
{
return ArrayStack.newStack(this);
}

@Override
public void each(Procedure<boolean> procedure)
{
ListIterate.forEach(this, procedure);
}

@Override
public void forEachWithIndex(ObjectIntProcedure<boolean> objectIntProcedure)
{
ListIterate.forEachWithIndex(this, objectIntProcedure);

@Override
public <P> void forEachWith(Procedure2<boolean, ? super P> procedure, P parameter)
{
ListIterate.forEachWith(this, procedure, parameter);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ public ImmutableLongList distinct()
@Override
public ImmutableLongList subList(int fromIndex, int toIndex)
{
throw new UnsupportedOperationException("subList not yet implemented!");
return new LongIntervalSubList<>(this, fromIndex, toIndex);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be replaced with just (another) LongInterval, so there is no need for the inner class.

}

/**
Expand Down Expand Up @@ -1101,4 +1101,122 @@ public boolean tryAdvance(LongConsumer action)
return this.current >= this.to;
}
}

protected static class LongIntervalSubList
extends LongIntervall
implements Serializable, RandomAccess
{
// Not important since it uses writeReplace()
private static final long serialVersionUID = 1L;

private final LongInterval original;
private final int offset;
private final int size;

protected SubList(ImmutableList<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 + ')');
}
this.original = list;
this.offset = fromIndex;
this.size = toIndex - fromIndex;
}

@Override
public T get(int index)
{
this.checkIfOutOfBounds(index);
return this.original.get(index + this.offset);
}

@Override
public int size()
{
return this.size;
}

@Override
public ImmutableLongList newWith(T newItem)
{
return LongArrayList.newList(this.original).with(element).toImmutable();
}

protected Object writeReplace()
{
return Lists.immutable.withAll(this.original);
}

@Override
public LongIntervalSubList subList(int fromIndex, int toIndex)
{
if (fromIndex < 0)
{
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
}
if (toIndex > this.size())
{
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
}
if (fromIndex > toIndex)
{
throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ')');
}

return new LongIntervalSubList<>(this.original, this.offset + fromIndex, this.offset + toIndex);
}

private void checkIfOutOfBounds(int index)
{
if (index >= this.size || index < 0)
{
throw new IndexOutOfBoundsException("Index: " + index + " Size: " + this.size);
}
}

@Override
public long getFirst()
{
return this.isEmpty() ? null : this.original.get(this.offset);
}

@Override
public long getLast()
{
return this.isEmpty() ? null : this.original.get(this.offset + this.size - 1);
}

@Override
public MutableStack<long> toStack()
{
return ArrayStack.newStack(this);
}

@Override
public void each(Procedure<long> procedure)
{
ListIterate.forEach(this, procedure);
}

@Override
public void forEachWithIndex(ObjectIntProcedure<long> objectIntProcedure)
{
ListIterate.forEachWithIndex(this, objectIntProcedure);
}

@Override
public <P> void forEachWith(Procedure2<long, ? super P> procedure, P parameter)
{
ListIterate.forEachWith(this, procedure, parameter);
}
}
}