@thi.ng/dcons
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -7,2 +7,3 @@ export interface ConsCell<T> { | ||
export default class DCons<T> { | ||
static DEFAULT_COMPARE: (a: number, b: number) => 0 | 1 | -1; | ||
protected head: ConsCell<T>; | ||
@@ -19,3 +20,5 @@ protected tail: ConsCell<T>; | ||
insertAfter(cell: ConsCell<T>, value: T): DCons<T>; | ||
insertSorted(value: T, comp: (a: T, b: T) => number): DCons<T>; | ||
find(value: T): ConsCell<T>; | ||
findWith(fn: (value: T) => boolean): ConsCell<T>; | ||
splice(cell: ConsCell<T>, num?: number): DCons<T>; | ||
@@ -27,4 +30,9 @@ swap(a: ConsCell<T>, b: ConsCell<T>): DCons<T>; | ||
peek(): T; | ||
setHead(v: T): DCons<T>; | ||
setTail(v: T): DCons<T>; | ||
setNth(i: number, v: T): this; | ||
nth(i: number, notFound?: T): any; | ||
rotateLeft(): DCons<T>; | ||
rotateRight(): DCons<T>; | ||
toString(): string; | ||
} |
80
index.js
@@ -58,3 +58,2 @@ "use strict"; | ||
cell.prev.next = newCell; | ||
cell.prev = newCell; | ||
} | ||
@@ -64,2 +63,3 @@ else { | ||
} | ||
cell.prev = newCell; | ||
this._length++; | ||
@@ -75,3 +75,2 @@ return this; | ||
cell.next.prev = newCell; | ||
cell.next = newCell; | ||
} | ||
@@ -81,5 +80,16 @@ else { | ||
} | ||
cell.next = newCell; | ||
this._length++; | ||
return this; | ||
} | ||
insertSorted(value, comp) { | ||
let cell = this.head; | ||
while (cell) { | ||
if (comp(value, cell.value) <= 0) { | ||
return this.insertBefore(cell, value); | ||
} | ||
cell = cell.next; | ||
} | ||
return this.push(value); | ||
} | ||
find(value) { | ||
@@ -94,2 +104,11 @@ let cell = this.head; | ||
} | ||
findWith(fn) { | ||
let cell = this.head; | ||
while (cell) { | ||
if (fn(cell.value)) { | ||
return cell; | ||
} | ||
cell = cell.next; | ||
} | ||
} | ||
splice(cell, num = 1) { | ||
@@ -154,2 +173,36 @@ while (cell && num-- > 0) { | ||
} | ||
setHead(v) { | ||
if (this.head) { | ||
this.head.value = v; | ||
return this; | ||
} | ||
return this.cons(v); | ||
} | ||
setTail(v) { | ||
if (this.tail) { | ||
this.tail.value = v; | ||
return this; | ||
} | ||
return this.push(v); | ||
} | ||
setNth(i, v) { | ||
if (i < 0 || i >= this._length) { | ||
throw new Error(`index out of bounds: ${i}`); | ||
} | ||
let cell, dir; | ||
if (i <= this._length >> 1) { | ||
cell = this.head; | ||
dir = "next"; | ||
} | ||
else { | ||
cell = this.tail; | ||
dir = "prev"; | ||
i = this._length - i - 1; | ||
} | ||
while (i-- > 0 && cell) { | ||
cell = cell[dir]; | ||
} | ||
cell.value = v; | ||
return this; | ||
} | ||
nth(i, notFound) { | ||
@@ -174,2 +227,24 @@ if (i < 0 || i >= this._length) { | ||
} | ||
rotateLeft() { | ||
switch (this._length) { | ||
case 0: | ||
case 1: | ||
return this; | ||
case 2: | ||
return this.swap(this.head, this.tail); | ||
default: | ||
return this.push(this.drop()); | ||
} | ||
} | ||
rotateRight() { | ||
switch (this._length) { | ||
case 0: | ||
case 1: | ||
return this; | ||
case 2: | ||
return this.swap(this.head, this.tail); | ||
default: | ||
return this.cons(this.pop()); | ||
} | ||
} | ||
toString() { | ||
@@ -186,2 +261,3 @@ let res = [], cell = this.head; | ||
} | ||
DCons.DEFAULT_COMPARE = (a, b) => a < b ? -1 : a > b ? 1 : 0; | ||
exports.default = DCons; |
{ | ||
"name": "@thi.ng/dcons", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Doubly linked list structure w/ iterator support", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# @thi.ng/dcons | ||
Double sided, doubly linked list with ES6 iterator support. | ||
![npm (scoped)](https://img.shields.io/npm/v/@thi.ng/dcons.svg) | ||
Double sided, doubly linked list with: | ||
- ES6 iterator support | ||
- Stack API (front & back) | ||
- Random node access (read / write, O(N/2)) | ||
- Node insertion (also w/ custom comparator) | ||
- Rotation (left / right) | ||
- Splicing | ||
- Node finding | ||
- Node swapping | ||
## Usage | ||
```js | ||
// ES5 | ||
DCons = require("@thi.ng/dcons").default; | ||
// ES6 / TS | ||
import DCons from "@thi.ng/dcons"; | ||
``` | ||
## API | ||
```js | ||
list = new DCons([1, 2, 3]); | ||
``` | ||
### Head centric | ||
- `cons()` | ||
- `first()` | ||
- `drop()` | ||
- `setHead()` | ||
### Tail centric | ||
- `push()` | ||
- `peek()` | ||
- `pop()` | ||
- `setTail()` | ||
### Random Access | ||
- `.length` | ||
- `nth()` | ||
- `setNth()` | ||
# Insertion | ||
- `insertBefore()` | ||
- `insertAfter()` | ||
- `insertSorted()` | ||
# Finding | ||
- `find()` | ||
- `findWith()` | ||
# Structure | ||
- `copy()` | ||
- `splice()` | ||
- `swap()` | ||
- `rotateLeft()` | ||
- `rotateRight()` | ||
TODO... | ||
## Building | ||
@@ -17,3 +85,4 @@ | ||
``` | ||
cd thing-indicators | ||
git clone https://github.com/thi-ng/dcons.git | ||
cd dcons | ||
npm up | ||
@@ -20,0 +89,0 @@ npm run build |
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
9722
290
104