mqtt and socket added

This commit is contained in:
Miisa Ekholm
2022-10-21 11:17:48 +03:00
parent 52370df9ce
commit 9e3d953b9e
751 changed files with 185105 additions and 153 deletions

View File

@@ -0,0 +1,32 @@
import { Base, Container } from "../../ContainerBase/index";
declare abstract class HashContainer<K> extends Base {
protected static readonly sigma = 0.75;
protected static readonly treeifyThreshold = 8;
protected static readonly untreeifyThreshold = 6;
protected static readonly minTreeifySize = 64;
protected static readonly maxBucketNum: number;
protected bucketNum: number;
protected initBucketNum: number;
protected hashFunc: (x: K) => number;
protected abstract hashTable: Container<unknown>[];
protected constructor(initBucketNum?: number, hashFunc?: (x: K) => number);
clear(): void;
/**
* @description Growth the hash table.
* @protected
*/
protected abstract reAllocate(): void;
abstract forEach(callback: (element: unknown, index: number) => void): void;
/**
* @description Remove the elements of the specified value.
* @param key The element you want to remove.
*/
abstract eraseElementByKey(key: K): void;
/**
* @param key The element you want to find.
* @return Boolean about if the specified element in the hash set.
*/
abstract find(key: K): void;
abstract [Symbol.iterator](): Generator<unknown, void, undefined>;
}
export default HashContainer;

View File

@@ -0,0 +1,39 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../../ContainerBase/index");
class HashContainer extends index_1.Base {
constructor(initBucketNum = 16, hashFunc = (x) => {
let str;
if (typeof x !== 'string') {
str = JSON.stringify(x);
}
else
str = x;
let hashCode = 0;
const strLength = str.length;
for (let i = 0; i < strLength; i++) {
const ch = str.charCodeAt(i);
hashCode = ((hashCode << 5) - hashCode) + ch;
hashCode |= 0;
}
return hashCode >>> 0;
}) {
super();
if (initBucketNum < 16 || (initBucketNum & (initBucketNum - 1)) !== 0) {
throw new RangeError('InitBucketNum range error');
}
this.bucketNum = this.initBucketNum = initBucketNum;
this.hashFunc = hashFunc;
}
clear() {
this.length = 0;
this.bucketNum = this.initBucketNum;
this.hashTable = [];
}
}
HashContainer.sigma = 0.75;
HashContainer.treeifyThreshold = 8;
HashContainer.untreeifyThreshold = 6;
HashContainer.minTreeifySize = 64;
HashContainer.maxBucketNum = (1 << 30);
exports.default = HashContainer;

View File

@@ -0,0 +1,26 @@
import HashContainer from './Base/index';
import Vector from '../SequentialContainer/Vector';
import OrderedMap from '../TreeContainer/OrderedMap';
import { initContainer } from "../ContainerBase/index";
declare class HashMap<K, V> extends HashContainer<K> {
protected hashTable: (Vector<[K, V]> | OrderedMap<K, V>)[];
constructor(container?: initContainer<[K, V]>, initBucketNum?: number, hashFunc?: (x: K) => number);
protected reAllocate(): void;
forEach(callback: (element: [K, V], index: number) => void): void;
/**
* @description Insert a new key-value pair to hash map or set value by key.
* @param key The key you want to insert.
* @param value The value you want to insert.
* @example HashMap.setElement(1, 2); // insert a key-value pair [1, 2]
*/
setElement(key: K, value: V): void;
/**
* @description Get the value of the element which has the specified key.
* @param key The key you want to get.
*/
getElementByKey(key: K): V | undefined;
eraseElementByKey(key: K): void;
find(key: K): boolean;
[Symbol.iterator](): Generator<[K, V], void, unknown>;
}
export default HashMap;

View File

