-
Notifications
You must be signed in to change notification settings - Fork 0
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 46b613a
Showing
68 changed files
with
7,821 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,4 @@ | ||
module.exports = { | ||
presets: ['@babel/preset-env'], | ||
plugins:['@babel/plugin-transform-runtime'] | ||
} |
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,10 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 | ||
trim_trailing_whitespace = true | ||
quote_type = single |
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 @@ | ||
node_modules |
Empty file.
Empty file.
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,5 @@ | ||
.DS_Store | ||
.idea | ||
node_modules | ||
coverage | ||
dist |
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 @@ | ||
|
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 @@ | ||
engine-strict=true |
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 @@ | ||
v20.14.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,3 @@ | ||
node_modules | ||
.github | ||
.vscode |
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,15 @@ | ||
{ | ||
"arrowParens": "always", | ||
"bracketSpacing": true, | ||
"endOfLine": "lf", | ||
"insertPragma": false, | ||
"printWidth": 100, | ||
"proseWrap": "preserve", | ||
"quoteProps": "as-needed", | ||
"requirePragma": false, | ||
"semi": false, | ||
"singleQuote": true, | ||
"tabWidth": 2, | ||
"trailingComma": "none", | ||
"useTabs": 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,5 @@ | ||
{ | ||
"recommendations": [ | ||
|
||
] | ||
} |
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,6 @@ | ||
{ | ||
"cSpell.words": [ | ||
"logn", | ||
"nlogn" | ||
] | ||
} |
Empty file.
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,103 @@ | ||
## Get started | ||
|
||
### Install | ||
|
||
Use package manager: | ||
|
||
```shell | ||
$ npm i es-containers --save | ||
|
||
``` | ||
|
||
CDN: | ||
|
||
```html | ||
<script src="https://unpkg.com/es-containers@latest/dist/es-containers.js"></script> | ||
|
||
``` | ||
|
||
### Usage | ||
|
||
```js | ||
// commonjs | ||
const { Stack, HashMap, BinaryTree } = require('es-containers') | ||
const { ArrayList, Queue } = require('es-containers/list') | ||
|
||
// esm | ||
import { Stack, HashMap, BinaryTree } from 'es-containers' | ||
import { ArrayList, Queue } from 'es-containers/list' | ||
|
||
``` | ||
|
||
## Containers | ||
|
||
- lists | ||
- `ArrayList`: A list backed by a dynamic array that grows and shrinks implicitly. | ||
- `LinkedList`: A list where each element points to the next element in the list. | ||
- `DoublyLinkedList`: A list where each element points to the next and previous elements in the list. | ||
- `LinkedListStack` | ||
- `ArrayStack` | ||
- `LinkedListQueue` | ||
- `ArrayQueue` | ||
- `PriorityQueue` | ||
|
||
- Sets | ||
- `HashSet`: A set backed by a hash table. It makes no guarantees as to the iteration order of the set. | ||
- `TreeSet`: A set backed by a red-black tree to keep the elements ordered with respect to the comparator. | ||
- `LinkedHashSet`: A set that preserves insertion-order. Data structure is backed by a hash table to store values an`d doubly-linked list to store insertion ordering. | ||
|
||
- Maps | ||
- `HashMap`: A map based on hash tables. Keys are unordered. | ||
- `TreeMap`: A map based on red-black tree. Keys are ordered with respect to the comparator. | ||
- `LinkedHashMap`: A map that preserves insertion-order. It is backed by a hash table to store values and doubly-linked list to store ordering. | ||
- `HashBidiMap`: A map based on two hashmaps. Keys are unordered. | ||
- `TreeBidiMap`: A map based on red-black tree | ||
|
||
- Trees | ||
- `RedBlackTree` | ||
- `BinaryTree` | ||
- `BTree` | ||
- `AVLTree` | ||
|
||
- Graphs | ||
- `DirectedGraph` | ||
|
||
## Examples | ||
|
||
### Lists | ||
|
||
```js | ||
import { LinkedList, ArrayStack } from 'es-containers/list' | ||
import { StringComparator } from 'es-containers/util' | ||
|
||
``` | ||
|
||
### Sets | ||
|
||
```js | ||
import { HashSet } from 'es-containers/set' | ||
|
||
```` | ||
|
||
### Maps | ||
|
||
```js | ||
import { HashMap } from 'es-containers/map' | ||
``` | ||
|
||
### Trees | ||
|
||
```js | ||
import { BinaryTree } from 'es-containers/tree' | ||
``` | ||
|
||
### Graphs | ||
|
||
```js | ||
import { DirectedGraph } from 'es-containers/graph' | ||
``` | ||
|
||
## Test & Benchmark |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
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 @@ | ||
export {} |
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,6 @@ | ||
export * from './graph' | ||
export * from './list' | ||
export * from './map' | ||
export * from './set' | ||
export * from './tree' | ||
export * from './util' |
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 @@ | ||
export function NumberComparator(a:number, b:number) {} | ||
export function StringComparator(a:string, b:string) {} | ||
export function DateComparator(a:Date, b:Date) {} |
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 @@ | ||
export interface Container<T> { | ||
isEmpty():boolean | ||
size():number | ||
clear():void | ||
toString():string | ||
} | ||
|
Empty file.
Empty file.
Empty file.
Empty file.
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,10 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import LinkedList from '../linkedList' | ||
|
||
describe('', ()=>{ | ||
it('', ()=>{ | ||
|
||
|
||
|
||
}) | ||
}) |
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,138 @@ | ||
import type { List } from './types' | ||
|
||
type CompareFn<T> = (x:T, y:T)=>boolean | ||
|
||
const lt: CompareFn<number|string> = (x, y) => x < y | ||
const gt: CompareFn<number|string> = (x, y) => x > y | ||
|
||
class OutOfRangeError extends Error { | ||
constructor() { | ||
super() | ||
} | ||
} | ||
|
||
class IllegalIndexError extends Error { | ||
constructor() { | ||
super() | ||
} | ||
} | ||
|
||
export default class ArrayList<T> implements List<T> { | ||
private cap = 2**32 - 1 | ||
private length = 0 | ||
private items:T[] = [] | ||
|
||
private isIllegal(index:number) { | ||
return index < 0 || index >= this.length | ||
} | ||
|
||
constructor() | ||
constructor(iterable:Iterable<T>) | ||
constructor(length:number, fillValue:T) | ||
constructor(lengthOrIterable?:number|Iterable<T>, fillValue?:T) { | ||
|
||
} | ||
|
||
append(item:T) { | ||
if(this.isFull()) throw '' | ||
this.items[this.length++] = item | ||
} | ||
|
||
insert(index:number, item:T) { | ||
if(this.isIllegal(index)) throw '' | ||
if(this.isFull()) throw '' | ||
|
||
for(let i = this.length; i > index; i--) { | ||
this.items[i] = this.items[i-1] | ||
} | ||
|
||
this.items[index] = item | ||
this.length++ | ||
} | ||
|
||
remove(item:T) { | ||
const pos = this.indexOf(item) | ||
|
||
if(pos === -1) return | ||
|
||
for(let i = pos; i < this.length; i++) { | ||
this.items[i] = this.items[i+1] | ||
} | ||
|
||
this.length-- | ||
} | ||
|
||
indexOf(item:T) { | ||
for(let i = 0; i < this.length; i++) { | ||
if(this.items[i] === item) return i | ||
} | ||
return -1 | ||
} | ||
|
||
isFull() { | ||
return this.length >= this.cap | ||
} | ||
|
||
get(index: number) { | ||
if(this.length === 0 || index < 0 || index >= this.length) {} | ||
|
||
return this.items[index] | ||
} | ||
|
||
set(index: number, item: T) { | ||
|
||
} | ||
|
||
removeAt(index: number) { | ||
if(this.isEmpty() || this.isIllegal(index)) throw '' | ||
|
||
const item = this.items[index] | ||
|
||
for(let i = index; i < this.length; i++) { | ||
this.items[i] = this.items[i+1] | ||
} | ||
|
||
this.length-- | ||
|
||
return item | ||
} | ||
|
||
clone() { | ||
return this | ||
} | ||
|
||
clear() { | ||
this.items.length = 0 | ||
this.length = 0 | ||
} | ||
|
||
isEmpty() { | ||
return this.length === 0 | ||
} | ||
|
||
size() { return this.length } | ||
|
||
sort(compareFn:CompareFn<T>=gt as CompareFn<T>) { | ||
for(let i = 0; i < this.length; i++) { | ||
for(let j = i+1; j < this.length; j++) { | ||
if(compareFn(this.items[i], this.items[j])) { | ||
const temp = this.items[i] | ||
this.items[i] = this.items[j] | ||
this.items[j] = temp | ||
} | ||
} | ||
} | ||
} | ||
|
||
reverse() { | ||
for(let i = 0, j = this.length-1; i < j; i++, j--) { | ||
const temp = this.items[i] | ||
this.items[i] = this.items[j] | ||
this.items[j] = temp | ||
} | ||
} | ||
|
||
toString() { | ||
return `ArrayList { ${this.items.join(', ')} }` | ||
} | ||
} |
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 @@ | ||
export default class ArrayQueue<T> { | ||
|
||
} |
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 @@ | ||
export default class ArrayStack<T> { | ||
|
||
} |
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,45 @@ | ||
import { Node } from './types' | ||
|
||
export default class CircularLinkedList<T> { | ||
private head:Omit<Node<T>, 'data'> = { prev:null, next:null } | ||
private tail: Node<T>|null = null | ||
private length = 0 | ||
|
||
constructor() | ||
constructor(iterable:Iterable<T>) | ||
constructor(length:number, fillValue:T) | ||
constructor(lengthOrIterable?:number|Iterable<T>, fillValue?:T) { | ||
this.head.next = this.head as Node<T> | ||
this.head.prev = this.head as Node<T> | ||
|
||
if(fillValue) {} | ||
else if(lengthOrIterable) { | ||
for(const item of lengthOrIterable as Iterable<T>) { | ||
|
||
} | ||
} | ||
} | ||
|
||
insert(index:number, item:T) {} | ||
|
||
removeAt(index:number) {} | ||
|
||
indexOf(item:T) {} | ||
|
||
isEmpty() { | ||
return this.head.next === this.head | ||
} | ||
|
||
toString() { | ||
const items:T[] = [] | ||
|
||
let node = this.head | ||
|
||
while(node.next) { | ||
items.push(node.next.data) | ||
node = node.next | ||
} | ||
|
||
return `CircularLinkedList { ${items.join(', ')} }` | ||
} | ||
} |
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 @@ | ||
export default class DoubleEndedQueue { | ||
|
||
} |
Oops, something went wrong.