-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.go
62 lines (55 loc) · 1.03 KB
/
linkedlist.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
package eventsourcing
import "github.com/0x9ef/eventsourcing-go/event"
type node struct {
value event.Eventer
next *node
}
type linkedList struct {
len int
head *node
}
func (l *linkedList) add(value event.Eventer) {
initNode := &node{
value: value,
}
if l.head == nil {
l.head = initNode
l.len++
return
}
current := l.head
for current.next != nil {
current = current.next
}
current.next = initNode
l.len++
}
func (l *linkedList) remove(value event.Eventer) {
var prev *node
current := l.head
for current != nil {
if current.value.GetAggregateId() == value.GetAggregateId() &&
current.value.GetVersion() == value.GetVersion() {
if prev == nil {
l.head = current.next
} else {
prev.next = current.next
}
} else {
prev = current
}
current = current.next
}
l.len--
}
func (l *linkedList) traverse(f func(value event.Eventer) error) error {
current := l.head
for current != nil {
err := f(current.value)
if err != nil {
return err
}
current = current.next
}
return nil
}