-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasklist.go
112 lines (90 loc) · 1.37 KB
/
tasklist.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
package fuq
type TaskList []byte
func NewTaskList(num int, fill bool) TaskList {
ngrp := num / 8
rem := num % 8
if rem > 0 {
ngrp++
}
tl := make(TaskList, ngrp, ngrp)
if fill {
for i := range tl {
tl[i] = 0xff
}
if rem > 0 {
tl[ngrp-1] = tl[ngrp-1] >> uint(8-rem)
}
}
return tl
}
func (tl TaskList) Next() int {
ind := 0
for i, v := range tl {
if v == 0 {
ind += 8
continue
}
m := byte(1)
for v&m == 0 {
m = m << 1
ind++
}
tl[i] = v & ^m
return ind
}
return -1
}
func (tl TaskList) Has(i int) bool {
if i < 0 {
return false
}
grp := i / 8
if grp >= len(tl) {
return false
}
itm := uint(i % 8)
return tl[grp]&(1<<itm) != 0
}
func (tl TaskList) Set(i int, v bool) {
if i < 0 {
return
}
grp := i / 8
if grp >= len(tl) {
return
}
itm := uint(i % 8)
if v {
tl[grp] = tl[grp] | (1 << itm)
} else {
tl[grp] = tl[grp] & ^(1 << itm)
}
}
func (tl TaskList) Intervals() [][2]int {
i0 := -1
i1 := -1
intervals := make([][2]int, 0)
for i, v := range tl {
if v == 0 {
continue
}
ind := 8 * i
for v != 0 {
if v&1 == 1 {
if i0 == -1 {
i0, i1 = ind, ind
} else if ind != i1+1 {
intervals = append(intervals, [2]int{i0, i1})
i0 = ind
}
i1 = ind
}
v = v >> 1
ind++
}
}
if i0 != -1 {
intervals = append(intervals, [2]int{i0, i1})
}
return intervals
}