2018-04-11 13:25:24 +02:00
|
|
|
function Lib(ws) {
|
2018-04-12 23:01:40 +02:00
|
|
|
return {
|
|
|
|
clearElement: clearElement,
|
2018-04-13 10:39:50 +02:00
|
|
|
defaultCompare: defaultCompare,
|
|
|
|
funMap: funMap,
|
2018-04-12 23:01:40 +02:00
|
|
|
insert: insert,
|
|
|
|
send: send
|
|
|
|
};
|
2018-04-11 13:25:24 +02:00
|
|
|
|
|
|
|
function clearElement(elem) {
|
|
|
|
while(elem.firstChild) {
|
|
|
|
elem.removeChild(elem.firstChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 10:39:50 +02:00
|
|
|
function insert(obj, t, compare, min, max) {
|
2018-04-12 23:01:40 +02:00
|
|
|
min = min == undefined ? 0 : min;
|
|
|
|
max = max == undefined ? t.length : max;
|
2018-04-13 10:39:50 +02:00
|
|
|
compare = compare == undefined ? defaultCompare : compare;
|
2018-04-12 23:01:40 +02:00
|
|
|
if(max - min < 1) {
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
var avg = Math.floor((max + min) / 2);
|
2018-04-13 10:39:50 +02:00
|
|
|
if (compare(obj, t[avg]) < 0) {
|
|
|
|
return insert(obj, t, compare, min, avg);
|
|
|
|
} else {
|
|
|
|
return insert(obj, t, compare, avg+1, max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function defaultCompare(a, b) {
|
|
|
|
if(a < b) {
|
|
|
|
return -1;
|
|
|
|
} else if(a > b) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function funMap(projector, f) {
|
|
|
|
return function() {
|
|
|
|
var args = Array.prototype.map.call(arguments, projector);
|
|
|
|
return f.apply(null, args);
|
|
|
|
}
|
2018-04-12 23:01:40 +02:00
|
|
|
}
|
2018-04-11 13:25:24 +02:00
|
|
|
|
|
|
|
function send(o) {
|
|
|
|
ws.send(JSON.stringify(o));
|
|
|
|
}
|
|
|
|
}
|