UNPKG

@xtor/cga.js

Version:

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

113 lines (103 loc) 2.79 kB
export class ArrayEx extends Array { constructor(...args) { super(...args); } get lastElement() { return this.get(-1); } get(index) { if (index < 0) index = this.length + index; return this[index] } /** * 深度优先遍历 * @param {*} method */ forall(method) { for (let i = 0; i < this.length; i++) { method(this[i]); if (this[i] instanceof Array) this[i].forall(method); } } /** clone() { var result = new ArrayEx() for (let i = 0; i < this.length; i++) { var ele = this[i]; if (ele instanceof Number || ele instanceof String) result[i] = ele; else if (ele.clone) { result[i] = ele.clone(); } else throw ("数组有元素不能clone") } return result; } /** * 分类 * example: * var arry = [1,2,3,4,5,6] * var result = classify(this,(a)={return a%2===0}) * * @param {Function} classifyMethod 分类方法 */ classify(classifyMethod) { var result = []; for (let i = 0; i < this.length; i++) { for (let j = 0; j < result.length; j++) { if (classifyMethod(this[i], result[j][0], result[j])) { result[j].push(this[i]); } else { result.push([this[i]]); } } } return result; } /** * 去掉重复元素 * @param {Function} uniqueMethod 去重复 * @param {Function} sortMethod 排序 */ unique(uniqueMethod, sortMethod) { if (sortMethod) { this.sort(sortMethod); for (let i = 0; i < this.length; i++) { for (let j = i + 1; j < this.length; j++) { if (uniqueMethod(this[i], this[j]) === true) { this.splice(j, 1); j-- } else break; } } return this; } for (let i = 0; i < this.length; i++) { for (let j = i + 1; j < this.length; j++) { if (uniqueMethod(this[i], this[j]) === true) { this.splice(j, 1); j-- } } } return this; } }