Refactor ol.structs.IntegerSet to use a packed array

This commit is contained in:
Tom Payne
2013-03-19 00:53:22 +01:00
parent 070b80c28d
commit 1833ec15e2
2 changed files with 127 additions and 165 deletions
+35 -35
View File
@@ -10,7 +10,7 @@ describe('ol.structs.IntegerSet', function() {
it('constructs an empty instance', function() {
var is = new ol.structs.IntegerSet();
expect(is).to.be.an(ol.structs.IntegerSet);
expect(is.pack()).to.be.empty();
expect(is.getArray()).to.be.empty();
});
});
@@ -22,7 +22,7 @@ describe('ol.structs.IntegerSet', function() {
it('constructs with a valid array', function() {
var is = ol.structs.IntegerSet.unpack([0, 2, 4, 6]);
expect(is).to.be.an(ol.structs.IntegerSet);
expect(is.pack()).to.equalArray([0, 2, 4, 6]);
expect(is.getArray()).to.equalArray([0, 2, 4, 6]);
});
it('throws an exception with an odd number of elements', function() {
@@ -50,7 +50,7 @@ describe('ol.structs.IntegerSet', function() {
it('creates a new element', function() {
is.addRange(0, 2);
expect(is.pack()).to.equalArray([0, 2]);
expect(is.getArray()).to.equalArray([0, 2]);
});
});
@@ -129,52 +129,52 @@ describe('ol.structs.IntegerSet', function() {
it('inserts before the first element', function() {
is.addRange(0, 2);
expect(is.pack()).to.equalArray([0, 2, 4, 6, 8, 10, 12, 14]);
expect(is.getArray()).to.equalArray([0, 2, 4, 6, 8, 10, 12, 14]);
});
it('extends the first element to the left', function() {
is.addRange(0, 4);
expect(is.pack()).to.equalArray([0, 6, 8, 10, 12, 14]);
expect(is.getArray()).to.equalArray([0, 6, 8, 10, 12, 14]);
});
it('extends the first element to the right', function() {
is.addRange(6, 7);
expect(is.pack()).to.equalArray([4, 7, 8, 10, 12, 14]);
expect(is.getArray()).to.equalArray([4, 7, 8, 10, 12, 14]);
});
it('merges the first two elements', function() {
is.addRange(6, 8);
expect(is.pack()).to.equalArray([4, 10, 12, 14]);
expect(is.getArray()).to.equalArray([4, 10, 12, 14]);
});
it('extends middle elements to the left', function() {
is.addRange(7, 8);
expect(is.pack()).to.equalArray([4, 6, 7, 10, 12, 14]);
expect(is.getArray()).to.equalArray([4, 6, 7, 10, 12, 14]);
});
it('extends middle elements to the right', function() {
is.addRange(10, 11);
expect(is.pack()).to.equalArray([4, 6, 8, 11, 12, 14]);
expect(is.getArray()).to.equalArray([4, 6, 8, 11, 12, 14]);
});
it('merges the last two elements', function() {
is.addRange(10, 12);
expect(is.pack()).to.equalArray([4, 6, 8, 14]);
expect(is.getArray()).to.equalArray([4, 6, 8, 14]);
});
it('extends the last element to the left', function() {
is.addRange(11, 12);
expect(is.pack()).to.equalArray([4, 6, 8, 10, 11, 14]);
expect(is.getArray()).to.equalArray([4, 6, 8, 10, 11, 14]);
});
it('extends the last element to the right', function() {
is.addRange(14, 15);
expect(is.pack()).to.equalArray([4, 6, 8, 10, 12, 15]);
expect(is.getArray()).to.equalArray([4, 6, 8, 10, 12, 15]);
});
it('inserts after the last element', function() {
is.addRange(16, 18);
expect(is.pack()).to.equalArray([4, 6, 8, 10, 12, 14, 16, 18]);
expect(is.getArray()).to.equalArray([4, 6, 8, 10, 12, 14, 16, 18]);
});
});
@@ -183,7 +183,7 @@ describe('ol.structs.IntegerSet', function() {
it('clears the instance', function() {
is.clear();
expect(is.pack()).to.be.empty();
expect(is.getArray()).to.be.empty();
});
});
@@ -281,57 +281,57 @@ describe('ol.structs.IntegerSet', function() {
it('removes the first part of the first element', function() {
is.removeRange(4, 5);
expect(is.pack()).to.equalArray([5, 6, 8, 10, 12, 14]);
expect(is.getArray()).to.equalArray([5, 6, 8, 10, 12, 14]);
});
it('removes the last part of the first element', function() {
is.removeRange(5, 6);
expect(is.pack()).to.equalArray([4, 5, 8, 10, 12, 14]);
expect(is.getArray()).to.equalArray([4, 5, 8, 10, 12, 14]);
});
it('removes the first element', function() {
is.removeRange(4, 6);
expect(is.pack()).to.equalArray([8, 10, 12, 14]);
expect(is.getArray()).to.equalArray([8, 10, 12, 14]);
});
it('removes the first part of a middle element', function() {
is.removeRange(8, 9);
expect(is.pack()).to.equalArray([4, 6, 9, 10, 12, 14]);
expect(is.getArray()).to.equalArray([4, 6, 9, 10, 12, 14]);
});
it('removes the last part of a middle element', function() {
is.removeRange(9, 10);
expect(is.pack()).to.equalArray([4, 6, 8, 9, 12, 14]);
expect(is.getArray()).to.equalArray([4, 6, 8, 9, 12, 14]);
});
it('removes a middle element', function() {
is.removeRange(8, 10);
expect(is.pack()).to.equalArray([4, 6, 12, 14]);
expect(is.getArray()).to.equalArray([4, 6, 12, 14]);
});
it('removes the first part of the last element', function() {
is.removeRange(12, 13);
expect(is.pack()).to.equalArray([4, 6, 8, 10, 13, 14]);
expect(is.getArray()).to.equalArray([4, 6, 8, 10, 13, 14]);
});
it('removes the last part of the last element', function() {
is.removeRange(13, 14);
expect(is.pack()).to.equalArray([4, 6, 8, 10, 12, 13]);
expect(is.getArray()).to.equalArray([4, 6, 8, 10, 12, 13]);
});
it('removes the last element', function() {
is.removeRange(12, 14);
expect(is.pack()).to.equalArray([4, 6, 8, 10]);
expect(is.getArray()).to.equalArray([4, 6, 8, 10]);
});
it('can remove multiple ranges near the start', function() {
is.removeRange(3, 11);
expect(is.pack()).to.equalArray([12, 14]);
expect(is.getArray()).to.equalArray([12, 14]);
});
it('can remove multiple ranges near the start', function() {
is.removeRange(7, 15);
expect(is.pack()).to.equalArray([4, 6]);
expect(is.getArray()).to.equalArray([4, 6]);
});
it('throws an exception when passed an invalid range', function() {
@@ -400,29 +400,29 @@ describe('ol.structs.IntegerSet', function() {
it('removing an empty range has no effect', function() {
is.removeRange(0, 0);
expect(is.pack()).to.equalArray(
expect(is.getArray()).to.equalArray(
[0, 1, 2, 4, 5, 8, 9, 12, 13, 15, 16, 17]);
});
it('can remove elements from the middle of range', function() {
is.removeRange(6, 7);
expect(is.pack()).to.equalArray(
expect(is.getArray()).to.equalArray(
[0, 1, 2, 4, 5, 6, 7, 8, 9, 12, 13, 15, 16, 17]);
});
it('can remove multiple ranges', function() {
is.removeRange(2, 12);
expect(is.pack()).to.equalArray([0, 1, 13, 15, 16, 17]);
expect(is.getArray()).to.equalArray([0, 1, 13, 15, 16, 17]);
});
it('can remove multiple ranges and reduce others', function() {
is.removeRange(0, 10);
expect(is.pack()).to.equalArray([10, 12, 13, 15, 16, 17]);
expect(is.getArray()).to.equalArray([10, 12, 13, 15, 16, 17]);
});
it('can remove all ranges', function() {
is.removeRange(0, 18);
expect(is.pack()).to.equalArray([]);
expect(is.getArray()).to.equalArray([]);
});
});
@@ -446,7 +446,7 @@ describe('ol.structs.IntegerSet', function() {
this.integers_ = {};
};
SimpleIntegerSet.prototype.pack = function() {
SimpleIntegerSet.prototype.getArray = function() {
var integers = goog.array.map(
goog.object.getKeys(this.integers_), Number);
goog.array.sort(integers);
@@ -489,7 +489,7 @@ describe('ol.structs.IntegerSet', function() {
addStop = addStart + goog.math.randomInt(16);
is.addRange(addStart, addStop);
sis.addRange(addStart, addStop);
expect(is.pack()).to.equalArray(sis.pack());
expect(is.getArray()).to.equalArray(sis.getArray());
}
});
@@ -502,7 +502,7 @@ describe('ol.structs.IntegerSet', function() {
removeStop = removeStart + goog.math.randomInt(16);
is.removeRange(removeStart, removeStop);
sis.removeRange(removeStart, removeStop);
expect(is.pack()).to.equalArray(sis.pack());
expect(is.getArray()).to.equalArray(sis.getArray());
}
});
@@ -518,7 +518,7 @@ describe('ol.structs.IntegerSet', function() {
is.removeRange(start, stop);
sis.removeRange(start, stop);
}
expect(is.pack()).to.equalArray(sis.pack());
expect(is.getArray()).to.equalArray(sis.getArray());
}
});
@@ -538,7 +538,7 @@ describe('ol.structs.IntegerSet', function() {
is.clear();
sis.clear();
}
expect(is.pack()).to.equalArray(sis.pack());
expect(is.getArray()).to.equalArray(sis.getArray());
}
});