Skip to content

Commit

Permalink
feat: modify some files
Browse files Browse the repository at this point in the history
  • Loading branch information
yaxingson committed Nov 25, 2024
0 parents commit 46b613a
Show file tree
Hide file tree
Showing 68 changed files with 7,821 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .babelrc.js
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']
}
10 changes: 10 additions & 0 deletions .editorconfig
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
Empty file added .github/CONTRIBUTING.md
Empty file.
Empty file added .github/workflows/ci.yml
Empty file.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
.idea
node_modules
coverage
dist
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20.14.0
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.github
.vscode
15 changes: 15 additions & 0 deletions .prettierrc
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
}
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [

]
}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cSpell.words": [
"logn",
"nlogn"
]
}
Empty file added LICENSE
Empty file.
103 changes: 103 additions & 0 deletions README.md
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 added README.zh.md
Empty file.
Empty file added eslint.config.mjs
Empty file.
Empty file added lib/algorithms/index.ts
Empty file.
Empty file.
Empty file added lib/algorithms/sort/index.ts
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions lib/graph/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
6 changes: 6 additions & 0 deletions lib/index.ts
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'
3 changes: 3 additions & 0 deletions lib/interface/comparator/index.ts
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) {}
7 changes: 7 additions & 0 deletions lib/interface/container/index.ts
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 added lib/interface/index.ts
Empty file.
Empty file added lib/interface/iterator/index.ts
Empty file.
Empty file.
10 changes: 10 additions & 0 deletions lib/list/__tests__/linkedList.spec.ts
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('', ()=>{



})
})
138 changes: 138 additions & 0 deletions lib/list/arrayList.ts
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(', ')} }`
}
}
3 changes: 3 additions & 0 deletions lib/list/arrayQueue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default class ArrayQueue<T> {

}
3 changes: 3 additions & 0 deletions lib/list/arrayStack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default class ArrayStack<T> {

}
45 changes: 45 additions & 0 deletions lib/list/circularLinkedList.ts
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(', ')} }`
}
}
3 changes: 3 additions & 0 deletions lib/list/doubleEndedQueue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default class DoubleEndedQueue {

}
Loading

0 comments on commit 46b613a

Please sign in to comment.