30,369 → 30,394 |
var jsx = {}; |
} |
|
/* for JSDT only */ |
if (typeof jsx.python == "undefined") |
{ |
/** @namespace */ |
jsx.python = {}; |
} |
|
/** |
* @type jsx.python |
* @memberOf __jsx.python |
* @namespace |
*/ |
jsx.python = { |
/** |
* @version |
*/ |
version: "$Revision$ ($Date$)", |
copyright: "Copyright \xA9 2011-2013", |
author: "Thomas Lahn", |
email: "js@PointedEars.de", |
path: "http://PointedEars.de/scripts/" |
}; |
jsx.python = (/** @constructor */ function () { |
var _jsx_object = jsx.object; |
var _getKeys = _jsx_object.getKeys; |
var _isArray = _jsx_object.isArray; |
var _isObject = _jsx_object.isObject; |
|
/** |
* Returns a reference to an <code>Array</code> instance whose |
* items are the numbers from <var>start</var> inclusive to |
* <var>end</var> exclusive using step width <var>step</var> |
* (may be negative). |
* |
* @param {number} start |
* @param {number} end |
* @param {number} step = 1 |
* @return {Array} |
*/ |
jsx.python.range = function (start, end, step) { |
var result = []; |
return { |
/** |
* @version |
* @memberOf jsx.python |
*/ |
version: "$Revision$ ($Date$)", |
copyright: "Copyright \xA9 2011-2013", |
author: "Thomas Lahn", |
email: "js@PointedEars.de", |
path: "http://PointedEars.de/scripts/", |
|
if (!step) |
{ |
if (step === 0) |
{ |
jsx.throwThis( |
"jsx.InvalidArgumentError", "step argument must not be zero"); |
} |
/** |
* Returns a reference to an <code>Array</code> whose |
* items are the numbers from <var>start</var> inclusive to |
* <var>end</var> exclusive using step width <var>step</var> |
* (may be negative). |
* |
* @param {number} start |
* @param {number} end |
* @param {number} step = 1 |
* @return {Array} |
*/ |
range: function (start, end, step) { |
var result = []; |
|
step = 1; |
} |
if (!step) |
{ |
if (step === 0) |
{ |
jsx.throwThis( |
"jsx.InvalidArgumentError", "step argument must not be zero"); |
} |
|
end = +end; |
step = +step; |
step = 1; |
} |
|
for (var i = +start; (step > 0) ? (i < end) : (i > end); i += step) |
{ |
result.push(i); |
} |
end = +end; |
step = +step; |
|
return result; |
}; |
for (var i = +start; (step > 0) ? (i < end) : (i > end); i += step) |
{ |
result.push(i); |
} |
|
/** |
* Returns a reference to an Array instance containing |
* the names of the enumerable properties of another object; |
* returns a reference to a passed Array. |
* |
* @function |
*/ |
jsx.python.list = (function () { |
var _jsx_object = jsx.object; |
var _isArray = _jsx_object.isArray; |
var _hasOwnProperty = _jsx_object._hasOwnProperty; |
return result; |
}, |
|
/** |
* @param {Object} iterable |
* @return {Array} |
*/ |
return function (iterable) { |
var result = []; |
/** |
* Returns a reference to an Array instance containing |
* the names of the enumerable properties of another object; |
* returns a reference to a passed Array. |
* |
* @function |
*/ |
list: jsx.object.extend(( |
/** |
* @factory |
*/ |
function () { |
var _hasOwnProperty = _jsx_object._hasOwnProperty; |
|
if (_isArray(iterable)) |
{ |
return iterable; |
} |
/** |
* @param {Object} iterable |
* @return {Array} |
*/ |
return function (iterable) { |
var result = []; |
|
for (var property in iterable) |
{ |
if (_hasOwnProperty(iterable, property)) |
{ |
result.push(property); |
} |
} |
if (_isArray(iterable)) |
{ |
return iterable; |
} |
|
return result; |
}; |
}()); |
for (var property in iterable) |
{ |
if (_hasOwnProperty(iterable, property)) |
{ |
result.push(property); |
} |
} |
|
/** |
* List comprehension. |
* |
* Returns a reference to an <code>Array</code> |
* instance containing the result of passing each item |
* of <var>list</var> to <var>mapper</var> (or <var>mapper</var> |
* itself if <var>mapper</var> is not a function) for which |
* <var>condition</var> returns a true-value (or all items |
* if <var>condition</var> is a <code>true-value</code> or |
* <code>undefined</code>, and no items if it is another |
* false-value). |
*/ |
jsx.python.list.from = (function () { |
var _jsx_object = jsx.object; |
var _getKeys = _jsx_object.getKeys; |
var _isArray = _jsx_object.isArray; |
var _isObject = _jsx_object.isObject; |
var _range = jsx.python.range; |
return result; |
}; |
}()), { |
/** |
* List comprehension. |
* |
* Returns a reference to an <code>Array</code> |
* instance containing the result of passing each item |
* of <var>list</var> to <var>mapper</var> (or <var>mapper</var> |
* itself if <var>mapper</var> is not a function) for which |
* <var>condition</var> returns a true-value (or all items |
* if <var>condition</var> is a <code>true-value</code> or |
* <code>undefined</code>, and no items if it is another |
* false-value). |
* |
* @memberOf jsx.python.list |
* @param {Function|any} mapper |
* @param {Object} iterable |
* @param {Function|boolean} condition |
* @return {Array} |
*/ |
from: function (mapper, iterable, condition) { |
var result = []; |
|
/** |
* @param {Function|any} mapper |
* @param {Object} iterable |
* @param {Function|boolean} condition |
* @return {Array} |
*/ |
return function (mapper, iterable, condition) { |
var result = []; |
var iterableIsArray = _isArray(iterable); |
var _range = jsx.python.range; |
if (iterableIsArray) |
{ |
var len = iterable.length; |
var keys = _range(0, len); |
} |
else |
{ |
keys = _isObject(iterable) && _getKeys(iterable); |
len = keys.length; |
} |
|
var iterableIsArray = _isArray(iterable); |
if (iterableIsArray) |
{ |
var len = iterable.length; |
var keys = _range(0, len); |
} |
else |
{ |
keys = _isObject(iterable) && _getKeys(iterable); |
len = keys.length; |
} |
var hasCondition = (typeof condition == "function"); |
for (var i = 0; i < len; ++i) |
{ |
if (iterableIsArray && !(i in iterable)) |
{ |
continue; |
} |
|
var hasCondition = (typeof condition == "function"); |
for (var i = 0; i < len; ++i) |
{ |
if (iterableIsArray && !(i in iterable)) |
{ |
continue; |
var key = keys[i]; |
var item = iterable[key]; |
|
if (hasCondition) |
{ |
var conditionMet = condition(item, key, iterable); |
} |
else |
{ |
conditionMet = condition; |
} |
|
if (!hasCondition || conditionMet) |
{ |
if (typeof mapper == "function") |
{ |
var value = mapper(item, key, iterable); |
} |
else |
{ |
value = mapper; |
} |
|
result.push(value); |
} |
} |
|
return result; |
} |
} |
), |
|
var key = keys[i]; |
var item = iterable[key]; |
/** |
* Returns a reference to an object |
* |
* @param mapping |
* @return {Object} |
*/ |
dict: function (mapping, values) { |
var result = {}; |
|
if (hasCondition) |
if (typeof mapping == "undefined") |
{ |
var conditionMet = condition(item, key, iterable); |
return result; |
} |
else |
{ |
conditionMet = condition; |
} |
|
if (!hasCondition || conditionMet) |
if (mapping.length) |
{ |
if (typeof mapper == "function") |
if (arguments.length > 1) |
{ |
var value = mapper(item, key, iterable); |
for (var index in mapping) |
{ |
result[mapping[index]] = values[index]; |
} |
|
return result; |
} |
else |
|
var props = mapping[0]; |
var vals = mapping[1]; |
for (var index2 in props) |
{ |
value = mapper; |
result[props[index2]] = vals[index2]; |
} |
|
result.push(value); |
} |
} |
|
return result; |
}; |
}()); |
|
/** |
* Returns a reference to an object |
* |
* @param mapping |
* @return {Object} |
*/ |
jsx.python.dict = function (mapping, values) { |
var result = {}; |
|
if (typeof mapping == "undefined") |
{ |
return result; |
} |
|
if (mapping.length) |
{ |
if (arguments.length > 1) |
{ |
for (var index in mapping) |
else |
{ |
result[mapping[index]] = values[index]; |
for (var name in mapping) |
{ |
result[name] = mapping[name]; |
} |
} |
|
return result; |
} |
}, |
|
var props = mapping[0]; |
var vals = mapping[1]; |
for (var index2 in props) |
{ |
result[props[index2]] = vals[index2]; |
} |
} |
else |
{ |
for (var name in mapping) |
{ |
result[name] = mapping[name]; |
} |
} |
/** |
* Build an unordered collection of unique elements. |
* |
* @type jsx.python.set |
* @function |
*/ |
set: ( |
/** |
* @constructor |
* @factory |
*/ |
function () { |
var _jsx = jsx; |
var _jsx_object = _jsx.object; |
var _Map; |
var _getKeys = _jsx_object.getKeys; |
var _isMethod = _jsx_object.isMethod; |
|
return result; |
}; |
/** |
* @param {Object} iterable |
* @return {Array} |
*/ |
return function jsx_python_set (iterable) { |
if (!(this instanceof jsx_python_set)) |
{ |
return new jsx_python_set(iterable); |
} |
|
/** |
* Build an unordered collection of unique elements. |
*/ |
jsx.python.set = (function () { |
var _jsx = jsx; |
var _jsx_object = _jsx.object; |
var _Map; |
var _getKeys = _jsx_object.getKeys; |
var _isMethod = _jsx_object.isMethod; |
if (_isMethod(iterable, "slice")) |
{ |
var result = iterable.slice(); |
} |
else |
{ |
result = []; |
|
/** |
* @param {Object} iterable |
* @return {Array} |
*/ |
return function jsx_python_set (iterable) { |
if (!(this instanceof jsx_python_set)) |
{ |
return new jsx_python_set(iterable); |
} |
for (var i = 0, keys = _getKeys(iterable), len = keys.length; |
i < len; |
++i) |
{ |
result.push(iterable[keys[i]]); |
} |
} |
|
if (_isMethod(iterable, "slice")) |
{ |
var result = iterable.slice(); |
} |
else |
{ |
result = []; |
/* Prefer more efficient (best case: O(n)) Map approach if possible */ |
if (_Map || typeof _jsx.map != "undefined") |
{ |
if (!_Map) |
{ |
_Map = _jsx.map.Map; |
} |
|
for (var i = 0, keys = _getKeys(iterable), len = keys.length; |
i < len; |
++i) |
{ |
result.push(iterable[keys[i]]); |
} |
} |
var map = new _Map(); |
for (i = 0, len = result.length; i < len; ++i) |
{ |
map.put(result[i], true); |
} |
|
/* Prefer more efficient (best case: O(n)) Map approach if possible */ |
if (_Map || typeof _jsx.map != "undefined") |
{ |
if (!_Map) |
{ |
_Map = _jsx.map.Map; |
} |
result = map.keys(); |
} |
else |
{ |
/* comparisons(n) = (n^2 - n)/2 ~ O(n^2) */ |
for (var i = 0, len = result.length; i < len; ++i) |
{ |
for (var j = i + 1; j < len; ++j) |
{ |
if (result[i] === result[j]) |
{ |
result.splice(j, 1); |
--j; |
--len; |
} |
} |
} |
} |
|
var map = new _Map(); |
for (i = 0, len = result.length; i < len; ++i) |
{ |
map.put(result[i], true); |
} |
this._elements = result; |
this.length = this._elements.length; |
}; |
}() |
).extend(null, { |
/** |
* @memberOf jsx.python.set.prototype |
*/ |
intersection: function (other) { |
if (!(other instanceof jsx.python.set)) |
{ |
other = new jsx.python.set(other); |
} |
|
result = map.keys(); |
} |
else |
{ |
/* comparisons(n) = (n^2 - n)/2 ~ O(n^2) */ |
for (var i = 0, len = result.length; i < len; ++i) |
{ |
for (var j = i + 1; j < len; ++j) |
var elements = this._elements; |
elements.sort(); |
other.sort(); |
|
var intersection = []; |
|
/* FIXME: Identical elements need not sort to the same position */ |
for (var i = 0, len = elements.length; i < len; ++i) |
{ |
if (result[i] === result[j]) |
var element = elements[i]; |
if (element === other[i]) |
{ |
result.splice(j, 1); |
--j; |
--len; |
intersection.push(element); |
} |
} |
} |
} |
|
this._elements = result; |
this.length = this._elements.length; |
}; |
}()); |
return new jsx.python.set(intersection); |
}, |
|
jsx.python.set.extend(null, { |
intersection: function (other) { |
if (!(other instanceof jsx.python.set)) |
{ |
other = new jsx.python.set(other); |
} |
isDisjoint: function (other) { |
return (this.intersection(other).length === 0); |
}, |
|
var elements = this._elements; |
elements.sort(); |
other.sort(); |
toArray: function () { |
return this._elements; |
} |
}), |
|
var intersection = []; |
/** |
* Return an {@link Array} of <code>Array</code>s, where each |
* inner array contains the i-th element from each of the |
* argument arrays. |
* The returned <code>Array</code> is truncated in length |
* to the length of the shortest argument array. |
* |
* @params {Array} |
* @return {Array} |
*/ |
zip: function () { |
var result = []; |
|
/* FIXME: Identical elements need not sort to the same position */ |
for (var i = 0, len = elements.length; i < len; ++i) |
{ |
var element = elements[i]; |
if (element === other[i]) |
for (var i = 0, len = arguments[0].length; i < len; ++i) |
{ |
intersection.push(element); |
} |
} |
result[i] = []; |
|
return new jsx.python.set(intersection); |
}, |
for (var j = 0, len2 = arguments.length; j < len2; ++j) |
{ |
if (arguments[j].length - 1 < i) |
{ |
delete result[i]; |
break; |
} |
|
isdisjoint: function (other) { |
return (this.intersection(other).length === 0); |
}, |
result[i][j] = arguments[j][i]; |
} |
} |
|
toArray: function () { |
return this._elements; |
} |
}); |
return result; |
}, |
|
/** |
* Return an Array of Arrays, where each inner Array contains the i-th |
* element from each of the argument Arrays. The returned Array is |
* truncated in length to the length of the shortest argument Array. |
* |
* @param {Array} arg1 |
* @return {Array} |
*/ |
jsx.python.zip = function (arg1, arg2) { |
var result = []; |
|
for (var i = 0, len = arguments[0].length; i < len; ++i) |
{ |
result[i] = []; |
|
for (var j = 0, len2 = arguments.length; j < len2; ++j) |
{ |
if (arguments[j].length - 1 < i) |
/** |
* Extends an Array with elements from another Array. |
* |
* Different from Array.prototype.concat() in that the first Array is modified. |
* To emphasize this, there is no explicit return value (i.e. returns |
* <code>undefined</code>). |
* |
* @param {Array} list1 |
* Array which is to be extended |
* @param {Array} list2 |
* Array which elements should be appended to <var>list1</var> |
*/ |
extend: function (list1, list2) { |
for (var i = 0, len = list2.length; i < len; ++i) |
{ |
delete result[i]; |
break; |
Array.prototype.push.call(list1, list2[i]); |
} |
|
result[i][j] = arguments[j][i]; |
} |
} |
|
return result; |
}; |
|
/** |
* Extends an Array with elements from another Array. |
* |
* Different from Array.prototype.concat() in that the first Array is modified. |
* To emphasize this, there is no explicit return value (i.e. returns |
* <code>undefined</code>). |
* |
* @param {Array} list1 |
* Array which is to be extended |
* @param {Array} list2 |
* Array which elements should be appended to <var>list1</var> |
*/ |
jsx.python.extend = function (list1, list2) { |
for (var i = 0, len = list2.length; i < len; ++i) |
{ |
Array.prototype.push.call(list1, list2[i]); |
} |
}; |
}; |
}()); |