随机类移动至es命名空间

This commit is contained in:
yhh
2021-03-29 17:45:36 +08:00
parent e6096b644f
commit 0b8d752773
6 changed files with 1013 additions and 997 deletions

View File

@@ -649,12 +649,12 @@ declare module es {
* 返回第一个启用加载的类型为T的组件 * 返回第一个启用加载的类型为T的组件
* @param type * @param type
*/ */
findComponentOfType<T extends Component>(type: any): T; findComponentOfType<T extends Component>(type: new (...args: any[]) => T): T;
/** /**
* 返回类型为T的所有已启用已加载组件的列表 * 返回类型为T的所有已启用已加载组件的列表
* @param type * @param type
*/ */
findComponentsOfType<T extends Component>(type: any): T[]; findComponentsOfType<T extends Component>(type: new (...args: any[]) => T): T[];
/** /**
* 返回场景中包含特定组件的实体列表 * 返回场景中包含特定组件的实体列表
* @param type * @param type
@@ -3974,119 +3974,121 @@ declare module es {
isContainedIn(a: Rectangle, b: Rectangle): boolean; isContainedIn(a: Rectangle, b: Rectangle): boolean;
} }
} }
declare class ArrayUtils { declare module es {
/** class ArrayUtils {
* 执行冒泡排序 /**
* @param ary * 执行冒泡排序
*/ * @param ary
static bubbleSort(ary: number[]): void; */
/** static bubbleSort(ary: number[]): void;
* 执行插入排序 /**
* @param ary * 执行插入排序
*/ * @param ary
static insertionSort(ary: number[]): void; */
/** static insertionSort(ary: number[]): void;
* 执行二分搜索 /**
* @param ary 搜索的数组(必须排序过) * 执行二分搜索
* @param value 需要搜索的值 * @param ary 搜索的数组(必须排序过)
* @returns 返回匹配结果的数组索引 * @param value 需要搜索的值
*/ * @returns 返回匹配结果的数组索引
static binarySearch(ary: number[], value: number): number; */
/** static binarySearch(ary: number[], value: number): number;
* 返回匹配项的索引 /**
* @param ary * 返回匹配项的索引
* @param num * @param ary
*/ * @param num
static findElementIndex(ary: any[], num: any): any; */
/** static findElementIndex(ary: any[], num: any): any;
* 返回数组中最大值的索引 /**
* @param ary * 返回数组中最大值的索引
*/ * @param ary
static getMaxElementIndex(ary: number[]): number; */
/** static getMaxElementIndex(ary: number[]): number;
* 返回数组中最小值的索引 /**
* @param ary * 返回数组中最小值的索引
*/ * @param ary
static getMinElementIndex(ary: number[]): number; */
/** static getMinElementIndex(ary: number[]): number;
* 返回一个"唯一性"数组 /**
* @param ary 需要唯一性数组 * 返回一个"唯一性"数组
* @returns 唯一性的数组 * @param ary 需要唯一性的数组
* * @returns 唯一性的数组
* @tutorial *
* 比如: [1, 2, 2, 3, 4] * @tutorial
* 返回: [1, 2, 3, 4] * 比如: [1, 2, 2, 3, 4]
*/ * 返回: [1, 2, 3, 4]
static getUniqueAry(ary: number[]): number[]; */
/** static getUniqueAry(ary: number[]): number[];
* 返回2个数组中不同的部分 /**
* 比如数组A = [1, 2, 3, 4, 6] * 返回2个数组中不同的部分
* 数组B = [0, 2, 1, 3, 4] * 比如数组A = [1, 2, 3, 4, 6]
* 返回[6, 0] * 数组B = [0, 2, 1, 3, 4]
* @param aryA * 返回[6, 0]
* @param aryB * @param aryA
* @return * @param aryB
*/ * @return
static getDifferAry(aryA: number[], aryB: number[]): number[]; */
/** static getDifferAry(aryA: number[], aryB: number[]): number[];
* 交换数组元素 /**
* @param array 目标数组 * 交换数组元素
* @param index1 交换后的索引 * @param array 目标数组
* @param index2 交换的索引 * @param index1 交换的索引
*/ * @param index2 交换前的索引
static swap(array: any[], index1: number, index2: number): void; */
/** static swap(array: any[], index1: number, index2: number): void;
* 清除列表 /**
* @param ary * 清除列表
*/ * @param ary
static clearList(ary: any[]): void; */
/** static clearList(ary: any[]): void;
* 克隆一个数组 /**
* @param ary 需要克隆的数组 * 克隆一个数组
* @return 克隆的数组 * @param ary 需要克隆的数组
*/ * @return 克隆的数组
static cloneList(ary: any[]): any[]; */
/** static cloneList(ary: any[]): any[];
* 判断2个数组是否相同 /**
* @param ary1 数组1 * 判断2个数组是否相同
* @param ary2 数组2 * @param ary1 数组1
*/ * @param ary2 数组2
static equals(ary1: number[], ary2: number[]): Boolean; */
/** static equals(ary1: number[], ary2: number[]): Boolean;
* 根据索引插入元素,索引和索引后的元素都向后移动一位 /**
* @param ary * 根据索引插入元素,索引和索引后的元素都向后移动一位
* @param index 插入索引 * @param ary
* @param value 插入的元素 * @param index 插入索引
* @returns 插入的元素 未插入则返回空 * @param value 插入的元素
*/ * @returns 插入的元素 未插入则返回空
static insert(ary: any[], index: number, value: any): any; */
/** static insert(ary: any[], index: number, value: any): any;
* 打乱数组 FisherYates shuffle /**
* @param list * 打乱数组 FisherYates shuffle
*/ * @param list
static shuffle<T>(list: T[]): void; */
/** static shuffle<T>(list: T[]): void;
* 如果项目已经在列表中返回false如果成功添加返回true /**
* @param list * 如果项目已经在列表中返回false如果成功添加返回true
* @param item * @param list
*/ * @param item
static addIfNotPresent<T>(list: T[], item: T): boolean; */
/** static addIfNotPresent<T>(list: T[], item: T): boolean;
* 返回列表中的最后一项。列表中至少应该有一个项目 /**
* @param list * 返回列表中的最后一项。列表中至少应该有一个项目
*/ * @param list
static lastItem<T>(list: T[]): T; */
/** static lastItem<T>(list: T[]): T;
* 从列表中随机获取一个项目。不清空检查列表! /**
* @param list * 从列表中随机获取一个项目。不清空检查列表!
*/ * @param list
static randomItem<T>(list: T[]): T; */
/** static randomItem<T>(list: T[]): T;
* 从列表中随机获取物品。不清空检查列表也不验证列表数是否大于项目数。返回的List可以通过ListPool.free放回池中 /**
* @param list * 从列表中随机获取物品。不清空检查列表也不验证列表数是否大于项目数。返回的List可以通过ListPool.free放回池中
* @param itemCount 从列表中返回的随机项目的数量 * @param list
*/ * @param itemCount 从列表中返回的随机项目的数量
static randomItems<T>(list: T[], itemCount: number): T[]; */
static randomItems<T>(list: T[], itemCount: number): T[];
}
} }
declare module es { declare module es {
class Base64Utils { class Base64Utils {
@@ -4146,74 +4148,76 @@ declare module es {
static toNumber(value: any): number; static toNumber(value: any): number;
} }
} }
declare class RandomUtils { declare module es {
/** class RandomUtils {
* 在 start 与 stop之间取一个随机整数可以用step指定间隔 但不包括较大的端点start与stop较大的一个 /**
* 如 * 在 start 与 stop之间取一个随机整数可以用step指定间隔 但不包括较大的端点start与stop较大的一个
* this.randrange(1, 10, 3) * 如
* 则返回的可能是 1 或 4 或 7 , 注意 这里面不会返回10因为是10是大端点 * this.randrange(1, 10, 3)
* * 则返回的可能是 1 或 4 或 7 , 注意 这里面不会返回10因为是10是大端点
* @param start *
* @param stop * @param start
* @param step * @param stop
* @return 假设 start < stop, [start, stop) 区间内的随机整数 * @param step
* * @return 假设 start < stop, [start, stop) 区间内的随机整数
*/ *
static randrange(start: number, stop: number, step?: number): number; */
/** static randrange(start: number, stop: number, step?: number): number;
* 返回a 到 b之间的随机整数包括 a 和 b /**
* @param a * 返回a 到 b之间的随机整数包括 a 和 b
* @param b * @param a
* @return [a, b] 之间的随机整数 * @param b
* * @return [a, b] 之间的随机整数
*/ *
static randint(a: number, b: number): number; */
/** static randint(a: number, b: number): number;
* 返回 a - b之间的随机数不包括 Math.max(a, b) /**
* @param a * 返回 a - b之间的随机数不包括 Math.max(a, b)
* @param b * @param a
* @return 假设 a < b, [a, b) * @param b
*/ * @return 假设 a < b, [a, b)
static randnum(a: number, b: number): number; */
/** static randnum(a: number, b: number): number;
* 打乱数组 /**
* @param array * 打乱数组
* @return * @param array
*/ * @return
static shuffle(array: any[]): any[]; */
/** static shuffle(array: any[]): any[];
* 从序列中随机取一个元素 /**
* @param sequence 可以是 数组、 vector等只要是有length属性并且可以用数字索引获取元素的对象 * 从序列中随机取一个元素
* 另外,字符串也是允许的。 * @param sequence 可以是 数组、 vector等只要是有length属性并且可以用数字索引获取元素的对象
* @return 序列中的某一个元素 * 另外,字符串也是允许的。
* * @return 序列中的某一个元素
*/ *
static choice(sequence: any): any; */
/** static choice(sequence: any): any;
* 对列表中的元素进行随机采æ ? /**
* <pre> * 对列表中的元素进行随机采æ ?
* this.sample([1, 2, 3, 4, 5], 3) // Choose 3 elements * <pre>
* [4, 1, 5] * this.sample([1, 2, 3, 4, 5], 3) // Choose 3 elements
* </pre> * [4, 1, 5]
* @param sequence * </pre>
* @param num * @param sequence
* @return * @param num
* * @return
*/ *
static sample(sequence: any[], num: number): any[]; */
/** static sample(sequence: any[], num: number): any[];
* 返回 0.0 - 1.0 之间的随机数,等同于 Math.random() /**
* @return Math.random() * 返回 0.0 - 1.0 之间的随机数,等同于 Math.random()
* * @return Math.random()
*/ *
static random(): number; */
/** static random(): number;
* 计算概率 /**
* @param chance 概率 * 计算概率
* @return * @param chance 概率
*/ * @return
static boolean(chance?: number): boolean; */
private static _randomCompare; static boolean(chance?: number): boolean;
private static _randomCompare;
}
} }
declare module es { declare module es {
class RectangleExt { class RectangleExt {

View File

@@ -10114,297 +10114,301 @@ var es;
}()); }());
es.MaxRectsBinPack = MaxRectsBinPack; es.MaxRectsBinPack = MaxRectsBinPack;
})(es || (es = {})); })(es || (es = {}));
var ArrayUtils = /** @class */ (function () { var es;
function ArrayUtils() { (function (es) {
} var ArrayUtils = /** @class */ (function () {
/** function ArrayUtils() {
* 执行冒泡排序 }
* @param ary /**
*/ * 执行冒泡排序
ArrayUtils.bubbleSort = function (ary) { * @param ary
var isExchange = false; */
for (var i = 0; i < ary.length; i++) { ArrayUtils.bubbleSort = function (ary) {
isExchange = false; var isExchange = false;
for (var j = ary.length - 1; j > i; j--) { for (var i = 0; i < ary.length; i++) {
if (ary[j] < ary[j - 1]) { isExchange = false;
var temp = ary[j]; for (var j = ary.length - 1; j > i; j--) {
ary[j] = ary[j - 1]; if (ary[j] < ary[j - 1]) {
ary[j - 1] = temp; var temp = ary[j];
isExchange = true; ary[j] = ary[j - 1];
ary[j - 1] = temp;
isExchange = true;
}
} }
if (!isExchange)
break;
} }
if (!isExchange) };
break; /**
} * 执行插入排序
}; * @param ary
/** */
* 执行插入排序 ArrayUtils.insertionSort = function (ary) {
* @param ary var len = ary.length;
*/ for (var i = 1; i < len; i++) {
ArrayUtils.insertionSort = function (ary) { var val = ary[i];
var len = ary.length; for (var j = i; j > 0 && ary[j - 1] > val; j--) {
for (var i = 1; i < len; i++) { ary[j] = ary[j - 1];
var val = ary[i]; }
for (var j = i; j > 0 && ary[j - 1] > val; j--) { ary[j] = val;
ary[j] = ary[j - 1];
} }
ary[j] = val; };
} /**
}; * 执行二分搜索
/** * @param ary 搜索的数组(必须排序过)
* 执行二分搜索 * @param value 需要搜索的值
* @param ary 搜索的数组(必须排序过) * @returns 返回匹配结果的数组索引
* @param value 需要搜索的值 */
* @returns 返回匹配结果的数组索引 ArrayUtils.binarySearch = function (ary, value) {
*/ var startIndex = 0;
ArrayUtils.binarySearch = function (ary, value) { var endIndex = ary.length;
var startIndex = 0; var sub = (startIndex + endIndex) >> 1;
var endIndex = ary.length; while (startIndex < endIndex) {
var sub = (startIndex + endIndex) >> 1; if (value <= ary[sub])
while (startIndex < endIndex) { endIndex = sub;
if (value <= ary[sub]) else if (value >= ary[sub])
endIndex = sub; startIndex = sub + 1;
else if (value >= ary[sub]) sub = (startIndex + endIndex) >> 1;
startIndex = sub + 1;
sub = (startIndex + endIndex) >> 1;
}
if (ary[startIndex] == value)
return startIndex;
return -1;
};
/**
* 返回匹配项的索引
* @param ary
* @param num
*/
ArrayUtils.findElementIndex = function (ary, num) {
var len = ary.length;
for (var i = 0; i < len; ++i) {
if (ary[i] == num)
return i;
}
return null;
};
/**
* 返回数组中最大值的索引
* @param ary
*/
ArrayUtils.getMaxElementIndex = function (ary) {
var matchIndex = 0;
var len = ary.length;
for (var j = 1; j < len; j++) {
if (ary[j] > ary[matchIndex])
matchIndex = j;
}
return matchIndex;
};
/**
* 返回数组中最小值的索引
* @param ary
*/
ArrayUtils.getMinElementIndex = function (ary) {
var matchIndex = 0;
var len = ary.length;
for (var j = 1; j < len; j++) {
if (ary[j] < ary[matchIndex])
matchIndex = j;
}
return matchIndex;
};
/**
* 返回一个"唯一性"数组
* @param ary 需要唯一性的数组
* @returns 唯一性的数组
*
* @tutorial
* 比如: [1, 2, 2, 3, 4]
* 返回: [1, 2, 3, 4]
*/
ArrayUtils.getUniqueAry = function (ary) {
var uAry = [];
var newAry = [];
var count = ary.length;
for (var i = 0; i < count; ++i) {
var value = ary[i];
if (uAry.indexOf(value) == -1)
uAry.push(value);
}
count = uAry.length;
for (var i = count - 1; i >= 0; --i) {
newAry.unshift(uAry[i]);
}
return newAry;
};
/**
* 返回2个数组中不同的部分
* 比如数组A = [1, 2, 3, 4, 6]
* 数组B = [0, 2, 1, 3, 4]
* 返回[6, 0]
* @param aryA
* @param aryB
* @return
*/
ArrayUtils.getDifferAry = function (aryA, aryB) {
aryA = this.getUniqueAry(aryA);
aryB = this.getUniqueAry(aryB);
var ary = aryA.concat(aryB);
var uObj = {};
var newAry = [];
var count = ary.length;
for (var j = 0; j < count; ++j) {
if (!uObj[ary[j]]) {
uObj[ary[j]] = {};
uObj[ary[j]].count = 0;
uObj[ary[j]].key = ary[j];
uObj[ary[j]].count++;
} }
else { if (ary[startIndex] == value)
if (uObj[ary[j]] instanceof Object) { return startIndex;
return -1;
};
/**
* 返回匹配项的索引
* @param ary
* @param num
*/
ArrayUtils.findElementIndex = function (ary, num) {
var len = ary.length;
for (var i = 0; i < len; ++i) {
if (ary[i] == num)
return i;
}
return null;
};
/**
* 返回数组中最大值的索引
* @param ary
*/
ArrayUtils.getMaxElementIndex = function (ary) {
var matchIndex = 0;
var len = ary.length;
for (var j = 1; j < len; j++) {
if (ary[j] > ary[matchIndex])
matchIndex = j;
}
return matchIndex;
};
/**
* 返回数组中最小值的索引
* @param ary
*/
ArrayUtils.getMinElementIndex = function (ary) {
var matchIndex = 0;
var len = ary.length;
for (var j = 1; j < len; j++) {
if (ary[j] < ary[matchIndex])
matchIndex = j;
}
return matchIndex;
};
/**
* 返回一个"唯一性"数组
* @param ary 需要唯一性的数组
* @returns 唯一性的数组
*
* @tutorial
* 比如: [1, 2, 2, 3, 4]
* 返回: [1, 2, 3, 4]
*/
ArrayUtils.getUniqueAry = function (ary) {
var uAry = [];
var newAry = [];
var count = ary.length;
for (var i = 0; i < count; ++i) {
var value = ary[i];
if (uAry.indexOf(value) == -1)
uAry.push(value);
}
count = uAry.length;
for (var i = count - 1; i >= 0; --i) {
newAry.unshift(uAry[i]);
}
return newAry;
};
/**
* 返回2个数组中不同的部分
* 比如数组A = [1, 2, 3, 4, 6]
* 数组B = [0, 2, 1, 3, 4]
* 返回[6, 0]
* @param aryA
* @param aryB
* @return
*/
ArrayUtils.getDifferAry = function (aryA, aryB) {
aryA = this.getUniqueAry(aryA);
aryB = this.getUniqueAry(aryB);
var ary = aryA.concat(aryB);
var uObj = {};
var newAry = [];
var count = ary.length;
for (var j = 0; j < count; ++j) {
if (!uObj[ary[j]]) {
uObj[ary[j]] = {};
uObj[ary[j]].count = 0;
uObj[ary[j]].key = ary[j];
uObj[ary[j]].count++; uObj[ary[j]].count++;
} }
else {
if (uObj[ary[j]] instanceof Object) {
uObj[ary[j]].count++;
}
}
} }
} for (var i in uObj) {
for (var i in uObj) { if (uObj[i].count != 2) {
if (uObj[i].count != 2) { newAry.unshift(uObj[i].key);
newAry.unshift(uObj[i].key); }
} }
} return newAry;
return newAry; };
}; /**
/** * 交换数组元素
* 交换数组元素 * @param array 目标数组
* @param array 目标数组 * @param index1 交换后的索引
* @param index1 交换的索引 * @param index2 交换的索引
* @param index2 交换前的索引 */
*/ ArrayUtils.swap = function (array, index1, index2) {
ArrayUtils.swap = function (array, index1, index2) { var temp = array[index1];
var temp = array[index1]; array[index1] = array[index2];
array[index1] = array[index2]; array[index2] = temp;
array[index2] = temp; };
}; /**
/** * 清除列表
* 清除列表 * @param ary
* @param ary */
*/ ArrayUtils.clearList = function (ary) {
ArrayUtils.clearList = function (ary) { if (!ary)
if (!ary) return;
return; var length = ary.length;
var length = ary.length; for (var i = length - 1; i >= 0; i -= 1) {
for (var i = length - 1; i >= 0; i -= 1) { ary.splice(i, 1);
ary.splice(i, 1); }
} };
}; /**
/** * 克隆一个数组
* 克隆一个数组 * @param ary 需要克隆的数组
* @param ary 需要克隆的数组 * @return 克隆的数组
* @return 克隆的数组 */
*/ ArrayUtils.cloneList = function (ary) {
ArrayUtils.cloneList = function (ary) { if (!ary)
if (!ary) return null;
return null; return ary.slice(0, ary.length);
return ary.slice(0, ary.length); };
}; /**
/** * 判断2个数组是否相同
* 判断2个数组是否相同 * @param ary1 数组1
* @param ary1 数组1 * @param ary2 数组2
* @param ary2 数组2 */
*/ ArrayUtils.equals = function (ary1, ary2) {
ArrayUtils.equals = function (ary1, ary2) { if (ary1 == ary2)
if (ary1 == ary2) return true;
return true; var length = ary1.length;
var length = ary1.length; if (length != ary2.length)
if (length != ary2.length)
return false;
while (length--) {
if (ary1[length] != ary2[length])
return false; return false;
} while (length--) {
return true; if (ary1[length] != ary2[length])
}; return false;
/**
* 根据索引插入元素,索引和索引后的元素都向后移动一位
* @param ary
* @param index 插入索引
* @param value 插入的元素
* @returns 插入的元素 未插入则返回空
*/
ArrayUtils.insert = function (ary, index, value) {
if (!ary)
return null;
var length = ary.length;
if (index > length)
index = length;
if (index < 0)
index = 0;
if (index == length)
ary.push(value); //插入最后
else if (index == 0)
ary.unshift(value); //插入头
else {
for (var i = length - 1; i >= index; i -= 1) {
ary[i + 1] = ary[i];
} }
ary[index] = value; return true;
} };
return value; /**
}; * 根据索引插入元素,索引和索引后的元素都向后移动一位
/** * @param ary
* 打乱数组 FisherYates shuffle * @param index 插入索引
* @param list * @param value 插入的元素
*/ * @returns 插入的元素 未插入则返回空
ArrayUtils.shuffle = function (list) { */
var n = list.length; ArrayUtils.insert = function (ary, index, value) {
while (n > 1) { if (!ary)
n--; return null;
var k = RandomUtils.randint(0, n + 1); var length = ary.length;
var value = list[k]; if (index > length)
list[k] = list[n]; index = length;
list[n] = value; if (index < 0)
} index = 0;
}; if (index == length)
/** ary.push(value); //插入最后
* 如果项目已经在列表中返回false如果成功添加返回true else if (index == 0)
* @param list ary.unshift(value); //插入头
* @param item else {
*/ for (var i = length - 1; i >= index; i -= 1) {
ArrayUtils.addIfNotPresent = function (list, item) { ary[i + 1] = ary[i];
if (new es.List(list).contains(item)) }
return false; ary[index] = value;
list.push(item); }
return true; return value;
}; };
/** /**
* 返回列表中的最后一项。列表中至少应该有一个项目 * 打乱数组 FisherYates shuffle
* @param list * @param list
*/ */
ArrayUtils.lastItem = function (list) { ArrayUtils.shuffle = function (list) {
return list[list.length - 1]; var n = list.length;
}; while (n > 1) {
/** n--;
* 从列表中随机获取一个项目。不清空检查列表! var k = es.RandomUtils.randint(0, n + 1);
* @param list var value = list[k];
*/ list[k] = list[n];
ArrayUtils.randomItem = function (list) { list[n] = value;
return list[RandomUtils.randint(0, list.length - 1)]; }
}; };
/** /**
* 从列表中随机获取物品。不清空检查列表也不验证列表数是否大于项目数。返回的List可以通过ListPool.free放回池中 * 如果项目已经在列表中返回false如果成功添加返回true
* @param list * @param list
* @param itemCount 从列表中返回的随机项目的数量 * @param item
*/ */
ArrayUtils.randomItems = function (list, itemCount) { ArrayUtils.addIfNotPresent = function (list, item) {
var set = new Set(); if (new es.List(list).contains(item))
while (set.size != itemCount) { return false;
var item = this.randomItem(list); list.push(item);
if (!set.has(item)) return true;
set.add(item); };
} /**
var items = es.ListPool.obtain(); * 返回列表中的最后一项。列表中至少应该有一个项目
set.forEach(function (value) { return items.push(value); }); * @param list
return items; */
}; ArrayUtils.lastItem = function (list) {
return ArrayUtils; return list[list.length - 1];
}()); };
/**
* 从列表中随机获取一个项目。不清空检查列表!
* @param list
*/
ArrayUtils.randomItem = function (list) {
return list[es.RandomUtils.randint(0, list.length - 1)];
};
/**
* 从列表中随机获取物品。不清空检查列表也不验证列表数是否大于项目数。返回的List可以通过ListPool.free放回池中
* @param list
* @param itemCount 从列表中返回的随机项目的数量
*/
ArrayUtils.randomItems = function (list, itemCount) {
var set = new Set();
while (set.size != itemCount) {
var item = this.randomItem(list);
if (!set.has(item))
set.add(item);
}
var items = es.ListPool.obtain();
set.forEach(function (value) { return items.push(value); });
return items;
};
return ArrayUtils;
}());
es.ArrayUtils = ArrayUtils;
})(es || (es = {}));
var es; var es;
(function (es) { (function (es) {
var Base64Utils = /** @class */ (function () { var Base64Utils = /** @class */ (function () {
@@ -10578,131 +10582,135 @@ var es;
}()); }());
es.NumberExtension = NumberExtension; es.NumberExtension = NumberExtension;
})(es || (es = {})); })(es || (es = {}));
var RandomUtils = /** @class */ (function () { var es;
function RandomUtils() { (function (es) {
} var RandomUtils = /** @class */ (function () {
/** function RandomUtils() {
* 在 start 与 stop之间取一个随机整数可以用step指定间隔 但不包括较大的端点start与stop较大的一个
* 如
* this.randrange(1, 10, 3)
* 则返回的可能是 1 或 4 或 7 , 注意 这里面不会返回10因为是10是大端点
*
* @param start
* @param stop
* @param step
* @return 假设 start < stop, [start, stop) 区间内的随机整数
*
*/
RandomUtils.randrange = function (start, stop, step) {
if (step === void 0) { step = 1; }
if (step == 0)
throw new Error('step 不能为 0');
var width = stop - start;
if (width == 0)
throw new Error('没有可用的范围(' + start + ',' + stop + ')');
if (width < 0)
width = start - stop;
var n = Math.floor((width + step - 1) / step);
return Math.floor(this.random() * n) * step + Math.min(start, stop);
};
/**
* 返回a 到 b之间的随机整数包括 a 和 b
* @param a
* @param b
* @return [a, b] 之间的随机整数
*
*/
RandomUtils.randint = function (a, b) {
a = Math.floor(a);
b = Math.floor(b);
if (a > b)
a++;
else
b++;
return this.randrange(a, b);
};
/**
* 返回 a - b之间的随机数不包括 Math.max(a, b)
* @param a
* @param b
* @return 假设 a < b, [a, b)
*/
RandomUtils.randnum = function (a, b) {
return this.random() * (b - a) + a;
};
/**
* 打乱数组
* @param array
* @return
*/
RandomUtils.shuffle = function (array) {
array.sort(this._randomCompare);
return array;
};
/**
* 从序列中随机取一个元素
* @param sequence 可以是 数组、 vector等只要是有length属性并且可以用数字索引获取元素的对象
* 另外,字符串也是允许的。
* @return 序列中的某一个元素
*
*/
RandomUtils.choice = function (sequence) {
if (!sequence.hasOwnProperty("length"))
throw new Error('无法对此对象执行此操作');
var index = Math.floor(this.random() * sequence.length);
if (sequence instanceof String)
return String(sequence).charAt(index);
else
return sequence[index];
};
/**
* 对列表中的元素进行随机采æ ?
* <pre>
* this.sample([1, 2, 3, 4, 5], 3) // Choose 3 elements
* [4, 1, 5]
* </pre>
* @param sequence
* @param num
* @return
*
*/
RandomUtils.sample = function (sequence, num) {
var len = sequence.length;
if (num <= 0 || len < num)
throw new Error("采样数量不够");
var selected = [];
var indices = [];
for (var i = 0; i < num; i++) {
var index = Math.floor(this.random() * len);
while (indices.indexOf(index) >= 0)
index = Math.floor(this.random() * len);
selected.push(sequence[index]);
indices.push(index);
} }
return selected; /**
}; * 在 start 与 stop之间取一个随机整数可以用step指定间隔 但不包括较大的端点start与stop较大的一个
/** * 如
* 返回 0.0 - 1.0 之间的随机数,等同于 Math.random() * this.randrange(1, 10, 3)
* @return Math.random() * 则返回的可能是 1 或 4 或 7 , 注意 这里面不会返回10因为是10是大端点
* *
*/ * @param start
RandomUtils.random = function () { * @param stop
return Math.random(); * @param step
}; * @return 假设 start < stop, [start, stop) 区间内的随机整数
/** *
* 计算概率 */
* @param chance 概率 RandomUtils.randrange = function (start, stop, step) {
* @return if (step === void 0) { step = 1; }
*/ if (step == 0)
RandomUtils.boolean = function (chance) { throw new Error('step 不能为 0');
if (chance === void 0) { chance = .5; } var width = stop - start;
return (this.random() < chance) ? true : false; if (width == 0)
}; throw new Error('没有可用的范围(' + start + ',' + stop + ')');
RandomUtils._randomCompare = function (a, b) { if (width < 0)
return (this.random() > .5) ? 1 : -1; width = start - stop;
}; var n = Math.floor((width + step - 1) / step);
return RandomUtils; return Math.floor(this.random() * n) * step + Math.min(start, stop);
}()); };
/**
* 返回a 到 b之间的随机整数包括 a 和 b
* @param a
* @param b
* @return [a, b] 之间的随机整数
*
*/
RandomUtils.randint = function (a, b) {
a = Math.floor(a);
b = Math.floor(b);
if (a > b)
a++;
else
b++;
return this.randrange(a, b);
};
/**
* 返回 a - b之间的随机数不包括 Math.max(a, b)
* @param a
* @param b
* @return 假设 a < b, [a, b)
*/
RandomUtils.randnum = function (a, b) {
return this.random() * (b - a) + a;
};
/**
* 打乱数组
* @param array
* @return
*/
RandomUtils.shuffle = function (array) {
array.sort(this._randomCompare);
return array;
};
/**
* 从序列中随机取一个元素
* @param sequence 可以是 数组、 vector等只要是有length属性并且可以用数字索引获取元素的对象
* 另外,字符串也是允许的。
* @return 序列中的某一个元素
*
*/
RandomUtils.choice = function (sequence) {
if (!sequence.hasOwnProperty("length"))
throw new Error('无法对此对象执行此操作');
var index = Math.floor(this.random() * sequence.length);
if (sequence instanceof String)
return String(sequence).charAt(index);
else
return sequence[index];
};
/**
* 对列表中的元素进行随机采æ ?
* <pre>
* this.sample([1, 2, 3, 4, 5], 3) // Choose 3 elements
* [4, 1, 5]
* </pre>
* @param sequence
* @param num
* @return
*
*/
RandomUtils.sample = function (sequence, num) {
var len = sequence.length;
if (num <= 0 || len < num)
throw new Error("采样数量不够");
var selected = [];
var indices = [];
for (var i = 0; i < num; i++) {
var index = Math.floor(this.random() * len);
while (indices.indexOf(index) >= 0)
index = Math.floor(this.random() * len);
selected.push(sequence[index]);
indices.push(index);
}
return selected;
};
/**
* 返回 0.0 - 1.0 之间的随机数,等同于 Math.random()
* @return Math.random()
*
*/
RandomUtils.random = function () {
return Math.random();
};
/**
* 计算概率
* @param chance 概率
* @return
*/
RandomUtils.boolean = function (chance) {
if (chance === void 0) { chance = .5; }
return (this.random() < chance) ? true : false;
};
RandomUtils._randomCompare = function (a, b) {
return (this.random() > .5) ? 1 : -1;
};
return RandomUtils;
}());
es.RandomUtils = RandomUtils;
})(es || (es = {}));
var es; var es;
(function (es) { (function (es) {
var RectangleExt = /** @class */ (function () { var RectangleExt = /** @class */ (function () {

File diff suppressed because one or more lines are too long

View File

@@ -212,7 +212,7 @@ module es {
* 返回第一个启用加载的类型为T的组件 * 返回第一个启用加载的类型为T的组件
* @param type * @param type
*/ */
public findComponentOfType<T extends Component>(type): T { public findComponentOfType<T extends Component>(type: new (...args) => T): T {
return this.entities.findComponentOfType<T>(type); return this.entities.findComponentOfType<T>(type);
} }
@@ -220,7 +220,7 @@ module es {
* 返回类型为T的所有已启用已加载组件的列表 * 返回类型为T的所有已启用已加载组件的列表
* @param type * @param type
*/ */
public findComponentsOfType<T extends Component>(type): T[] { public findComponentsOfType<T extends Component>(type: new (...args) => T): T[] {
return this.entities.findComponentsOfType<T>(type); return this.entities.findComponentsOfType<T>(type);
} }

View File

@@ -1,297 +1,299 @@
class ArrayUtils { module es {
/** export class ArrayUtils {
* 执行冒泡排序 /**
* @param ary * 执行冒泡排序
*/ * @param ary
public static bubbleSort(ary: number[]): void { */
let isExchange: Boolean = false; public static bubbleSort(ary: number[]): void {
for (let i: number = 0; i < ary.length; i++) { let isExchange: Boolean = false;
isExchange = false; for (let i: number = 0; i < ary.length; i++) {
for (let j: number = ary.length - 1; j > i; j--) { isExchange = false;
if (ary[j] < ary[j - 1]) { for (let j: number = ary.length - 1; j > i; j--) {
let temp: number = ary[j]; if (ary[j] < ary[j - 1]) {
let temp: number = ary[j];
ary[j] = ary[j - 1];
ary[j - 1] = temp;
isExchange = true;
}
}
if (!isExchange)
break;
}
}
/**
* 执行插入排序
* @param ary
*/
public static insertionSort(ary: number[]): void {
let len: number = ary.length;
for (let i: number = 1; i < len; i++) {
let val: number = ary[i];
for (var j: number = i; j > 0 && ary[j - 1] > val; j--) {
ary[j] = ary[j - 1]; ary[j] = ary[j - 1];
ary[j - 1] = temp;
isExchange = true;
} }
ary[j] = val;
} }
if (!isExchange)
break;
} }
}
/**
/** * 执行二分搜索
* 执行插入排序 * @param ary 搜索的数组(必须排序过)
* @param ary * @param value 需要搜索的值
*/ * @returns 返回匹配结果的数组索引
public static insertionSort(ary: number[]): void { */
let len: number = ary.length; public static binarySearch(ary: number[], value: number): number {
for (let i: number = 1; i < len; i++) { let startIndex: number = 0;
let val: number = ary[i]; let endIndex: number = ary.length;
for (var j: number = i; j > 0 && ary[j - 1] > val; j--) { let sub: number = (startIndex + endIndex) >> 1;
ary[j] = ary[j - 1]; while (startIndex < endIndex) {
if (value <= ary[sub]) endIndex = sub;
else if (value >= ary[sub]) startIndex = sub + 1;
sub = (startIndex + endIndex) >> 1;
} }
ary[j] = val; if (ary[startIndex] == value) return startIndex;
return -1;
} }
}
/** /**
* 执行二分搜索 * 返回匹配项的索引
* @param ary 搜索的数组(必须排序过) * @param ary
* @param value 需要搜索的值 * @param num
* @returns 返回匹配结果的数组索引 */
*/ public static findElementIndex(ary: any[], num: any): any {
public static binarySearch(ary: number[], value: number): number { let len: number = ary.length;
let startIndex: number = 0; for (let i: number = 0; i < len; ++i) {
let endIndex: number = ary.length; if (ary[i] == num)
let sub: number = (startIndex + endIndex) >> 1; return i;
while (startIndex < endIndex) { }
if (value <= ary[sub]) endIndex = sub; return null;
else if (value >= ary[sub]) startIndex = sub + 1;
sub = (startIndex + endIndex) >> 1;
} }
if (ary[startIndex] == value) return startIndex;
return -1; /**
} * 返回数组中最大值的索引
* @param ary
*/
/** public static getMaxElementIndex(ary: number[]): number {
* 返回匹配项的索引 let matchIndex: number = 0;
* @param ary let len: number = ary.length;
* @param num for (let j: number = 1; j < len; j++) {
*/ if (ary[j] > ary[matchIndex])
public static findElementIndex(ary: any[], num: any): any { matchIndex = j;
let len: number = ary.length; }
for (let i: number = 0; i < len; ++i) { return matchIndex;
if (ary[i] == num)
return i;
} }
return null;
} /**
* 返回数组中最小值的索引
/** * @param ary
* 返回数组中最大值的索引 */
* @param ary public static getMinElementIndex(ary: number[]): number {
*/ let matchIndex: number = 0;
public static getMaxElementIndex(ary: number[]): number { let len: number = ary.length;
let matchIndex: number = 0; for (let j: number = 1; j < len; j++) {
let len: number = ary.length; if (ary[j] < ary[matchIndex])
for (let j: number = 1; j < len; j++) { matchIndex = j;
if (ary[j] > ary[matchIndex]) }
matchIndex = j; return matchIndex;
} }
return matchIndex;
} /**
* 返回一个"唯一性"数组
/** * @param ary 需要唯一性的数组
* 返回数组中最小值的索引 * @returns 唯一性的数组
* @param ary *
*/ * @tutorial
public static getMinElementIndex(ary: number[]): number { * 比如: [1, 2, 2, 3, 4]
let matchIndex: number = 0; * 返回: [1, 2, 3, 4]
let len: number = ary.length; */
for (let j: number = 1; j < len; j++) { public static getUniqueAry(ary: number[]): number[] {
if (ary[j] < ary[matchIndex]) let uAry: number[] = [];
matchIndex = j; let newAry: number[] = [];
let count = ary.length;
for (let i: number = 0; i < count; ++i) {
let value: number = ary[i];
if (uAry.indexOf(value) == -1) uAry.push(value);
}
count = uAry.length;
for (let i: number = count - 1; i >= 0; --i) {
newAry.unshift(uAry[i]);
}
return newAry;
} }
return matchIndex;
}
/**
/** * 返回2个数组中不同的部分
* 返回一个"唯一性"数组 * 比如数组A = [1, 2, 3, 4, 6]
* @param ary 需要唯一性的数组 * 数组B = [0, 2, 1, 3, 4]
* @returns 唯一性的数组 * 返回[6, 0]
* * @param aryA
* @tutorial * @param aryB
* 比如: [1, 2, 2, 3, 4] * @return
* 返回: [1, 2, 3, 4] */
*/ public static getDifferAry(aryA: number[], aryB: number[]): number[] {
public static getUniqueAry(ary: number[]): number[] { aryA = this.getUniqueAry(aryA);
let uAry: number[] = []; aryB = this.getUniqueAry(aryB);
let newAry: number[] = []; let ary: number[] = aryA.concat(aryB);
let count = ary.length; let uObj: Object = {};
for (let i: number = 0; i < count; ++i) { let newAry: number[] = [];
let value: number = ary[i]; let count: number = ary.length;
if (uAry.indexOf(value) == -1) uAry.push(value); for (let j: number = 0; j < count; ++j) {
} if (!uObj[ary[j]]) {
uObj[ary[j]] = {};
count = uAry.length; uObj[ary[j]].count = 0;
for (let i: number = count - 1; i >= 0; --i) { uObj[ary[j]].key = ary[j];
newAry.unshift(uAry[i]);
}
return newAry;
}
/**
* 返回2个数组中不同的部分
* 比如数组A = [1, 2, 3, 4, 6]
* 数组B = [0, 2, 1, 3, 4]
* 返回[6, 0]
* @param aryA
* @param aryB
* @return
*/
public static getDifferAry(aryA: number[], aryB: number[]): number[] {
aryA = this.getUniqueAry(aryA);
aryB = this.getUniqueAry(aryB);
let ary: number[] = aryA.concat(aryB);
let uObj: Object = {};
let newAry: number[] = [];
let count: number = ary.length;
for (let j: number = 0; j < count; ++j) {
if (!uObj[ary[j]]) {
uObj[ary[j]] = {};
uObj[ary[j]].count = 0;
uObj[ary[j]].key = ary[j];
uObj[ary[j]].count++;
} else {
if (uObj[ary[j]] instanceof Object) {
uObj[ary[j]].count++; uObj[ary[j]].count++;
} else {
if (uObj[ary[j]] instanceof Object) {
uObj[ary[j]].count++;
}
} }
} }
for (let i in uObj) {
if (uObj[i].count != 2) {
newAry.unshift(uObj[i].key);
}
}
return newAry;
} }
for (let i in uObj) {
if (uObj[i].count != 2) { /**
newAry.unshift(uObj[i].key); * 交换数组元素
* @param array 目标数组
* @param index1 交换后的索引
* @param index2 交换前的索引
*/
public static swap(array: any[], index1: number, index2: number): void {
let temp: any = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
/**
* 清除列表
* @param ary
*/
public static clearList(ary: any[]): void {
if (!ary) return;
let length: number = ary.length;
for (let i: number = length - 1; i >= 0; i -= 1) {
ary.splice(i, 1);
} }
} }
return newAry;
} /**
* 克隆一个数组
/** * @param ary 需要克隆的数组
* 交换数组元素 * @return 克隆的数组
* @param array 目标数组 */
* @param index1 交换后的索引 public static cloneList(ary: any[]): any[] {
* @param index2 交换前的索引 if (!ary) return null;
*/ return ary.slice(0, ary.length);
public static swap(array: any[], index1: number, index2: number): void {
let temp: any = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
/**
* 清除列表
* @param ary
*/
public static clearList(ary: any[]): void {
if (!ary) return;
let length: number = ary.length;
for (let i: number = length - 1; i >= 0; i -= 1) {
ary.splice(i, 1);
} }
}
/**
/** * 判断2个数组是否相同
* 克隆一个数组 * @param ary1 数组1
* @param ary 需要克隆的数组 * @param ary2 数组2
* @return 克隆的数组 */
*/ public static equals(ary1: number[], ary2: number[]): Boolean {
public static cloneList(ary: any[]): any[] { if (ary1 == ary2) return true;
if (!ary) return null; let length: number = ary1.length;
return ary.slice(0, ary.length); if (length != ary2.length) return false;
} while (length--) {
if (ary1[length] != ary2[length])
/** return false;
* 判断2个数组是否相同 }
* @param ary1 数组1 return true;
* @param ary2 数组2 }
*/
public static equals(ary1: number[], ary2: number[]): Boolean { /**
if (ary1 == ary2) return true; * 根据索引插入元素,索引和索引后的元素都向后移动一位
let length: number = ary1.length; * @param ary
if (length != ary2.length) return false; * @param index 插入索引
while (length--) { * @param value 插入的元素
if (ary1[length] != ary2[length]) * @returns 插入的元素 未插入则返回空
*/
public static insert(ary: any[], index: number, value: any): any {
if (!ary) return null;
let length: number = ary.length;
if (index > length) index = length;
if (index < 0) index = 0;
if (index == length) ary.push(value); //插入最后
else if (index == 0) ary.unshift(value); //插入头
else {
for (let i: number = length - 1; i >= index; i -= 1) {
ary[i + 1] = ary[i];
}
ary[index] = value;
}
return value;
}
/**
* 打乱数组 FisherYates shuffle
* @param list
*/
public static shuffle<T>(list: T[]) {
let n = list.length;
while (n > 1) {
n--;
let k = RandomUtils.randint(0, n + 1);
let value: T = list[k];
list[k] = list[n];
list[n] = value;
}
}
/**
* 如果项目已经在列表中返回false如果成功添加返回true
* @param list
* @param item
*/
public static addIfNotPresent<T>(list: T[], item: T) {
if (new es.List(list).contains(item))
return false; return false;
list.push(item);
return true;
} }
return true;
} /**
* 返回列表中的最后一项。列表中至少应该有一个项目
/** * @param list
* 根据索引插入元素,索引和索引后的元素都向后移动一位 */
* @param ary public static lastItem<T>(list: T[]) {
* @param index 插入索引 return list[list.length - 1];
* @param value 插入的元素 }
* @returns 插入的元素 未插入则返回空
*/ /**
public static insert(ary: any[], index: number, value: any): any { * 从列表中随机获取一个项目。不清空检查列表!
if (!ary) return null; * @param list
let length: number = ary.length; */
if (index > length) index = length; public static randomItem<T>(list: T[]) {
if (index < 0) index = 0; return list[RandomUtils.randint(0, list.length - 1)];
if (index == length) ary.push(value); //插入最后 }
else if (index == 0) ary.unshift(value); //插入头
else { /**
for (let i: number = length - 1; i >= index; i -= 1) { * 从列表中随机获取物品。不清空检查列表也不验证列表数是否大于项目数。返回的List可以通过ListPool.free放回池中
ary[i + 1] = ary[i]; * @param list
* @param itemCount 从列表中返回的随机项目的数量
*/
public static randomItems<T>(list: T[], itemCount: number){
let set = new Set<T>();
while (set.size != itemCount) {
let item = this.randomItem(list);
if (!set.has(item))
set.add(item);
} }
ary[index] = value;
} let items = es.ListPool.obtain<T>();
return value; set.forEach(value => items.push(value));
} return items;
/**
* 打乱数组 FisherYates shuffle
* @param list
*/
public static shuffle<T>(list: T[]) {
let n = list.length;
while (n > 1) {
n--;
let k = RandomUtils.randint(0, n + 1);
let value: T = list[k];
list[k] = list[n];
list[n] = value;
} }
} }
}
/**
* 如果项目已经在列表中返回false如果成功添加返回true
* @param list
* @param item
*/
public static addIfNotPresent<T>(list: T[], item: T) {
if (new es.List(list).contains(item))
return false;
list.push(item);
return true;
}
/**
* 返回列表中的最后一项。列表中至少应该有一个项目
* @param list
*/
public static lastItem<T>(list: T[]) {
return list[list.length - 1];
}
/**
* 从列表中随机获取一个项目。不清空检查列表!
* @param list
*/
public static randomItem<T>(list: T[]) {
return list[RandomUtils.randint(0, list.length - 1)];
}
/**
* 从列表中随机获取物品。不清空检查列表也不验证列表数是否大于项目数。返回的List可以通过ListPool.free放回池中
* @param list
* @param itemCount 从列表中返回的随机项目的数量
*/
public static randomItems<T>(list: T[], itemCount: number){
let set = new Set<T>();
while (set.size != itemCount) {
let item = this.randomItem(list);
if (!set.has(item))
set.add(item);
}
let items = es.ListPool.obtain<T>();
set.forEach(value => items.push(value));
return items;
}
}

View File

@@ -1,133 +1,135 @@
class RandomUtils { module es {
/** export class RandomUtils {
* 在 start 与 stop之间取一个随机整数可以用step指定间隔 但不包括较大的端点start与stop较大的一个 /**
* 如 * 在 start 与 stop之间取一个随机整数可以用step指定间隔 但不包括较大的端点start与stop较大的一个
* this.randrange(1, 10, 3) * 如
* 则返回的可能是 1 或 4 或 7 , 注意 这里面不会返回10因为是10是大端点 * this.randrange(1, 10, 3)
* * 则返回的可能是 1 或 4 或 7 , 注意 这里面不会返回10因为是10是大端点
* @param start *
* @param stop * @param start
* @param step * @param stop
* @return 假设 start < stop, [start, stop) 区间内的随机整数 * @param step
* * @return 假设 start < stop, [start, stop) 区间内的随机整数
*/ *
public static randrange(start: number, stop: number, step: number = 1): number { */
if (step == 0) public static randrange(start: number, stop: number, step: number = 1): number {
throw new Error('step 不能为 0'); if (step == 0)
throw new Error('step 不能为 0');
let width: number = stop - start;
if (width == 0) let width: number = stop - start;
throw new Error('没有可用的范围(' + start + ',' + stop + ')'); if (width == 0)
if (width < 0) throw new Error('没有可用的范围(' + start + ',' + stop + ')');
width = start - stop; if (width < 0)
width = start - stop;
let n: number = Math.floor((width + step - 1) / step);
return Math.floor(this.random() * n) * step + Math.min(start, stop); let n: number = Math.floor((width + step - 1) / step);
} return Math.floor(this.random() * n) * step + Math.min(start, stop);
}
/**
* 返回a 到 b之间的随机整数包括 a 和 b /**
* @param a * 返回a 到 b之间的随机整数包括 a 和 b
* @param b * @param a
* @return [a, b] 之间的随机整数 * @param b
* * @return [a, b] 之间的随机整数
*/ *
public static randint(a: number, b: number): number { */
a = Math.floor(a); public static randint(a: number, b: number): number {
b = Math.floor(b); a = Math.floor(a);
if (a > b) b = Math.floor(b);
a++; if (a > b)
else a++;
b++; else
return this.randrange(a, b); b++;
} return this.randrange(a, b);
}
/**
* 返回 a - b之间的随机数不包括 Math.max(a, b) /**
* @param a * 返回 a - b之间的随机数不包括 Math.max(a, b)
* @param b * @param a
* @return 假设 a < b, [a, b) * @param b
*/ * @return 假设 a < b, [a, b)
public static randnum(a: number, b: number): number { */
return this.random() * (b - a) + a; public static randnum(a: number, b: number): number {
} return this.random() * (b - a) + a;
}
/**
* 打乱数组 /**
* @param array * 打乱数组
* @return * @param array
*/ * @return
public static shuffle(array: any[]): any[] { */
array.sort(this._randomCompare); public static shuffle(array: any[]): any[] {
return array; array.sort(this._randomCompare);
} return array;
}
/**
* 从序列中随机取一个元素 /**
* @param sequence 可以是 数组、 vector等只要是有length属性并且可以用数字索引获取元素的对象 * 从序列中随机取一个元素
* 另外,字符串也是允许的。 * @param sequence 可以是 数组、 vector等只要是有length属性并且可以用数字索引获取元素的对象
* @return 序列中的某一个元素 * 另外,字符串也是允许的。
* * @return 序列中的某一个元素
*/ *
public static choice(sequence: any): any { */
if (!sequence.hasOwnProperty("length")) public static choice(sequence: any): any {
throw new Error('无法对此对象执行此操作'); if (!sequence.hasOwnProperty("length"))
let index: number = Math.floor(this.random() * sequence.length); throw new Error('无法对此对象执行此操作');
if (sequence instanceof String) let index: number = Math.floor(this.random() * sequence.length);
return String(sequence).charAt(index); if (sequence instanceof String)
else return String(sequence).charAt(index);
return sequence[index]; else
} return sequence[index];
}
/**
* 对列表中的元素进行随机采æ ? /**
* <pre> * 对列表中的元素进行随机采æ ?
* this.sample([1, 2, 3, 4, 5], 3) // Choose 3 elements * <pre>
* [4, 1, 5] * this.sample([1, 2, 3, 4, 5], 3) // Choose 3 elements
* </pre> * [4, 1, 5]
* @param sequence * </pre>
* @param num * @param sequence
* @return * @param num
* * @return
*/ *
public static sample(sequence: any[], num: number): any[] { */
let len: number = sequence.length; public static sample(sequence: any[], num: number): any[] {
if (num <= 0 || len < num) let len: number = sequence.length;
throw new Error("采样数量不够"); if (num <= 0 || len < num)
throw new Error("采样数量不够");
let selected: any[] = [];
let indices: any[] = []; let selected: any[] = [];
for (let i: number = 0; i < num; i++) { let indices: any[] = [];
let index: number = Math.floor(this.random() * len); for (let i: number = 0; i < num; i++) {
while (indices.indexOf(index) >= 0) let index: number = Math.floor(this.random() * len);
index = Math.floor(this.random() * len); while (indices.indexOf(index) >= 0)
index = Math.floor(this.random() * len);
selected.push(sequence[index]);
indices.push(index); selected.push(sequence[index]);
indices.push(index);
}
return selected;
}
/**
* 返回 0.0 - 1.0 之间的随机数,等同于 Math.random()
* @return Math.random()
*
*/
public static random(): number {
return Math.random();
}
/**
* 计算概率
* @param chance 概率
* @return
*/
public static boolean(chance: number = .5): boolean {
return (this.random() < chance) ? true : false;
}
private static _randomCompare(a: Object, b: Object): number {
return (this.random() > .5) ? 1 : -1;
} }
return selected;
} }
}
/**
* 返回 0.0 - 1.0 之间的随机数,等同于 Math.random()
* @return Math.random()
*
*/
public static random(): number {
return Math.random();
}
/**
* 计算概率
* @param chance 概率
* @return
*/
public static boolean(chance: number = .5): boolean {
return (this.random() < chance) ? true : false;
}
private static _randomCompare(a: Object, b: Object): number {
return (this.random() > .5) ? 1 : -1;
}
}