-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.go
187 lines (156 loc) · 3.7 KB
/
linked_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package cronjob
import (
"time"
)
// Node represents a node in the storage system.
type Node struct {
// The id of the node in the storage system.
Id int
// The schedule the node is set to be activated on.
Schedule Schedule
// The job which needs to be ran.
Job *Job
// The ptr to the next node.
next *Node
}
// linked list will point to the root node.
type linkedList struct {
head *Node
len int
}
// NextCycle gets the duration of sleeping before activating.
func (l *linkedList) NextCycle(now time.Time) time.Duration {
if l.head == nil {
return -1
}
// the head node has the shortest duration.
if v := l.head.Schedule.Calculate(now); v < 0 {
return 0
} else {
return v
}
}
// RunNow gets all the node that need to be ran now.
//
// This includes any nodes scheduled to run now or in the past.
func (l *linkedList) RunNow(now time.Time) (nodes []*Node) {
ptr := l.head
for i := 0; i < l.len; i++ {
if ptr.Schedule.Calculate(now) <= 0 {
nodes = append(nodes, ptr)
}
ptr = ptr.next
}
return
}
// ScheduleJob will add the node in the respective position
// based on the schedule.
//
// no-op if node is nil.
func (l *linkedList) AddNode(now time.Time, node *Node) {
// return if pointer is nil.
if node == nil {
return
}
// if inserting a cyclic schedule, move to next activation time.
if sched, ok := node.Schedule.(CyclicSchedule); ok {
sched.MoveNextAvtivation(now)
}
// if head is nil add node as the head.
if l.head == nil {
l.len++
l.head = node
return
}
durInsertNode := node.Schedule.Calculate(now)
ptr := l.head
for i := 0; i < l.len; i++ {
if durInsertNode <= ptr.Schedule.Calculate(now) {
// this can only happen for the first node as all the other nodes are already checked
// for in the next condition.
l.len++
l.addFirst(node)
return
} else if ptr.next == nil || durInsertNode <= ptr.next.Schedule.Calculate(now) {
// add node after current node if next ptr is either nill (end of list)
// or duration of the ptr to the next node is less then desired node.
l.len++
ptr.insertAfter(node)
return
}
// advance in list.
ptr = ptr.next
}
}
// RemoveJob removes the job with the given id.
//
// no-op if node not found or list empty.
func (l *linkedList) RemoveNode(id int) {
if l.len == 0 {
return
}
ptr := l.head
for i := 0; i < l.len; i++ {
if ptr.Id == id {
if i > 0 {
prevNode := l.getAt(i - 1)
prevNode.next = l.getAt(i).next
} else {
l.head = ptr.next
}
l.len--
return
}
ptr = ptr.next
}
}
// GetAll returns all the jobs in the storage system.
func (l *linkedList) GetAll() (jobs []*Node) {
ptr := l.head
for i := 0; i < l.len; i++ {
jobs = append(jobs, ptr)
ptr = ptr.next
}
return
}
// Clean removes the node (field) and re-calculates appropriate nodes.
func (l *linkedList) Clean(now time.Time, nodes []*Node) {
for _, node := range nodes {
switch node.Schedule.(type) {
case CyclicSchedule:
// remove the ran node node.
l.RemoveNode(node.Id)
// then re-add the cyclic node.
l.AddNode(now, node)
case Schedule:
// remove nodes with constand schedule.
l.RemoveNode(node.Id)
}
}
}
// addFirst changes the head of the linked list.
func (l *linkedList) addFirst(node *Node) {
ptrTemp := l.head
l.head = node
node.next = ptrTemp
}
// insertAfter inserts the node (field) between the instance it is called upon and
// the node which is linked to the instance it is called upon.
func (n *Node) insertAfter(node *Node) {
prtTemp := n.next
n.next = node
node.next = prtTemp
}
func (l *linkedList) getAt(pos int) *Node {
if pos < 0 {
return l.head
}
if pos > (l.len - 1) {
return nil
}
ptr := l.head
for i := 0; i < pos; i++ {
ptr = ptr.next
}
return ptr
}