Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

array-order

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-order - npm Package Compare versions

Comparing version
0.8.0
to
0.9.0
+5
index.js
export {default as sort} from './sort.js'
export {default as rank} from './rank.js'
export {default as heap} from './heap.js'
export {default as swap} from './swap.js'
export {default as shift} from './shift.js'
export default function(seq, arr) {
var len = seq.length
for (var i=0; i<len; ++i) {
var j = seq[i]
while (j<i) j = seq[j]
if (i!==j) {
var t = arr[i]
arr[i] = arr[j]
arr[j] = t
}
}
return arr
}
// testing utils
export default function(dim) {
var seq = dim.length ? dim : [],
n = dim.length || dim,
i = 0
while (i<n) seq[i] = i++
return seq
}
import heap from '../heap.js'
import unique from './is-unique.js'
unique(heap,3,2)
unique(heap,7,2)
unique(heap,8,1)
import rank from '../rank.js'
import ordered from './in-order.js'
ordered(rank,3)
ordered(rank,6)
ordered(rank,9)
import {sort} from 'array-order'
function isinc(arr) {
for (var i=0; i<arr.length-1;) if (arr[i] > arr[++i]) throw Error(arr.join(''))
}
function isdec(arr) {
for (var i=0; i<arr.length-1;) if (arr[i] < arr[++i]) throw Error(arr.join(''))
}
function test(arr, dir, chk) {
chk( sort(arr, dir)(arr.slice()) )
}
test([0,1,2,3,4], void 0, isinc)
test([0,1,2,3,4], +1, isinc)
test([0,1,2,3,4], (a,b)=>a-b, isinc)
test([4,3,2,1,0], void 0, isinc)
test([4,0,2,1,3], +1, isinc)
test([4,0,2,1,3], (a,b)=>a-b, isinc)
test([4,3,2,1,0], -1, isdec)
test([4,0,2,1,3], -1, isdec)
test([0,1,2,3,4], (a,b)=>b-a, isdec)
test([4,0,2,1,3], (a,b)=>b-a, isdec)
+5
-4

@@ -11,10 +11,11 @@ <!-- markdownlint-disable MD022 MD024 MD026 MD032 MD041 -->

## [0.8.0] - 2020-04-25
## [0.9.0] - 2020-04-26
### Changed
- rank finding now called `rank` with the arguments reversed to `(source, target)`
### Added
- inplace sorting of arrays based on given raking
- heap permutations
- more tests with permutations
### Changed
- simplified sort
## [0.7.0] - 2017-03-19

@@ -21,0 +22,0 @@ ### Changed

@@ -12,5 +12,7 @@ import shift from './shift.js'

