/
ring_list.go
107 lines (87 loc) · 1.72 KB
/
ring_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package utils
import (
"sync/atomic"
)
type tRingNode struct {
prev *tRingNode
next *tRingNode
val interface{}
}
func (this *tRingNode)clean() {
this.next = nil
this.prev = nil
this.val = nil
}
type TRingList struct {
head *tRingNode
tail *tRingNode
len int32
}
type IRingIter interface {
HasMore() (bool)
Next() (interface{})
}
type tRingIter struct {
owner *TRingList
node *tRingNode
}
func (this *tRingIter)HasMore()(bool) {
if nil == this.node {
this.node = this.owner.head
}
return nil != this.node && nil != this.node.val
}
func (this *tRingIter)Next() (interface{}) {
val := this.node.val
this.node = this.node.next
return val
}
func NewRingList() (*TRingList) {
return &TRingList{}
}
func (this *TRingList)NewIter() (IRingIter) {
return &tRingIter{owner:this, node:nil}
}
func (this *TRingList)Len() (int32) {
return atomic.LoadInt32(&this.len)
}
func (this *TRingList)Add(_val interface{}) {
node := &tRingNode{prev:this.tail, next:nil, val:_val}
this.tail = node
if nil == this.head {
this.head = node
}
atomic.AddInt32(&this.len, 1)
}
func (this *TRingList)Del(_val interface{}) {
node := this.find(_val)
if nil != node {
prev := node.prev
next := node.next
if nil != prev {
prev.next = next
}
if nil != next {
next.prev = prev
}
if this.head == node {
this.head = next
}
if this.tail == node {
this.tail = prev
}
node.clean()
atomic.AddInt32(&this.len, -1)
}
}
func (this *TRingList)find(_val interface{}) (*tRingNode) {
tmp := this.head
for nil != tmp {
if tmp.val == _val {
return tmp
} else {
tmp = tmp.next
}
}
return nil
}