@Override public boolean hasNext() { return count < size(); }
@Override public int size() { return inner.size(); }
ListSlice( ListValue inner, int from, int to ) { assert from >= 0; assert to <= inner.size(); assert from <= to; this.inner = inner; this.from = from; this.to = to; }
@Override public int size() { return prepended.length + base.size(); }
@Override public int size() { return base.size() + appended.length; }
@Override public int length() { return size(); }
public boolean isEmpty() { return size() == 0; }
public boolean nonEmpty() { return size() != 0; }
public AnyValue last() { int size = size(); if ( size == 0 ) { throw new NoSuchElementException( "last of empty list" ); } return value( size - 1 ); }
private AnyValue[] randomAccessAsArray() { int size = size(); AnyValue[] values = new AnyValue[size]; for ( int i = 0; i < values.length; i++ ) { values[i] = value( i ); } return values; }
public AnyValue head() { int size = size(); if ( size == 0 ) { throw new NoSuchElementException( "head of empty list" ); } return value( 0 ); }
public ListValue drop( int n ) { int size = size(); int start = Math.max( 0, Math.min( n, size ) ); return new ListSlice( this, start, size ); }
public ListValue take( int n ) { int end = Math.max( 0, Math.min( n, size() ) ); return new ListSlice( this, 0, end ); }
public ListValue slice( int from, int to ) { int f = Math.max( from, 0 ); int t = Math.min( to, size() ); if ( f > t ) { return EMPTY_LIST; } else { return new ListSlice( this, f, t ); } }
public ListValue tail() { return slice( 1, size() ); }
private int randomAccessComputeHash() { int hashCode = 1; int size = size(); for ( int i = 0; i < size; i++ ) { hashCode = 31 * hashCode + value( i ).hashCode(); } return hashCode; }
private <E extends Exception> void iterationWriteTo( AnyValueWriter<E> writer ) throws E { writer.beginList( size() ); for ( AnyValue value : this ) { value.writeTo( writer ); } writer.endList(); }
public static ListValue fromSlice( AnyValue collection, AnyValue fromValue ) { int from = asInt( fromValue ); ListValue list = makeTraversable( collection ); if ( from >= 0 ) { return list.drop( from ); } else { return list.drop( list.size() + from ); } }
public static ListValue toSlice( AnyValue collection, AnyValue fromValue ) { int from = asInt( fromValue ); ListValue list = makeTraversable( collection ); if ( from >= 0 ) { return list.take( from ); } else { return list.take( list.size() + from ); } }