Skip to content

Commit

Permalink
feat(types): ported to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
upupming authored and harttle committed Jun 14, 2021
1 parent 2f3a5fb commit 9f3edb8
Show file tree
Hide file tree
Showing 79 changed files with 7,396 additions and 5,374 deletions.
31 changes: 31 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
extends: 'standard-with-typescript',
parserOptions: {
project: './tsconfig.json'
},
rules: {
'@typescript-eslint/no-non-null-assertion': 0,
// we should be able to use if (xxx) just like C++ where xxx can be non boolean value
'@typescript-eslint/strict-boolean-expressions': 0,
'@typescript-eslint/brace-style': 0
},
overrides: [
{
files: ['./*.ts'],
rules: {
// https://github.com/typescript-eslint/typescript-eslint/blob/ef88a696a157f617d38ce6d49207a4a4a089a19b/packages/eslint-plugin/docs/rules/naming-convention.md#enforce-that-interface-names-do-not-begin-with-an-i
'@typescript-eslint/naming-convention': [
'error',
{
selector: 'interface',
format: ['PascalCase'],
custom: {
regex: '^I[A-Z]',
match: true
}
}
]
}
}
]
}
12 changes: 0 additions & 12 deletions .eslintrc.json

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.*.swp
.*.swo
.vscode
.vscode
lib
50 changes: 50 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Tests
test

# sources are inlined
src

# CI
.github

# Eslint
.eslintignore
.eslintrc.js
commitlint.config.js
.prettierignore
.prettierrc

# Internal jest config
jest.config.js

# Tsconfig
tsconfig.build.json
tsconfig.json
tsconfig.spec.json

# Logs
logs
*.log
npm-debug.log*

# Coverage directory used by tools like istanbul
coverage

# Dependency directories
node_modules

# Optional npm cache directory
.npm

# others
*.tgz
.editorconfig
.npmignore
.npmrc
.vscode
.cache
.gitattributes
.gitignore
icon.png
.idea
.husky
40 changes: 21 additions & 19 deletions README.en.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# contest.js

