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(array:Array, 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_(array:Array, val:T, ind:number):Array { array.splice(ind, 0, val); return array } /** * Convert a set to an ordered array */ function set2SortedArray(input:Set):Array { 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_(array:Array, val:T, coldstart=false):Array { // 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)}) }