-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfifo.go
58 lines (49 loc) · 1010 Bytes
/
fifo.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
package list
type FifoList struct {
root Node
tail *Node
free func(interface{})
}
func NewFifoList(free func(interface{})) *FifoList {
if free == nil {
free = dummy_free
}
lst := &FifoList{free: free}
lst.tail = &lst.root
return lst
}
func (lst *FifoList) Move() *FifoList {
new_lst := &FifoList{root: lst.root, tail: lst.tail, free: lst.free}
lst.root.next = nil
lst.root.val = nil
lst.tail = &lst.root
return new_lst
}
func (lst *FifoList) IsEmpty() bool {
return lst.root.next == nil
}
func (lst *FifoList) Clear() {
for !lst.IsEmpty() {
val, ok := lst.root.RemoveNext()
if !ok {
break
}
if val != nil {
lst.free(val)
}
}
}
func (lst *FifoList) PopFront() (interface{}, bool) {
if lst.root.next == lst.tail {
lst.tail = &lst.root
}
return lst.root.RemoveNext()
}
func (lst *FifoList) PushBack(val interface{}) *Node {
n := lst.tail.InsertNext(val)
lst.tail = lst.tail.next
return n
}
func (lst *FifoList) Front() Iterator {
return AsIterator(&lst.root)
}