Added class inheritance to HashSet, Hashmap

This commit is contained in:
Lucas Dower 2021-08-02 22:36:03 +01:00
parent 35c399932b
commit 943d13aca6
5 changed files with 85 additions and 134 deletions

View File

@ -1,78 +0,0 @@
const { Vector3 } = require('./vector.js');
const { VoxelManager } = require('./voxel_manager.js');
class HashSet {
constructor(numBins) {
this.numBins = numBins;
this.bins = new Array(numBins);
for (let i = 0; i < numBins; ++i) {
this.bins[i] = [];
}
}
_getBin(key) {
const hash = key.hash(); // A bit naughty
return Math.abs(hash) % this.numBins;
}
add(key) {
const binIndex = this._getBin(key);
this.bins[binIndex].push(key);
}
contains(key) {
const binIndex = this._getBin(key);
const list = this.bins[binIndex];
for (const item of list) {
if (item.equals(key)) {
return true;
}
}
return false;
}
}
class HashMap {
constructor(numBins) {
this.numBins = numBins;
this.bins = new Array(numBins);
}
_getBin(key) {
const hash = key.hash(); // A bit naughty
return Math.abs(hash) % this.numBins;
}
add(key, value) {
const binIndex = this._getBin(key);
//console.log(binIndex);
if (!this.bins[binIndex]) {
this.bins[binIndex] = [ {key: key, value: value} ];
} else {
this.bins[binIndex].push({key: key, value: value});
}
}
get(key) {
const binIndex = this._getBin(key);
if (!this.bins[binIndex]) {
return;
}
const list = this.bins[binIndex];
for (const item of list) {
if (item.key.equals(key)) {
return item.value;
}
}
}
}
module.exports.HashSet = HashSet;
module.exports.HashMap = HashMap;

58
src/hash_map.ts Normal file
View File

@ -0,0 +1,58 @@
export abstract class Hashable {
abstract hash(): number;
abstract equals(other: Hashable): boolean
}
export class HashSet<T extends Hashable> {
private _numBins: number;
protected _bins: Array<Array<T>>;
constructor(numBins: number) {
this._numBins = numBins;
this._bins = new Array(numBins);
for (let i = 0; i < numBins; ++i) {
this._bins[i] = [];
}
}
_getBin(key: T) {
const hash = key.hash();
return Math.abs(hash) % this._numBins;
}
add(key: T) {
const binIndex = this._getBin(key);
this._bins[binIndex].push(key);
}
contains(key: T) {
const binIndex = this._getBin(key);
const list = this._bins[binIndex];
for (const item of list) {
if (item.equals(key)) {
return true;
}
}
return false;
}
}
export class HashMap<T extends Hashable> extends HashSet<T> {
get(key: T): (T | undefined) {
const binIndex = this._getBin(key);
const list = this._bins[binIndex];
for (const item of list) {
if (item.equals(key)) {
return item;
}
}
}
}

View File

@ -1,54 +0,0 @@
// Not apart of rendering, SIMD optimisation not necessary
const { Vector3 } = require('./vector.js');
/**
* Retrieve the array key corresponding to the largest element in the array.
*
* @param {Array.<number>} array Input array
* @return {number} Index of array element with largest value
*/
function argMax(array) {
return array.map((x, i) => [x, i]).reduce((r, a) => (a[0] > r[0] ? a : r))[1];
}
function fastCrossXAxis(vec) {
return new Vector3(0.0, -vec.z, vec.y);
}
function fastCrossYAxis(vec) {
return new Vector3(vec.z, 0.0, -vec.x);
}
function fastCrossZAxis(vec) {
return new Vector3(-vec.y, vec.x, 0.0);
}
/*
function roundVector3To(vec, round) {
vec[0] = roundTo(vec[0], round);
vec[1] = roundTo(vec[1], round);
vec[2] = roundTo(vec[2], round);
}
module.exports.floorTo = floorTo;
module.exports.ceilTo = ceilTo;
*/
module.exports.fastCrossXAxis = fastCrossXAxis;
module.exports.fastCrossYAxis = fastCrossYAxis;
module.exports.fastCrossZAxis = fastCrossZAxis;
/*
module.exports.fastDotXAxis = fastDotXAxis;
module.exports.fastDotYAxis = fastDotYAxis;
module.exports.fastDotZAxis = fastDotZAxis;
*/
module.exports.xAxis = new Vector3(1.0, 0.0, 0.0);
module.exports.yAxis = new Vector3(0.0, 1.0, 0.0);
module.exports.zAxis = new Vector3(0.0, 0.0, 1.0);
module.exports.argMax = argMax;
//module.exports.roundVector3To = roundVector3To;

22
src/math.ts Normal file
View File

@ -0,0 +1,22 @@
import { Vector3 } from "./vector";
export const argMax = (array: [number]) => {
return array.map((x, i) => [x, i]).reduce((r, a) => (a[0] > r[0] ? a : r))[1];
}
export const fastCrossXAxis = (vec: Vector3) => {
return new Vector3(0.0, -vec.z, vec.y);
}
export const fastCrossYAxis = (vec: Vector3) => {
return new Vector3(vec.z, 0.0, -vec.x);
}
export const fastCrossZAxis = (vec: Vector3) => {
return new Vector3(-vec.y, vec.x, 0.0);
}
export const xAxis = new Vector3(1.0, 0.0, 0.0);
export const yAxis = new Vector3(0.0, 1.0, 0.0);
export const zAxis = new Vector3(0.0, 0.0, 1.0);

View File

@ -1,10 +1,13 @@
export class Vector3 {
import { Hashable } from "./hash_map";
export class Vector3 extends Hashable {
x: number;
y: number;
z: number;
constructor(x: number, y: number, z: number) {
super();
this.x = x;
this.y = y;
this.z = z;
@ -102,7 +105,7 @@ export class Vector3 {
);
}
hash() {
override hash() {
const p0 = 73856093;
const p1 = 19349663;
const p2 = 83492791;