Skip to content

Commit b0290b7

Browse files
committed
add unit test & update package-lock
1 parent 31f5900 commit b0290b7

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

FastBitSet.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,9 @@ FastBitSet.prototype.difference = function (otherbitmap) {
331331
// (for this set A and other set B,
332332
// this computes B = A - B and returns B)
333333
FastBitSet.prototype.difference2 = function (otherbitmap) {
334-
const newcount = Math.min(this.words.length, otherbitmap.words.length);
334+
const mincount = Math.min(this.words.length, otherbitmap.words.length);
335335
let k = 0 | 0;
336-
for (; k + 7 < newcount; k += 8) {
336+
for (; k + 7 < mincount; k += 8) {
337337
otherbitmap.words[k] = this.words[k] & ~otherbitmap.words[k];
338338
otherbitmap.words[k + 1] = this.words[k + 1] & ~otherbitmap.words[k + 1];
339339
otherbitmap.words[k + 2] = this.words[k + 2] & ~otherbitmap.words[k + 2];
@@ -343,9 +343,14 @@ FastBitSet.prototype.difference2 = function (otherbitmap) {
343343
otherbitmap.words[k + 6] = this.words[k + 6] & ~otherbitmap.words[k + 6];
344344
otherbitmap.words[k + 7] = this.words[k + 7] & ~otherbitmap.words[k + 7];
345345
}
346-
for (; k < newcount; ++k) {
346+
for (; k < mincount; ++k) {
347347
otherbitmap.words[k] = this.words[k] & ~otherbitmap.words[k];
348348
}
349+
// remaining words are all part of difference
350+
for (k = this.words.length - 1; k >= mincount; --k) {
351+
otherbitmap.words[k] = this.words[k];
352+
}
353+
otherbitmap.words = otherbitmap.words.slice(0, this.words.length);
349354
return otherbitmap;
350355
};
351356

package-lock.json

+25-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unit/basictests.js

+13
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ describe("BitSet", function () {
135135
if (mb2.size() != 2) throw "bad diff";
136136
});
137137

138+
it("Testing difference2", function () {
139+
const a1 = [1, 2, 4, 5, 10];
140+
const a2 = [1, 2, 4, 5, 10, 100, 1000];
141+
let mb1 = new FastBitSet(a1);
142+
let mb2 = new FastBitSet(a2);
143+
mb1.difference2(mb2);
144+
if (!mb2.isEmpty()) throw "bad diff1";
145+
mb1 = new FastBitSet(a1);
146+
mb2 = new FastBitSet(a2);
147+
mb2.difference2(mb1);
148+
if (mb1.size() != 2) throw "bad diff2";
149+
});
150+
138151
it("Testing union", function () {
139152
const a1 = [1, 2, 4, 5, 10];
140153
const a2 = [1, 2, 4, 5, 10, 100, 1000];

0 commit comments

Comments
 (0)