Skip to content

Commit 0e61035

Browse files
author
Petko Nikolov
committed
conform to spark style; rm redundant asserts; more unit tests added; use arraycopy instead of loop
1 parent d53cdb9 commit 0e61035

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed

core/src/main/scala/org/apache/spark/util/collection/BitSet.scala

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,17 @@ class BitSet(numBits: Int) extends Serializable {
9595
*/
9696
def ^(other: BitSet): BitSet = {
9797
val newBS = new BitSet(math.max(capacity, other.capacity))
98-
assert(newBS.numWords >= numWords)
99-
assert(newBS.numWords >= other.numWords)
10098
val smaller = math.min(numWords, other.numWords)
10199
var ind = 0
102-
while( ind < smaller ) {
100+
while ( ind < smaller ) {
103101
newBS.words(ind) = words(ind) ^ other.words(ind)
104102
ind += 1
105103
}
106-
while( ind < numWords ) {
107-
newBS.words(ind) = words(ind)
108-
ind += 1
104+
if ( ind < numWords ) {
105+
Array.copy( words, ind, newBS.words, ind, numWords - ind )
109106
}
110-
while( ind < other.numWords ) {
111-
newBS.words(ind) = other.words(ind)
112-
ind += 1
107+
if ( ind < other.numWords ) {
108+
Array.copy( other.words, ind, newBS.words, ind, other.numWords - ind )
113109
}
114110
newBS
115111
}
@@ -118,19 +114,16 @@ class BitSet(numBits: Int) extends Serializable {
118114
* Compute the difference of the two sets by performing bit-wise AND-NOT returning the
119115
* result.
120116
*/
121-
def &~(other: BitSet): BitSet = {
122-
val newBS = new BitSet(math.max(capacity, other.capacity))
123-
assert(newBS.numWords >= numWords)
124-
assert(newBS.numWords >= other.numWords)
117+
def andNot(other: BitSet): BitSet = {
118+
val newBS = new BitSet(capacity)
125119
val smaller = math.min(numWords, other.numWords)
126120
var ind = 0
127-
while( ind < smaller ) {
121+
while ( ind < smaller ) {
128122
newBS.words(ind) = words(ind) & ~other.words(ind)
129123
ind += 1
130124
}
131-
while( ind < numWords ) {
132-
newBS.words(ind) = words(ind)
133-
ind += 1
125+
if ( ind < numWords ) {
126+
Array.copy( words, ind, newBS.words, ind, numWords - ind )
134127
}
135128
newBS
136129
}

core/src/test/scala/org/apache/spark/util/collection/BitSetSuite.scala

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class BitSetSuite extends FunSuite {
7070
assert(bitset.nextSetBit(97) === -1)
7171
}
7272

73-
test( "xor" ) {
73+
test( "xor len(bitsetX) < len(bitsetY)" ) {
7474
val setBitsX = Seq( 0, 2, 3, 37, 41 )
7575
val setBitsY = Seq( 0, 1, 3, 37, 38, 41, 85)
7676
val bitsetX = new BitSet(60)
@@ -92,15 +92,37 @@ class BitSetSuite extends FunSuite {
9292

9393
}
9494

95-
test( "and-not" ) {
95+
test( "xor len(bitsetX) > len(bitsetY)" ) {
96+
val setBitsX = Seq( 0, 1, 3, 37, 38, 41, 85)
97+
val setBitsY = Seq( 0, 2, 3, 37, 41 )
98+
val bitsetX = new BitSet(100)
99+
setBitsX.foreach( i => bitsetX.set(i))
100+
val bitsetY = new BitSet(60)
101+
setBitsY.foreach( i => bitsetY.set(i))
102+
103+
val bitsetXor = bitsetX ^ bitsetY
104+
105+
assert(bitsetXor.nextSetBit(0) === 1)
106+
assert(bitsetXor.nextSetBit(1) === 1)
107+
assert(bitsetXor.nextSetBit(2) === 2)
108+
assert(bitsetXor.nextSetBit(3) === 38)
109+
assert(bitsetXor.nextSetBit(38) === 38)
110+
assert(bitsetXor.nextSetBit(39) === 85)
111+
assert(bitsetXor.nextSetBit(42) === 85)
112+
assert(bitsetXor.nextSetBit(85) === 85)
113+
assert(bitsetXor.nextSetBit(86) === -1)
114+
115+
}
116+
117+
test( "andNot len(bitsetX) < len(bitsetY)" ) {
96118
val setBitsX = Seq( 0, 2, 3, 37, 41, 48 )
97119
val setBitsY = Seq( 0, 1, 3, 37, 38, 41, 85)
98120
val bitsetX = new BitSet(60)
99121
setBitsX.foreach( i => bitsetX.set(i))
100122
val bitsetY = new BitSet(100)
101123
setBitsY.foreach( i => bitsetY.set(i))
102124

103-
val bitsetDiff = bitsetX &~ bitsetY
125+
val bitsetDiff = bitsetX.andNot( bitsetY )
104126

105127
assert(bitsetDiff.nextSetBit(0) === 2)
106128
assert(bitsetDiff.nextSetBit(1) === 2)
@@ -110,4 +132,27 @@ class BitSetSuite extends FunSuite {
110132
assert(bitsetDiff.nextSetBit(49) === -1)
111133
assert(bitsetDiff.nextSetBit(65) === -1)
112134
}
135+
136+
test( "andNot len(bitsetX) > len(bitsetY)" ) {
137+
for( i <- 0 until 10000 ) {
138+
139+
val setBitsX = Seq( 0, 1, 3, 37, 38, 41, 85)
140+
val setBitsY = Seq( 0, 2, 3, 37, 41, 48 )
141+
val bitsetX = new BitSet(100)
142+
setBitsX.foreach( i => bitsetX.set(i))
143+
val bitsetY = new BitSet(60)
144+
setBitsY.foreach( i => bitsetY.set(i))
145+
146+
val bitsetDiff = bitsetX.andNot( bitsetY )
147+
148+
assert(bitsetDiff.nextSetBit(0) === 1)
149+
assert(bitsetDiff.nextSetBit(1) === 1)
150+
assert(bitsetDiff.nextSetBit(2) === 38)
151+
assert(bitsetDiff.nextSetBit(3) === 38)
152+
assert(bitsetDiff.nextSetBit(38) === 38)
153+
assert(bitsetDiff.nextSetBit(39) === 85)
154+
assert(bitsetDiff.nextSetBit(85) === 85)
155+
assert(bitsetDiff.nextSetBit(86) === -1)
156+
}
157+
}
113158
}

0 commit comments

Comments
 (0)