Merge pull request #794 from ahocevar/simplify-ranges

Simplifying ranges structure by adding compiler hints. r=@tschaub,@twpayne,@elemoine
This commit is contained in:
ahocevar
2013-06-18 01:27:24 -07:00
2 changed files with 30 additions and 29 deletions
+21 -20
View File
@@ -19,7 +19,7 @@ ol.geom2.LineString;
* This is an internal class that will be removed from the API. * This is an internal class that will be removed from the API.
* @constructor * @constructor
* @param {ol.structs.Buffer} buf Buffer. * @param {ol.structs.Buffer} buf Buffer.
* @param {Object.<number, Array.<number>>=} opt_ranges Ranges. * @param {Object.<string, number>=} opt_ranges Ranges.
* @param {number=} opt_dim Dimension. * @param {number=} opt_dim Dimension.
*/ */
ol.geom2.LineStringCollection = function(buf, opt_ranges, opt_dim) { ol.geom2.LineStringCollection = function(buf, opt_ranges, opt_dim) {
@@ -30,7 +30,7 @@ ol.geom2.LineStringCollection = function(buf, opt_ranges, opt_dim) {
this.buf = buf; this.buf = buf;
/** /**
* @type {Object.<number, Array.<number>>} * @type {Object.<string, number>}
*/ */
this.ranges = goog.isDef(opt_ranges) ? opt_ranges : {}; this.ranges = goog.isDef(opt_ranges) ? opt_ranges : {};
@@ -79,7 +79,7 @@ ol.geom2.LineStringCollection.pack =
} }
capacity *= dim; capacity *= dim;
var arr = new Array(capacity); var arr = new Array(capacity);
/** @type {Object.<number, Array.<number>>} */ /** @type {Object.<string, number>} */
var ranges = {}; var ranges = {};
var offset = 0; var offset = 0;
var start; var start;
@@ -87,7 +87,7 @@ ol.geom2.LineStringCollection.pack =
goog.asserts.assert(unpackedLineStrings[i].length > 1); goog.asserts.assert(unpackedLineStrings[i].length > 1);
start = offset; start = offset;
offset = ol.geom2.packPoints(arr, offset, unpackedLineStrings[i], dim); offset = ol.geom2.packPoints(arr, offset, unpackedLineStrings[i], dim);
ranges[start] = [start, offset]; ranges[/** @type {string} */ (start)] = offset;
} }
goog.asserts.assert(offset <= capacity); goog.asserts.assert(offset <= capacity);
var buf = new ol.structs.Buffer(arr, offset); var buf = new ol.structs.Buffer(arr, offset);
@@ -103,7 +103,7 @@ ol.geom2.LineStringCollection.prototype.add = function(lineString) {
var n = lineString.length * this.dim; var n = lineString.length * this.dim;
var offset = this.buf.allocate(n); var offset = this.buf.allocate(n);
goog.asserts.assert(offset != -1); goog.asserts.assert(offset != -1);
this.ranges[offset] = [offset, offset + n]; this.ranges[/** @type {string} */ (offset)] = offset + n;
ol.geom2.packPoints(this.buf.getArray(), offset, lineString, this.dim); ol.geom2.packPoints(this.buf.getArray(), offset, lineString, this.dim);
return offset; return offset;
}; };
@@ -115,9 +115,9 @@ ol.geom2.LineStringCollection.prototype.add = function(lineString) {
*/ */
ol.geom2.LineStringCollection.prototype.get = function(offset) { ol.geom2.LineStringCollection.prototype.get = function(offset) {
goog.asserts.assert(offset in this.ranges); goog.asserts.assert(offset in this.ranges);
var range = this.ranges[offset]; var range = this.ranges[/** @type {string} */ (offset)];
return ol.geom2.unpackPoints( return ol.geom2.unpackPoints(
this.buf.getArray(), range[0], range[1], this.dim); this.buf.getArray(), offset, range, this.dim);
}; };
@@ -147,11 +147,12 @@ ol.geom2.LineStringCollection.prototype.getIndices = function() {
goog.array.sort(offsets); goog.array.sort(offsets);
var n = offsets.length; var n = offsets.length;
var indices = []; var indices = [];
var i, j, range, stop; var i, j, range, offset, stop;
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
range = this.ranges[offsets[i]]; offset = offsets[i];
stop = range[1] / dim - 1; range = this.ranges[offset];
for (j = range[0] / dim; j < stop; ++j) { stop = range / dim - 1;
for (j = offset / dim; j < stop; ++j) {
indices.push(j, j + 1); indices.push(j, j + 1);
} }
} }
@@ -164,9 +165,9 @@ ol.geom2.LineStringCollection.prototype.getIndices = function() {
*/ */
ol.geom2.LineStringCollection.prototype.remove = function(offset) { ol.geom2.LineStringCollection.prototype.remove = function(offset) {
goog.asserts.assert(offset in this.ranges); goog.asserts.assert(offset in this.ranges);
var range = this.ranges[offset]; var range = this.ranges[/** @type {string} */ (offset)];
this.buf.remove(range[1] - range[0], range[0]); this.buf.remove(range - offset, offset);
delete this.ranges[offset]; delete this.ranges[/** @type {string} */ (offset)];
}; };
@@ -178,10 +179,10 @@ ol.geom2.LineStringCollection.prototype.remove = function(offset) {
ol.geom2.LineStringCollection.prototype.set = function(offset, lineString) { ol.geom2.LineStringCollection.prototype.set = function(offset, lineString) {
var dim = this.dim; var dim = this.dim;
goog.asserts.assert(offset in this.ranges); goog.asserts.assert(offset in this.ranges);
var range = this.ranges[offset]; var range = this.ranges[/** @type {string} */ (offset)];
if (lineString.length * dim == range[1] - range[0]) { if (lineString.length * dim == range - offset) {
ol.geom2.packPoints(this.buf.getArray(), range[0], lineString, dim); ol.geom2.packPoints(this.buf.getArray(), offset, lineString, dim);
this.buf.markDirty(range[1] - range[0], range[0]); this.buf.markDirty(range - offset, offset);
return offset; return offset;
} else { } else {
this.remove(offset); this.remove(offset);
@@ -200,9 +201,9 @@ ol.geom2.LineStringCollection.prototype.unpack = function() {
var i = 0; var i = 0;
var offset, range; var offset, range;
for (offset in this.ranges) { for (offset in this.ranges) {
range = this.ranges[Number(offset)]; range = this.ranges[offset];
lineStrings[i++] = ol.geom2.unpackPoints( lineStrings[i++] = ol.geom2.unpackPoints(
this.buf.getArray(), range[0], range[1], dim); this.buf.getArray(), Number(offset), range, dim);
} }
return lineStrings; return lineStrings;
}; };
+9 -9
View File
@@ -41,8 +41,8 @@ describe('ol.geom2.LineStringCollection', function() {
[[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9]]]); [[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9]]]);
expect(lsc.buf.getArray()).to.eql([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); expect(lsc.buf.getArray()).to.eql([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
expect(lsc.getCount()).to.be(2); expect(lsc.getCount()).to.be(2);
expect(lsc.ranges[0]).to.eql([0, 6]); expect(lsc.ranges[0]).to.be(6);
expect(lsc.ranges[6]).to.eql([6, 10]); expect(lsc.ranges[6]).to.be(10);
expect(lsc.dim).to.be(2); expect(lsc.dim).to.be(2);
}); });
@@ -51,8 +51,8 @@ describe('ol.geom2.LineStringCollection', function() {
[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]); [[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]);
expect(lsc.buf.getArray()).to.eql([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); expect(lsc.buf.getArray()).to.eql([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
expect(lsc.getCount()).to.be(2); expect(lsc.getCount()).to.be(2);
expect(lsc.ranges[0]).to.eql([0, 6]); expect(lsc.ranges[0]).to.be(6);
expect(lsc.ranges[6]).to.eql([6, 12]); expect(lsc.ranges[6]).to.be(12);
expect(lsc.dim).to.be(3); expect(lsc.dim).to.be(3);
}); });
@@ -63,8 +63,8 @@ describe('ol.geom2.LineStringCollection', function() {
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
expect(lsc.buf.getArray()).to.have.length(32); expect(lsc.buf.getArray()).to.have.length(32);
expect(lsc.getCount()).to.be(2); expect(lsc.getCount()).to.be(2);
expect(lsc.ranges[0]).to.eql([0, 6]); expect(lsc.ranges[0]).to.be(6);
expect(lsc.ranges[6]).to.eql([6, 10]); expect(lsc.ranges[6]).to.be(10);
expect(lsc.dim).to.be(2); expect(lsc.dim).to.be(2);
}); });
@@ -106,7 +106,7 @@ describe('ol.geom2.LineStringCollection', function() {
var offset = lsc.add([[0, 1], [2, 3]]); var offset = lsc.add([[0, 1], [2, 3]]);
expect(offset).to.be(0); expect(offset).to.be(0);
expect(lsc.getCount()).to.be(1); expect(lsc.getCount()).to.be(1);
expect(lsc.ranges[0]).to.eql([0, 4]); expect(lsc.ranges[0]).to.be(4);
expect(lsc.dim).to.be(2); expect(lsc.dim).to.be(2);
}); });
@@ -163,8 +163,8 @@ describe('ol.geom2.LineStringCollection', function() {
var offset2 = lsc.add([[4, 5], [6, 7]]); var offset2 = lsc.add([[4, 5], [6, 7]]);
expect(offset2).to.be(4); expect(offset2).to.be(4);
expect(lsc.getCount()).to.be(2); expect(lsc.getCount()).to.be(2);
expect(lsc.ranges[0]).to.eql([0, 4]); expect(lsc.ranges[0]).to.be(4);
expect(lsc.ranges[4]).to.eql([4, 8]); expect(lsc.ranges[4]).to.be(8);
expect(lsc.dim).to.be(2); expect(lsc.dim).to.be(2);
}); });