Apply fixjsstyle to libtess.js

This commit is contained in:
Tom Payne
2013-09-03 11:29:03 +02:00
parent 31f3e92494
commit f8efa7c6a4
21 changed files with 604 additions and 410 deletions
+7 -4
View File
@@ -37,6 +37,8 @@
// TODO(bckenny): more specific typing on key
/**
* [PQHandleElem description]
* @constructor
@@ -49,7 +51,7 @@ libtess.PQHandleElem = function() {
* @type {libtess.PQKey}
*/
this.key = null;
/**
* [node description]
* @type {libtess.PQHandle}
@@ -57,14 +59,15 @@ libtess.PQHandleElem = function() {
this.node = 0;
};
/**
* Allocate a PQHandleElem array of size size. If oldArray is not null, its
* contents are copied to the beginning of the new array. The rest of the array
* is filled with new PQHandleElems.
*
* @param {?Array.<libtess.PQHandleElem>} oldArray [description]
* @param {number} size [description]
* @return {Array.<libtess.PQHandleElem>} [description]
* @param {?Array.<libtess.PQHandleElem>} oldArray [description].
* @param {number} size [description].
* @return {Array.<libtess.PQHandleElem>} [description].
*/
libtess.PQHandleElem.realloc = function(oldArray, size) {
var newArray = new Array(size);
+6 -3
View File
@@ -38,6 +38,8 @@
// TODO(bckenny): maybe just have these created inline as literals
// (or unboxed directly - PQHandle is just an array index number)
/**
* [PQNode description]
* @constructor
@@ -50,14 +52,15 @@ libtess.PQNode = function() {
this.handle = 0;
};
/**
* Allocate a PQNode array of size size. If oldArray is not null, its contents
* are copied to the beginning of the new array. The rest of the array is
* filled with new PQNodes.
*
* @param {?Array.<libtess.PQNode>} oldArray [description]
* @param {number} size [description]
* @return {Array.<libtess.PQNode>} [description]
* @param {?Array.<libtess.PQNode>} oldArray [description].
* @param {number} size [description].
* @return {Array.<libtess.PQNode>} [description].
*/
libtess.PQNode.realloc = function(oldArray, size) {
var newArray = new Array(size);
+39 -26
View File
@@ -39,10 +39,12 @@
// TODO(bckenny): preallocating arrays may actually be hurting us in sort
// performance (esp if theres some undefs in there)
/**
* [PriorityQ description]
* @constructor
* @param {function(Object, Object): boolean} leq [description]
* @param {function(Object, Object): boolean} leq [description].
*/
libtess.PriorityQ = function(leq) {
/**
@@ -51,21 +53,21 @@ libtess.PriorityQ = function(leq) {
* @type {Array.<libtess.PQKey>}
*/
this.keys_ = libtess.PriorityQ.prototype.PQKeyRealloc_(null, libtess.PriorityQ.INIT_SIZE_);
/**
* Array of indexes into this.keys_
* @private
* @type {Array.<number>}
*/
this.order_ = null;
/**
* [size description]
* @private
* @type {number}
*/
this.size_ = 0;
/**
* [max_ description]
* @private
@@ -97,6 +99,7 @@ libtess.PriorityQ = function(leq) {
this.heap_ = new libtess.PriorityQHeap(this.leq_);
};
/**
* [INIT_SIZE_ description]
* @private
@@ -105,6 +108,7 @@ libtess.PriorityQ = function(leq) {
*/
libtess.PriorityQ.INIT_SIZE_ = 32;
/**
* [deleteQ description]
*/
@@ -117,6 +121,7 @@ libtess.PriorityQ.prototype.deleteQ = function() {
// NOTE(bckenny): nulled at callsite (sweep.donePriorityQ_)
};
/**
* [init description]
*/
@@ -154,16 +159,17 @@ libtess.PriorityQ.prototype.init = function() {
var p = 0;
var r = p + this.size_ - 1;
for (i = p; i < r; ++i) {
libtess.assert(this.leq_(this.keys_[this.order_[i+1]], this.keys_[this.order_[i]]));
libtess.assert(this.leq_(this.keys_[this.order_[i + 1]], this.keys_[this.order_[i]]));
}
}
// #endif
};
/**
* [insert description]
* @param {libtess.PQKey} keyNew [description]
* @return {libtess.PQHandle} [description]
* @param {libtess.PQKey} keyNew [description].
* @return {libtess.PQHandle} [description].
*/
libtess.PriorityQ.prototype.insert = function(keyNew) {
// NOTE(bckenny): originally returned LONG_MAX as alloc failure signal. no longer does.
@@ -181,18 +187,19 @@ libtess.PriorityQ.prototype.insert = function(keyNew) {
this.keys_[curr] = keyNew;
// Negative handles index the sorted array.
return -(curr+1);
return -(curr + 1);
};
/**
* Allocate a PQKey array of size size. If oldArray is not null, its
* contents are copied to the beginning of the new array. The rest of the array
* is filled with nulls.
*
* @private
* @param {?Array.<libtess.PQKey>} oldArray [description]
* @param {number} size [description]
* @return {Array.<(?libtess.PQKey)>} [description]
* @param {?Array.<libtess.PQKey>} oldArray [description].
* @param {number} size [description].
* @return {Array.<(?libtess.PQKey)>} [description].
*/
libtess.PriorityQ.prototype.PQKeyRealloc_ = function(oldArray, size) {
// TODO(bckenny): double check return type. can we have ? there?
@@ -213,12 +220,13 @@ libtess.PriorityQ.prototype.PQKeyRealloc_ = function(oldArray, size) {
return newArray;
};
/**
* [keyLessThan_ description]
* @private
* @param {number} x [description]
* @param {number} y [description]
* @return {boolean} [description]
* @param {number} x [description].
* @param {number} y [description].
* @return {boolean} [description].
*/
libtess.PriorityQ.prototype.keyLessThan_ = function(x, y) {
// NOTE(bckenny): was macro LT
@@ -227,12 +235,13 @@ libtess.PriorityQ.prototype.keyLessThan_ = function(x, y) {
return !this.leq_(keyY, keyX);
};
/**
* [keyGreaterThan_ description]
* @private
* @param {number} x [description]
* @param {number} y [description]
* @return {boolean} [description]
* @param {number} x [description].
* @param {number} y [description].
* @return {boolean} [description].
*/
libtess.PriorityQ.prototype.keyGreaterThan_ = function(x, y) {
// NOTE(bckenny): was macro GT
@@ -241,16 +250,17 @@ libtess.PriorityQ.prototype.keyGreaterThan_ = function(x, y) {
return !this.leq_(keyX, keyY);
};
/**
* [extractMin description]
* @return {libtess.PQKey} [description]
* @return {libtess.PQKey} [description].
*/
libtess.PriorityQ.prototype.extractMin = function() {
if (this.size_ === 0) {
return this.heap_.extractMin();
}
var sortMin = this.keys_[this.order_[this.size_-1]];
var sortMin = this.keys_[this.order_[this.size_ - 1]];
if (!this.heap_.isEmpty()) {
var heapMin = this.heap_.minimum();
if (this.leq_(heapMin, sortMin)) {
@@ -260,21 +270,22 @@ libtess.PriorityQ.prototype.extractMin = function() {
do {
--this.size_;
} while(this.size_ > 0 && this.keys_[this.order_[this.size_-1]] === null);
} while (this.size_ > 0 && this.keys_[this.order_[this.size_ - 1]] === null);
return sortMin;
};
/**
* [minimum description]
* @return {libtess.PQKey} [description]
* @return {libtess.PQKey} [description].
*/
libtess.PriorityQ.prototype.minimum = function() {
if (this.size_ === 0) {
return this.heap_.minimum();
}
var sortMin = this.keys_[this.order_[this.size_-1]];
var sortMin = this.keys_[this.order_[this.size_ - 1]];
if (!this.heap_.isEmpty()) {
var heapMin = this.heap_.minimum();
if (this.leq_(heapMin, sortMin)) {
@@ -285,29 +296,31 @@ libtess.PriorityQ.prototype.minimum = function() {
return sortMin;
};
/**
* [isEmpty description]
* @return {boolean} [description]
* @return {boolean} [description].
*/
libtess.PriorityQ.prototype.isEmpty = function() {
return (this.size_ === 0) && this.heap_.isEmpty();
};
/**
* [remove description]
* @param {libtess.PQHandle} curr [description]
* @param {libtess.PQHandle} curr [description].
*/
libtess.PriorityQ.prototype.remove = function(curr) {
if (curr >= 0) {
this.heap_.remove(curr);
return;
}
curr = -(curr+1);
curr = -(curr + 1);
libtess.assert(curr < this.max_ && this.keys_[curr] !== null);
this.keys_[curr] = null;
while(this.size_ > 0 && this.keys_[this.order_[this.size_-1]] === null) {
while (this.size_ > 0 && this.keys_[this.order_[this.size_ - 1]] === null) {
--this.size_;
}
};
+26 -14
View File
@@ -39,10 +39,12 @@
// TODO(bckenny): keys appear to always be GluVertex in this case?
/**
* [PriorityQHeap description]
* @constructor
* @param {function(libtess.PQKey, libtess.PQKey): boolean} leq [description]
* @param {function(libtess.PQKey, libtess.PQKey): boolean} leq [description].
*/
libtess.PriorityQHeap = function(leq) {
/**
@@ -102,11 +104,12 @@ libtess.PriorityQHeap = function(leq) {
* @type {function(libtess.PQKey, libtess.PQKey): boolean}
*/
this.leq_ = leq;
// so that minimum returns null
this.nodes_[1].handle = 1;
};
/**
* [INIT_SIZE_ description]
* @private
@@ -115,6 +118,7 @@ libtess.PriorityQHeap = function(leq) {
*/
libtess.PriorityQHeap.INIT_SIZE_ = 32;
/**
* [deleteHeap description]
*/
@@ -125,19 +129,21 @@ libtess.PriorityQHeap.prototype.deleteHeap = function() {
// NOTE(bckenny): nulled at callsite in PriorityQ.deleteQ
};
/**
* Initializing ordering of the heap. Must be called before any method other than
* insert is called to ensure correctness when removing or querying.
*/
libtess.PriorityQHeap.prototype.init = function() {
// This method of building a heap is O(n), rather than O(n lg n).
for(var i = this.size_; i >= 1; --i) {
for (var i = this.size_; i >= 1; --i) {
this.floatDown_(i);
}
this.initialized_ = true;
};
/**
* Insert a new key into the heap.
* @param {libtess.PQKey} keyNew The key to insert.
@@ -147,7 +153,7 @@ libtess.PriorityQHeap.prototype.insert = function(keyNew) {
var curr = ++this.size_;
// if the heap overflows, double its size.
if ((curr*2) > this.max_) {
if ((curr * 2) > this.max_) {
this.max_ *= 2;
this.nodes_ = libtess.PQNode.realloc(this.nodes_, this.max_ + 1);
this.handles_ = libtess.PQHandleElem.realloc(this.handles_, this.max_ + 1);
@@ -172,6 +178,7 @@ libtess.PriorityQHeap.prototype.insert = function(keyNew) {
return free;
};
/**
* @return {boolean} Whether the heap is empty.
*/
@@ -179,19 +186,21 @@ libtess.PriorityQHeap.prototype.isEmpty = function() {
return this.size_ === 0;
};
/**
* Returns the minimum key in the heap. If the heap is empty, null will be
* returned.
* @return {libtess.PQKey} [description]
* @return {libtess.PQKey} [description].
*/
libtess.PriorityQHeap.prototype.minimum = function() {
return this.handles_[this.nodes_[1].handle].key;
};
/**
* Removes the minimum key from the heap and returns it. If the heap is empty,
* null will be returned.
* @return {libtess.PQKey} [description]
* @return {libtess.PQKey} [description].
*/
libtess.PriorityQHeap.prototype.extractMin = function() {
var n = this.nodes_;
@@ -207,7 +216,7 @@ libtess.PriorityQHeap.prototype.extractMin = function() {
h[hMin].node = this.freeList_;
this.freeList_ = hMin;
if (--this.size_ > 0 ) {
if (--this.size_ > 0) {
this.floatDown_(1);
}
}
@@ -215,9 +224,10 @@ libtess.PriorityQHeap.prototype.extractMin = function() {
return min;
};
/**
* Remove key associated with handle hCurr (returned from insert) from heap.
* @param {libtess.PQHandle} hCurr [description]
* @param {libtess.PQHandle} hCurr [description].
*/
libtess.PriorityQHeap.prototype.remove = function(hCurr) {
var n = this.nodes_;
@@ -230,7 +240,7 @@ libtess.PriorityQHeap.prototype.remove = function(hCurr) {
h[n[curr].handle].node = curr;
if (curr <= --this.size_) {
if (curr <= 1 || this.leq_(h[n[curr>>1].handle].key, h[n[curr].handle].key)) {
if (curr <= 1 || this.leq_(h[n[curr >> 1].handle].key, h[n[curr].handle].key)) {
this.floatDown_(curr);
} else {
this.floatUp_(curr);
@@ -242,21 +252,22 @@ libtess.PriorityQHeap.prototype.remove = function(hCurr) {
this.freeList_ = hCurr;
};
/**
* [floatDown_ description]
* @private
* @param {libtess.PQHandle} curr [description]
* @param {libtess.PQHandle} curr [description].
*/
libtess.PriorityQHeap.prototype.floatDown_ = function(curr) {
var n = this.nodes_;
var h = this.handles_;
var hCurr = n[curr].handle;
for( ;; ) {
for (;; ) {
// The children of node i are nodes 2i and 2i+1.
// set child to the index of the child with the minimum key
var child = curr << 1;
if (child < this.size_ && this.leq_(h[n[child+1].handle].key, h[n[child].handle].key)) {
if (child < this.size_ && this.leq_(h[n[child + 1].handle].key, h[n[child].handle].key)) {
++child;
}
@@ -274,17 +285,18 @@ libtess.PriorityQHeap.prototype.floatDown_ = function(curr) {
}
};
/**
* [floatUp_ description]
* @private
* @param {libtess.PQHandle} curr [description]
* @param {libtess.PQHandle} curr [description].
*/
libtess.PriorityQHeap.prototype.floatUp_ = function(curr) {
var n = this.nodes_;
var h = this.handles_;
var hCurr = n[curr].handle;
for( ;; ) {
for (;; ) {
var parent = curr >> 1;
var hParent = n[parent].handle;
if (parent === 0 || this.leq_(h[hParent].key, h[hCurr].key)) {