move functions from math.ts to collections.ts
This commit is contained in:
@@ -1,3 +1,20 @@
|
||||
function union<T>(array0: T[], array1: T[]) {
|
||||
const set = new Set([...array0, ...array1]);
|
||||
return [...set];
|
||||
}
|
||||
|
||||
function uniq(sortedArray: any[]) {
|
||||
const filtered = [];
|
||||
let lastItem;
|
||||
for (const item of sortedArray) {
|
||||
if (item !== lastItem) {
|
||||
filtered.push(item);
|
||||
lastItem = item;
|
||||
}
|
||||
}
|
||||
return filtered;
|
||||
}
|
||||
|
||||
function mapGetOrInit<K, V>(map: Map<K, V>, key: K, defaultItem: V) {
|
||||
const item = map.get(key);
|
||||
if (item !== undefined) {
|
||||
@@ -7,3 +24,16 @@ function mapGetOrInit<K, V>(map: Map<K, V>, key: K, defaultItem: V) {
|
||||
return defaultItem;
|
||||
}
|
||||
}
|
||||
|
||||
function findMinPositive<T>(items: T[], evaluate: (item: T) => number) {
|
||||
let bestScore = Infinity;
|
||||
let bestItem = undefined;
|
||||
for (const item of items) {
|
||||
const score = evaluate(item);
|
||||
if (score > 0 && score < bestScore) {
|
||||
bestScore = score;
|
||||
bestItem = item;
|
||||
}
|
||||
}
|
||||
return bestItem;
|
||||
}
|
||||
|
||||
@@ -12,36 +12,6 @@ function sum(...list: number[]) {
|
||||
return list.reduce((acc, val) => acc + val);
|
||||
}
|
||||
|
||||
function union<T>(array0: T[], array1: T[]) {
|
||||
const set = new Set([...array0, ...array1]);
|
||||
return [...set];
|
||||
}
|
||||
|
||||
function uniq<T>(sortedArray: T[]) {
|
||||
const filtered = [];
|
||||
let lastItem;
|
||||
for (const item of sortedArray) {
|
||||
if (item !== lastItem) {
|
||||
filtered.push(item);
|
||||
lastItem = item;
|
||||
}
|
||||
}
|
||||
return filtered;
|
||||
}
|
||||
|
||||
function findMinPositive<T>(items: T[], evaluate: (item: T) => number) {
|
||||
let bestScore = Infinity;
|
||||
let bestItem = undefined;
|
||||
for (const item of items) {
|
||||
const score = evaluate(item);
|
||||
if (score > 0 && score < bestScore) {
|
||||
bestScore = score;
|
||||
bestItem = item;
|
||||
}
|
||||
}
|
||||
return bestItem;
|
||||
}
|
||||
|
||||
function rectEquals(a: QmlRect, b: QmlRect) {
|
||||
return a.x === b.x &&
|
||||
a.y === b.y &&
|
||||
|
||||
Reference in New Issue
Block a user