large scale refactoring

This commit is contained in:
Kevin Jahns
2018-11-25 03:17:00 +01:00
parent ade3e1949d
commit 9c0da271eb
154 changed files with 1769 additions and 1199 deletions

View File

@@ -2,7 +2,7 @@
/**
* Handles named events.
*/
export default class NamedEventHandler {
export class NamedEventHandler {
constructor () {
this._eventListener = new Map()
this._stateListener = new Map()
@@ -57,7 +57,7 @@ export default class NamedEventHandler {
let state = this._stateListener.get(name)
if (state === undefined) {
state = {}
state.promise = new Promise(function (resolve) {
state.promise = new Promise(resolve => {
state.resolve = resolve
})
this._stateListener.set(name, state)

View File

@@ -1,5 +1,8 @@
/**
* @module tree
*/
function rotate (tree, parent, newParent, n) {
const rotate = (tree, parent, newParent, n) => {
if (parent === null) {
tree.root = newParent
newParent._parent = null
@@ -111,10 +114,16 @@ class N {
}
}
const isBlack = node =>
node !== null ? node.isBlack() : true
const isRed = (node) =>
node !== null ? node.isRed() : false
/*
* This is a Red Black Tree implementation
*/
export default class Tree {
export class Tree {
constructor () {
this.root = null
this.length = 0
@@ -310,12 +319,6 @@ export default class Tree {
}
}
_fixDelete (n) {
function isBlack (node) {
return node !== null ? node.isBlack() : true
}
function isRed (node) {
return node !== null ? node.isRed() : false
}
if (n.parent === null) {
// this can only be called after the first iteration of fixDelete.
return

View File

@@ -1,3 +1,6 @@
/**
* @module binary
*/
export const BITS32 = 0xFFFFFFFF
export const BITS21 = (1 << 21) - 1

View File

@@ -1,3 +1,6 @@
/**
* @module decoding
*/
/* global Buffer */
@@ -17,17 +20,26 @@ export class Decoder {
}
/**
* @function
* @param {ArrayBuffer} buffer
* @return {Decoder}
*/
export const createDecoder = buffer => new Decoder(buffer)
/**
* @function
* @param {Decoder} decoder
* @return {boolean}
*/
export const hasContent = decoder => decoder.pos !== decoder.arr.length
/**
* Clone a decoder instance.
* Optionally set a new position parameter.
*
* @function
* @param {Decoder} decoder The decoder instance
* @param {number} [newPos] Defaults to current position
* @return {Decoder} A clone of `decoder`
*/
export const clone = (decoder, newPos = decoder.pos) => {
@@ -38,6 +50,7 @@ export const clone = (decoder, newPos = decoder.pos) => {
/**
* Read `len` bytes as an ArrayBuffer.
* @function
* @param {Decoder} decoder The decoder instance
* @param {number} len The length of bytes to read
* @return {ArrayBuffer}
@@ -52,6 +65,7 @@ export const readArrayBuffer = (decoder, len) => {
/**
* Read variable length payload as ArrayBuffer
* @function
* @param {Decoder} decoder
* @return {ArrayBuffer}
*/
@@ -59,6 +73,7 @@ export const readPayload = decoder => readArrayBuffer(decoder, readVarUint(decod
/**
* Read the rest of the content as an ArrayBuffer
* @function
* @param {Decoder} decoder
* @return {ArrayBuffer}
*/
@@ -66,6 +81,7 @@ export const readTail = decoder => readArrayBuffer(decoder, decoder.arr.length -
/**
* Skip one byte, jump to the next position.
* @function
* @param {Decoder} decoder The decoder instance
* @return {number} The next position
*/
@@ -73,6 +89,7 @@ export const skip8 = decoder => decoder.pos++
/**
* Read one byte as unsigned integer.
* @function
* @param {Decoder} decoder The decoder instance
* @return {number} Unsigned 8-bit integer
*/
@@ -81,6 +98,7 @@ export const readUint8 = decoder => decoder.arr[decoder.pos++]
/**
* Read 4 bytes as unsigned integer.
*
* @function
* @param {Decoder} decoder
* @return {number} An unsigned integer.
*/
@@ -98,6 +116,7 @@ export const readUint32 = decoder => {
* Look ahead without incrementing position.
* to the next byte and read it as unsigned integer.
*
* @function
* @param {Decoder} decoder
* @return {number} An unsigned integer.
*/
@@ -109,6 +128,7 @@ export const peekUint8 = decoder => decoder.arr[decoder.pos]
* * numbers < 2^7 is stored in one bytlength
* * numbers < 2^14 is stored in two bylength
*
* @function
* @param {Decoder} decoder
* @return {number} An unsigned integer.length
*/
@@ -137,6 +157,7 @@ export const readVarUint = decoder => {
* But most environments have a maximum number of arguments per functions.
* For effiency reasons we apply a maximum of 10000 characters at once.
*
* @function
* @param {Decoder} decoder
* @return {String} The read String.
*/
@@ -157,6 +178,8 @@ export const readVarString = decoder => {
/**
* Look ahead and read varString without incrementing position
*
* @function
* @param {Decoder} decoder
* @return {string}
*/

View File

@@ -1,3 +1,6 @@
/**
* @module diff
*/
/**
* A SimpleDiff describes a change on a String.
@@ -27,7 +30,7 @@
* @param {String} b The updated version of the string
* @return {SimpleDiff} The diff description.
*/
export default function simpleDiff (a, b) {
export const simpleDiff = (a, b) => {
let left = 0 // number of same characters counting from left
let right = 0 // number of same characters counting from right
while (left < a.length && left < b.length && a[left] === b[left]) {

View File

@@ -1,4 +1,6 @@
/**
* @module encoding
*/
import * as globals from './globals.js'
const bits7 = 0b1111111
@@ -15,10 +17,18 @@ export class Encoder {
}
}
/**
* @function
* @return {Encoder}
*/
export const createEncoder = () => new Encoder()
/**
* The current length of the encoded data.
*
* @function
* @param {Encoder} encoder
* @return {number}
*/
export const length = encoder => {
let len = encoder.cpos
@@ -30,6 +40,8 @@ export const length = encoder => {
/**
* Transform to ArrayBuffer. TODO: rename to .toArrayBuffer
*
* @function
* @param {Encoder} encoder
* @return {ArrayBuffer} The created ArrayBuffer.
*/
@@ -48,6 +60,7 @@ export const toBuffer = encoder => {
/**
* Write one byte to the encoder.
*
* @function
* @param {Encoder} encoder
* @param {number} num The byte that is to be encoded.
*/
@@ -64,6 +77,7 @@ export const write = (encoder, num) => {
* Write one byte at a specific position.
* Position must already be written (i.e. encoder.length > pos)
*
* @function
* @param {Encoder} encoder
* @param {number} pos Position to which to write data
* @param {number} num Unsigned 8-bit integer
@@ -89,6 +103,7 @@ export const set = (encoder, pos, num) => {
/**
* Write one byte as an unsigned integer.
*
* @function
* @param {Encoder} encoder
* @param {number} num The number that is to be encoded.
*/
@@ -97,6 +112,7 @@ export const writeUint8 = (encoder, num) => write(encoder, num & bits8)
/**
* Write one byte as an unsigned Integer at a specific location.
*
* @function
* @param {Encoder} encoder
* @param {number} pos The location where the data will be written.
* @param {number} num The number that is to be encoded.
@@ -106,6 +122,7 @@ export const setUint8 = (encoder, pos, num) => set(encoder, pos, num & bits8)
/**
* Write two bytes as an unsigned integer.
*
* @function
* @param {Encoder} encoder
* @param {number} num The number that is to be encoded.
*/
@@ -116,6 +133,7 @@ export const writeUint16 = (encoder, num) => {
/**
* Write two bytes as an unsigned integer at a specific location.
*
* @function
* @param {Encoder} encoder
* @param {number} pos The location where the data will be written.
* @param {number} num The number that is to be encoded.
@@ -128,6 +146,7 @@ export const setUint16 = (encoder, pos, num) => {
/**
* Write two bytes as an unsigned integer
*
* @function
* @param {Encoder} encoder
* @param {number} num The number that is to be encoded.
*/
@@ -141,6 +160,7 @@ export const writeUint32 = (encoder, num) => {
/**
* Write two bytes as an unsigned integer at a specific location.
*
* @function
* @param {Encoder} encoder
* @param {number} pos The location where the data will be written.
* @param {number} num The number that is to be encoded.
@@ -157,6 +177,7 @@ export const setUint32 = (encoder, pos, num) => {
*
* Encodes integers in the range from [0, 4294967295] / [0, 0xffffffff]. (max 32 bit unsigned integer)
*
* @function
* @param {Encoder} encoder
* @param {number} num The number that is to be encoded.
*/
@@ -171,6 +192,7 @@ export const writeVarUint = (encoder, num) => {
/**
* Write a variable length string.
*
* @function
* @param {Encoder} encoder
* @param {String} str The string that is to be encoded.
*/
@@ -188,6 +210,7 @@ export const writeVarString = (encoder, str) => {
*
* TODO: can be improved!
*
* @function
* @param {Encoder} encoder The enUint8Arr
* @param {Encoder} append The BinaryEncoder to be written.
*/
@@ -196,6 +219,7 @@ export const writeBinaryEncoder = (encoder, append) => writeArrayBuffer(encoder,
/**
* Append an arrayBuffer to the encoder.
*
* @function
* @param {Encoder} encoder
* @param {ArrayBuffer} arrayBuffer
*/
@@ -209,6 +233,7 @@ export const writeArrayBuffer = (encoder, arrayBuffer) => {
}
/**
* @function
* @param {Encoder} encoder
* @param {ArrayBuffer} arrayBuffer
*/

View File

@@ -1,3 +1,7 @@
/**
* @module globals
*/
/* eslint-env browser */
export const Uint8Array_ = Uint8Array

View File

@@ -1,3 +1,7 @@
/**
* @module idb
*/
/* eslint-env browser */
import * as globals from './globals.js'
@@ -115,7 +119,7 @@ export const getAllKeysValues = (store, range) =>
* Iterate on keys and values
* @param {IDBObjectStore} store
* @param {IDBKeyRange?} keyrange
* @param {function(any, any)} f Return true in order to continue the cursor
* @param {Function} f Return true in order to continue the cursor
*/
export const iterate = (store, keyrange, f) => globals.createPromise((resolve, reject) => {
const request = store.openCursor(keyrange)
@@ -135,9 +139,10 @@ export const iterate = (store, keyrange, f) => globals.createPromise((resolve, r
/**
* Iterate on the keys (no values)
*
* @param {IDBObjectStore} store
* @param {IDBKeyRange} keyrange
* @param {function(IDBCursor)} f Call `idbcursor.continue()` to iterate further
* @param {function} f Call `idbcursor.continue()` to iterate further
*/
export const iterateKeys = (store, keyrange, f) => {
/**

View File

@@ -1,4 +1,4 @@
import * as test from './test.js'
import * as test from './testing.js'
import * as idb from './idb.js'
import * as logging from './logging.js'

View File

@@ -1,3 +1,6 @@
/**
* @module logging
*/
import * as globals from './globals.js'

View File

@@ -1,2 +1,4 @@
/**
* @module math
*/
export const floor = Math.floor

View File

@@ -4,11 +4,10 @@
*
* @example
* const mutex = createMutex()
* mutex(function () {
* mutex(() => {
* // This function is immediately executed
* mutex(function () {
* // This function is never executed, as it is called with the same
* // mutex function
* mutex(() => {
* // This function is not executed, as the mutex is already active.
* })
* })
*

View File

@@ -1,2 +1,6 @@
/**
* @module number
*/
export const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER
export const MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER

View File

@@ -1,11 +1,12 @@
/**
* @module prng
*/
const N = 624
const M = 397
function twist (u, v) {
return ((((u & 0x80000000) | (v & 0x7fffffff)) >>> 1) ^ ((v & 1) ? 0x9908b0df : 0))
}
const twist = (u, v) => ((((u & 0x80000000) | (v & 0x7fffffff)) >>> 1) ^ ((v & 1) ? 0x9908b0df : 0))
function nextState (state) {
const nextState = (state) => {
let p = 0
let j
for (j = N - M + 1; --j; p++) {
@@ -29,7 +30,7 @@ function nextState (state) {
*
* @public
*/
export default class Mt19937 {
export class Mt19937 {
/**
* @param {Number} seed The starting point for the random number generation. If you use the same seed, the generator will return the same sequence of random numbers.
*/

View File

@@ -1,13 +1,16 @@
/**
* @module prng
*/
import Mt19937 from './Mt19937.js'
import Xoroshiro128plus from './Xoroshiro128plus.js'
import Xorshift32 from './Xorshift32.js'
import { Mt19937 } from './Mt19937.js'
import { Xoroshiro128plus } from './Xoroshiro128plus.js'
import { Xorshift32 } from './Xorshift32.js'
import * as time from '../../time.js'
const DIAMETER = 300
const NUMBERS = 10000
function runPRNG (name, Gen) {
const runPRNG = (name, Gen) => {
console.log('== ' + name + ' ==')
const gen = new Gen(1234)
let head = 0

View File

@@ -1,5 +1,8 @@
/**
* @module prng
*/
import Xorshift32 from './Xorshift32.js'
import { Xorshift32 } from './Xorshift32.js'
/**
* This is a variant of xoroshiro128plus - the fastest full-period generator passing BigCrush without systematic failures.
@@ -14,7 +17,7 @@ import Xorshift32 from './Xorshift32.js'
*
* [Reference implementation](http://vigna.di.unimi.it/xorshift/xoroshiro128plus.c)
*/
export default class Xoroshiro128plus {
export class Xoroshiro128plus {
constructor (seed) {
this.seed = seed
// This is a variant of Xoroshiro128plus to fill the initial state

View File

@@ -1,8 +1,11 @@
/**
* @module prng
*/
/**
* Xorshift32 is a very simple but elegang PRNG with a period of `2^32-1`.
*/
export default class Xorshift32 {
export class Xorshift32 {
/**
* @param {number} seed The starting point for the random number generation. If you use the same seed, the generator will return the same sequence of random numbers.
*/

View File

@@ -1,10 +1,13 @@
/**
* @module prng
*/
import * as binary from '../binary.js'
import { fromCharCode, fromCodePoint } from '../string.js'
import { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } from '../number.js'
import * as math from '../math.js'
import DefaultPRNG from './PRNG/Xoroshiro128plus.js'
import { Xoroshiro128plus as DefaultPRNG } from './PRNG/Xoroshiro128plus.js'
/**
* Description of the function

View File

@@ -1,3 +1,7 @@
/**
* @module prng
*/
/**
*TODO: enable tests
import * as rt from '../rich-text/formatters.mjs'

3
lib/random.js Normal file
View File

@@ -0,0 +1,3 @@
/**
* @module random
*/

View File

@@ -1,2 +1,6 @@
/**
* @module string
*/
export const fromCharCode = String.fromCharCode
export const fromCodePoint = String.fromCodePoint

View File

@@ -1,5 +1,9 @@
/**
* @module testing
*/
import * as logging from './logging.js'
import simpleDiff from './simpleDiff.js'
import { simpleDiff } from './diff.js'
export const run = async (name, f) => {
console.log(`%cStart:%c ${name}`, 'color:blue;', '')