]> Cypherpunks repositories - gostls13.git/commitdiff
container/list: Add Len() method to List.
authorDavid Symonds <dsymonds@golang.org>
Sun, 26 Jul 2009 00:18:05 +0000 (17:18 -0700)
committerDavid Symonds <dsymonds@golang.org>
Sun, 26 Jul 2009 00:18:05 +0000 (17:18 -0700)
R=r
APPROVED=gri
DELTA=23  (23 added, 0 deleted, 0 changed)
OCL=32106
CL=32189

src/pkg/container/list/list.go
src/pkg/container/list/list_test.go

index 7e8daa65a79a29f95763d9829e12f77351156a1a..3f598bf5e4091369f8453490eee595218ba1727e 100755 (executable)
@@ -18,12 +18,14 @@ type Element struct {
 // List represents a doubly linked list.
 type List struct {
        front, back *Element;
+       len int;
 }
 
 // Init initializes or clears a List.
 func (l *List) Init() *List {
        l.front = nil;
        l.back = nil;
+       l.len = 0;
        return l
 }
 
@@ -57,6 +59,7 @@ func (l *List) Remove(e *Element) {
 
        e.prev = nil;
        e.next = nil;
+       l.len--;
 }
 
 func (l *List) insertFront(e *Element) {
@@ -68,6 +71,7 @@ func (l *List) insertFront(e *Element) {
        } else {
                l.back = e;
        }
+       l.len++;
 }
 
 func (l *List) insertBack(e *Element) {
@@ -79,6 +83,7 @@ func (l *List) insertBack(e *Element) {
        } else {
                l.front = e;
        }
+       l.len++;
 }
 
 // PushFront inserts the value at the front of the list, and returns a new Element containing it.
@@ -113,6 +118,11 @@ func (l *List) MoveToBack(e *Element) {
        l.insertBack(e);
 }
 
+// Len returns the number of elements in the list.
+func (l *List) Len() int {
+       return l.len
+}
+
 func (l *List) iterate(c chan <- *Element) {
        var next *Element;
        for e := l.front; e != nil; e = next {
index d5b2672e055962bf7089d299f94b61b9e811c073..bdfed357884d6efb03e6ccf419d8f4d728b0c8ba 100755 (executable)
@@ -42,19 +42,29 @@ func checkListPointers(t *testing.T, l *List, es []*Element) {
        }
 }
 
+func checkListLen(t *testing.T, l *List, n int) {
+       if an := l.Len(); an != n {
+               t.Errorf("l.Len() = %d, want %d", an, n);
+       }
+}
+
 func TestList(t *testing.T) {
        l := New();
        checkListPointers(t, l, []*Element{});
+       checkListLen(t, l, 0);
 
        // Single element list
        e := l.PushFront("a");
+       checkListLen(t, l, 1);
        checkListPointers(t, l, []*Element{ e });
        l.MoveToFront(e);
        checkListPointers(t, l, []*Element{ e });
        l.MoveToBack(e);
        checkListPointers(t, l, []*Element{ e });
+       checkListLen(t, l, 1);
        l.Remove(e);
        checkListPointers(t, l, []*Element{});
+       checkListLen(t, l, 0);
 
        // Bigger list
        e2 := l.PushFront(2);
@@ -62,9 +72,11 @@ func TestList(t *testing.T) {
        e3 := l.PushBack(3);
        e4 := l.PushBack("banana");
        checkListPointers(t, l, []*Element{ e1, e2, e3, e4 });
+       checkListLen(t, l, 4);
 
        l.Remove(e2);
        checkListPointers(t, l, []*Element{ e1, e3, e4 });
+       checkListLen(t, l, 3);
 
        l.MoveToFront(e3);  // move from middle
        checkListPointers(t, l, []*Element{ e3, e1, e4 });
@@ -88,4 +100,5 @@ func TestList(t *testing.T) {
                l.Remove(e);
        }
        checkListPointers(t, l, []*Element{});
+       checkListLen(t, l, 0);
 }