123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- "use strict";
- function Class(){}
- Class.extend = function extend(methods){
- function Type(){
- for(var m in methods)
- this[m] = methods[m];
- }
- Type.prototype = this.prototype;
- var result = methods.init;
- result.prototype = new Type(); // inherit this.prototype
- result.prototype.constructor = result; // to see constructor name in diagnostic
-
- result.extend = extend;
- return result;
- };
- var impl = {
- extend: Class.extend,
- typeGuard: function(from, to){
- if (!(from instanceof to))
- throw new Error("typeguard assertion failed");
- return from;
- },
- makeArray: function(/*dimensions, initializer*/){
- var forward = Array.prototype.slice.call(arguments);
- var result = new Array(forward.shift());
- var i;
- if (forward.length == 1){
- var init = forward[0];
- if (typeof init == "function")
- for(i = 0; i < result.length; ++i)
- result[i] = init();
- else
- for(i = 0; i < result.length; ++i)
- result[i] = init;
- }
- else
- for(i = 0; i < result.length; ++i)
- result[i] = this.makeArray.apply(this, forward);
- return result;
- },
- makeSet: function(/*...*/){
- var result = 0;
-
- function checkBit(b){
- if (b < 0 || b > 31)
- throw new Error("integers between 0 and 31 expected, got " + b);
- }
- function setBit(b){
- checkBit(b);
- result |= 1 << b;
- }
-
- for(var i = 0; i < arguments.length; ++i){
- var b = arguments[i];
- if (b instanceof Array){
- var from = b[0];
- var to = b[1];
- if (from < to)
- throw new Error("invalid SET diapason: " + from + ".." + to);
- for(var bi = from; bi <= to; ++bi)
- setBit(bi);
- }
- else
- setBit(b);
- }
- return result;
- },
- makeRef: function(obj, prop){
- return {set: function(v){ obj[prop] = v; },
- get: function(){ return obj[prop]; }};
- },
- setInclL: function(l, r){return (l & r) == l;},
- setInclR: function(l, r){return (l & r) == r;},
- assignArrayFromString: function(a, s){
- var i;
- for(i = 0; i < s.length; ++i)
- a[i] = s.charCodeAt(i);
- for(i = s.length; i < a.length; ++i)
- a[i] = 0;
- },
- strToArray: function(s){
- var result = new Array(s.length);
- for(var i = 0; i < s.length; ++i)
- result[i] = s.charCodeAt(i);
- return result;
- },
- strCmp: function(s1, s2){
- var cmp = 0;
- var i = 0;
- while (!cmp && i < s1.length && i < s2.length){
- cmp = s1[i] - s2[i];
- ++i;
- }
- return cmp ? cmp : s1.length - s2.length;
- },
- copy: function(from, to){
- for(var prop in to){
- if (to.hasOwnProperty(prop)){
- var v = from[prop];
- if (v !== null && typeof v == "object")
- this.copy(v, to[prop]);
- else
- to[prop] = v;
- }
- }
- },
- assert: function(condition, code){
- if (!condition)
- throw new Error("assertion failed"
- + ((code !== undefined) ? " with code " + code : ""));
- }
- };
- exports.Class = Class;
- for(var e in impl)
- exports[e] = impl[e];
|