From 83940d7c4a2a7ddf5c485f2745573552b096775c Mon Sep 17 00:00:00 2001 From: David Symonds Date: Tue, 28 Jul 2009 18:19:16 -0700 Subject: [PATCH] Add a unique list ID to list elements, and verify it as necessary. 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 | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/pkg/container/list/list.go b/src/pkg/container/list/list.go index 3f598bf5e4..8ef5641a66 100755 --- a/src/pkg/container/list/list.go +++ b/src/pkg/container/list/list.go @@ -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); -- 2.48.1