> Original work by [@harttle](https://github.com/harttle/contest.js), ported to TypeScript by [@upupming](https://github.com/upupming/contest.js).
[简体中文](./README.md)

Ready for contest use! Data structures and algorithms in pure JavaScript with zero dependency. Features:

- Ready to copy! Supports all LTS/* Node.js and has ZERO dependency.
- Ready to copy! Supports all LTS/* Node.ts and has ZERO dependency.
- Easy to change! Implemented in simplified code with less abstraction.
- Available via npm! Can be imported as part of the WORKING code.

Expand All @@ -25,11 +27,11 @@ Ready for contest use! Data structures and algorithms in pure JavaScript with ze
- [Binomial](#Binomial): Binomial coefficient, Pascal's Triangle
- [Euclidean](#Euclidean): euclidean/GCD algorithm, extended-euclidean/extended-GCD algorithm and modular inverse.
- [Functional](#Functional): create2DArray, create3DArray, greater, less, valid2D, adjacent2D
- [Use in Node.js](#Use-in-Node.js): how to use contest.js in Node.js via npm.
- [Use in Node.ts](#Use-in-Node.ts): how to use contest.js in Node.ts via npm.

## Algorithm

[Source](https://github.com/harttle/contest.js/blob/master/bit.js) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/bit.js)
[Source](https://github.com/harttle/contest.js/blob/master/bit.ts) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/bit.ts)

### Modifying the Sequence

Expand Down Expand Up @@ -70,7 +72,7 @@ console.log(sort(arr)) // [1, 2, 3]

## String

[Source](https://github.com/harttle/contest.js/blob/master/string.js) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/string.js)
[Source](https://github.com/harttle/contest.js/blob/master/string.ts) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/string.ts)

**kmp(str: string, pattern: string)**: find index of `pattern` in `str` using KMP method, return `-1` if not found.

Expand All @@ -86,7 +88,7 @@ rabinkarp('what a wonderful world', 'a wonderful') // return 5

## Queue

[Source](https://github.com/harttle/contest.js/blob/master/queue.js) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/queue.js)
[Source](https://github.com/harttle/contest.js/blob/master/queue.ts) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/queue.ts)

**new Queue(collection?: Iterable)**: create a queue.

Expand All @@ -104,7 +106,7 @@ rabinkarp('what a wonderful world', 'a wonderful') // return 5

## Deque

[Source](https://github.com/harttle/contest.js/blob/master/deque.js) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/deque.js)
[Source](https://github.com/harttle/contest.js/blob/master/deque.ts) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/deque.ts)

**new Deque(collection?: Iterable)**: create a deque.

Expand Down Expand Up @@ -138,7 +140,7 @@ for (let val of deque) {

## Heap

[Source](https://github.com/harttle/contest.js/blob/master/heap.js) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/heap.js)
[Source](https://github.com/harttle/contest.js/blob/master/heap.ts) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/heap.ts)

**new Heap(collection?: Iterable, compare?: ((l, r) => number) = (l, r) => l - r)**: create a min heap (less elements pop out first) from elements of the `collection`, and compare elements using `compare` (accepts two arguments, return `true` if first argument is less).

Expand Down Expand Up @@ -167,7 +169,7 @@ while(maxHeap.size()) console.log(maxHeap.pop()) // outputs 3, 2, 1

## TreeSet

[Source](https://github.com/harttle/contest.js/blob/master/treeset.js) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/treeset.js)
[Source](https://github.com/harttle/contest.js/blob/master/treeset.ts) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/treeset.ts)

A worst-case time complexity log(n) set implemented by RedBlackTree (see follows).

Expand All @@ -191,7 +193,7 @@ A worst-case time complexity log(n) set implemented by RedBlackTree (see follows

## TreeMultiSet

[Source](https://github.com/harttle/contest.js/blob/master/treeset.js) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/treeset.js)
[Source](https://github.com/harttle/contest.js/blob/master/treeset.ts) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/treeset.ts)

A worst-case time complexity log(n) multiset implemented by RedBlackTree (see follows).

Expand All @@ -215,7 +217,7 @@ A worst-case time complexity log(n) multiset implemented by RedBlackTree (see fo

## BitSet

[Source](https://github.com/harttle/contest.js/blob/master/bitset.js) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/bitset.js)
[Source](https://github.com/harttle/contest.js/blob/master/bitset.ts) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/bitset.ts)

A bitset implemented by bigint, which is very space efficient but not efficient for read/write.

Expand Down Expand Up @@ -245,7 +247,7 @@ A bitset implemented by bigint, which is very space efficient but not efficient

## Binary Indexed Tree

[Source](https://github.com/harttle/contest.js/blob/master/bit.js) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/bit.js)
[Source](https://github.com/harttle/contest.js/blob/master/bit.ts) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/bit.ts)

A binary indexed tree implementation, also called [Fenwick Tree](https://en.wikipedia.org/wiki/Fenwick_tree).

Expand All @@ -265,7 +267,7 @@ bit.sum(10) // elements in [1, 10] sums to 10 + 20 + 100 = 130
```
## Disjoint Union Set

[Source](https://github.com/harttle/contest.js/blob/master/dsu.js) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/dsu.js)
[Source](https://github.com/harttle/contest.js/blob/master/dsu.ts) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/dsu.ts)

A disjoint union set implementation supports path compression and union by rank, providing nearly constant time complexity (Inverse Ackermann Function) `find/union` operations.

Expand All @@ -277,7 +279,7 @@ A disjoint union set implementation supports path compression and union by rank,

## Primes

[Source](https://github.com/harttle/contest.js/blob/master/prime.js) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/prime.js)
[Source](https://github.com/harttle/contest.js/blob/master/prime.ts) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/prime.ts)

**prime(nth: number)**: get nth (1-indexed) prime. e.g. `prime(1)` return `2`

Expand All @@ -299,7 +301,7 @@ for (let [prime, count] of factors) {

## Factorial

[Source](https://github.com/harttle/contest.js/blob/master/factorial.js) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/factorial.js)
[Source](https://github.com/harttle/contest.js/blob/master/factorial.ts) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/factorial.ts)

**factorial(n: number)**: factorial of `n`, e.g. `factorial(3)` return `6`.

Expand All @@ -311,7 +313,7 @@ for (let [prime, count] of factors) {

## Binomial

[Source](https://github.com/harttle/contest.js/blob/master/binomial.js) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/binomial.js)
[Source](https://github.com/harttle/contest.js/blob/master/binomial.ts) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/binomial.ts)

**pascalsTriangle(n: number)**: return the `n`-th Pascal's Triangle, e.g. `pascalsTriangle(3)` return `[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]]`, in which the value of `P[n][k]` represents the value of C(n, k).

Expand All @@ -323,7 +325,7 @@ for (let [prime, count] of factors) {

## Euclidean

[Source](https://github.com/harttle/contest.js/blob/master/euclidean.js) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/euclidean.js)
[Source](https://github.com/harttle/contest.js/blob/master/euclidean.ts) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/euclidean.ts)

**gcd(a: number, b: number)**: run Euclidean algorithm to compute the greatest common divisor.

Expand All @@ -333,7 +335,7 @@ for (let [prime, count] of factors) {

## Functional

[Source](https://github.com/harttle/contest.js/blob/master/funcitonal.js) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/functional.js)
[Source](https://github.com/harttle/contest.js/blob/master/funcitonal.ts) [Raw](https://raw.githubusercontent.com/harttle/contest.js/master/functional.ts)

**memorized(fn: Function, getKey? ((...args: any[]) => string) = ((...args) => args.join(',')))**: create a new function memorizing arguments and result for each call, return that result if the same arguments are passed in. A custom `getKey` can be specified to avoid conflict or to be more efficient.

Expand All @@ -356,9 +358,9 @@ for (let [ni, nj] of adjacent2D(arr, 1, 0)) {

**valid2D(arr, i, j)**: test if index `[i, j]` is valid or not for 2D array `arr`, return `true` if valid and `false` otherwise.

## Use in Node.js
## Use in Node.ts

```javascript
const {Heap} = require('classics')
let heap = new Heap()
```
```
Loading

0 comments on commit 9f3edb8

Please sign in to comment.