Skip to content

Commit 2538288

Browse files
committed
UnsetIterator: address review comments
Signed-off-by: Roger Peppe <[email protected]>
1 parent 9ebb1c3 commit 2538288

File tree

4 files changed

+53
-51
lines changed

4 files changed

+53
-51
lines changed

arraycontainer.go

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -62,56 +62,8 @@ func (ac *arrayContainer) getManyIterator() manyIterable {
6262
return &shortIterator{ac.content, 0}
6363
}
6464

65-
type arrayContainerUnsetIterator struct {
66-
content []uint16
67-
pos int
68-
nextVal int
69-
}
70-
71-
func (acui *arrayContainerUnsetIterator) next() uint16 {
72-
val := acui.nextVal
73-
acui.nextVal++
74-
for acui.pos < len(acui.content) && uint16(acui.nextVal) == acui.content[acui.pos] {
75-
acui.nextVal++
76-
acui.pos++
77-
}
78-
return uint16(val)
79-
}
80-
81-
func (acui *arrayContainerUnsetIterator) hasNext() bool {
82-
return acui.nextVal < 65536
83-
}
84-
85-
func (acui *arrayContainerUnsetIterator) peekNext() uint16 {
86-
return uint16(acui.nextVal)
87-
}
88-
89-
func (acui *arrayContainerUnsetIterator) advanceIfNeeded(minval uint16) {
90-
if !acui.hasNext() || acui.peekNext() >= minval {
91-
return
92-
}
93-
acui.nextVal = int(minval)
94-
acui.pos = binarySearch(acui.content, minval)
95-
if acui.pos < 0 {
96-
acui.pos = -acui.pos - 1
97-
}
98-
for acui.pos < len(acui.content) && uint16(acui.nextVal) == acui.content[acui.pos] {
99-
acui.nextVal++
100-
acui.pos++
101-
}
102-
}
103-
104-
func newArrayContainerUnsetIterator(a *arrayContainer) *arrayContainerUnsetIterator {
105-
acui := &arrayContainerUnsetIterator{content: a.content, pos: 0, nextVal: 0}
106-
for acui.pos < len(acui.content) && uint16(acui.nextVal) == acui.content[acui.pos] {
107-
acui.nextVal++
108-
acui.pos++
109-
}
110-
return acui
111-
}
112-
11365
func (ac *arrayContainer) getUnsetIterator() shortPeekable {
114-
return newArrayContainerUnsetIterator(ac)
66+
return newArrayContainerUnsetIterator(ac.content)
11567
}
11668

11769
func (ac *arrayContainer) minimum() uint16 {

roaring.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ func (iui *unsetIterator) init() {
812812
c := iui.highlowcontainer.getContainerAtIndex(iui.containerIndex)
813813
switch t := c.(type) {
814814
case *arrayContainer:
815-
iui.arrayUnsetIter = *newArrayContainerUnsetIterator(t)
815+
iui.arrayUnsetIter = *newArrayContainerUnsetIterator(t.content)
816816
iui.iter = &iui.arrayUnsetIter
817817
case *runContainer16:
818818
iui.runUnsetIter = *t.newRunUnsetIterator16()

runcontainer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2002,7 +2002,7 @@ func (rui *runUnsetIterator16) hasNext() bool {
20022002
func (rui *runUnsetIterator16) next() uint16 {
20032003
val := rui.nextVal
20042004
rui.nextVal++
2005-
if rui.curIndex < len(rui.rc.iv) && uint16(rui.nextVal) == rui.rc.iv[rui.curIndex].start {
2005+
if rui.curIndex < len(rui.rc.iv) && uint16(rui.nextVal) >= rui.rc.iv[rui.curIndex].start {
20062006
rui.nextVal = int(rui.rc.iv[rui.curIndex].start) + int(rui.rc.iv[rui.curIndex].length) + 1
20072007
rui.curIndex++
20082008
}

shortiterator.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,53 @@ func (si *reverseIterator) next() uint16 {
5050
si.loc--
5151
return a
5252
}
53+
54+
type arrayContainerUnsetIterator struct {
55+
content []uint16
56+
// pos is the index of the next set bit that is >= nextVal.
57+
// When nextVal reaches content[pos], pos is incremented.
58+
pos int
59+
nextVal int
60+
}
61+
62+
func (acui *arrayContainerUnsetIterator) next() uint16 {
63+
val := acui.nextVal
64+
acui.nextVal++
65+
for acui.pos < len(acui.content) && uint16(acui.nextVal) >= acui.content[acui.pos] {
66+
acui.nextVal++
67+
acui.pos++
68+
}
69+
return uint16(val)
70+
}
71+
72+
func (acui *arrayContainerUnsetIterator) hasNext() bool {
73+
return acui.nextVal < 65536
74+
}
75+
76+
func (acui *arrayContainerUnsetIterator) peekNext() uint16 {
77+
return uint16(acui.nextVal)
78+
}
79+
80+
func (acui *arrayContainerUnsetIterator) advanceIfNeeded(minval uint16) {
81+
if !acui.hasNext() || acui.peekNext() >= minval {
82+
return
83+
}
84+
acui.nextVal = int(minval)
85+
acui.pos = binarySearch(acui.content, minval)
86+
if acui.pos < 0 {
87+
acui.pos = -acui.pos - 1
88+
}
89+
for acui.pos < len(acui.content) && uint16(acui.nextVal) >= acui.content[acui.pos] {
90+
acui.nextVal++
91+
acui.pos++
92+
}
93+
}
94+
95+
func newArrayContainerUnsetIterator(content []uint16) *arrayContainerUnsetIterator {
96+
acui := &arrayContainerUnsetIterator{content: content, pos: 0, nextVal: 0}
97+
for acui.pos < len(acui.content) && uint16(acui.nextVal) >= acui.content[acui.pos] {
98+
acui.nextVal++
99+
acui.pos++
100+
}
101+
return acui
102+
}

0 commit comments

Comments
 (0)