@thi.ng/arrays
Advanced tools
Comparing version 0.4.0 to 0.5.0
import { Fn3, TypedArray } from "@thi.ng/api"; | ||
export declare type AnyArray = any[] | TypedArray; | ||
export declare type SwapFn = Fn3<AnyArray, number, number, void>; | ||
//# sourceMappingURL=api.d.ts.map |
import { Fn } from "@thi.ng/api"; | ||
/** | ||
* Returns the supposed index of `x` in pre-sorted array-like collection | ||
* `buf`. If `x` can't be found, returns `-index-1`, representing the | ||
* negative of the index, were `x` to be inserted into `buf`. E.g if the | ||
* return value is -3, `x` would appear/insert at index 2. | ||
* `buf`. | ||
* | ||
* ``` | ||
* @remarks | ||
* If `x` can't be found, returns `-index-1`, representing the negative | ||
* of the index, were `x` to be inserted into `buf`. E.g if the return | ||
* value is -3, `x` would appear/insert at index 2. | ||
* | ||
* The optional `key` function is used to obtain the actual sort value | ||
* of `x` and each array item (default: identity). | ||
* | ||
* The optional `cmp` comparator (default: | ||
* {@link @thi.ng/compare#compare}) is then used to identify the index | ||
* of `x`. The sort order of `buf` MUST be compatible with that of | ||
* `cmp`. | ||
* | ||
* @example | ||
* ```ts | ||
* binarySearch([2, 4, 6], 5); | ||
@@ -13,24 +25,76 @@ * // -3 | ||
* | ||
* The optional `key` function is used to obtain the actual sort value | ||
* of `x` and each array item (default: identity). | ||
* @param buf - array | ||
* @param x - search value | ||
* @param key - key function | ||
* @param cmp - comparator | ||
* @param low - min index | ||
* @param high - max index | ||
*/ | ||
export declare const binarySearch: <A, B>(buf: ArrayLike<A>, x: A, key?: Fn<A, B>, cmp?: import("@thi.ng/api").Fn2<B, B, number>, low?: number, high?: number) => number; | ||
/** | ||
* Similar to {@link binarySearch}, but optimized for numeric arrays and | ||
* supporting custom comparators (default: | ||
* {@link @thi.ng/compare#compareNumAsc}). | ||
* | ||
* The optional `cmp` comparator (default: thi.ng/compare) is then used | ||
* to identify the index of `x`. The sort order of `buf` MUST be | ||
* compatible with that of `cmp`. | ||
* @param buf - array | ||
* @param x - search value | ||
* @param cmp - comparator | ||
* @param low - min index | ||
* @param high - max index | ||
*/ | ||
export declare const binarySearchNumeric: (buf: ArrayLike<number>, x: number, cmp?: import("@thi.ng/api").Fn2<number, number, number>, low?: number, high?: number) => number; | ||
/** | ||
* {@link binarySearch} result index classifier for predecessor queries. | ||
* Returns index of last item less than search value or -1 if no such | ||
* values exist. | ||
* | ||
* @param buf | ||
* @param x | ||
* @param key | ||
* @param cmp | ||
* @example | ||
* ```ts | ||
* bsLT(binarySearch([10, 20, 30, 40], 25)) | ||
* // 1 | ||
* ``` | ||
* | ||
* @param i - binarySearch result index | ||
*/ | ||
export declare const binarySearch: <A, B>(buf: ArrayLike<A>, x: A, key?: Fn<A, B>, cmp?: import("@thi.ng/api").Fn2<B, B, number>) => number; | ||
export declare const bsLT: (i: number) => number; | ||
/** | ||
* Similar to `binarySearch()`, but optimized for numeric arrays and | ||
* supporting custom comparators (default: `compareNumAsc` from | ||
* thi.ng/compare pkg). | ||
* Similar to {@link bsLT}, but for less-than-equals queries. | ||
* | ||
* @param buf | ||
* @param x | ||
* @param cmp | ||
* @param i - binarySearch result index | ||
*/ | ||
export declare const binarySearchNumeric: (buf: ArrayLike<number>, x: number, cmp?: import("@thi.ng/api").Fn2<number, number, number>) => number; | ||
export declare const bsLE: (i: number) => number; | ||
/** | ||
* {@link binarySearch} result index classifier for successor queries. | ||
* Returns index of first item greater than search value or -1 if no | ||
* such values exist. | ||
* | ||
* @example | ||
* ```ts | ||
* src = [10, 20, 30, 40]; | ||
* | ||
* bsGT(binarySearch(src, 25), src.length) | ||
* // 2 | ||
* | ||
* bsGT(binarySearch(src, 40), src.length) | ||
* // -1 | ||
* ``` | ||
* | ||
* @param i - binarySearch result index | ||
* @param n - array length | ||
*/ | ||
export declare const bsGT: (i: number, n: number) => number; | ||
/** | ||
* Similar to {@link bsGT}, but for greater-than-equals queries. | ||
* | ||
* @param i - binarySearch result index | ||
* @param n - array length | ||
*/ | ||
export declare const bsGE: (i: number, n: number) => number; | ||
/** | ||
* {@link binarySearch} result index classifier for equals queries. | ||
* Merely syntax sugar, casting any non-found result indices to -1. | ||
* | ||
* @param i - binarySearch result index | ||
*/ | ||
export declare const bsEQ: (i: number) => number; | ||
//# sourceMappingURL=binary-search.d.ts.map |
import { compare, compareNumAsc } from "@thi.ng/compare"; | ||
/** | ||
* Returns the supposed index of `x` in pre-sorted array-like collection | ||
* `buf`. If `x` can't be found, returns `-index-1`, representing the | ||
* negative of the index, were `x` to be inserted into `buf`. E.g if the | ||
* return value is -3, `x` would appear/insert at index 2. | ||
* `buf`. | ||
* | ||
* ``` | ||
* binarySearch([2, 4, 6], 5); | ||
* // -3 | ||
* ``` | ||
* @remarks | ||
* If `x` can't be found, returns `-index-1`, representing the negative | ||
* of the index, were `x` to be inserted into `buf`. E.g if the return | ||
* value is -3, `x` would appear/insert at index 2. | ||
* | ||
@@ -16,15 +14,22 @@ * The optional `key` function is used to obtain the actual sort value | ||
* | ||
* The optional `cmp` comparator (default: thi.ng/compare) is then used | ||
* to identify the index of `x`. The sort order of `buf` MUST be | ||
* compatible with that of `cmp`. | ||
* The optional `cmp` comparator (default: | ||
* {@link @thi.ng/compare#compare}) is then used to identify the index | ||
* of `x`. The sort order of `buf` MUST be compatible with that of | ||
* `cmp`. | ||
* | ||
* @param buf | ||
* @param x | ||
* @param key | ||
* @param cmp | ||
* @example | ||
* ```ts | ||
* binarySearch([2, 4, 6], 5); | ||
* // -3 | ||
* ``` | ||
* | ||
* @param buf - array | ||
* @param x - search value | ||
* @param key - key function | ||
* @param cmp - comparator | ||
* @param low - min index | ||
* @param high - max index | ||
*/ | ||
export const binarySearch = (buf, x, key = (x) => x, cmp = compare) => { | ||
export const binarySearch = (buf, x, key = (x) => x, cmp = compare, low = 0, high = buf.length - 1) => { | ||
const kx = key(x); | ||
let low = 0; | ||
let high = buf.length - 1; | ||
while (low <= high) { | ||
@@ -46,13 +51,13 @@ const mid = (low + high) >>> 1; | ||
/** | ||
* Similar to `binarySearch()`, but optimized for numeric arrays and | ||
* supporting custom comparators (default: `compareNumAsc` from | ||
* thi.ng/compare pkg). | ||
* Similar to {@link binarySearch}, but optimized for numeric arrays and | ||
* supporting custom comparators (default: | ||
* {@link @thi.ng/compare#compareNumAsc}). | ||
* | ||
* @param buf | ||
* @param x | ||
* @param cmp | ||
* @param buf - array | ||
* @param x - search value | ||
* @param cmp - comparator | ||
* @param low - min index | ||
* @param high - max index | ||
*/ | ||
export const binarySearchNumeric = (buf, x, cmp = compareNumAsc) => { | ||
let low = 0; | ||
let high = buf.length - 1; | ||
export const binarySearchNumeric = (buf, x, cmp = compareNumAsc, low = 0, high = buf.length - 1) => { | ||
while (low <= high) { | ||
@@ -73,1 +78,55 @@ const mid = (low + high) >>> 1; | ||
}; | ||
/** | ||
* {@link binarySearch} result index classifier for predecessor queries. | ||
* Returns index of last item less than search value or -1 if no such | ||
* values exist. | ||
* | ||
* @example | ||
* ```ts | ||
* bsLT(binarySearch([10, 20, 30, 40], 25)) | ||
* // 1 | ||
* ``` | ||
* | ||
* @param i - binarySearch result index | ||
*/ | ||
export const bsLT = (i) => (i < 0 ? -i - 2 : i - 1); | ||
/** | ||
* Similar to {@link bsLT}, but for less-than-equals queries. | ||
* | ||
* @param i - binarySearch result index | ||
*/ | ||
export const bsLE = (i) => (i < 0 ? -i - 2 : i); | ||
/** | ||
* {@link binarySearch} result index classifier for successor queries. | ||
* Returns index of first item greater than search value or -1 if no | ||
* such values exist. | ||
* | ||
* @example | ||
* ```ts | ||
* src = [10, 20, 30, 40]; | ||
* | ||
* bsGT(binarySearch(src, 25), src.length) | ||
* // 2 | ||
* | ||
* bsGT(binarySearch(src, 40), src.length) | ||
* // -1 | ||
* ``` | ||
* | ||
* @param i - binarySearch result index | ||
* @param n - array length | ||
*/ | ||
export const bsGT = (i, n) => ((i = i < 0 ? -i - 1 : i + 1), i < n ? i : -1); | ||
/** | ||
* Similar to {@link bsGT}, but for greater-than-equals queries. | ||
* | ||
* @param i - binarySearch result index | ||
* @param n - array length | ||
*/ | ||
export const bsGE = (i, n) => ((i = i < 0 ? -i - 1 : i), i < n ? i : -1); | ||
/** | ||
* {@link binarySearch} result index classifier for equals queries. | ||
* Merely syntax sugar, casting any non-found result indices to -1. | ||
* | ||
* @param i - binarySearch result index | ||
*/ | ||
export const bsEQ = (i) => (i < 0 ? -1 : i); |
@@ -6,2 +6,13 @@ # Change Log | ||
# [0.5.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/arrays@0.4.0...@thi.ng/arrays@0.5.0) (2020-01-24) | ||
### Features | ||
* **arrays:** add binary search predicates, tests, update readme ([b8f421e](https://github.com/thi-ng/umbrella/commit/b8f421eb8888fa1b57a9287f6841cd29952bf19f)) | ||
# [0.4.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/arrays@0.3.0...@thi.ng/arrays@0.4.0) (2019-11-30) | ||
@@ -8,0 +19,0 @@ |
/** | ||
* Returns true if the last items of `buf` are the same items as in | ||
* `needle`. This means `buf` should have at least the same length as | ||
* `needle` for this to be true. | ||
* `needle`. | ||
* | ||
* By default, uses thi.ng/equiv for equality checking. | ||
* @remarks | ||
* This means `buf` should have at least the same length as `needle` for | ||
* this to be true. | ||
* | ||
* @see startsWith | ||
* By default, uses {@link @thi.ng/equiv#equiv} for equality checking. | ||
* | ||
* @param buf | ||
* @param needle | ||
* @param equiv | ||
* {@link startsWith} | ||
* | ||
* @param buf - array | ||
* @param needle - search values (array) | ||
* @param equiv - equivalence predicate | ||
*/ | ||
export declare const endsWith: <T>(buf: ArrayLike<T>, needle: ArrayLike<T>, equiv?: (a: any, b: any) => boolean) => boolean; | ||
//# sourceMappingURL=ends-with.d.ts.map |
import { equiv as _eq } from "@thi.ng/equiv"; | ||
/** | ||
* Returns true if the last items of `buf` are the same items as in | ||
* `needle`. This means `buf` should have at least the same length as | ||
* `needle` for this to be true. | ||
* `needle`. | ||
* | ||
* By default, uses thi.ng/equiv for equality checking. | ||
* @remarks | ||
* This means `buf` should have at least the same length as `needle` for | ||
* this to be true. | ||
* | ||
* @see startsWith | ||
* By default, uses {@link @thi.ng/equiv#equiv} for equality checking. | ||
* | ||
* @param buf | ||
* @param needle | ||
* @param equiv | ||
* {@link startsWith} | ||
* | ||
* @param buf - array | ||
* @param needle - search values (array) | ||
* @param equiv - equivalence predicate | ||
*/ | ||
@@ -15,0 +18,0 @@ export const endsWith = (buf, needle, equiv = _eq) => { |
/** | ||
* Helper function to avoid unnecessary copying if `x` is already an | ||
* array. First checks if `x` is an array and if so returns it. Else | ||
* attempts to obtain an iterator from `x` and if successful collects it | ||
* as array and returns it. Throws error if `x` isn't iterable. | ||
* array. | ||
* | ||
* @param x | ||
* @remarks | ||
* First checks if `x` is an array and if so returns it. Else attempts | ||
* to obtain an iterator from `x` and if successful collects it as array | ||
* and returns it. Throws error if `x` isn't iterable. | ||
* | ||
* @param x - | ||
*/ | ||
export declare const ensureArray: (x: any) => any[]; | ||
/** | ||
* Similar to `ensureArray()`, but for `ArrayLike` types. | ||
* Similar to {@link ensureArray}, but for `ArrayLike` types. | ||
* | ||
* @see ensureArray | ||
* {@link ensureArray} | ||
* | ||
* @param x | ||
* @param x - | ||
*/ | ||
export declare const ensureArrayLike: (x: any) => ArrayLike<any>; | ||
//# sourceMappingURL=ensure-array.d.ts.map |
@@ -5,16 +5,19 @@ import { isArray, isArrayLike } from "@thi.ng/checks"; | ||
* Helper function to avoid unnecessary copying if `x` is already an | ||
* array. First checks if `x` is an array and if so returns it. Else | ||
* attempts to obtain an iterator from `x` and if successful collects it | ||
* as array and returns it. Throws error if `x` isn't iterable. | ||
* array. | ||
* | ||
* @param x | ||
* @remarks | ||
* First checks if `x` is an array and if so returns it. Else attempts | ||
* to obtain an iterator from `x` and if successful collects it as array | ||
* and returns it. Throws error if `x` isn't iterable. | ||
* | ||
* @param x - | ||
*/ | ||
export const ensureArray = (x) => isArray(x) ? x : [...ensureIterable(x)]; | ||
/** | ||
* Similar to `ensureArray()`, but for `ArrayLike` types. | ||
* Similar to {@link ensureArray}, but for `ArrayLike` types. | ||
* | ||
* @see ensureArray | ||
* {@link ensureArray} | ||
* | ||
* @param x | ||
* @param x - | ||
*/ | ||
export const ensureArrayLike = (x) => isArrayLike(x) ? x : [...ensureIterable(x)]; |
@@ -5,4 +5,5 @@ /** | ||
* | ||
* @param x | ||
* @param x - | ||
*/ | ||
export declare const ensureIterable: (x: any) => Iterable<any>; | ||
//# sourceMappingURL=ensure-iterable.d.ts.map |
@@ -6,3 +6,3 @@ import { illegalArgs } from "@thi.ng/errors"; | ||
* | ||
* @param x | ||
* @param x - | ||
*/ | ||
@@ -9,0 +9,0 @@ export const ensureIterable = (x) => { |
/** | ||
* Similar to `Array.find()`, but uses thi.ng/equiv as default | ||
* predicate. | ||
* Similar to `Array.find()`, but uses {@link @thi.ng/equiv#equiv} as | ||
* default predicate. | ||
* | ||
* @param src | ||
* @param x | ||
* @param equiv | ||
* @param buf - array | ||
* @param x - search value | ||
* @param equiv - equivalence predicate | ||
*/ | ||
export declare const find: <T>(src: ArrayLike<T>, x: T, equiv?: import("@thi.ng/api").Fn2<T, T, boolean>) => T | undefined; | ||
export declare const find: <T>(buf: ArrayLike<T>, x: T, equiv?: import("@thi.ng/api").Fn2<T, T, boolean>) => T | undefined; | ||
/** | ||
* Similar to `Array.findIndex()`, but uses thi.ng/equiv as default | ||
* predicate. | ||
* Similar to `Array.findIndex()`, but uses {@link @thi.ng/equiv#equiv} | ||
* as default predicate. | ||
* | ||
* @param src | ||
* @param x | ||
* @param equiv | ||
* @param buf - array | ||
* @param x - search value | ||
* @param equiv - equivalence predicate | ||
*/ | ||
export declare const findIndex: <T>(src: ArrayLike<T>, x: T, equiv?: import("@thi.ng/api").Fn2<T, T, boolean>) => number; | ||
export declare const findIndex: <T>(buf: ArrayLike<T>, x: T, equiv?: import("@thi.ng/api").Fn2<T, T, boolean>) => number; | ||
//# sourceMappingURL=find.d.ts.map |
32
find.js
import { equiv as _equiv } from "@thi.ng/equiv"; | ||
/** | ||
* Similar to `Array.find()`, but uses thi.ng/equiv as default | ||
* predicate. | ||
* Similar to `Array.find()`, but uses {@link @thi.ng/equiv#equiv} as | ||
* default predicate. | ||
* | ||
* @param src | ||
* @param x | ||
* @param equiv | ||
* @param buf - array | ||
* @param x - search value | ||
* @param equiv - equivalence predicate | ||
*/ | ||
export const find = (src, x, equiv = _equiv) => { | ||
const i = findIndex(src, x, equiv); | ||
return i !== -1 ? src[i] : undefined; | ||
export const find = (buf, x, equiv = _equiv) => { | ||
const i = findIndex(buf, x, equiv); | ||
return i !== -1 ? buf[i] : undefined; | ||
}; | ||
/** | ||
* Similar to `Array.findIndex()`, but uses thi.ng/equiv as default | ||
* predicate. | ||
* Similar to `Array.findIndex()`, but uses {@link @thi.ng/equiv#equiv} | ||
* as default predicate. | ||
* | ||
* @param src | ||
* @param x | ||
* @param equiv | ||
* @param buf - array | ||
* @param x - search value | ||
* @param equiv - equivalence predicate | ||
*/ | ||
export const findIndex = (src, x, equiv = _equiv) => { | ||
for (let i = src.length; --i >= 0;) { | ||
if (equiv(x, src[i])) | ||
export const findIndex = (buf, x, equiv = _equiv) => { | ||
for (let i = buf.length; --i >= 0;) { | ||
if (equiv(x, buf[i])) | ||
return i; | ||
@@ -26,0 +26,0 @@ } |
@@ -5,14 +5,16 @@ /** | ||
* | ||
* The optional `equiv` predicate can be used to customize | ||
* item equality checking. Uses @thi.ng/equiv by default. | ||
* @remarks | ||
* The optional `equiv` predicate can be used to customize item equality | ||
* checking. Uses {@link @thi.ng/equiv#equiv} by default. | ||
* | ||
* Adapted and generalized from: | ||
* https://github.com/bevacqua/fufuzzyzzysearch (MIT) | ||
* {@link https://github.com/bevacqua/fufuzzyzzysearch} (MIT) | ||
* | ||
* @see thi.ng/transducers/xform/filterFuzzy | ||
* {@link @thi.ng/transducers#(filterFuzzy:1)} | ||
* | ||
* @param domain | ||
* @param query | ||
* @param equiv | ||
* @param domain - array | ||
* @param query - search value | ||
* @param equiv - equivalence predicate | ||
*/ | ||
export declare const fuzzyMatch: <T>(domain: ArrayLike<T>, query: ArrayLike<T>, equiv?: import("@thi.ng/api").Fn2<any, any, boolean>) => boolean; | ||
//# sourceMappingURL=fuzzy-match.d.ts.map |
@@ -6,13 +6,14 @@ import { equiv as _eq } from "@thi.ng/equiv"; | ||
* | ||
* The optional `equiv` predicate can be used to customize | ||
* item equality checking. Uses @thi.ng/equiv by default. | ||
* @remarks | ||
* The optional `equiv` predicate can be used to customize item equality | ||
* checking. Uses {@link @thi.ng/equiv#equiv} by default. | ||
* | ||
* Adapted and generalized from: | ||
* https://github.com/bevacqua/fufuzzyzzysearch (MIT) | ||
* {@link https://github.com/bevacqua/fufuzzyzzysearch} (MIT) | ||
* | ||
* @see thi.ng/transducers/xform/filterFuzzy | ||
* {@link @thi.ng/transducers#(filterFuzzy:1)} | ||
* | ||
* @param domain | ||
* @param query | ||
* @param equiv | ||
* @param domain - array | ||
* @param query - search value | ||
* @param equiv - equivalence predicate | ||
*/ | ||
@@ -19,0 +20,0 @@ export const fuzzyMatch = (domain, query, equiv = _eq) => { |
@@ -15,1 +15,2 @@ export * from "./binary-search"; | ||
export * from "./swizzle"; | ||
//# sourceMappingURL=index.d.ts.map |
/** | ||
* Returns true if the given array and its elements in the selected | ||
* index range (entire array, by default) are in the order defined by | ||
* the given comparator (thi.ng/compare by default). Always returns | ||
* true, if effective index range (or array length) has less than two | ||
* elements. No bounds checking. | ||
* the given comparator ({@link @thi.ng/compare#compare} by default). | ||
* | ||
* ``` | ||
* @remarks | ||
* Always returns true, if effective index range (or array length) has | ||
* less than two elements. No bounds checking. | ||
* | ||
* @example | ||
* ```ts | ||
* isSorted([3, 2, 1]) | ||
@@ -17,7 +20,8 @@ * // false | ||
* | ||
* @param arr | ||
* @param cmp | ||
* @param start | ||
* @param end | ||
* @param arr - array | ||
* @param cmp - comparator | ||
* @param start - start index | ||
* @param end - end index | ||
*/ | ||
export declare const isSorted: <T>(arr: ArrayLike<T>, cmp?: import("@thi.ng/api").Fn2<T, T, number>, start?: number, end?: number) => boolean; | ||
//# sourceMappingURL=is-sorted.d.ts.map |
@@ -5,7 +5,10 @@ import { compare } from "@thi.ng/compare"; | ||
* index range (entire array, by default) are in the order defined by | ||
* the given comparator (thi.ng/compare by default). Always returns | ||
* true, if effective index range (or array length) has less than two | ||
* elements. No bounds checking. | ||
* the given comparator ({@link @thi.ng/compare#compare} by default). | ||
* | ||
* ``` | ||
* @remarks | ||
* Always returns true, if effective index range (or array length) has | ||
* less than two elements. No bounds checking. | ||
* | ||
* @example | ||
* ```ts | ||
* isSorted([3, 2, 1]) | ||
@@ -19,6 +22,6 @@ * // false | ||
* | ||
* @param arr | ||
* @param cmp | ||
* @param start | ||
* @param end | ||
* @param arr - array | ||
* @param cmp - comparator | ||
* @param start - start index | ||
* @param end - end index | ||
*/ | ||
@@ -25,0 +28,0 @@ export const isSorted = (arr, cmp = compare, start = 0, end = arr.length) => { |
@@ -16,1 +16,2 @@ import { Nullable } from "@thi.ng/api"; | ||
export declare function arrayIterator<T>(buf: Nullable<ArrayLike<T>>, start?: number, end?: number): Generator<T, void, unknown>; | ||
//# sourceMappingURL=iterator.d.ts.map |
@@ -12,6 +12,4 @@ 'use strict'; | ||
const binarySearch = (buf, x, key = (x) => x, cmp = compare.compare) => { | ||
const binarySearch = (buf, x, key = (x) => x, cmp = compare.compare, low = 0, high = buf.length - 1) => { | ||
const kx = key(x); | ||
let low = 0; | ||
let high = buf.length - 1; | ||
while (low <= high) { | ||
@@ -32,5 +30,3 @@ const mid = (low + high) >>> 1; | ||
}; | ||
const binarySearchNumeric = (buf, x, cmp = compare.compareNumAsc) => { | ||
let low = 0; | ||
let high = buf.length - 1; | ||
const binarySearchNumeric = (buf, x, cmp = compare.compareNumAsc, low = 0, high = buf.length - 1) => { | ||
while (low <= high) { | ||
@@ -51,2 +47,7 @@ const mid = (low + high) >>> 1; | ||
}; | ||
const bsLT = (i) => (i < 0 ? -i - 2 : i - 1); | ||
const bsLE = (i) => (i < 0 ? -i - 2 : i); | ||
const bsGT = (i, n) => ((i = i < 0 ? -i - 1 : i + 1), i < n ? i : -1); | ||
const bsGE = (i, n) => ((i = i < 0 ? -i - 1 : i), i < n ? i : -1); | ||
const bsEQ = (i) => (i < 0 ? -1 : i); | ||
@@ -71,9 +72,9 @@ const endsWith = (buf, needle, equiv$1 = equiv.equiv) => { | ||
const find = (src, x, equiv$1 = equiv.equiv) => { | ||
const i = findIndex(src, x, equiv$1); | ||
return i !== -1 ? src[i] : undefined; | ||
const find = (buf, x, equiv$1 = equiv.equiv) => { | ||
const i = findIndex(buf, x, equiv$1); | ||
return i !== -1 ? buf[i] : undefined; | ||
}; | ||
const findIndex = (src, x, equiv$1 = equiv.equiv) => { | ||
for (let i = src.length; --i >= 0;) { | ||
if (equiv$1(x, src[i])) | ||
const findIndex = (buf, x, equiv$1 = equiv.equiv) => { | ||
for (let i = buf.length; --i >= 0;) { | ||
if (equiv$1(x, buf[i])) | ||
return i; | ||
@@ -127,3 +128,3 @@ } | ||
const peek = (x) => x[x.length - 1]; | ||
const peek = (buf) => buf[buf.length - 1]; | ||
@@ -251,2 +252,7 @@ const swap = (arr, x, y) => { | ||
exports.binarySearchNumeric = binarySearchNumeric; | ||
exports.bsEQ = bsEQ; | ||
exports.bsGE = bsGE; | ||
exports.bsGT = bsGT; | ||
exports.bsLE = bsLE; | ||
exports.bsLT = bsLT; | ||
exports.endsWith = endsWith; | ||
@@ -253,0 +259,0 @@ exports.ensureArray = ensureArray; |
@@ -1,1 +0,1 @@ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@thi.ng/compare"),require("@thi.ng/equiv"),require("@thi.ng/checks"),require("@thi.ng/errors"),require("@thi.ng/api"),require("@thi.ng/random")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/compare","@thi.ng/equiv","@thi.ng/checks","@thi.ng/errors","@thi.ng/api","@thi.ng/random"],r):r(((e=e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.arrays={}),e.thi.ng.compare,e.thi.ng.equiv,e.thi.ng.checks,e.thi.ng.errors,e.thi.ng.api,e.thi.ng.random)}(this,(function(e,r,t,n,i,u,o){"use strict";const s=e=>((null==e||!e[Symbol.iterator])&&i.illegalArgs(`value is not iterable: ${e}`),e),c=(e,r,n=t.equiv)=>{for(let t=e.length;--t>=0;)if(n(r,e[t]))return t;return-1};const a=(e,r,t)=>{const n=e[r];e[r]=e[t],e[t]=n};const l=(e,r=0,t=e.length,n=o.SYSTEM)=>{u.assert(r>=0&&t>=r&&t<=e.length,`illegal range ${r}..${t}`);let i=t-r;const s=i;if(s>1)for(;--i>=0;){const t=r+n.float(s)|0,i=r+n.float(s)|0,u=e[t];e[t]=e[i],e[i]=u}return e};e.arrayIterator=function*(e,r=0,t){if(!e)return;r=r,void 0===t&&(t=e.length);const n=r<=t?1:-1;for(;r!==t;r+=n)yield e[r]},e.binarySearch=(e,t,n=(e=>e),i=r.compare)=>{const u=n(t);let o=0,s=e.length-1;for(;o<=s;){const r=o+s>>>1,t=i(n(e[r]),u);if(t<0)o=r+1;else{if(!(t>0))return r;s=r-1}}return-o-1},e.binarySearchNumeric=(e,t,n=r.compareNumAsc)=>{let i=0,u=e.length-1;for(;i<=u;){const r=i+u>>>1,o=n(e[r],t);if(o<0)i=r+1;else{if(!(o>0))return r;u=r-1}}return-i-1},e.endsWith=(e,r,n=t.equiv)=>{let i=e.length,u=r.length;if(i<u)return!1;for(;--i,--u>=0&&n(e[i],r[u]););return u<0},e.ensureArray=e=>n.isArray(e)?e:[...s(e)],e.ensureArrayLike=e=>n.isArrayLike(e)?e:[...s(e)],e.ensureIterable=s,e.find=(e,r,n=t.equiv)=>{const i=c(e,r,n);return-1!==i?e[i]:void 0},e.findIndex=c,e.fuzzyMatch=(e,r,n=t.equiv)=>{const i=e.length,u=r.length;if(u>i)return!1;if(u===i)return n(r,e);e:for(let t=0,o=0;t<u;t++){const u=r[t];for(;o<i;)if(n(e[o++],u))continue e;return!1}return!0},e.isSorted=(e,t=r.compare,n=0,i=e.length)=>{let u=e[n];for(;++n<i;){const r=e[n];if(t(u,r)>0)return!1;u=r}return!0},e.multiSwap=(...e)=>{const[r,t,n]=e,i=e.length;switch(i){case 0:return a;case 1:return(e,t,n)=>{a(e,t,n),a(r,t,n)};case 2:return(e,n,i)=>{a(e,n,i),a(r,n,i),a(t,n,i)};case 3:return(e,i,u)=>{a(e,i,u),a(r,i,u),a(t,i,u),a(n,i,u)};default:return(r,t,n)=>{a(r,t,n);for(let r=i;--r>=0;)a(e[r],t,n)}}},e.peek=e=>e[e.length-1],e.quickSort=function e(t,n=r.compare,i=a,u=0,o=t.length-1){if(u<o){const r=t[u+(o-u>>1)];let s=u-1,c=o+1;for(;;){do{s++}while(n(t[s],r)<0);do{c--}while(n(t[c],r)>0);if(s>=c)break;i(t,s,c)}e(t,n,i,u,c),e(t,n,i,c+1,o)}return t},e.shuffle=(e,r=e.length,t=o.SYSTEM)=>l(e,0,r,t),e.shuffleRange=l,e.startsWith=(e,r,n=t.equiv)=>{let i=e.length,u=r.length;if(i<u)return!1;for(;-u>=0&&n(e[u],r[u]););return u<0},e.swap=a,e.swizzle=e=>{const[r,t,n,i,u,o,s,c]=e;switch(e.length){case 0:return()=>[];case 1:return e=>[e[r]];case 2:return e=>[e[r],e[t]];case 3:return e=>[e[r],e[t],e[n]];case 4:return e=>[e[r],e[t],e[n],e[i]];case 5:return e=>[e[r],e[t],e[n],e[i],e[u]];case 6:return e=>[e[r],e[t],e[n],e[i],e[u],e[o]];case 7:return e=>[e[r],e[t],e[n],e[i],e[u],e[o],e[s]];case 8:return e=>[e[r],e[t],e[n],e[i],e[u],e[o],e[s],e[c]];default:return r=>{const t=[];for(let n=e.length;--n>=0;)t[n]=r[e[n]];return t}}},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@thi.ng/compare"),require("@thi.ng/equiv"),require("@thi.ng/checks"),require("@thi.ng/errors"),require("@thi.ng/api"),require("@thi.ng/random")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/compare","@thi.ng/equiv","@thi.ng/checks","@thi.ng/errors","@thi.ng/api","@thi.ng/random"],r):r(((e=e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.arrays={}),e.thi.ng.compare,e.thi.ng.equiv,e.thi.ng.checks,e.thi.ng.errors,e.thi.ng.api,e.thi.ng.random)}(this,(function(e,r,t,n,i,u,s){"use strict";const o=e=>((null==e||!e[Symbol.iterator])&&i.illegalArgs(`value is not iterable: ${e}`),e),c=(e,r,n=t.equiv)=>{for(let t=e.length;--t>=0;)if(n(r,e[t]))return t;return-1};const a=(e,r,t)=>{const n=e[r];e[r]=e[t],e[t]=n};const h=(e,r=0,t=e.length,n=s.SYSTEM)=>{u.assert(r>=0&&t>=r&&t<=e.length,`illegal range ${r}..${t}`);let i=t-r;const o=i;if(o>1)for(;--i>=0;){const t=r+n.float(o)|0,i=r+n.float(o)|0,u=e[t];e[t]=e[i],e[i]=u}return e};e.arrayIterator=function*(e,r=0,t){if(!e)return;r=r,void 0===t&&(t=e.length);const n=r<=t?1:-1;for(;r!==t;r+=n)yield e[r]},e.binarySearch=(e,t,n=(e=>e),i=r.compare,u=0,s=e.length-1)=>{const o=n(t);for(;u<=s;){const r=u+s>>>1,t=i(n(e[r]),o);if(t<0)u=r+1;else{if(!(t>0))return r;s=r-1}}return-u-1},e.binarySearchNumeric=(e,t,n=r.compareNumAsc,i=0,u=e.length-1)=>{for(;i<=u;){const r=i+u>>>1,s=n(e[r],t);if(s<0)i=r+1;else{if(!(s>0))return r;u=r-1}}return-i-1},e.bsEQ=e=>e<0?-1:e,e.bsGE=(e,r)=>(e=e<0?-e-1:e)<r?e:-1,e.bsGT=(e,r)=>(e=e<0?-e-1:e+1)<r?e:-1,e.bsLE=e=>e<0?-e-2:e,e.bsLT=e=>e<0?-e-2:e-1,e.endsWith=(e,r,n=t.equiv)=>{let i=e.length,u=r.length;if(i<u)return!1;for(;--i,--u>=0&&n(e[i],r[u]););return u<0},e.ensureArray=e=>n.isArray(e)?e:[...o(e)],e.ensureArrayLike=e=>n.isArrayLike(e)?e:[...o(e)],e.ensureIterable=o,e.find=(e,r,n=t.equiv)=>{const i=c(e,r,n);return-1!==i?e[i]:void 0},e.findIndex=c,e.fuzzyMatch=(e,r,n=t.equiv)=>{const i=e.length,u=r.length;if(u>i)return!1;if(u===i)return n(r,e);e:for(let t=0,s=0;t<u;t++){const u=r[t];for(;s<i;)if(n(e[s++],u))continue e;return!1}return!0},e.isSorted=(e,t=r.compare,n=0,i=e.length)=>{let u=e[n];for(;++n<i;){const r=e[n];if(t(u,r)>0)return!1;u=r}return!0},e.multiSwap=(...e)=>{const[r,t,n]=e,i=e.length;switch(i){case 0:return a;case 1:return(e,t,n)=>{a(e,t,n),a(r,t,n)};case 2:return(e,n,i)=>{a(e,n,i),a(r,n,i),a(t,n,i)};case 3:return(e,i,u)=>{a(e,i,u),a(r,i,u),a(t,i,u),a(n,i,u)};default:return(r,t,n)=>{a(r,t,n);for(let r=i;--r>=0;)a(e[r],t,n)}}},e.peek=e=>e[e.length-1],e.quickSort=function e(t,n=r.compare,i=a,u=0,s=t.length-1){if(u<s){const r=t[u+(s-u>>1)];let o=u-1,c=s+1;for(;;){do{o++}while(n(t[o],r)<0);do{c--}while(n(t[c],r)>0);if(o>=c)break;i(t,o,c)}e(t,n,i,u,c),e(t,n,i,c+1,s)}return t},e.shuffle=(e,r=e.length,t=s.SYSTEM)=>h(e,0,r,t),e.shuffleRange=h,e.startsWith=(e,r,n=t.equiv)=>{let i=e.length,u=r.length;if(i<u)return!1;for(;-u>=0&&n(e[u],r[u]););return u<0},e.swap=a,e.swizzle=e=>{const[r,t,n,i,u,s,o,c]=e;switch(e.length){case 0:return()=>[];case 1:return e=>[e[r]];case 2:return e=>[e[r],e[t]];case 3:return e=>[e[r],e[t],e[n]];case 4:return e=>[e[r],e[t],e[n],e[i]];case 5:return e=>[e[r],e[t],e[n],e[i],e[u]];case 6:return e=>[e[r],e[t],e[n],e[i],e[u],e[s]];case 7:return e=>[e[r],e[t],e[n],e[i],e[u],e[s],e[o]];case 8:return e=>[e[r],e[t],e[n],e[i],e[u],e[s],e[o],e[c]];default:return r=>{const t=[];for(let n=e.length;--n>=0;)t[n]=r[e[n]];return t}}},Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@thi.ng/arrays", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Array / Arraylike utilities", | ||
@@ -26,21 +26,23 @@ "module": "./index.js", | ||
"doc": "node_modules/.bin/typedoc --mode modules --out doc src", | ||
"doc:ae": "mkdir -p .ae/doc .ae/temp && node_modules/.bin/api-extractor run --local --verbose", | ||
"pub": "yarn build:release && yarn publish --access public" | ||
}, | ||
"devDependencies": { | ||
"@istanbuljs/nyc-config-typescript": "^0.1.3", | ||
"@types/mocha": "^5.2.6", | ||
"@types/node": "^12.12.11", | ||
"mocha": "^6.2.2", | ||
"nyc": "^14.0.0", | ||
"ts-node": "^8.5.2", | ||
"typedoc": "^0.15.2", | ||
"typescript": "^3.7.2" | ||
"@istanbuljs/nyc-config-typescript": "^1.0.1", | ||
"@microsoft/api-extractor": "^7.7.7", | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^13.5.0", | ||
"mocha": "^7.0.0", | ||
"nyc": "^15.0.0", | ||
"ts-node": "^8.6.2", | ||
"typedoc": "^0.16.8", | ||
"typescript": "^3.7.5" | ||
}, | ||
"dependencies": { | ||
"@thi.ng/api": "^6.6.0", | ||
"@thi.ng/checks": "^2.4.2", | ||
"@thi.ng/compare": "^1.1.0", | ||
"@thi.ng/equiv": "^1.0.11", | ||
"@thi.ng/errors": "^1.2.2", | ||
"@thi.ng/random": "^1.1.14" | ||
"@thi.ng/api": "^6.7.0", | ||
"@thi.ng/checks": "^2.5.0", | ||
"@thi.ng/compare": "^1.1.1", | ||
"@thi.ng/equiv": "^1.0.12", | ||
"@thi.ng/errors": "^1.2.3", | ||
"@thi.ng/random": "^1.1.15" | ||
}, | ||
@@ -63,3 +65,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "36c4d9e967bd80ccdbfa0f4a42f594080f95f105" | ||
"gitHead": "93d8af817724c1c5b06d80ffa2492fe5b4fb7bc4" | ||
} |
/** | ||
* Returns last element of given array or `undefined` if array is empty. | ||
* | ||
* @param x | ||
* @param buf - array | ||
*/ | ||
export declare const peek: <T>(x: ArrayLike<T>) => T; | ||
export declare const peek: <T>(buf: ArrayLike<T>) => T; | ||
//# sourceMappingURL=peek.d.ts.map |
/** | ||
* Returns last element of given array or `undefined` if array is empty. | ||
* | ||
* @param x | ||
* @param buf - array | ||
*/ | ||
export const peek = (x) => x[x.length - 1]; | ||
export const peek = (buf) => buf[buf.length - 1]; |
@@ -5,12 +5,16 @@ import { Comparator, Fn3, TypedArray } from "@thi.ng/api"; | ||
* based swap function, useful for sorting multiple related arrays in | ||
* parallel, based on a single sort criteria. Supports sorting of | ||
* sub-ranges only, via optionally given `start`/`end` indices (both | ||
* inclusive). | ||
* parallel, based on a single sort criteria. | ||
* | ||
* Uses Hoare partitioning scheme. thi.ng/compare is used as default | ||
* comparator and `swap` from this package as default swap function. | ||
* @remarks | ||
* Supports sorting of sub-ranges only, via optionally given | ||
* `start`/`end` indices (both inclusive). | ||
* | ||
* https://en.wikipedia.org/wiki/Quicksort#Hoare_partition_scheme | ||
* Uses Hoare partitioning scheme. {@link @thi.ng/compare#compare} is | ||
* used as default comparator and {@link swap} from this package as | ||
* default swap function. Also see {@link multiSwap}. | ||
* | ||
* ``` | ||
* {@link https://en.wikipedia.org/wiki/Quicksort#Hoare_partition_scheme} | ||
* | ||
* @example | ||
* ```ts | ||
* a = [4, 3, 1, 8, 5] | ||
@@ -30,9 +34,10 @@ * b = [40, 30, 10, 80, 50] | ||
* | ||
* @param arr | ||
* @param _cmp | ||
* @param _swap | ||
* @param start | ||
* @param end | ||
* @param arr - array to sort | ||
* @param _cmp - comparator | ||
* @param _swap - swap function | ||
* @param start - start index | ||
* @param end - end index (inclusive) | ||
*/ | ||
export declare function quickSort<T>(arr: T[], _cmp?: Comparator<T>, _swap?: Fn3<T[], number, number, void>, start?: number, end?: number): T[]; | ||
export declare function quickSort<T extends TypedArray>(arr: T, _cmp?: Comparator<number>, _swap?: Fn3<T, number, number, void>, start?: number, end?: number): T; | ||
//# sourceMappingURL=quicksort.d.ts.map |
@@ -17,2 +17,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
- [API](#api) | ||
- [Binary search result predicates](#binary-search-result-predicates) | ||
- [Authors](#authors) | ||
@@ -35,10 +36,12 @@ - [License](#license) | ||
Package sizes (gzipped): ESM: 1.3KB / CJS: 1.4KB / UMD: 1.4KB | ||
## Dependencies | ||
- [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/master/packages/api) | ||
- [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/master/packages/checks) | ||
- [@thi.ng/compare](https://github.com/thi-ng/umbrella/tree/master/packages/compare) | ||
- [@thi.ng/equiv](https://github.com/thi-ng/umbrella/tree/master/packages/equiv) | ||
- [@thi.ng/errors](https://github.com/thi-ng/umbrella/tree/master/packages/errors) | ||
- [@thi.ng/random](https://github.com/thi-ng/umbrella/tree/master/packages/random) | ||
- [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api) | ||
- [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/develop/packages/checks) | ||
- [@thi.ng/compare](https://github.com/thi-ng/umbrella/tree/develop/packages/compare) | ||
- [@thi.ng/equiv](https://github.com/thi-ng/umbrella/tree/develop/packages/equiv) | ||
- [@thi.ng/errors](https://github.com/thi-ng/umbrella/tree/develop/packages/errors) | ||
- [@thi.ng/random](https://github.com/thi-ng/umbrella/tree/develop/packages/random) | ||
@@ -49,17 +52,45 @@ ## API | ||
- [binarySearch()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/binary-search.ts) | ||
- [endsWith()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/ends-with.ts) | ||
- [ensureArray()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/ensure-array.ts) | ||
- [ensureIterable()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/ensure-iterable.ts) | ||
- [fuzzyMatch()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/fuzzy-match.ts) | ||
- [isSorted()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/is-sorted.ts) | ||
- [multiSwap()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/swap.ts) | ||
- [peek()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/peek.ts) | ||
- [quickSort()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/quicksort.ts) | ||
- [shuffle()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/shuffle.ts) (w/ custom PRNG support) | ||
- [shuffleRange()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/shuffle.ts) (w/ custom PRNG support) | ||
- [startsWith()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/starts-with.ts) | ||
- [swap()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/swap.ts) | ||
- [swizzle()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/swizzle.ts) | ||
- [binarySearch()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/binary-search.ts) | ||
- [endsWith()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/ends-with.ts) | ||
- [ensureArray()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/ensure-array.ts) | ||
- [ensureIterable()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/ensure-iterable.ts) | ||
- [fuzzyMatch()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/fuzzy-match.ts) | ||
- [isSorted()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/is-sorted.ts) | ||
- [multiSwap()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/swap.ts) | ||
- [peek()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/peek.ts) | ||
- [quickSort()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/quicksort.ts) | ||
- [shuffle()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/shuffle.ts) (w/ custom PRNG support) | ||
- [shuffleRange()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/shuffle.ts) (w/ custom PRNG support) | ||
- [startsWith()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/starts-with.ts) | ||
- [swap()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/swap.ts) | ||
- [swizzle()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/swizzle.ts) | ||
### Binary search result predicates | ||
The following predicates can be used to perform predecessor / successor | ||
queries using `binarySearch()`. | ||
- `bsLT()` - Returns index of last item less than search value or -1 if | ||
no such values exist | ||
- `bsLE()` - Similar to `bsLT()`, but for less-than-equals queries | ||
- `bsGT()` - Returns index of first item greater than search value or -1 | ||
if no such values exist | ||
- `bsGE()` - Similar to `bsGT()`, but for less-than-equals queries | ||
- `bsEQ()` - Merely syntax sugar, casting any non-found result indices to -1 | ||
```ts | ||
const src = [10, 20, 30, 40]; | ||
bsLT(binarySearch(src, 25)) | ||
// 1 | ||
// greater-than queries also require the array length | ||
bsGT(binarySearch(src, 25), src.length) | ||
// 2 | ||
bsGT(binarySearch(src, 40), src.length) | ||
// -1 | ||
``` | ||
## Authors | ||
@@ -71,2 +102,2 @@ | ||
© 2018 - 2019 Karsten Schmidt // Apache Software License 2.0 | ||
© 2018 - 2020 Karsten Schmidt // Apache Software License 2.0 |
@@ -30,2 +30,4 @@ # ${pkg.name} | ||
${pkg.size} | ||
## Dependencies | ||
@@ -41,17 +43,45 @@ | ||
- [binarySearch()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/binary-search.ts) | ||
- [endsWith()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/ends-with.ts) | ||
- [ensureArray()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/ensure-array.ts) | ||
- [ensureIterable()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/ensure-iterable.ts) | ||
- [fuzzyMatch()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/fuzzy-match.ts) | ||
- [isSorted()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/is-sorted.ts) | ||
- [multiSwap()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/swap.ts) | ||
- [peek()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/peek.ts) | ||
- [quickSort()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/quicksort.ts) | ||
- [shuffle()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/shuffle.ts) (w/ custom PRNG support) | ||
- [shuffleRange()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/shuffle.ts) (w/ custom PRNG support) | ||
- [startsWith()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/starts-with.ts) | ||
- [swap()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/swap.ts) | ||
- [swizzle()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/swizzle.ts) | ||
- [binarySearch()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/binary-search.ts) | ||
- [endsWith()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/ends-with.ts) | ||
- [ensureArray()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/ensure-array.ts) | ||
- [ensureIterable()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/ensure-iterable.ts) | ||
- [fuzzyMatch()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/fuzzy-match.ts) | ||
- [isSorted()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/is-sorted.ts) | ||
- [multiSwap()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/swap.ts) | ||
- [peek()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/peek.ts) | ||
- [quickSort()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/quicksort.ts) | ||
- [shuffle()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/shuffle.ts) (w/ custom PRNG support) | ||
- [shuffleRange()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/shuffle.ts) (w/ custom PRNG support) | ||
- [startsWith()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/starts-with.ts) | ||
- [swap()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/swap.ts) | ||
- [swizzle()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/swizzle.ts) | ||
### Binary search result predicates | ||
The following predicates can be used to perform predecessor / successor | ||
queries using `binarySearch()`. | ||
- `bsLT()` - Returns index of last item less than search value or -1 if | ||
no such values exist | ||
- `bsLE()` - Similar to `bsLT()`, but for less-than-equals queries | ||
- `bsGT()` - Returns index of first item greater than search value or -1 | ||
if no such values exist | ||
- `bsGE()` - Similar to `bsGT()`, but for less-than-equals queries | ||
- `bsEQ()` - Merely syntax sugar, casting any non-found result indices to -1 | ||
```ts | ||
const src = [10, 20, 30, 40]; | ||
bsLT(binarySearch(src, 25)) | ||
// 1 | ||
// greater-than queries also require the array length | ||
bsGT(binarySearch(src, 25), src.length) | ||
// 2 | ||
bsGT(binarySearch(src, 40), src.length) | ||
// -1 | ||
``` | ||
## Authors | ||
@@ -58,0 +88,0 @@ |
@@ -5,20 +5,26 @@ import { IRandom } from "@thi.ng/random"; | ||
* Shuffles the items in the given index range of array `buf` using | ||
* Fisher-yates and optional `rnd` PRNG. If neither `start` / `end` are | ||
* given, the entire array will be shuffled. Mutates original array. | ||
* Fisher-yates and optional `rnd` PRNG. | ||
* | ||
* @param buf | ||
* @param n | ||
* @param rnd | ||
* @remarks | ||
* If neither `start` / `end` are given, the entire array will be | ||
* shuffled. Mutates original array. | ||
* | ||
* See {@link @thi.ng/random#IRandom} | ||
* | ||
* @param buf - array | ||
* @param n - num items | ||
* @param rnd - PRNG | ||
*/ | ||
export declare const shuffleRange: <T extends AnyArray>(buf: T, start?: number, end?: number, rnd?: IRandom) => T; | ||
/** | ||
* Applies `shuffleRange()` to the given array. If `n` is given, only | ||
* the first `n` items are shuffled. Mutates original array. | ||
* Applies {@link shuffleRange} to the given array. If `n` is given, | ||
* only the first `n` items are shuffled. Mutates original array. | ||
* | ||
* @see shuffleRange | ||
* {@link shuffleRange} | ||
* | ||
* @param buf | ||
* @param n | ||
* @param rnd | ||
* @param buf - array | ||
* @param n - num items | ||
* @param rnd - PRNG | ||
*/ | ||
export declare const shuffle: <T extends any[] | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array>(buf: T, n?: number, rnd?: IRandom) => T; | ||
//# sourceMappingURL=shuffle.d.ts.map |
@@ -5,8 +5,13 @@ import { assert } from "@thi.ng/api"; | ||
* Shuffles the items in the given index range of array `buf` using | ||
* Fisher-yates and optional `rnd` PRNG. If neither `start` / `end` are | ||
* given, the entire array will be shuffled. Mutates original array. | ||
* Fisher-yates and optional `rnd` PRNG. | ||
* | ||
* @param buf | ||
* @param n | ||
* @param rnd | ||
* @remarks | ||
* If neither `start` / `end` are given, the entire array will be | ||
* shuffled. Mutates original array. | ||
* | ||
* See {@link @thi.ng/random#IRandom} | ||
* | ||
* @param buf - array | ||
* @param n - num items | ||
* @param rnd - PRNG | ||
*/ | ||
@@ -29,11 +34,11 @@ export const shuffleRange = (buf, start = 0, end = buf.length, rnd = SYSTEM) => { | ||
/** | ||
* Applies `shuffleRange()` to the given array. If `n` is given, only | ||
* the first `n` items are shuffled. Mutates original array. | ||
* Applies {@link shuffleRange} to the given array. If `n` is given, | ||
* only the first `n` items are shuffled. Mutates original array. | ||
* | ||
* @see shuffleRange | ||
* {@link shuffleRange} | ||
* | ||
* @param buf | ||
* @param n | ||
* @param rnd | ||
* @param buf - array | ||
* @param n - num items | ||
* @param rnd - PRNG | ||
*/ | ||
export const shuffle = (buf, n = buf.length, rnd = SYSTEM) => shuffleRange(buf, 0, n, rnd); |
/** | ||
* Returns true if the first items of `buf` are the same items as in | ||
* `needle`. This means `buf` should have at least the same length as | ||
* `needle` for this to be true. | ||
* `needle`. | ||
* | ||
* By default, uses thi.ng/equiv for equality checking. | ||
* @remarks | ||
* This means `buf` should have at least the same length as `needle` for | ||
* this to be true. | ||
* | ||
* @see endsWith | ||
* By default, uses {@link @thi.ng/equiv#equiv} for equality checking. | ||
* | ||
* @param buf | ||
* @param needle | ||
* @param equiv | ||
* {@link endsWith} | ||
* | ||
* @param buf - array | ||
* @param needle - search value | ||
* @param equiv - equivalence predicate | ||
*/ | ||
export declare const startsWith: (buf: ArrayLike<any>, needle: ArrayLike<any>, equiv?: (a: any, b: any) => boolean) => boolean; | ||
//# sourceMappingURL=starts-with.d.ts.map |
import { equiv as _eq } from "@thi.ng/equiv"; | ||
/** | ||
* Returns true if the first items of `buf` are the same items as in | ||
* `needle`. This means `buf` should have at least the same length as | ||
* `needle` for this to be true. | ||
* `needle`. | ||
* | ||
* By default, uses thi.ng/equiv for equality checking. | ||
* @remarks | ||
* This means `buf` should have at least the same length as `needle` for | ||
* this to be true. | ||
* | ||
* @see endsWith | ||
* By default, uses {@link @thi.ng/equiv#equiv} for equality checking. | ||
* | ||
* @param buf | ||
* @param needle | ||
* @param equiv | ||
* {@link endsWith} | ||
* | ||
* @param buf - array | ||
* @param needle - search value | ||
* @param equiv - equivalence predicate | ||
*/ | ||
@@ -15,0 +18,0 @@ export const startsWith = (buf, needle, equiv = _eq) => { |
@@ -5,15 +5,22 @@ import { AnyArray } from "./api"; | ||
* | ||
* @param arr | ||
* @param x | ||
* @param y | ||
* @param arr - array | ||
* @param x - first index | ||
* @param y - other index | ||
*/ | ||
export declare const swap: (arr: AnyArray, x: number, y: number) => void; | ||
/** | ||
* Higher-order version of `swap` for swapping elements in multiple | ||
* arrays at once. The returned function takes the same args as `swap`, | ||
* and when called swaps 2 elements in the array given to that function | ||
* AND in the arrays given to `multiSwap` itself. Provides fast routes | ||
* for up to 3 extra arrays, then falls back to a loop-based approach. | ||
* Higher-order version of {@link swap} for swapping elements in | ||
* multiple arrays at once and hence useful for sorting multiple arrays | ||
* based on a single criteria. | ||
* | ||
* ``` | ||
* @remarks | ||
* The returned function takes the same args as `swap`, and when called | ||
* swaps 2 elements in the array given to that function AND in the | ||
* arrays given to {@link multiSwap} itself. Provides fast routes for up to 3 | ||
* extra arrays, then falls back to a loop-based approach. | ||
* | ||
* {@link (quickSort:1)} | ||
* | ||
* @example | ||
* ```ts | ||
* a = [2, 1]; | ||
@@ -31,4 +38,5 @@ * b = [20, 10]; | ||
* | ||
* @param xs | ||
* @param xs - arrays to swap in later | ||
*/ | ||
export declare const multiSwap: (...xs: AnyArray[]) => import("@thi.ng/api").Fn3<AnyArray, number, number, void>; | ||
//# sourceMappingURL=swap.d.ts.map |
27
swap.js
/** | ||
* Swaps values at index `x`/`y` in given array. | ||
* | ||
* @param arr | ||
* @param x | ||
* @param y | ||
* @param arr - array | ||
* @param x - first index | ||
* @param y - other index | ||
*/ | ||
@@ -14,9 +14,16 @@ export const swap = (arr, x, y) => { | ||
/** | ||
* Higher-order version of `swap` for swapping elements in multiple | ||
* arrays at once. The returned function takes the same args as `swap`, | ||
* and when called swaps 2 elements in the array given to that function | ||
* AND in the arrays given to `multiSwap` itself. Provides fast routes | ||
* for up to 3 extra arrays, then falls back to a loop-based approach. | ||
* Higher-order version of {@link swap} for swapping elements in | ||
* multiple arrays at once and hence useful for sorting multiple arrays | ||
* based on a single criteria. | ||
* | ||
* ``` | ||
* @remarks | ||
* The returned function takes the same args as `swap`, and when called | ||
* swaps 2 elements in the array given to that function AND in the | ||
* arrays given to {@link multiSwap} itself. Provides fast routes for up to 3 | ||
* extra arrays, then falls back to a loop-based approach. | ||
* | ||
* {@link (quickSort:1)} | ||
* | ||
* @example | ||
* ```ts | ||
* a = [2, 1]; | ||
@@ -34,3 +41,3 @@ * b = [20, 10]; | ||
* | ||
* @param xs | ||
* @param xs - arrays to swap in later | ||
*/ | ||
@@ -37,0 +44,0 @@ export const multiSwap = (...xs) => { |
import { Fn } from "@thi.ng/api"; | ||
/** | ||
* Returns optimized function to immutably select, repeat, reshape and / | ||
* or reorder array / object values in the specified index order. Fast | ||
* paths for up to 8 indices are provided, before a loop based approach | ||
* is used. | ||
* or reorder array / object values in the specified index order. | ||
* | ||
* ``` | ||
* @remarks | ||
* Fast paths for up to 8 indices are provided, before a loop based | ||
* approach is used. | ||
* | ||
* @example | ||
* ```ts | ||
* swizzle([0, 0, 0])([1, 2, 3, 4]) // [ 1, 1, 1 ] | ||
@@ -14,11 +17,13 @@ * swizzle([1, 1, 3, 3])([1, 2, 3, 4]) // [ 2, 2, 4, 4 ] | ||
* | ||
* @example | ||
* Objects can be used as input to the generated function, but the | ||
* result will always be in array form. | ||
* | ||
* ``` | ||
* ```ts | ||
* swizzle(["a", "c", "b"])({a: 1, b: 2, c: 3}) // [ 1, 3, 2 ] | ||
* ``` | ||
* | ||
* @param order indices | ||
* @param order - indices | ||
*/ | ||
export declare const swizzle: <T>(order: string | (string | number | symbol)[]) => Fn<T, any[]>; | ||
//# sourceMappingURL=swizzle.d.ts.map |
/** | ||
* Returns optimized function to immutably select, repeat, reshape and / | ||
* or reorder array / object values in the specified index order. Fast | ||
* paths for up to 8 indices are provided, before a loop based approach | ||
* is used. | ||
* or reorder array / object values in the specified index order. | ||
* | ||
* ``` | ||
* @remarks | ||
* Fast paths for up to 8 indices are provided, before a loop based | ||
* approach is used. | ||
* | ||
* @example | ||
* ```ts | ||
* swizzle([0, 0, 0])([1, 2, 3, 4]) // [ 1, 1, 1 ] | ||
@@ -13,10 +16,11 @@ * swizzle([1, 1, 3, 3])([1, 2, 3, 4]) // [ 2, 2, 4, 4 ] | ||
* | ||
* @example | ||
* Objects can be used as input to the generated function, but the | ||
* result will always be in array form. | ||
* | ||
* ``` | ||
* ```ts | ||
* swizzle(["a", "c", "b"])({a: 1, b: 2, c: 3}) // [ 1, 3, 2 ] | ||
* ``` | ||
* | ||
* @param order indices | ||
* @param order - indices | ||
*/ | ||
@@ -23,0 +27,0 @@ export const swizzle = (order) => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
84850
1192
100
9
41
Updated@thi.ng/api@^6.7.0
Updated@thi.ng/checks@^2.5.0
Updated@thi.ng/compare@^1.1.1
Updated@thi.ng/equiv@^1.0.12
Updated@thi.ng/errors@^1.2.3
Updated@thi.ng/random@^1.1.15