Permalink
Browse files

Updating builds

  • Loading branch information...
1 parent 578ea7e commit e0f2f611e6b4b42dfadb8096741f0aa9f2d8cc5f @iros iros committed Oct 27, 2012
View
@@ -1 +1 @@
-2012/10/26 02:13
+2012/10/27 04:34
View
@@ -1,5 +1,5 @@
/**
-* Miso.Dataset - v0.3.0 - 10/26/2012
+* Miso.Dataset - v0.3.0 - 10/27/2012
* http://github.com/misoproject/dataset
* Copyright (c) 2012 Alex Graul, Irene Ros;
* Dual Licensed: MIT, GPL
@@ -695,114 +695,52 @@
* Parameters:
* options - Optional
*/
- sort : function(args) {
- var options = {};
+ sort : function(args) {
+ var options = {}, cachedRows = [];
//If the first param is the comparator, set it as such.
if ( _.isFunction(args) ) {
options.comparator = args;
} else {
- options = args || options;
+ options = args || {};
}
if (options.comparator) {
this.comparator = options.comparator;
- }
-
- if (_.isUndefined(this.comparator)) {
+ } else if (_.isUndefined(this.comparator)) {
throw new Error("Cannot sort without this.comparator.");
- }
-
- var count = this.length, end;
-
- if (count === 1) {
- // we're done. only one item, all sorted.
- return;
}
- var swap = _.bind(function(from, to) {
-
- // move second row over to first
- var row = this.rowByPosition(to);
-
- _.each(row, function(value, column) {
- var colPosition = this._columnPositionByName[column],
- value2 = this._columns[colPosition].data[from];
- this._columns[colPosition].data.splice(from, 1, value);
- this._columns[colPosition].data.splice(to, 1, value2);
- }, this);
- }, this);
-
- var siftDown = _.bind(function(start, end) {
- var root = start, child;
- while (root * 2 <= end) {
- child = root * 2;
- var root_node = this.rowByPosition(root);
-
- if ((child + 1 < end) &&
- this.comparator(
- this.rowByPosition(child),
- this.rowByPosition(child+1)
- ) < 0) {
- child++;
- }
-
- if (this.comparator(
- root_node,
- this.rowByPosition(child)) < 0) {
-
- swap(root, child);
- root = child;
- } else {
- return;
- }
-
- }
-
- }, this);
-
+ // cache rows
+ var i, j, row;
+ for(i = 0; i < this.length; i++) {
+ cachedRows[i] = this._row(i);
+ }
- // puts data in max-heap order
- var heapify = function(count) {
- var start = Math.round((count - 2) / 2);
- while (start >= 0) {
- siftDown(start, count - 1);
- start--;
- }
- };
+ cachedRows.sort( this.comparator );
- if (count > 2) {
- heapify(count);
+ // iterate through cached rows, overwriting data in columns
+ i = cachedRows.length;
+ while ( i-- ) {
+ row = cachedRows[i];
- end = count - 1;
- while (end > 1) {
-
- swap(end, 0);
- end--;
- siftDown(0, end);
+ this._rowIdByPosition[i] = row[ this.idAttribute ];
+ this._rowPositionById[ row[ this.idAttribute ] ] = i;
+ j = this._columns.length;
+ while ( j-- ) {
+ var col = this._columns[j];
+ col.data[i] = row[ col.name ];
}
- } else {
- if (this.comparator(
- this.rowByPosition(0),
- this.rowByPosition(1)) > 0) {
- swap(0,1);
- }
- }
-
- // check last two rows, they seem to always be off sync.
- if (this.comparator(
- this.rowByPosition(this.length - 2),
- this.rowByPosition(this.length - 1)) > 0) {
- swap(this.length - 1,this.length - 2);
}
- if (this.syncable && options.silent) {
+ if (this.syncable && !options.silent) {
this.trigger("sort");
}
+
return this;
},
-
+
/**
* Exports a version of the dataset in json format.
* Returns:
View
@@ -1,5 +1,5 @@
/**
-* Miso.Dataset - v0.3.0 - 10/26/2012
+* Miso.Dataset - v0.3.0 - 10/27/2012
* http://github.com/misoproject/dataset
* Copyright (c) 2012 Alex Graul, Irene Ros;
* Dual Licensed: MIT, GPL
@@ -695,114 +695,52 @@
* Parameters:
* options - Optional
*/
- sort : function(args) {
- var options = {};
+ sort : function(args) {
+ var options = {}, cachedRows = [];
//If the first param is the comparator, set it as such.
if ( _.isFunction(args) ) {
options.comparator = args;
} else {
- options = args || options;
+ options = args || {};
}
if (options.comparator) {
this.comparator = options.comparator;
- }
-
- if (_.isUndefined(this.comparator)) {
+ } else if (_.isUndefined(this.comparator)) {
throw new Error("Cannot sort without this.comparator.");
- }
-
- var count = this.length, end;
-
- if (count === 1) {
- // we're done. only one item, all sorted.
- return;
}
- var swap = _.bind(function(from, to) {
-
- // move second row over to first
- var row = this.rowByPosition(to);
-
- _.each(row, function(value, column) {
- var colPosition = this._columnPositionByName[column],
- value2 = this._columns[colPosition].data[from];
- this._columns[colPosition].data.splice(from, 1, value);
- this._columns[colPosition].data.splice(to, 1, value2);
- }, this);
- }, this);
-
- var siftDown = _.bind(function(start, end) {
- var root = start, child;
- while (root * 2 <= end) {
- child = root * 2;
- var root_node = this.rowByPosition(root);
-
- if ((child + 1 < end) &&
- this.comparator(
- this.rowByPosition(child),
- this.rowByPosition(child+1)
- ) < 0) {
- child++;
- }
-
- if (this.comparator(
- root_node,
- this.rowByPosition(child)) < 0) {
-
- swap(root, child);
- root = child;
- } else {
- return;
- }
-
- }
-
- }, this);
-
+ // cache rows
+ var i, j, row;
+ for(i = 0; i < this.length; i++) {
+ cachedRows[i] = this._row(i);
+ }
- // puts data in max-heap order
- var heapify = function(count) {
- var start = Math.round((count - 2) / 2);
- while (start >= 0) {
- siftDown(start, count - 1);
- start--;
- }
- };
+ cachedRows.sort( this.comparator );
- if (count > 2) {
- heapify(count);
+ // iterate through cached rows, overwriting data in columns
+ i = cachedRows.length;
+ while ( i-- ) {
+ row = cachedRows[i];
- end = count - 1;
- while (end > 1) {
-
- swap(end, 0);
- end--;
- siftDown(0, end);
+ this._rowIdByPosition[i] = row[ this.idAttribute ];
+ this._rowPositionById[ row[ this.idAttribute ] ] = i;
+ j = this._columns.length;
+ while ( j-- ) {
+ var col = this._columns[j];
+ col.data[i] = row[ col.name ];
}
- } else {
- if (this.comparator(
- this.rowByPosition(0),
- this.rowByPosition(1)) > 0) {
- swap(0,1);
- }
- }
-
- // check last two rows, they seem to always be off sync.
- if (this.comparator(
- this.rowByPosition(this.length - 2),
- this.rowByPosition(this.length - 1)) > 0) {
- swap(this.length - 1,this.length - 2);
}
- if (this.syncable && options.silent) {
+ if (this.syncable && !options.silent) {
this.trigger("sort");
}
+
return this;
},
-
+
/**
* Exports a version of the dataset in json format.
* Returns:
Oops, something went wrong.

0 comments on commit e0f2f61

Please sign in to comment.