@@ -0,0 +1,200 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = __importDefault(require("./Base/index"));
const Vector_1 = __importDefault(require("../SequentialContainer/Vector"));
const OrderedMap_1 = __importDefault(require("../TreeContainer/OrderedMap"));
class HashMap extends index_1.default {
constructor(container = [], initBucketNum, hashFunc) {
super(initBucketNum, hashFunc);
this.hashTable = [];
container.forEach(element => this.setElement(element[0], element[1]));
}
reAllocate() {
if (this.bucketNum >= index_1.default.maxBucketNum)
return;
const newHashTable = [];
const originalBucketNum = this.bucketNum;
this.bucketNum <<= 1;
const keys = Object.keys(this.hashTable);
const keyNums = keys.length;
for (let i = 0; i < keyNums; ++i) {
const index = parseInt(keys[i]);
const container = this.hashTable[index];
const size = container.size();
if (size === 0)
continue;
if (size === 1) {
const element = container.front();
newHashTable[this.hashFunc(element[0]) & (this.bucketNum - 1)] = new Vector_1.default([element], false);
continue;
}
const lowList = [];
const highList = [];
container.forEach(element => {
const hashCode = this.hashFunc(element[0]);
if ((hashCode & originalBucketNum) === 0) {
lowList.push(element);
}
else
highList.push(element);
});
if (container instanceof OrderedMap_1.default) {
if (lowList.length > index_1.default.untreeifyThreshold) {
newHashTable[index] = new OrderedMap_1.default(lowList);
}
else if (lowList.length) {
newHashTable[index] = new Vector_1.default(lowList, false);
}
if (highList.length > index_1.default.untreeifyThreshold) {
newHashTable[index + originalBucketNum] = new OrderedMap_1.default(highList);
}
else if (highList.length) {
newHashTable[index + originalBucketNum] = new Vector_1.default(highList, false);
}
}
else {
if (lowList.length >= index_1.default.treeifyThreshold) {
newHashTable[index] = new OrderedMap_1.default(lowList);
}
else if (lowList.length) {
newHashTable[index] = new Vector_1.default(lowList, false);
}
if (highList.length >= index_1.default.treeifyThreshold) {
newHashTable[index + originalBucketNum] = new OrderedMap_1.default(highList);
}
else if (highList.length) {
newHashTable[index + originalBucketNum] = new Vector_1.default(highList, false);
}
}
}
this.hashTable = newHashTable;
}
forEach(callback) {
const containers = Object.values(this.hashTable);
const containersNum = containers.length;
let index = 0;
for (let i = 0; i < containersNum; ++i) {
containers[i].forEach(element => callback(element, index++));
}
}
/**
* @description Insert a new key-value pair to hash map or set value by key.
* @param key The key you want to insert.
* @param value The value you want to insert.
* @example HashMap.setElement(1, 2); // insert a key-value pair [1, 2]
*/
setElement(key, value) {
const index = this.hashFunc(key) & (this.bucketNum - 1);
const container = this.hashTable[index];
if (!container) {
this.length += 1;
this.hashTable[index] = new Vector_1.default([[key, value]], false);
}
else {
const preSize = container.size();
if (container instanceof Vector_1.default) {
for (const pair of container) {
if (pair[0] === key) {
pair[1] = value;
return;
}
}
container.pushBack([key, value]);
if (preSize + 1 >= HashMap.treeifyThreshold) {
if (this.bucketNum <= HashMap.minTreeifySize) {
this.length += 1;
this.reAllocate();
return;
}
this.hashTable[index] = new OrderedMap_1.default(this.hashTable[index]);
}
this.length += 1;
}
else {
container.setElement(key, value);
const curSize = container.size();
this.length += curSize - preSize;
}
}
if (this.length > this.bucketNum * HashMap.sigma) {
this.reAllocate();
}
}
/**
* @description Get the value of the element which has the specified key.
* @param key The key you want to get.
*/
getElementByKey(key) {
const index = this.hashFunc(key) & (this.bucketNum - 1);
const container = this.hashTable[index];
if (!container)
return undefined;
if (container instanceof OrderedMap_1.default) {
return container.getElementByKey(key);
}
else {
for (const pair of container) {
if (pair[0] === key)
return pair[1];
}
return undefined;
}
}
eraseElementByKey(key) {
const index = this.hashFunc(key) & (this.bucketNum - 1);
const container = this.hashTable[index];
if (!container)
return;
if (container instanceof Vector_1.default) {
let pos = 0;
for (const pair of container) {
if (pair[0] === key) {
container.eraseElementByPos(pos);
this.length -= 1;
return;
}
pos += 1;
}
}
else {
const preSize = container.size();
container.eraseElementByKey(key);
const curSize = container.size();
this.length += curSize - preSize;
if (curSize <= index_1.default.untreeifyThreshold) {
this.hashTable[index] = new Vector_1.default(container);
}
}
}
find(key) {
const index = this.hashFunc(key) & (this.bucketNum - 1);
const container = this.hashTable[index];
if (!container)
return false;
if (container instanceof OrderedMap_1.default) {
return !container.find(key)
.equals(container.end());
}
for (const pair of container) {
if (pair[0] === key)
return true;
}
return false;
}
[Symbol.iterator]() {
return function* () {
const containers = Object.values(this.hashTable);
const containersNum = containers.length;
for (let i = 0; i < containersNum; ++i) {
const container = containers[i];
for (const element of container) {
yield element;
}
}
}.bind(this)();
}
}
exports.default = HashMap;

View File

@@ -0,0 +1,19 @@
import HashContainer from './Base/index';
import Vector from '../SequentialContainer/Vector';
import OrderedSet from '../TreeContainer/OrderedSet';
import { initContainer } from "../ContainerBase/index";
declare class HashSet<K> extends HashContainer<K> {
protected hashTable: (Vector<K> | OrderedSet<K>)[];
constructor(container?: initContainer<K>, initBucketNum?: number, hashFunc?: (x: K) => number);
protected reAllocate(): void;
forEach(callback: (element: K, index: number) => void): void;
/**
* @description Insert element to hash set.
* @param element The element you want to insert.
*/
insert(element: K): void;
eraseElementByKey(key: K): void;
find(element: K): boolean;
[Symbol.iterator](): Generator<K, void, unknown>;
}
export default HashSet;