var n = a.length,
c = new Uint16Array(n),
i = 0
c = new Uint16Array(n), //to avoid setting all to 0...
i= 0
while (i<n) c[i++] = 0
return function() {
i = 0
while (c[i] >= i) {

@@ -24,3 +26,2 @@ c[i] = 0

swap(a, 0, n-1)
i=0
return a //same as the initial sequence

@@ -31,5 +32,4 @@ }

++c[i]
i = 0
return a
}
}
{
"name": "array-order",
"version": "0.8.0",
"version": "0.9.0",
"type": "module",
"exports": "./index.js",
"description": "sort an array in-place to a preset order",

@@ -14,3 +15,2 @@ "keywords": [

"author": "Hugo Villeneuve",
"main": "./sort.js",
"devDependencies": {

@@ -20,3 +20,3 @@ "@types/node": "^13.13.2"

"scripts": {
"test": "node ./tst/test-heap && node ./tst/test-sort"
"test": "node ./tst/heap && node ./tst/rank && node ./tst/sort"
},

@@ -23,0 +23,0 @@ "repository": {

+26
-11
<!-- markdownlint-disable MD004 MD007 MD010 MD012 MD041 MD022 MD024 MD032 MD036 -->
# array-order
*sort an array in-place to a preset order. All in place, no intermediate structures created*
*in-place sorting of arrays*
• [Example](#example) • [Features](#features) • [Limitations](#limitations) • [Why](#why) • [API](#api) • [License](#license)
## summary
* **sort** creates a sorter to sort multiple columns `(ref:Array [,compare]) => (a:Array) => a`
* **rank** performs reordering based on given ranks `(ranks:Array, a:Array) => a:Array`
* **heap** creates a function that performs permutation, each one different `a:Array => () => a`
## example
```javascript
var arrayOrder = require('array-order'),
order = [2,1,0,3]
import sort from 'array-order/sort.js'
arrayOrder([2,1,0,3], order) // [0,1,2,3]
arrayOrder(['c','b','a','d'], order) // ['a','b','c','d']
const columns = [[2,1,0,3], ['c','b','a','d']]
columns.forEach(sort(columns[0]))
//each column now sorted according to the first one
//columns == [ [0,1,2,3], ['a','b','c','d'] ]
```
## other exported utils
## exports
* named: `import {sort} from 'array-order'`
* default: `import rank from 'array-order/rank.js'`
the following are needed for testing and are also available
## sort order
the **sort** factory can take an additional compare function to change the sort order: `(ref:Array [,compare]) => (a:Array) => a`
* **undefined** increasing numeric order
* **any negative number** decreasing numeric order
* **a normal compare function** just like Array.prototype.sort: `(a, b) => a - b`
* `array-order/heap.js` creates a function that generates every permutations `(a:Array) => () => a:Array`
* `array-order/swap.js` in place swap of 2 items `(a:Array, i:number, j:number) => a:Array`
* `array-order/shift.js` in place left-swap from i to j `(a:Array, i:number, j:number) => a:Array`
## other utilities
the following other in-place utilities are needed internally and are also available
* `array-order/swap.js` in place swap of 2 items `(a:Array, i:number, j:number) => a`
* `array-order/shift.js` in place left-swap from i to j `(a:Array, i:number, j:number) => a`

@@ -25,0 +40,0 @@ ## License

+12
-12

@@ -1,13 +0,13 @@

export default function(arr, seq) {
var len = seq.length
for (var i=0; i<len; ++i) {
var j = seq[i]
while (j<i) j = seq[j]
if (i!==j) {
var t = arr[i]
arr[i] = arr[j]
arr[j] = t
}
}
return arr
import rank from './rank.js'
export default function(ref, dir) {
var seq = [],
i = 0
while (i<ref.length) seq[i] = i++
seq.sort(
dir < 0 ? function(a,b) { return ref[b]-ref[a] }
: typeof dir === 'function' ? function(a,b) { return dir(ref[a], ref[b]) }
: function(a,b) { return ref[a]-ref[b] }
)
return rank.bind(null,seq)
}

@@ -1,2 +0,2 @@

import sequence from './sequence.js'
import sequence from '../sequence.js'
import heap from '../heap.js'

@@ -18,14 +18,5 @@

sequence(seq) //reset
algo(seq, ref) //seq reordered to match ref
same(seq, ref)
algo(ref, seq) //seq reordered to match ref
same(ref, seq)
}
}
//order(mix, seq) => mix (unchanged)
//order(seq, mix) => mix (mutated to match mix)
//order(mix, mix) => xim
//order(xim, mix) => seq
//order(xim, xim) => mix
//order(mix, xim) => seq

@@ -1,2 +0,2 @@

import sequence from './sequence.js'
import sequence from '../sequence.js'

@@ -3,0 +3,0 @@ // testing utils

// testing utils
export default function(dim) {
var seq = dim.length ? dim : Array(dim),
n = seq.length,
i = 0
while (i<n) seq[i] = i++
return seq
}
import heap from '../heap.js'
import unique from './is-unique.js'
unique(heap,3,2)
unique(heap,7,2)
unique(heap,8,1)
import mark from '../old/mark.js'
import ordered from './in-order.js'
ordered(mark,3)
ordered(mark,6)
ordered(mark,9)