diff options
author | David Symonds <dsymonds@golang.org> | 2009-07-28 18:19:16 -0700 |
---|---|---|
committer | David Symonds <dsymonds@golang.org> | 2009-07-28 18:19:16 -0700 |
commit | 7bba2df39505a609a1d3c5a134b3707a46045c1b (patch) | |
tree | 3fad60934aa8da060e1a05f7bf9f11f32659cc35 /src/pkg/container | |
parent | c1a6ce63e6531b899c0132e3911ee2faee2759c3 (diff) | |
download | golang-7bba2df39505a609a1d3c5a134b3707a46045c1b.tar.gz |
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
Diffstat (limited to 'src/pkg/container')
-rwxr-xr-x | src/pkg/container/list/list.go | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/src/pkg/container/list/list.go b/src/pkg/container/list/list.go index 3f598bf5e..8ef5641a6 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); |