Debian Codegraph @

multichoose.go

raw symbols SCIP
1 package multichoose
2
3 type MultiChoose struct {
4 selected []uint64
5 length int
6 limit int
7 }
8
9 func (mc *MultiChoose) getSelected(index int) bool {
10 if index < 0 || index > mc.length {
11 return false
12 }
13 i := uint64(index) >> 6
14 j := uint64(index) & 0x3F
15 return (mc.selected[i] & (1 << j)) != 0
16 }
17
18 func (mc *MultiChoose) setSelected(index int, value bool) {
19 if index < 0 || index > mc.length {
20 return
21 }
22 i := uint64(index) >> 6
23 j := uint64(index) & 0x3F
24 if value {
25 mc.selected[i] |= (1 << j)
26 } else {
27 mc.selected[i] &^= (1 << j)
28 }
29 }
30
31 func New(length int) *MultiChoose {
32 return &MultiChoose{
33 selected: make([]uint64, (length>>6)+1),
34 length: length,
35 limit: -1,
36 }
37 }
38
39 func (mc MultiChoose) Count() int {
40 count := 0
41 for i := 0; i <= mc.length; i++ {
42 if mc.getSelected(i) {
43 count++
44 }
45 }
46 return count
47 }
48
49 func (mc *MultiChoose) Select(index int) {
50 if mc.limit >= 0 && mc.Count() >= mc.limit {
51 return
52 }
53
54 mc.setSelected(index, true)
55 }
56
57 func (mc *MultiChoose) Deselect(index int) {
58 mc.setSelected(index, false)
59 }
60
61 func (mc MultiChoose) IsSelected(index int) bool {
62 return mc.getSelected(index)
63 }