View File

@@ -0,0 +1,164 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = __importDefault(require("./Base/index"));
const Vector_1 = __importDefault(require("../SequentialContainer/Vector"));
const OrderedSet_1 = __importDefault(require("../TreeContainer/OrderedSet"));
class HashSet extends index_1.default {
constructor(container = [], initBucketNum, hashFunc) {
super(initBucketNum, hashFunc);
this.hashTable = [];
container.forEach(element => this.insert(element));
}
reAllocate() {
if (this.bucketNum >= index_1.default.maxBucketNum)
return;
const newHashTable = [];
const originalBucketNum = this.bucketNum;
this.bucketNum <<= 1;
const keys = Object.keys(this.hashTable);
const keyNums = keys.length;
for (let i = 0; i < keyNums; ++i) {
const index = parseInt(keys[i]);
const container = this.hashTable[index];
const size = container.size();
if (size === 0)
continue;
if (size === 1) {
const element = container.front();
newHashTable[this.hashFunc(element) & (this.bucketNum - 1)] = new Vector_1.default([element], false);
continue;
}
const lowList = [];
const highList = [];
container.forEach(element => {
const hashCode = this.hashFunc(element);
if ((hashCode & originalBucketNum) === 0) {
lowList.push(element);
}
else
highList.push(element);
});
if (container instanceof OrderedSet_1.default) {
if (lowList.length > index_1.default.untreeifyThreshold) {
newHashTable[index] = new OrderedSet_1.default(lowList);
}
else if (lowList.length) {
newHashTable[index] = new Vector_1.default(lowList, false);
}
if (highList.length > index_1.default.untreeifyThreshold) {
newHashTable[index + originalBucketNum] = new OrderedSet_1.default(highList);
}
else if (highList.length) {
newHashTable[index + originalBucketNum] = new Vector_1.default(highList, false);
}
}
else {
if (lowList.length >= index_1.default.treeifyThreshold) {
newHashTable[index] = new OrderedSet_1.default(lowList);
}
else if (lowList.length) {
newHashTable[index] = new Vector_1.default(lowList, false);
}
if (highList.length >= index_1.default.treeifyThreshold) {
newHashTable[index + originalBucketNum] = new OrderedSet_1.default(highList);
}
else if (highList.length) {
newHashTable[index + originalBucketNum] = new Vector_1.default(highList, false);
}
}
}
this.hashTable = newHashTable;
}
forEach(callback) {
const containers = Object.values(this.hashTable);
const containersNum = containers.length;
let index = 0;
for (let i = 0; i < containersNum; ++i) {
containers[i].forEach(element => callback(element, index++));
}
}
/**
* @description Insert element to hash set.
* @param element The element you want to insert.
*/
insert(element) {
const index = this.hashFunc(element) & (this.bucketNum - 1);
const container = this.hashTable[index];
if (!container) {
this.hashTable[index] = new Vector_1.default([element], false);
this.length += 1;
}
else {
const preSize = container.size();
if (container instanceof Vector_1.default) {
if (!container.find(element)
.equals(container.end()))
return;
container.pushBack(element);
if (preSize + 1 >= index_1.default.treeifyThreshold) {
if (this.bucketNum <= index_1.default.minTreeifySize) {
this.length += 1;
this.reAllocate();
return;
}
this.hashTable[index] = new OrderedSet_1.default(container);
}
this.length += 1;
}
else {
container.insert(element);
const curSize = container.size();
this.length += curSize - preSize;
}
}
if (this.length > this.bucketNum * index_1.default.sigma) {
this.reAllocate();
}
}
eraseElementByKey(key) {
const index = this.hashFunc(key) & (this.bucketNum - 1);
const container = this.hashTable[index];
if (!container)
return;
const preSize = container.size();
if (preSize === 0)
return;
if (container instanceof Vector_1.default) {
container.eraseElementByValue(key);
const curSize = container.size();
this.length += curSize - preSize;
}
else {
container.eraseElementByKey(key);
const curSize = container.size();
this.length += curSize - preSize;
if (curSize <= index_1.default.untreeifyThreshold) {
this.hashTable[index] = new Vector_1.default(container);
}
}
}
find(element) {
const index = this.hashFunc(element) & (this.bucketNum - 1);
const container = this.hashTable[index];
if (!container)
return false;
return !container.find(element)
.equals(container.end());
}
[Symbol.iterator]() {
return function* () {
const containers = Object.values(this.hashTable);
const containersNum = containers.length;
for (let i = 0; i < containersNum; ++i) {
const container = containers[i];
for (const element of container) {
yield element;
}
}
}.bind(this)();
}
}
exports.default = HashSet;