edu.cmu.cs.crystal.util
Class ConsList<T>

java.lang.Object
  extended by edu.cmu.cs.crystal.util.ConsList<T>
Type Parameters:
T - The type of elements this list holds.
All Implemented Interfaces:
Iterable<T>, Collection<T>, List<T>

public abstract class ConsList<T>
extends Object
implements List<T>

An immutable cons list. Because this list cannot be modified, many lists can share the same elements. Many operations must traverse, and therefore run in linear time. Adding things to the front of the list is done in constant time. Implements all methods of the List interface, except those which are optional because they modify the list in place.

Since:
Sep 8, 2008
Author:
Nels E. Beckman

Constructor Summary
ConsList()
           
 
Method Summary
 void add(int index, T element)
           
 boolean add(T e)
           
 boolean addAll(Collection<? extends T> c)
           
 boolean addAll(int index, Collection<? extends T> c)
           
 void clear()
           
static
<T> ConsList<T>
concat(ConsList<T> front, ConsList<T> back)
          Concatenate the two given lists.
static
<T> ConsList<T>
cons(T hd, ConsList<T> tl)
          Create a new ConsList with hd as the first element and tl as the rest of the list.
 boolean contains(Object o)
           
abstract  boolean containsAll(Collection<?> c)
           
static
<T> ConsList<T>
empty()
          Create a new, empty list.
 ConsList<T> filter(Lambda<? super T,? extends Boolean> lam)
          Given a 'first-class function' that takes elements of type T and returns a boolean, call that function on every element of this list, returning a new list that only contains the elements for which the function call returned true.
<O> O
foldl(Lambda2<? super T,? super O,? extends O> lam, O o)
          Fold over the elements of this list.
 T get(int index)
           
abstract  T hd()
          Get the first element of this list.
abstract  int indexOf(Object o)
           
abstract  boolean isEmpty()
           
 Iterator<T> iterator()
           
abstract  int lastIndexOf(Object o)
           
static
<T> ConsList<T>
list(T... ts)
          Create a ConsList with the given elements.
 ListIterator<T> listIterator()
          Note: For ConsList<T>, this method is less efficient than iterator() and should only be used if iterating in the reverse direction is really important.
 ListIterator<T> listIterator(int index)
          Note: For ConsList<T>, this method is less efficient than iterator() and should only be used if iterating in the reverse direction is really important.
<O> ConsList<O>
map(Lambda<? super T,? extends O> lam)
          Given a 'first-class function' that takes elements of type T and returns elements of type O, call that function on every element of this list, returning a new list of Os.
 T remove(int index)
           
 boolean remove(Object o)
           
 boolean removeAll(Collection<?> c)
           
 ConsList<T> removeElement(T t)
          Removes every element in the list where hd().equals(t) == true.
 ConsList<T> removeElementOnce(T t)
          Removes the first element in the list where hd().equals(t) == true.
 boolean retainAll(Collection<?> c)
           
 T set(int index, T element)
           
static
<T> ConsList<T>
singleton(T hd)
          Create a list with one element.
abstract  int size()
           
 ConsList<T> subList(int fromIndex, int toIndex)
           
abstract  ConsList<T> tl()
          Return this list without the first element.
 Object[] toArray()
           
<S> S[]
toArray(S[] a)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.List
equals, hashCode
 

Constructor Detail

ConsList

public ConsList()
Method Detail

empty

public static <T> ConsList<T> empty()
Create a new, empty list.


singleton

public static <T> ConsList<T> singleton(T hd)
Create a list with one element.


list

public static <T> ConsList<T> list(T... ts)
Create a ConsList with the given elements.


cons

public static <T> ConsList<T> cons(T hd,
                                   ConsList<T> tl)
Create a new ConsList with hd as the first element and tl as the rest of the list.


concat

public static <T> ConsList<T> concat(ConsList<T> front,
                                     ConsList<T> back)
Concatenate the two given lists.


hd

public abstract T hd()
Get the first element of this list.


tl

