Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 1,422 Bytes
63858e7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import * as _ from 'lodash'
function ascOrder(n1, n2) {
if (n1 < n2) {
return -1;
}
else if (n1 > n2) {
return 1;
}
return 0;
}
export {findAllIndexes, insertAt_, orderedInsert_, set2SortedArray}
/**
* Find all indexes that match a particular predicate
*/
function findAllIndexes<T>(array:Array<T>, predicate:(a:T) => boolean): number[] {
let fromIndex=0;
let results = [];
let i = _.findIndex(array, predicate, fromIndex);
while (i != -1) {
results.push(i);
i = _.findIndex(array, predicate, i+1)
}
return results;
};
function insertAt_<T>(array:Array<T>, val:T, ind:number):Array<T> {
array.splice(ind, 0, val);
return array
}
/**
* Convert a set to an ordered array
*/
function set2SortedArray<T>(input:Set<T>):Array<T> {
return Array.from(input).sort(ascOrder)
}
/**
* Insert a value into array in sorted order IN PLACE
*
* WARNING: Only handles numbers, sorted from least to greatest
* - Assumes already sorted array
*/
function orderedInsert_<T>(array:Array<T>, val:T, coldstart=false):Array<T> {
// Resort array if desired
if (coldstart) {
array.sort(ascOrder)
}
const ind = _.sortedIndex(array, val);
return insertAt_(array, val, ind)
}
export function makeRandom(len:number){
const a:number[] = new Array(len).fill(0)
return a.map((x) => {return _.random(-5, 5, true)})
} |