@thi.ng/arrays
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -5,3 +5,3 @@ import { Fn } from "@thi.ng/api"; | ||
* `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 | ||
* 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. | ||
@@ -27,1 +27,11 @@ * | ||
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; | ||
/** | ||
* Similar to `binarySearch()`, but optimized for numeric arrays and | ||
* supporting custom comparators (default: `compareNumAsc` from | ||
* thi.ng/compare pkg). | ||
* | ||
* @param buf | ||
* @param x | ||
* @param cmp | ||
*/ | ||
export declare const binarySearchNumeric: (buf: ArrayLike<number>, x: number, cmp?: import("@thi.ng/api").Fn2<number, number, number>) => number; |
@@ -1,6 +0,6 @@ | ||
import { compare } from "@thi.ng/compare"; | ||
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 | ||
* 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. | ||
@@ -44,1 +44,28 @@ * | ||
}; | ||
/** | ||
* Similar to `binarySearch()`, but optimized for numeric arrays and | ||
* supporting custom comparators (default: `compareNumAsc` from | ||
* thi.ng/compare pkg). | ||
* | ||
* @param buf | ||
* @param x | ||
* @param cmp | ||
*/ | ||
export const binarySearchNumeric = (buf, x, cmp = compareNumAsc) => { | ||
let low = 0; | ||
let high = buf.length - 1; | ||
while (low <= high) { | ||
const mid = (low + high) >>> 1; | ||
const c = cmp(buf[mid], x); | ||
if (c < 0) { | ||
low = mid + 1; | ||
} | ||
else if (c > 0) { | ||
high = mid - 1; | ||
} | ||
else { | ||
return mid; | ||
} | ||
} | ||
return -low - 1; | ||
}; |
@@ -6,2 +6,14 @@ # Change Log | ||
# [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) | ||
### Features | ||
* **arrays:** add arraySeq(), arrayIterator() & tests ([d94df57](https://github.com/thi-ng/umbrella/commit/d94df5786dddf6ef6915af79c3fbf0331ddfd2bd)) | ||
* **arrays:** add binarySearchNumeric() ([7b38202](https://github.com/thi-ng/umbrella/commit/7b38202480db71753d24aa52a9c09d3ac78d36ae)) | ||
# [0.3.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/arrays@0.2.5...@thi.ng/arrays@0.3.0) (2019-11-09) | ||
@@ -8,0 +20,0 @@ |
@@ -8,2 +8,3 @@ export * from "./binary-search"; | ||
export * from "./is-sorted"; | ||
export * from "./iterator"; | ||
export * from "./peek"; | ||
@@ -10,0 +11,0 @@ export * from "./quicksort"; |
@@ -8,2 +8,3 @@ export * from "./binary-search"; | ||
export * from "./is-sorted"; | ||
export * from "./iterator"; | ||
export * from "./peek"; | ||
@@ -10,0 +11,0 @@ export * from "./quicksort"; |
@@ -31,2 +31,20 @@ 'use strict'; | ||
}; | ||
const binarySearchNumeric = (buf, x, cmp = compare.compareNumAsc) => { | ||
let low = 0; | ||
let high = buf.length - 1; | ||
while (low <= high) { | ||
const mid = (low + high) >>> 1; | ||
const c = cmp(buf[mid], x); | ||
if (c < 0) { | ||
low = mid + 1; | ||
} | ||
else if (c > 0) { | ||
high = mid - 1; | ||
} | ||
else { | ||
return mid; | ||
} | ||
} | ||
return -low - 1; | ||
}; | ||
@@ -95,2 +113,13 @@ const endsWith = (buf, needle, equiv$1 = equiv.equiv) => { | ||
function* arrayIterator(buf, start = 0, end) { | ||
if (!buf) | ||
return; | ||
start = start; | ||
end === undefined && (end = buf.length); | ||
const step = start <= end ? 1 : -1; | ||
for (; start !== end; start += step) { | ||
yield buf[start]; | ||
} | ||
} | ||
const peek = (x) => x[x.length - 1]; | ||
@@ -216,3 +245,5 @@ | ||
exports.arrayIterator = arrayIterator; | ||
exports.binarySearch = binarySearch; | ||
exports.binarySearchNumeric = binarySearchNumeric; | ||
exports.endsWith = endsWith; | ||
@@ -219,0 +250,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),h=(e,r,n=t.equiv)=>{for(let t=e.length;--t>=0;)if(n(r,e[t]))return t;return-1},l=(e,r,t)=>{const n=e[r];e[r]=e[t],e[t]=n};const a=(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.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.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=h(e,r,n);return-1!==i?e[i]:void 0},e.findIndex=h,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 l;case 1:return(e,t,n)=>{l(e,t,n),l(r,t,n)};case 2:return(e,n,i)=>{l(e,n,i),l(r,n,i),l(t,n,i)};case 3:return(e,i,u)=>{l(e,i,u),l(r,i,u),l(t,i,u),l(n,i,u)};default:return(r,t,n)=>{l(r,t,n);for(let r=i;--r>=0;)l(e[r],t,n)}}},e.peek=e=>e[e.length-1],e.quickSort=function e(t,n=r.compare,i=l,u=0,o=t.length-1){if(u<o){const r=t[u+(o-u>>1)];let s=u-1,h=o+1;for(;;){do{s++}while(n(t[s],r)<0);do{h--}while(n(t[h],r)>0);if(s>=h)break;i(t,s,h)}e(t,n,i,u,h),e(t,n,i,h+1,o)}return t},e.shuffle=(e,r=e.length,t=o.SYSTEM)=>a(e,0,r,t),e.shuffleRange=a,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=l,e.swizzle=e=>{const[r,t,n,i,u,o,s,h]=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[h]];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,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})})); |
{ | ||
"name": "@thi.ng/arrays", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Array / Arraylike utilities", | ||
@@ -21,23 +21,26 @@ "module": "./index.js", | ||
"build:test": "rimraf build && tsc -p test/tsconfig.json", | ||
"test": "yarn build:test && mocha build/test/*.js", | ||
"cover": "yarn build:test && nyc mocha build/test/*.js && nyc report --reporter=lcov", | ||
"test": "mocha test", | ||
"cover": "nyc mocha test && nyc report --reporter=lcov", | ||
"clean": "rimraf *.js *.d.ts .nyc_output build coverage doc lib", | ||
"doc": "node_modules/.bin/typedoc --mode modules --out doc --ignoreCompilerErrors src", | ||
"doc:readme": "../../scripts/generate-readme", | ||
"doc": "node_modules/.bin/typedoc --mode modules --out doc src", | ||
"pub": "yarn build:release && yarn publish --access public" | ||
}, | ||
"devDependencies": { | ||
"@istanbuljs/nyc-config-typescript": "^0.1.3", | ||
"@types/mocha": "^5.2.6", | ||
"@types/node": "^12.6.3", | ||
"mocha": "^6.1.4", | ||
"@types/node": "^12.12.11", | ||
"mocha": "^6.2.2", | ||
"nyc": "^14.0.0", | ||
"typedoc": "^0.15.0", | ||
"typescript": "^3.6.4" | ||
"ts-node": "^8.5.2", | ||
"typedoc": "^0.15.2", | ||
"typescript": "^3.7.2" | ||
}, | ||
"dependencies": { | ||
"@thi.ng/api": "^6.5.0", | ||
"@thi.ng/checks": "^2.4.1", | ||
"@thi.ng/compare": "^1.0.10", | ||
"@thi.ng/equiv": "^1.0.10", | ||
"@thi.ng/errors": "^1.2.1", | ||
"@thi.ng/random": "^1.1.13" | ||
"@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" | ||
}, | ||
@@ -57,3 +60,6 @@ "keywords": [ | ||
"sideEffects": false, | ||
"gitHead": "97add769f24aa32a1a5e13c5c941605e1b9eb569" | ||
"thi.ng": { | ||
"year": 2018 | ||
}, | ||
"gitHead": "36c4d9e967bd80ccdbfa0f4a42f594080f95f105" | ||
} |
@@ -0,4 +1,6 @@ | ||
<!-- This file is generated - DO NOT EDIT! --> | ||
# @thi.ng/arrays | ||
[![npm (scoped)](https://img.shields.io/npm/v/@thi.ng/arrays.svg)](https://www.npmjs.com/package/@thi.ng/arrays) | ||
[![npm version](https://img.shields.io/npm/v/@thi.ng/arrays.svg)](https://www.npmjs.com/package/@thi.ng/arrays) | ||
![npm downloads](https://img.shields.io/npm/dm/@thi.ng/arrays.svg) | ||
@@ -10,8 +12,6 @@ [![Twitter Follow](https://img.shields.io/twitter/follow/thing_umbrella.svg?style=flat-square&label=twitter)](https://twitter.com/thing_umbrella) | ||
<!-- TOC depthFrom:2 depthTo:3 --> | ||
- [About](#about) | ||
- [Status](#status) | ||
- [Installation](#installation) | ||
- [Dependencies](#dependencies) | ||
- [Usage examples](#usage-examples) | ||
- [API](#api) | ||
@@ -21,8 +21,10 @@ - [Authors](#authors) | ||
<!-- /TOC --> | ||
## About | ||
Array & ArrayLike utilities. | ||
Array / Arraylike utilities. | ||
### Status | ||
**STABLE** - used in production | ||
## Installation | ||
@@ -43,10 +45,6 @@ | ||
## Usage examples | ||
## API | ||
```ts | ||
import * as a from "@thi.ng/arrays"; | ||
``` | ||
[Generated API docs](https://docs.thi.ng/umbrella/arrays/) | ||
## API | ||
- [binarySearch()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/binary-search.ts) | ||
@@ -69,6 +67,6 @@ - [endsWith()](https://github.com/thi-ng/umbrella/tree/master/packages/arrays/src/ends-with.ts) | ||
- Karsten Schmidt | ||
Karsten Schmidt | ||
## License | ||
© 2018 Karsten Schmidt // Apache Software License 2.0 | ||
© 2018 - 2019 Karsten Schmidt // Apache Software License 2.0 |
@@ -23,2 +23,2 @@ import { IRandom } from "@thi.ng/random"; | ||
*/ | ||
export declare const shuffle: <T extends AnyArray>(buf: T, n?: number, rnd?: IRandom) => T; | ||
export declare const shuffle: <T extends any[] | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array>(buf: T, n?: number, rnd?: IRandom) => T; |
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
77918
56
1004
8
69
Updated@thi.ng/api@^6.6.0
Updated@thi.ng/checks@^2.4.2
Updated@thi.ng/compare@^1.1.0
Updated@thi.ng/equiv@^1.0.11
Updated@thi.ng/errors@^1.2.2
Updated@thi.ng/random@^1.1.14