]> Cypherpunks repositories - gostls13.git/commitdiff
Add a unique list ID to list elements, and verify it as necessary.
authorDavid Symonds <dsymonds@golang.org>
Wed, 29 Jul 2009 01:19:16 +0000 (18:19 -0700)
committerDavid Symonds <dsymonds@golang.org>
Wed, 29 Jul 2009 01:19:16 +0000 (18:19 -0700)
This makes the list closed under its provided operations.

R=rsc,gri
APPROVED=rsc
DELTA=18  (14 added, 0 deleted, 4 changed)
OCL=32388
CL=32395

src/pkg/container/list/list.go

index 3f598bf5e4091369f8453490eee595218ba1727e..8ef5641a66d9e5b31ecf17b0d3b5742e91bbbd22 100755 (executable)
@@ -11,6 +11,9 @@ type Element struct {
        // The front of the list has prev = nil, and the back has next = nil.
        next, prev *Element;
 
+       // A unique ID for the list to which this element belongs.
+       id *byte;
+
        // The contents of this list element.
        Value interface {};
 }
@@ -19,6 +22,7 @@ type Element struct {
 type List struct {
        front, back *Element;
        len int;
+       id *byte;
 }
 
 // Init initializes or clears a List.
@@ -26,6 +30,7 @@ func (l *List) Init() *List {
        l.front = nil;
        l.back = nil;
        l.len = 0;
+       l.id = new(byte);
        return l
 }
 
@@ -46,6 +51,9 @@ func (l *List) Back() *Element {
 
 // Remove removes the element from the list.
 func (l *List) Remove(e *Element) {
+       if e.id != l.id {
+               return
+       }
        if e.prev == nil {
                l.front = e.next;
        } else {
@@ -88,21 +96,27 @@ func (l *List) insertBack(e *Element) {
 
 // PushFront inserts the value at the front of the list, and returns a new Element containing it.
 func (l *List) PushFront(value interface {}) *Element {
-       e := &Element{ nil, nil, value };
+       if l.id == nil {
+               l.Init();
+       }
+       e := &Element{ nil, nil, l.id, value };
        l.insertFront(e);
        return e
 }
 
 // PushBack inserts the value at the back of the list, and returns a new Element containing it.
 func (l *List) PushBack(value interface {}) *Element {
-       e := &Element{ nil, nil, value };
+       if l.id == nil {
+               l.Init();
+       }
+       e := &Element{ nil, nil, l.id, value };
        l.insertBack(e);
        return e
 }
 
 // MoveToFront moves the element to the front of the list.
 func (l *List) MoveToFront(e *Element) {
-       if l.front == e {
+       if e.id != l.id || l.front == e {
                return
        }
        l.Remove(e);
@@ -111,7 +125,7 @@ func (l *List) MoveToFront(e *Element) {
 
 // MoveToBack moves the element to the back of the list.
 func (l *List) MoveToBack(e *Element) {
-       if l.back == e {
+       if e.id != l.id || l.back == e {
                return
        }
        l.Remove(e);