-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwheel.go
More file actions
39 lines (34 loc) · 757 Bytes
/
Copy pathwheel.go
File metadata and controls
39 lines (34 loc) · 757 Bytes
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
package timer
import (
"github.com/singchia/go-timer/v2/pkg/linker"
)
type wheel struct {
slots []*slot
cur uint
numSlots uint
//position in whole timer
position uint
}
func newWheel(numSlots uint, position uint) *wheel {
slots := make([]*slot, numSlots, numSlots)
wil := &wheel{cur: 0, numSlots: numSlots, position: position}
wil.slots = slots
return wil
}
func (w *wheel) add(n uint, tick *tick) *tick {
if w.slots[n] == nil {
w.slots[n] = newSlot(w)
}
return w.slots[n].add(tick)
}
// increace n on cur
func (w *wheel) incN(n uint) *linker.Doublinker {
w.cur += n
if w.cur >= w.numSlots {
w.cur -= w.numSlots
}
if w.slots[w.cur] == nil || w.slots[w.cur].length() == 0 {
return nil
}
return w.slots[w.cur].remove()
}