Skip to content

Instantly share code, notes, and snippets.

@alecgorge
Created October 11, 2010 01:43
Show Gist options
  • Save alecgorge/619827 to your computer and use it in GitHub Desktop.
Save alecgorge/619827 to your computer and use it in GitHub Desktop.
function range ( low, high, step ) {
// http://kevin.vanzonneveld.net
// + original by: Waldo Malqui Silva
// * example 1: range ( 0, 12 );
// * returns 1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
// * example 2: range( 0, 100, 10 );
// * returns 2: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
// * example 3: range( 'a', 'i' );
// * returns 3: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
// * example 4: range( 'c', 'a' );
// * returns 4: ['c', 'b', 'a']
var matrix = [];
var inival, endval, plus;
var walker = step || 1;
var chars = false;
if ( !isNaN( low ) && !isNaN( high ) ) {
inival = low;
endval = high;
} else if ( isNaN( low ) && isNaN( high ) ) {
chars = true;
inival = low.charCodeAt( 0 );
endval = high.charCodeAt( 0 );
} else {
inival = ( isNaN( low ) ? 0 : low );
endval = ( isNaN( high ) ? 0 : high );
}
plus = ( ( inival > endval ) ? false : true );
if ( plus ) {
while ( inival <= endval ) {
matrix.push( ( ( chars ) ? String.fromCharCode( inival ) : inival ) );
inival += walker;
}
} else {
while ( inival >= endval ) {
matrix.push( ( ( chars ) ? String.fromCharCode( inival ) : inival ) );
inival -= walker;
}
}
return matrix;
}
/**
* Concatenates the values of a variable into an easily readable string
* by Matt Hackett [scriptnode.com]
* @param {Object} x The variable to debug
* @param {Number} max The maximum number of recursions allowed (keep low, around 5 for HTML elements to prevent errors) [default: 10]
* @param {String} sep The separator to use between [default: a single space ' ']
* @param {Number} l The current level deep (amount of recursion). Do not use this parameter: it's for the function's own use
*/
function print_r(x, max, sep, l) {
l = l || 0;
max = max || 10;
sep = sep || ' ';
if (l > max) {
return "[WARNING: Too much recursion]\n";
}
var
i,
r = '',
t = typeof x,
tab = '';
if (x === null) {
r += "(null)\n";
} else if (t == 'object') {
l++;
for (i = 0; i < l; i++) {
tab += sep;
}
if (x && x.length) {
t = 'array';
}
r += '(' + t + ") :\n";
for (i in x) {
try {
r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
} catch(e) {
return "[ERROR: " + e + "]\n";
}
}
} else {
if (t == 'string') {
if (x == '') {
x = '(empty)';
}
}
r += '(' + t + ') ' + x + "\n";
}
return r;
};
var_dump = print_r;
/*
* Recursively merge properties of two objects
*/
function merge(obj1, obj2) {
for (var p in obj2) {
try {
// Property in destination object set; update its value.
if ( obj2[p].constructor==Object ) {
obj1[p] = merge(obj1[p], obj2[p]);
} else {
obj1[p] = obj2[p];
}
} catch(e) {
// Property in destination object not set; create it and set its value.
obj1[p] = obj2[p];
}
}
return obj1;
}
var lines = [];
/*
..*.....**
.**..*****
.*...*....
..****.***
..****.***
*/
lines.push("..*.....**");
lines.push(".**..*****");
lines.push(".*...*....");
lines.push("..****.***");
lines.push("..****.***");
var matches = [];
var starts = [];
// find start stop positions of stars
lines.forEach(function (v) {
var last = '';
var thisStarts = [];
var thisEnds = [];
var startGroup = [];
v.split('').forEach(function (c,i) {
if(last != "*" && c == "*") thisStarts.push(i);
if(last == "*" && c != "*") thisEnds.push(i);
last = c;
});
thisStarts.forEach(function (startPoint,index) {
if(typeof(thisEnds[index]) == 'undefined') thisEnds[index] = 10;
startGroup.push([startPoint, thisEnds[index]]);
});
starts.push(startGroup);
matches.push(v.match(/([*]+)/));
});
var expandedRanges = [];
// expand ranges
starts.forEach(function (v) {
var thisRanges = [];
v.forEach(function (x) {
if(x[1] - x[0] == 1) thisRanges.push([x[0]]);
else thisRanges.push(range(x[0], x[1]-1));
});
expandedRanges.push(thisRanges);
});
function connections(one, two) {
var retVals = [];
one.forEach(function (v) {
v.forEach(function (x) {
two.forEach(function (y) {
if(y.indexOf(x) > -1) {
retVals.push(x);
}
});
});
});
return retVals;
}
function connecting_only(rawRowArray, filter) {
var valid = [];
rawRowArray.forEach(function (v) {
var added = false;
filter.forEach(function (y) {
if(added) return;
if(v.indexOf(y) > -1) {
valid.push(v);
added = true;
}
});
});
return valid;
}
keys = [];
function count_downstream (full, starting_row) {
i = starting_row;
starting_row = full[i];
row = starting_row;
next = full[i+1];
if(typeof(full[i-1]) !== 'undefined')
prev = row;
else
prev = full[i-1];
var thisCount = [];
starting_row.forEach(function (v) {
k = v.join(",")+"";
keys.push(k);
thisCount[k] = v.length;
});
if(typeof(next) != 'undefined') {
var result = count_downstream(full,i+1);
for (var p in result) {
if(typeof(thisCount[p]) == 'undefined') thisCount[p] = 0;
thisCount[p] += parseInt(result[p]);
}
}
return thisCount;
}
/*
..*.....**
.**..*****
.*...*....
..****.***
..****.***
*/
// print(var_dump(connecting_only(expandedRanges[2], connections(expandedRanges[2],expandedRanges[3]))));
// print(var_dump(keys));
// print('FINAL', var_dump(count_downstream(expandedRanges, 0)));
print('FINAL', var_dump(create_stream(expandedRanges, [8,9])));
//print(var_dump(expandedRanges, 20, " "));
// debug output
expandedRanges.forEach(function (v) {
return;
v.forEach(function (x) {
print(x);
});
print("---");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment