public class Queue extends Collection
{
private Object[] elems;
private Queue( Object[] theElems ) { this.elems = theElems; }
public Queue snoc(Object x) {
return new Queue( array_append( this.elems, new Object[] { x } ) );
}
public boolean isEmpty() { return array_isNull(elems); }
public Object head() { return array_car(elems); }
public Queue tail() { return new Queue( array_cdr( elems )); }
public static Queue empty() { return new Queue( array_null() ); }
public Queue addElem( Object val ) { return this.snoc(val); }
public Collection.Pair anyElem() {
return new Collection.Pair( this.head(), this.tail() );
}
private static boolean array_isNull( Object[] x ) { return x.length == 0; }
private static Object[] array_null() { return new Object[0]; }
private static Object array_car( Object[] x ) { return x[0]; }
private static Object[] array_cdr( Object[] x ) {
Object[] z = new Object[x.length - 1];
System.arraycopy( x, 1, z, 0, x.length - 1);
return z;
}
static Object[] array_append( Object[] x, Object[] y ) {
Object[] z = new Object[x.length + y.length];
System.arraycopy( x, 0, z, 0, x.length );
System.arraycopy( y, 0, z, x.length, y.length );
return z;
}
}