Skip to content

Commit

Permalink
#20 implemented forward pick optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuque committed Jan 31, 2014
1 parent 715a4de commit bc5498b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
17 changes: 15 additions & 2 deletions inner/js/data/moving-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ if (!window.mobmap) window.mobmap={};
function TimeList(objId) {
this.id = objId;
this.recordList = [];

this.cahcedIndex = -1;
this.cahcedTime = -1;
}

TimeList.prototype = {
Expand All @@ -237,9 +240,15 @@ if (!window.mobmap) window.mobmap={};
pickAt: function(pickPool, pickedRec, seconds, extraProps, pickIndex) {
var ls = this.recordList;
var len = ls.length;

var startIndex = 0;

// Forward pick optimization
if (this.cahcedIndex >= 0 && this.cahcedTime < seconds) {
startIndex = this.cahcedIndex;
}

var i, previ = -1;
for (i = 0;i < len;++i) {
for (i = startIndex;i < len;++i) {
var rec = ls[i];
if (rec._time > seconds) {
break;
Expand All @@ -249,6 +258,10 @@ if (!window.mobmap) window.mobmap={};
if (len > 0) {
if (i > 0) {
previ = i - 1;

// Update fwd-pick cache
this.cahcedIndex = previ;
this.cahcedTime = ls[previ]._time;
}

// Set output record object if null
Expand Down
16 changes: 12 additions & 4 deletions js/moving-data-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,17 @@ if (!window.mobmap) window.mobmap={};
pickAt: function(pickPool, pickedRec, seconds, extraProps, pickIndex) {
var ls = this.recordList;
var len = ls.length;
var prevTime = -1;
var startIndex = 0;

// Forward pick optimization
if (this.cahcedIndex >= 0 && this.cahcedTime < seconds) {
startIndex = this.cahcedIndex;
}

var i, previ = -1;
for (i = 0;i < len;++i) {
for (i = startIndex;i < len;++i) {
var rec = ls[i];
prevTime = rec._time;
if (prevTime > seconds) {
if (rec._time > seconds) {
break;
}
}
Expand All @@ -190,6 +194,10 @@ if (!window.mobmap) window.mobmap={};
if (len > 0) {
if (i > 0) {
previ = i - 1;

// Update fwd-pick cache
this.cahcedIndex = previ;
this.cahcedTime = ls[previ]._time;
}

// Set output record object if null
Expand Down
4 changes: 2 additions & 2 deletions js/picker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
var mdat = testMovingData;

var tStart = new Date();
for (var i = 0;i < 10;++i) {
var pickTime = dataMinSec + 3600 * i + 1800;
for (var i = 0;i < 19;++i) {
var pickTime = dataMinSec + 1800 * i + 1800;
//var pickTime = dataMinSec + 6;

mdPickPool.clear();
Expand Down

0 comments on commit bc5498b

Please sign in to comment.