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

View File

@@ -10114,297 +10114,301 @@ var es;
}());
es.MaxRectsBinPack = MaxRectsBinPack;
})(es || (es = {}));
var ArrayUtils = /** @class */ (function () {
function ArrayUtils() {
}
/**
* 执行冒泡排序
* @param ary
*/
ArrayUtils.bubbleSort = function (ary) {
var isExchange = false;
for (var i = 0; i < ary.length; i++) {
isExchange = false;
for (var j = ary.length - 1; j > i; j--) {
if (ary[j] < ary[j - 1]) {
var temp = ary[j];
ary[j] = ary[j - 1];
ary[j - 1] = temp;
isExchange = true;
var es;
(function (es) {
var ArrayUtils = /** @class */ (function () {
function ArrayUtils() {
}
/**
* 执行冒泡排序
* @param ary
*/
ArrayUtils.bubbleSort = function (ary) {
var isExchange = false;
for (var i = 0; i < ary.length; i++) {
isExchange = false;
for (var j = ary.length - 1; j > i; j--) {
if (ary[j] < ary[j - 1]) {
var temp = ary[j];
ary[j] = ary[j - 1];
ary[j - 1] = temp;
isExchange = true;
}
}
if (!isExchange)
break;
}
if (!isExchange)
break;
}
};
/**
* 执行插入排序
* @param ary
*/
ArrayUtils.insertionSort = function (ary) {
var len = ary.length;
for (var i = 1; i < len; i++) {
var val = ary[i];
for (var j = i; j > 0 && ary[j - 1] > val; j--) {
ary[j] = ary[j - 1];
};
/**
* 执行插入排序
* @param ary
*/
ArrayUtils.insertionSort = function (ary) {
var len = ary.length;
for (var i = 1; i < len; i++) {
var val = ary[i];
for (var j = i; j > 0 && ary[j - 1] > val; j--) {
ary[j] = ary[j - 1];
}
ary[j] = val;
}
ary[j] = val;
}
};
/**
* 执行二分搜索
* @param ary 搜索的数组(必须排序过)
* @param value 需要搜索的值
* @returns 返回匹配结果的数组索引
*/
ArrayUtils.binarySearch = function (ary, value) {
var startIndex = 0;
var endIndex = ary.length;
var sub = (startIndex + endIndex) >> 1;
while (startIndex < endIndex) {
if (value <= ary[sub])
endIndex = sub;
else if (value >= ary[sub])
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++;
};
/**
* 执行二分搜索
* @param ary 搜索的数组(必须排序过)
* @param value 需要搜索的值
* @returns 返回匹配结果的数组索引
*/
ArrayUtils.binarySearch = function (ary, value) {
var startIndex = 0;
var endIndex = ary.length;
var sub = (startIndex + endIndex) >> 1;
while (startIndex < endIndex) {
if (value <= ary[sub])
endIndex = sub;
else if (value >= ary[sub])
startIndex = sub + 1;
sub = (startIndex + endIndex) >> 1;
}
else {
if (uObj[ary[j]] instanceof Object) {
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 (uObj[ary[j]] instanceof Object) {
uObj[ary[j]].count++;
}
}
}
}
for (var i in uObj) {
if (uObj[i].count != 2) {
newAry.unshift(uObj[i].key);
for (var i in uObj) {
if (uObj[i].count != 2) {
newAry.unshift(uObj[i].key);
}
}
}
return newAry;
};
/**
* 交换数组元素
* @param array 目标数组
* @param index1 交换的索引
* @param index2 交换前的索引
*/
ArrayUtils.swap = function (array, index1, index2) {
var temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
};
/**
* 清除列表
* @param ary
*/
ArrayUtils.clearList = function (ary) {
if (!ary)
return;
var length = ary.length;
for (var i = length - 1; i >= 0; i -= 1) {
ary.splice(i, 1);
}
};
/**
* 克隆一个数组
* @param ary 需要克隆的数组
* @return 克隆的数组
*/
ArrayUtils.cloneList = function (ary) {
if (!ary)
return null;
return ary.slice(0, ary.length);
};
/**
* 判断2个数组是否相同
* @param ary1 数组1
* @param ary2 数组2
*/
ArrayUtils.equals = function (ary1, ary2) {
if (ary1 == ary2)
return true;
var length = ary1.length;
if (length != ary2.length)
return false;
while (length--) {
if (ary1[length] != ary2[length])
return newAry;
};
/**
* 交换数组元素
* @param array 目标数组
* @param index1 交换后的索引
* @param index2 交换的索引
*/
ArrayUtils.swap = function (array, index1, index2) {
var temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
};
/**
* 清除列表
* @param ary
*/
ArrayUtils.clearList = function (ary) {
if (!ary)
return;
var length = ary.length;
for (var i = length - 1; i >= 0; i -= 1) {
ary.splice(i, 1);
}
};
/**
* 克隆一个数组
* @param ary 需要克隆的数组
* @return 克隆的数组
*/
ArrayUtils.cloneList = function (ary) {
if (!ary)
return null;
return ary.slice(0, ary.length);
};
/**
* 判断2个数组是否相同
* @param ary1 数组1
* @param ary2 数组2
*/
ArrayUtils.equals = function (ary1, ary2) {
if (ary1 == ary2)
return true;
var length = ary1.length;
if (length != ary2.length)
return false;
}
return true;
};
/**
* 根据索引插入元素,索引和索引后的元素都向后移动一位
* @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];
while (length--) {
if (ary1[length] != ary2[length])
return false;
}
ary[index] = value;
}
return value;
};
/**
* 打乱数组 FisherYates shuffle
* @param list
*/
ArrayUtils.shuffle = function (list) {
var n = list.length;
while (n > 1) {
n--;
var k = RandomUtils.randint(0, n + 1);
var value = list[k];
list[k] = list[n];
list[n] = value;
}
};
/**
* 如果项目已经在列表中返回false如果成功添加返回true
* @param list
* @param item
*/
ArrayUtils.addIfNotPresent = function (list, item) {
if (new es.List(list).contains(item))
return false;
list.push(item);
return true;
};
/**
* 返回列表中的最后一项。列表中至少应该有一个项目
* @param list
*/
ArrayUtils.lastItem = function (list) {
return list[list.length - 1];
};
/**
* 从列表中随机获取一个项目。不清空检查列表!
* @param list
*/
ArrayUtils.randomItem = function (list) {
return list[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;
}());
return true;
};
/**
* 根据索引插入元素,索引和索引后的元素都向后移动一位
* @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 value;
};
/**
* 打乱数组 FisherYates shuffle
* @param list
*/
ArrayUtils.shuffle = function (list) {
var n = list.length;
while (n > 1) {
n--;
var k = es.RandomUtils.randint(0, n + 1);
var value = list[k];
list[k] = list[n];
list[n] = value;
}
};
/**
* 如果项目已经在列表中返回false如果成功添加返回true
* @param list
* @param item
*/
ArrayUtils.addIfNotPresent = function (list, item) {
if (new es.List(list).contains(item))
return false;
list.push(item);
return true;
};
/**
* 返回列表中的最后一项。列表中至少应该有一个项目
* @param list
*/
ArrayUtils.lastItem = function (list) {
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;
(function (es) {
var Base64Utils = /** @class */ (function () {
@@ -10578,131 +10582,135 @@ var es;
}());
es.NumberExtension = NumberExtension;
})(es || (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);
var es;
(function (es) {
var RandomUtils = /** @class */ (function () {
function RandomUtils() {
}
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;
}());
/**
* 在 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;
};
/**
* 返回 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;
(function (es) {
var RectangleExt = /** @class */ (function () {

File diff suppressed because one or more lines are too long

View File

@@ -212,7 +212,7 @@ module es {
* 返回第一个启用加载的类型为T的组件
* @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);
}
@@ -220,7 +220,7 @@ module es {
* 返回类型为T的所有已启用已加载组件的列表
* @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);
}

View File

@@ -1,297 +1,299 @@
class ArrayUtils {
/**
* 执行冒泡排序
* @param ary
*/
public static bubbleSort(ary: number[]): void {
let isExchange: Boolean = false;
for (let i: number = 0; i < ary.length; i++) {
isExchange = false;
for (let j: number = ary.length - 1; j > i; j--) {
if (ary[j] < ary[j - 1]) {
let temp: number = ary[j];
module es {
export class ArrayUtils {
/**
* 执行冒泡排序
* @param ary
*/
public static bubbleSort(ary: number[]): void {
let isExchange: Boolean = false;
for (let i: number = 0; i < ary.length; i++) {
isExchange = false;
for (let j: number = ary.length - 1; j > i; 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 - 1] = temp;
isExchange = true;
}
ary[j] = val;
}
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];
/**
* 执行二分搜索
* @param ary 搜索的数组(必须排序过)
* @param value 需要搜索的值
* @returns 返回匹配结果的数组索引
*/
public static binarySearch(ary: number[], value: number): number {
let startIndex: number = 0;
let endIndex: number = ary.length;
let sub: number = (startIndex + endIndex) >> 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 value 需要搜索的值
* @returns 返回匹配结果的数组索引
*/
public static binarySearch(ary: number[], value: number): number {
let startIndex: number = 0;
let endIndex: number = ary.length;
let sub: number = (startIndex + endIndex) >> 1;
while (startIndex < endIndex) {
if (value <= ary[sub]) endIndex = sub;
else if (value >= ary[sub]) startIndex = sub + 1;
sub = (startIndex + endIndex) >> 1;
/**
* 返回匹配项的索引
* @param ary
* @param num
*/
public static findElementIndex(ary: any[], num: any): any {
let len: number = ary.length;
for (let i: number = 0; i < len; ++i) {
if (ary[i] == num)
return i;
}
return null;
}
if (ary[startIndex] == value) return startIndex;
return -1;
}
/**
* 返回匹配项的索引
* @param ary
* @param num
*/
public static findElementIndex(ary: any[], num: any): any {
let len: number = ary.length;
for (let i: number = 0; i < len; ++i) {
if (ary[i] == num)
return i;
/**
* 返回数组中最大值的索引
* @param ary
*/
public static getMaxElementIndex(ary: number[]): number {
let matchIndex: number = 0;
let len: number = ary.length;
for (let j: number = 1; j < len; j++) {
if (ary[j] > ary[matchIndex])
matchIndex = j;
}
return matchIndex;
}
return null;
}
/**
* 返回数组中最大值的索引
* @param ary
*/
public static getMaxElementIndex(ary: number[]): number {
let matchIndex: number = 0;
let len: number = ary.length;
for (let j: number = 1; j < len; j++) {
if (ary[j] > ary[matchIndex])
matchIndex = j;
/**
* 返回数组中最小值的索引
* @param ary
*/
public static getMinElementIndex(ary: number[]): number {
let matchIndex: number = 0;
let len: number = ary.length;
for (let j: number = 1; j < len; j++) {
if (ary[j] < ary[matchIndex])
matchIndex = j;
}
return matchIndex;
}
return matchIndex;
}
/**
* 返回数组中最小值的索引
* @param ary
*/
public static getMinElementIndex(ary: number[]): number {
let matchIndex: number = 0;
let len: number = ary.length;
for (let j: number = 1; j < len; j++) {
if (ary[j] < ary[matchIndex])
matchIndex = j;
/**
* 返回一个"唯一性"数组
* @param ary 需要唯一性的数组
* @returns 唯一性的数组
*
* @tutorial
* 比如: [1, 2, 2, 3, 4]
* 返回: [1, 2, 3, 4]
*/
public static getUniqueAry(ary: number[]): number[] {
let uAry: number[] = [];
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;
}
/**
* 返回一个"唯一性"数组
* @param ary 需要唯一性的数组
* @returns 唯一性的数组
*
* @tutorial
* 比如: [1, 2, 2, 3, 4]
* 返回: [1, 2, 3, 4]
*/
public static getUniqueAry(ary: number[]): number[] {
let uAry: number[] = [];
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;
}
/**
* 返回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) {
/**
* 返回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++;
}
}
}
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 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);
/**
* 克隆一个数组
* @param ary 需要克隆的数组
* @return 克隆的数组
*/
public static cloneList(ary: any[]): any[] {
if (!ary) return null;
return ary.slice(0, ary.length);
}
}
/**
* 克隆一个数组
* @param ary 需要克隆的数组
* @return 克隆的数组
*/
public static cloneList(ary: any[]): any[] {
if (!ary) return null;
return ary.slice(0, ary.length);
}
/**
* 判断2个数组是否相同
* @param ary1 数组1
* @param ary2 数组2
*/
public static equals(ary1: number[], ary2: number[]): Boolean {
if (ary1 == ary2) return true;
let length: number = ary1.length;
if (length != ary2.length) return false;
while (length--) {
if (ary1[length] != ary2[length])
/**
* 判断2个数组是否相同
* @param ary1 数组1
* @param ary2 数组2
*/
public static equals(ary1: number[], ary2: number[]): Boolean {
if (ary1 == ary2) return true;
let length: number = ary1.length;
if (length != ary2.length) return false;
while (length--) {
if (ary1[length] != ary2[length])
return false;
}
return true;
}
/**
* 根据索引插入元素,索引和索引后的元素都向后移动一位
* @param ary
* @param index 插入索引
* @param value 插入的元素
* @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;
list.push(item);
return true;
}
return true;
}
/**
* 根据索引插入元素,索引和索引后的元素都向后移动一位
* @param ary
* @param index 插入索引
* @param value 插入的元素
* @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];
/**
* 返回列表中的最后一项。列表中至少应该有一个项目
* @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);
}
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;
let items = es.ListPool.obtain<T>();
set.forEach(value => items.push(value));
return items;
}
}
/**
* 如果项目已经在列表中返回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 {
/**
* 在 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) 区间内的随机整数
*
*/
public static randrange(start: number, stop: number, step: number = 1): number {
if (step == 0)
throw new Error('step 不能为 0');
let width: number = stop - start;
if (width == 0)
throw new Error('没有可用的范围(' + 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);
}
/**
* 返回a 到 b之间的随机整数包括 a 和 b
* @param a
* @param b
* @return [a, b] 之间的随机整数
*
*/
public static randint(a: number, b: number): number {
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)
*/
public static randnum(a: number, b: number): number {
return this.random() * (b - a) + a;
}
/**
* 打乱数组
* @param array
* @return
*/
public static shuffle(array: any[]): any[] {
array.sort(this._randomCompare);
return array;
}
/**
* 从序列中随机取一个元素
* @param sequence 可以是 数组、 vector等只要是有length属性并且可以用数字索引获取元素的对象
* 另外,字符串也是允许的。
* @return 序列中的某一个元素
*
*/
public static choice(sequence: any): any {
if (!sequence.hasOwnProperty("length"))
throw new Error('无法对此对象执行此操作');
let index: number = 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
*
*/
public static sample(sequence: any[], num: number): any[] {
let len: number = sequence.length;
if (num <= 0 || len < num)
throw new Error("采样数量不够");
let selected: any[] = [];
let indices: any[] = [];
for (let i: number = 0; i < num; i++) {
let index: number = Math.floor(this.random() * len);
while (indices.indexOf(index) >= 0)
index = Math.floor(this.random() * len);
selected.push(sequence[index]);
indices.push(index);
module es {
export class 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) 区间内的随机整数
*
*/
public static randrange(start: number, stop: number, step: number = 1): number {
if (step == 0)
throw new Error('step 不能为 0');
let width: number = stop - start;
if (width == 0)
throw new Error('没有可用的范围(' + 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);
}
/**
* 返回a 到 b之间的随机整数包括 a 和 b
* @param a
* @param b
* @return [a, b] 之间的随机整数
*
*/
public static randint(a: number, b: number): number {
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)
*/
public static randnum(a: number, b: number): number {
return this.random() * (b - a) + a;
}
/**
* 打乱数组
* @param array
* @return
*/
public static shuffle(array: any[]): any[] {
array.sort(this._randomCompare);
return array;
}
/**
* 从序列中随机取一个元素
* @param sequence 可以是 数组、 vector等只要是有length属性并且可以用数字索引获取元素的对象
* 另外,字符串也是允许的。
* @return 序列中的某一个元素
*
*/
public static choice(sequence: any): any {
if (!sequence.hasOwnProperty("length"))
throw new Error('无法对此对象执行此操作');
let index: number = 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
*
*/
public static sample(sequence: any[], num: number): any[] {
let len: number = sequence.length;
if (num <= 0 || len < num)
throw new Error("采样数量不够");
let selected: any[] = [];
let indices: any[] = [];
for (let i: number = 0; i < num; i++) {
let index: number = 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()
*
*/
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;
}
}
}