├── .gitignore ├── LICENSE ├── README.md ├── bench.js ├── example.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # value-sort 2 | 3 | Fast and iterable data structure for sorting bounded values 4 | 5 | ``` 6 | npm install value-sort 7 | ``` 8 | 9 | Uses a fixed size array to store values and a [indexed-bitfield](https://github.com/mafintosh/indexed-bitfield) to iterate them. 10 | 11 | ## Usage 12 | 13 | ``` js 14 | const valueSort = require('value-sort') 15 | 16 | // all values are between [0, 65536[ 17 | const vs = valueSort(65536) 18 | 19 | vs.add(55) 20 | vs.add(1004) 21 | vs.add(0) 22 | vs.add(9999) 23 | vs.add(66) 24 | 25 | // the valueSort instance if iterable 26 | for (const v of vs) { 27 | console.log(v) 28 | } 29 | ``` 30 | 31 | Running the above prints 32 | 33 | ```js 34 | 0 35 | 55 36 | 66 37 | 1004 38 | 9999 39 | ``` 40 | 41 | If you want the values as a sorted array instead use 42 | 43 | ```js 44 | const arr = vs.toArray() 45 | ``` 46 | 47 | If you pass an object as the 2nd arg to add that object will 48 | show up in the iteration instead of the value 49 | 50 | ```js 51 | // the object on the right is returned instead of 42 on iteration 52 | vs.add(42, {index: 42, hello: 'world'}) 53 | ``` 54 | 55 | ## License 56 | 57 | MIT 58 | -------------------------------------------------------------------------------- /bench.js: -------------------------------------------------------------------------------- 1 | const valueSort = require('./') 2 | 3 | const start = Date.now() 4 | const algo = process.argv[2] 5 | 6 | if (algo === 'value-sort') { 7 | for (var i = 0; i < 100; i++) { 8 | const vs = valueSort(1000000) 9 | 10 | for (var j = 0; j < 100000; j++) { 11 | vs.add(Math.floor(Math.random() * 1000000)) 12 | } 13 | 14 | for (const k of vs) {} 15 | } 16 | } else if (algo === 'array-sort') { 17 | for (var i = 0; i < 100; i++) { 18 | const arr = [] 19 | 20 | for (var j = 0; j < 100000; j++) { 21 | arr.push(Math.floor(Math.random() * 1000000)) 22 | } 23 | 24 | for (const k of arr.sort(cmp)) {} 25 | } 26 | } else { 27 | console.error('Usage: node bench.js array-sort|value-sort') 28 | process.exit(1) 29 | } 30 | 31 | console.log(algo + ' took ' + (Date.now() - start) + 'ms') 32 | 33 | function cmp (a, b) { 34 | return a - b 35 | } 36 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | const valueSort = require('./') 2 | 3 | const vs = valueSort(65536) 4 | 5 | vs.add(55) 6 | vs.add(1004) 7 | vs.add(0) 8 | vs.add(9999) 9 | vs.add(66) 10 | 11 | for (const v of vs) { 12 | console.log(v) 13 | } 14 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const bitfield = require('indexed-bitfield') 2 | 3 | module.exports = size => new ValueSort(size) 4 | 5 | class ValueSort { 6 | constructor (size) { 7 | this.size = size 8 | this.values = 0 9 | this._bucket = new Array(size) 10 | this._set = bitfield(size) 11 | } 12 | 13 | add (value, item) { 14 | this._bucket[value] = item === undefined ? value : item 15 | if (this._set.set(value, true)) this.values++ 16 | } 17 | 18 | toArray () { 19 | const ite = this._set.iterator() 20 | const arr = new Array(this.values) 21 | for (var i = 0; i < this.values; i++) { 22 | arr[i] = this._bucket[ite.next(true)] 23 | } 24 | return arr 25 | } 26 | 27 | [Symbol.iterator] () { 28 | return new Iterator(this) 29 | } 30 | } 31 | 32 | class Iterator { 33 | constructor (sort) { 34 | this._bucket = sort._bucket 35 | this._ite = sort._set.iterator() 36 | } 37 | 38 | next () { 39 | const i = this._ite.next(true) 40 | if (i === -1) return {done: true, value: null} 41 | return {done: false, value: this._bucket[i]} 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "value-sort", 3 | "version": "1.0.1", 4 | "description": "Fast and iterable data structure for sorting bounded values", 5 | "main": "index.js", 6 | "dependencies": { 7 | "indexed-bitfield": "^2.0.0" 8 | }, 9 | "devDependencies": {}, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/mafintosh/value-sort.git" 13 | }, 14 | "author": "Mathias Buus (@mafintosh)", 15 | "license": "MIT", 16 | "bugs": { 17 | "url": "https://github.com/mafintosh/value-sort/issues" 18 | }, 19 | "homepage": "https://github.com/mafintosh/value-sort" 20 | } 21 | --------------------------------------------------------------------------------