@@ -30,7 +30,7 @@ func New(n ...int) BitSet {
3030 if len (n ) == 0 {
3131 return BitSet {}
3232 }
33- maxElem := n [ 0 ]
33+ maxElem := - 1
3434 for _ , e := range n {
3535 if e > maxElem {
3636 maxElem = e
@@ -83,11 +83,10 @@ func (bs BitSet) Equal(other BitSet) bool {
8383
8484// Subset tells if bs is a subset of other.
8585func (bs BitSet ) Subset (other BitSet ) bool {
86- bsLen := len (bs )
87- if bsLen > len (other ) {
86+ if len (bs ) > len (other ) {
8887 return false
8988 }
90- for i := 0 ; i < bsLen ; i ++ {
89+ for i := 0 ; i < len ( bs ) ; i ++ {
9190 if bs [i ]&^other [i ] != 0 {
9291 return false
9392 }
@@ -98,11 +97,10 @@ func (bs BitSet) Subset(other BitSet) bool {
9897// Max returns the maximum element of the bitset.
9998// If the set is empty, -1 is returned.
10099func (bs BitSet ) Max () int {
101- bsLen := len (bs )
102- if bsLen == 0 {
100+ if len (bs ) == 0 {
103101 return - 1
104102 }
105- i := bsLen - 1
103+ i := len ( bs ) - 1
106104 return (i << shift ) + bits .Len64 (bs [i ]) - 1
107105}
108106
@@ -123,12 +121,12 @@ func (bs BitSet) Empty() bool {
123121// Next returns the next element n, n > m, in the set,
124122// or -1 if there is no such element.
125123func (bs BitSet ) Next (m int ) int {
126- l := len (bs )
127- if l == 0 {
124+ if len (bs ) == 0 {
128125 return - 1
129126 }
127+ l := len (bs )
130128 if m < 0 {
131- if ( bs [ 0 ] & 1 ) != 0 {
129+ if bs . Contains ( 0 ) {
132130 return 0
133131 }
134132 m = 0
@@ -152,10 +150,10 @@ func (bs BitSet) Next(m int) int {
152150// Prev returns the previous element n, n < m, in the set,
153151// or -1 if there is no such element.
154152func (bs BitSet ) Prev (m int ) int {
155- l := len (bs )
156- if l == 0 || m <= 0 {
153+ if len (bs ) == 0 || m <= 0 {
157154 return - 1
158155 }
156+ l := len (bs )
159157 lastIdx := l - 1
160158 maxPossible := (lastIdx << shift ) + bits .Len64 (bs [lastIdx ]) - 1
161159 if m > maxPossible {
@@ -218,9 +216,9 @@ func (bs BitSet) VisitAll(do func(n int)) {
218216 })
219217}
220218
221- // bitMask returns a bit div64rem with bits set from m to n inclusive, 0 ≤ m ≤ n < bpw.
222- func bitMask (m , n int ) uint64 {
223- return maxw >> uint (bpw - 1 - (n - m )) << uint (m )
219+ // bitMask returns a uint64 with bits set from start to end inclusive, 0 ≤ start ≤ end < bpw.
220+ func bitMask (start , end int ) uint64 {
221+ return maxw >> uint (bpw - 1 - (end - start )) << uint (start )
224222}
225223
226224// nextPow2 returns the smallest power of two p such that p > n, or math.MaxInt if overflow.
0 commit comments