UNPKG

xtorcga

Version:

Xtor Compute Geometry Algorithm Libary 计算几何算法库

171 lines (170 loc) 5.71 kB
"use strict"; var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ArrayList = void 0; var common_1 = require("../../alg/common"); // TMaterial extends Material | Material[] = Material | Material[], var ArrayList = /** @class */ (function () { function ArrayList(data) { var _a, _b; this.isArrayList = true; this._array = new Array(); if (Array.isArray(data)) (_a = this._array).push.apply(_a, data); else if (data.isArrayList === true) (_b = this._array).push.apply(_b, data === null || data === void 0 ? void 0 : data._array); } Object.defineProperty(ArrayList.prototype, "array", { get: function () { return this._array; }, set: function (val) { this._array = val; }, enumerable: false, configurable: true }); Object.defineProperty(ArrayList.prototype, "length", { get: function () { return this._array.length; }, enumerable: false, configurable: true }); Object.defineProperty(ArrayList.prototype, "last", { get: function () { return this.get(-1); }, enumerable: false, configurable: true }); Object.defineProperty(ArrayList.prototype, "first", { get: function () { return this._array[0]; }, enumerable: false, configurable: true }); ArrayList.prototype.map = function (callbackfn) { return this._array.map(callbackfn); }; ArrayList.prototype.push = function () { var _a; var values = []; for (var _i = 0; _i < arguments.length; _i++) { values[_i] = arguments[_i]; } (_a = this._array).push.apply(_a, values); }; ArrayList.prototype.reverse = function () { this._array.reverse(); return this; }; ArrayList.prototype.pop = function () { return Array.prototype.pop.apply(this._array); }; ArrayList.prototype.unshift = function () { var _a; var items = []; for (var _i = 0; _i < arguments.length; _i++) { items[_i] = arguments[_i]; } return (_a = this._array).unshift.apply(_a, items); }; ArrayList.prototype.insertAt = function (i) { var _a; var value = []; for (var _i = 1; _i < arguments.length; _i++) { value[_i - 1] = arguments[_i]; } (_a = this._array).splice.apply(_a, __spreadArrays([i, 0], value)); }; ArrayList.prototype.splice = function (start, deleteCount) { var _a; var items = []; for (var _i = 2; _i < arguments.length; _i++) { items[_i - 2] = arguments[_i]; } (_a = this._array).splice.apply(_a, __spreadArrays([start, deleteCount], items)); }; ArrayList.prototype.get = function (index) { if (index < 0) index = this._array.length + index; return this._array[index]; }; /** * 遍历 * @param {*} method */ ArrayList.prototype.forall = function (method) { for (var i = 0; i < this._array.length; i++) { method(this._array[i]); } }; /** * 克隆 */ ArrayList.prototype.clone = function () { return new this.constructor(common_1.clone(this._array)); }; /** * 分类 * example: * var arry = [1,2,3,4,5,6] * var result = classify(this._array,(a)={return a%2===0}) * * @param {Function} classifyMethod 分类方法 */ ArrayList.prototype.classify = function (classifyMethod) { var result = []; for (var i = 0; i < this._array.length; i++) { for (var j = 0; j < result.length; j++) { if (classifyMethod(this._array[i], result[j][0], result[j])) { result[j].push(this._array[i]); } else { result.push([this._array[i]]); } } } return result; }; /** * 去掉重复元素 * @param {Function} uniqueMethod 去重复 * @param {Function} sortMethod 排序 */ ArrayList.prototype.unique = function (uniqueMethod, sortMethod) { if (sortMethod) { this._array.sort(sortMethod); for (var i = 0; i < this._array.length; i++) { for (var j = i + 1; j < this._array.length; j++) { if (uniqueMethod(this._array[i], this._array[j]) === true) { this._array.splice(j, 1); j--; } else break; } } return this; } for (var i = 0; i < this._array.length; i++) { for (var j = i + 1; j < this._array.length; j++) { if (uniqueMethod(this._array[i], this._array[j]) === true) { this._array.splice(j, 1); j--; } } } return this; }; return ArrayList; }()); exports.ArrayList = ArrayList;