public abstract ConsList<T> tl()
Return this list without the first element.


removeElement

public final ConsList<T> removeElement(T t)
Removes every element in the list where hd().equals(t) == true.


removeElementOnce

public final ConsList<T> removeElementOnce(T t)
Removes the first element in the list where hd().equals(t) == true.


map

public final <O> ConsList<O> map(Lambda<? super T,? extends O> lam)
Given a 'first-class function' that takes elements of type T and returns elements of type O, call that function on every element of this list, returning a new list of Os.


filter

public final ConsList<T> filter(Lambda<? super T,? extends Boolean> lam)
Given a 'first-class function' that takes elements of type T and returns a boolean, call that function on every element of this list, returning a new list that only contains the elements for which the function call returned true.


foldl

public final <O> O foldl(Lambda2<? super T,? super O,? extends O> lam,
                         O o)
Fold over the elements of this list.


size

public abstract int size()
Specified by:
size in interface Collection<T>
Specified by:
size in interface List<T>

isEmpty

public abstract boolean isEmpty()
Specified by:
isEmpty in interface Collection<T>
Specified by:
isEmpty in interface List<T>

indexOf

public abstract int indexOf(Object o)
Specified by:
indexOf in interface List<T>

lastIndexOf

public abstract int lastIndexOf(Object o)
Specified by:
lastIndexOf in interface List<T>

listIterator

public final ListIterator<T> listIterator(int index)
Note: For ConsList<T>, this method is less efficient than iterator() and should only be used if iterating in the reverse direction is really important. In particular, calling the previous() method is linear in the size of the list.

Specified by:
listIterator in interface List<T>

listIterator

public final ListIterator<T> listIterator()
Note: For ConsList<T>, this method is less efficient than iterator() and should only be used if iterating in the reverse direction is really important. In particular, calling the previous() method is linear in the size of the list.

Specified by:
listIterator in interface List<T>

get

public final T get(int index)
Specified by:
get in interface List<T>

iterator

public final Iterator<T> iterator()
Specified by:
iterator in interface Iterable<T>
Specified by:
iterator in interface Collection<T>
Specified by:
iterator in interface List<T>

containsAll

public abstract boolean containsAll(Collection<?> c)
Specified by:
containsAll in interface Collection<T>
Specified by:
containsAll in interface List<T>

subList

public final ConsList<T> subList(int fromIndex,
                                 int toIndex)
Specified by:
subList in interface List<T>

contains

public final boolean contains(Object o)
Specified by:
contains in interface Collection<T>
Specified by:
contains in interface List<T>

toArray

public final Object[] toArray()
Specified by:
toArray in interface Collection<T>
Specified by:
toArray in interface List<T>

toArray

public final <S> S[] toArray(S[] a)
Specified by:
toArray in interface Collection<T>
Specified by:
toArray in interface List<T>

add

public final void add(int index,
                      T element)
Specified by:
add in interface List<T>

add

public final boolean add(T e)
Specified by:
add in interface Collection<T>
Specified by:
add in interface List<T>

addAll

public final boolean addAll(Collection<? extends T> c)
Specified by:
addAll in interface Collection<T>
Specified by:
addAll in interface List<T>

addAll

public final boolean addAll(int index,
                            Collection<? extends T> c)
Specified by:
addAll in interface List<T>

clear

public final void clear()
Specified by:
clear in interface Collection<T>
Specified by:
clear in interface List<T>

remove

public final T remove(int index)
Specified by:
remove in interface List<T>

remove

public final boolean remove(Object o)
Specified by:
remove in interface Collection<T>
Specified by:
remove in interface List<T>

removeAll

public final boolean removeAll(Collection<?> c)
Specified by:
removeAll in interface Collection<T>
Specified by:
removeAll in interface List<T>

retainAll

public final boolean retainAll(Collection<?> c)
Specified by:
retainAll in interface Collection<T>
Specified by:
retainAll in interface List<T>

set

public final T set(int index,
                   T element)
Specified by:
set in interface List<T>