sdr 0.7

net.cscott.sdr.util
Class LL<T>

java.lang.Object
  extended by net.cscott.sdr.util.LL<T>
All Implemented Interfaces:
Iterable<T>

public class LL<T>
extends Object
implements Iterable<T>

Persistent Linked List.


Field Summary
 T head
          Element at the start of the list
 LL<T> tail
          Remainder of the elements in the list.
 
Constructor Summary
LL(T head, LL<T> tail)
          Add an element to the front of tail.
 
Method Summary
static
<T> LL<T>
create(List<T> elements)
          Factory: create a list.
static
<T> LL<T>
create(T... elements)
          Factory: create a list.
 boolean isEmpty()
          Are there any elements in this list?
 Iterator<T> iterator()
          Iterate over all the elements in this list from head to tail.
static
<T> LL<T>
NULL()
          Return a new empty list.
 LL<T> pop()
          Remove the head of the list.
 LL<T> push(T head)
          Add the given element to the start of the list.
 LL<T> reverse()
          Return a list with elements in reverse order.
 int size()
          Return the number of items in this list.
 List<T> toList()
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

head

public final T head
Element at the start of the list


tail

public final LL<T> tail
Remainder of the elements in the list.

Constructor Detail

LL

public LL(T head,
          LL<T> tail)
Add an element to the front of tail.

Method Detail

create

public static <T> LL<T> create(T... elements)
Factory: create a list.

Tests:
js> l = LL.create("a","b","c","d");
[a, b, c, d]

create

public static <T> LL<T> create(List<T> elements)
Factory: create a list.

Tests:
js> l = LL.create(java.util.Arrays.asList("a","b","c","d"));
[a, b, c, d]

push

public LL<T> push(T head)
Add the given element to the start of the list.

Tests:
js> l = LL.create("a","b","c","d");
[a, b, c, d]
js> l = l.push("e");
[e, a, b, c, d]
js> l = l.push("f");
[f, e, a, b, c, d]

pop

public LL<T> pop()
Remove the head of the list.

Tests:
js> l = LL.create("a","b","c","d");
[a, b, c, d]
js> l = l.pop();
[b, c, d]
js> l = l.pop();
[c, d]
js> l = l.pop();
[d]
js> l = l.pop();
[]

isEmpty

public boolean isEmpty()
Are there any elements in this list?

Tests:
js> l = LL.create("a","b","c","d");
[a, b, c, d]
js> l.isEmpty()
false
js> LL.NULL().isEmpty()
true

size

public int size()
Return the number of items in this list.

Tests:
js> l = LL.create("a","b","c","d");
[a, b, c, d]
js> l.size()
4
js> LL.NULL().size()
0

iterator

public Iterator<T> iterator()
Iterate over all the elements in this list from head to tail.

Specified by:
iterator in interface Iterable<T>
Tests:
js> l = LL.create("a","b","c","d");
[a, b, c, d]
js> [x for (x in Iterator(l))]
a,b,c,d
js> [x for (x in Iterator(LL.NULL()))].length
0

NULL

public static <T> LL<T> NULL()
Return a new empty list.

Tests:
js> LL.NULL();
[]
js> LL.NULL().isEmpty();
true

reverse

public LL<T> reverse()
Return a list with elements in reverse order.

Tests:
js> l = LL.create("asda","bar","foo");
[asda, bar, foo]
js> l.reverse();
[foo, bar, asda]
js> l = LL.NULL();
[]
js> l.reverse();
[]

toString

public String toString()
Overrides:
toString in class Object

toList

public List<T> toList()

sdr 0.7

Copyright © 2006-2009 C. Scott Ananian