-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fec94a0
Showing
13 changed files
with
346 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = tab | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.yml] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
github: sindresorhus | ||
open_collective: sindresorhus | ||
custom: https://sindresorhus.com/donate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: CI | ||
on: | ||
- push | ||
- pull_request | ||
jobs: | ||
test: | ||
name: Node.js ${{ matrix.node-version }} | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
node-version: | ||
- 14 | ||
- 12 | ||
- 10 | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- run: npm install | ||
- run: npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
declare class Queue<ValueType> implements Iterable<ValueType> { | ||
/** | ||
The size of the queue. | ||
*/ | ||
readonly size: number; | ||
|
||
/** | ||
Tiny queue data structure. | ||
The instance is an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols), which means you can iterate over the queue front to back with a βforβ¦ofβ loop, or use spreading to convert the queue to an array. Don't do this unless you really need to though, since it's slow. | ||
@example | ||
``` | ||
import Queue = require('yocto-queue'); | ||
const queue = new Queue(); | ||
queue.enqueue('π¦'); | ||
queue.enqueue('π'); | ||
console.log(queue.size); | ||
//=> 2 | ||
console.log(...queue); | ||
//=> 'π¦ π' | ||
console.log(queue.dequeue()); | ||
//=> 'π¦' | ||
console.log(queue.dequeue()); | ||
//=> 'π' | ||
``` | ||
*/ | ||
constructor(); | ||
|
||
[Symbol.iterator](): IterableIterator<ValueType>; | ||
|
||
/** | ||
Add a value to the queue. | ||
*/ | ||
enqueue(value: ValueType): void; | ||
|
||
/** | ||
Remove the next value in the queue. | ||
@returns The removed value or `undefined` if the queue is empty. | ||
*/ | ||
dequeue(): ValueType | undefined; | ||
|
||
/** | ||
Clear the queue. | ||
*/ | ||
clear(): void; | ||
} | ||
|
||
export = Queue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
class Node { | ||
/// value; | ||
/// next; | ||
|
||
constructor(value) { | ||
this.value = value; | ||
|
||
// TODO: Remove this when targeting Node.js 12. | ||
this.next = undefined; | ||
} | ||
} | ||
|
||
class Queue { | ||
// TODO: Use private class fields when targeting Node.js 12. | ||
// #_head; | ||
// #_tail; | ||
// #_size; | ||
|
||
constructor() { | ||
this.clear(); | ||
} | ||
|
||
enqueue(value) { | ||
const node = new Node(value); | ||
|
||
if (this._head) { | ||
this._tail.next = node; | ||
this._tail = node; | ||
} else { | ||
this._head = node; | ||
this._tail = node; | ||
} | ||
|
||
this._size++; | ||
} | ||
|
||
dequeue() { | ||
const current = this._head; | ||
if (!current) { | ||
return; | ||
} | ||
|
||
this._head = this._head.next; | ||
this._size--; | ||
return current.value; | ||
} | ||
|
||
clear() { | ||
this._head = undefined; | ||
this._tail = undefined; | ||
this._size = 0; | ||
} | ||
|
||
get size() { | ||
return this._size; | ||
} | ||
|
||
* [Symbol.iterator]() { | ||
let current = this._head; | ||
|
||
while (current) { | ||
yield current.value; | ||
current = current.next; | ||
} | ||
} | ||
} | ||
|
||
module.exports = Queue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {expectType} from 'tsd'; | ||
import Queue = require('.'); | ||
|
||
const queue = new Queue<string>(); | ||
queue.enqueue('π¦'); | ||
|
||
expectType<string | undefined>(queue.dequeue()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
MIT License | ||
|
||
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "yocto-queue", | ||
"version": "0.0.0", | ||
"description": "Tiny queue data structure", | ||
"license": "MIT", | ||
"repository": "sindresorhus/yocto-queue", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "[email protected]", | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=10" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"queue", | ||
"data", | ||
"structure", | ||
"algorithm", | ||
"queues", | ||
"queuing", | ||
"list", | ||
"array", | ||
"linkedlist", | ||
"fifo", | ||
"enqueue", | ||
"dequeue", | ||
"data-structure" | ||
], | ||
"devDependencies": { | ||
"ava": "^2.4.0", | ||
"tsd": "^0.13.1", | ||
"xo": "^0.35.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# yocto-queue [![](https://badgen.net/bundlephobia/minzip/yocto-queue)](https://bundlephobia.com/result?p=yocto-queue) | ||
|
||
> Tiny queue data structure | ||
You should use this package instead of an array if you do a lot of `Array#push()` and `Array#shift()` on large arrays, since `Array#shift()` has [linear time complexity](https://medium.com/@ariel.salem1989/an-easy-to-use-guide-to-big-o-time-complexity-5dcf4be8a444#:~:text=O(N)%E2%80%94Linear%20Time) *O(n)* while `Queue#dequeue()` has [constant time complexity](https://medium.com/@ariel.salem1989/an-easy-to-use-guide-to-big-o-time-complexity-5dcf4be8a444#:~:text=O(1)%20%E2%80%94%20Constant%20Time) *O(1)*. That makes a huge difference for large arrays. | ||
|
||
> A [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) is an ordered list of elements where an element is inserted at the end of the queue and is removed from the front of the queue. A queue works based on the first-in, first-out ([FIFO](https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics))) principle. | ||
## Install | ||
|
||
``` | ||
$ npm install yocto-queue | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
const Queue = require('yocto-queue'); | ||
|
||
const queue = new Queue(); | ||
|
||
queue.enqueue('π¦'); | ||
queue.enqueue('π'); | ||
|
||
console.log(queue.size); | ||
//=> 2 | ||
|
||
console.log(...queue); | ||
//=> 'π¦ π' | ||
|
||
console.log(queue.dequeue()); | ||
//=> 'π¦' | ||
|
||
console.log(queue.dequeue()); | ||
//=> 'π' | ||
``` | ||
|
||
## API | ||
|
||
### `queue = new Queue()` | ||
|
||
The instance is an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols), which means you can iterate over the queue front to back with a βforβ¦ofβ loop, or use spreading to convert the queue to an array. Don't do this unless you really need to though, since it's slow. | ||
|
||
#### `.enqueue(value)` | ||
|
||
Add a value to the queue. | ||
|
||
#### `.dequeue()` | ||
|
||
Remove the next value in the queue. | ||
|
||
Returns the removed value or `undefined` if the queue is empty. | ||
|
||
#### `.clear()` | ||
|
||
Clear the queue. | ||
|
||
#### `.size` | ||
|
||
The size of the queue. | ||
|
||
## Related | ||
|
||
- [quick-lru](https://github.com/sindresorhus/quick-lru) - Simple βLeast Recently Usedβ (LRU) cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import test from 'ava'; | ||
import Queue from '.'; | ||
|
||
test('.enqueue()', t => { | ||
const queue = new Queue(); | ||
queue.enqueue('π¦'); | ||
t.is(queue.dequeue(), 'π¦'); | ||
queue.enqueue('π'); | ||
queue.enqueue('β€οΈ'); | ||
t.is(queue.dequeue(), 'π'); | ||
t.is(queue.dequeue(), 'β€οΈ'); | ||
}); | ||
|
||
test('.dequeue()', t => { | ||
const queue = new Queue(); | ||
t.is(queue.dequeue(), undefined); | ||
t.is(queue.dequeue(), undefined); | ||
queue.enqueue('π¦'); | ||
t.is(queue.dequeue(), 'π¦'); | ||
t.is(queue.dequeue(), undefined); | ||
}); | ||
|
||
test('.clear()', t => { | ||
const queue = new Queue(); | ||
queue.clear(); | ||
queue.enqueue(1); | ||
queue.clear(); | ||
t.is(queue.size, 0); | ||
queue.enqueue(1); | ||
queue.enqueue(2); | ||
queue.enqueue(3); | ||
queue.clear(); | ||
t.is(queue.size, 0); | ||
}); | ||
|
||
test('.size', t => { | ||
const queue = new Queue(); | ||
t.is(queue.size, 0); | ||
queue.clear(); | ||
t.is(queue.size, 0); | ||
queue.enqueue('π¦'); | ||
t.is(queue.size, 1); | ||
queue.enqueue('π¦'); | ||
t.is(queue.size, 2); | ||
queue.dequeue(); | ||
t.is(queue.size, 1); | ||
queue.dequeue(); | ||
t.is(queue.size, 0); | ||
queue.dequeue(); | ||
t.is(queue.size, 0); | ||
}); | ||
|
||
test('iterable', t => { | ||
const queue = new Queue(); | ||
queue.enqueue('π¦'); | ||
queue.enqueue('π'); | ||
t.deepEqual([...queue], ['π¦', 'π']); | ||
}); |