Skip to content

Commit

Permalink
Fix symbol class
Browse files Browse the repository at this point in the history
  • Loading branch information
magnified103 committed Nov 7, 2024
1 parent d1a0e84 commit 9e52d50
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 27 deletions.
6 changes: 5 additions & 1 deletion packages/node/src/sync/encoder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { Symbol, HashedSymbol } from "./symbol.js"
import { RandomMapping } from "./mapping.js";


class SymbolMapping {
sourceIdx: number;
codedIdx: number;
Expand All @@ -15,7 +19,7 @@ class MappingHeap {
private fixHead(): void {
let curr = 0;
while (true) {
let child = curr * 2 + 1;
let child = (curr << 1) + 1;
if (child >= this.heap.length) {
break;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/node/src/sync/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export class RandomMapping {
}

nextIndex(): number {
const multiplier = 0xda942042e4dd58b5n;
this.prng = (this.prng * multiplier) & 0xffffffffffffffffn;
// xorshift128++

this.lastIdx += Math.ceil(
(this.lastIdx + 1.5) * (2 ** 32 / Math.sqrt(Number(this.prng) + 1) - 1),
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/sync/sketch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { HashedSymbol, CodedSymbol } from "./symbol.js";
import { RandomMapping } from "./mapping.js";
import { Decoder } from "./decoder.js";

class Sketch<T extends Symbol<T>> {
export class Sketch<T extends Symbol<T>> {
s: CodedSymbol<T>[] = [];

addHashedSymbol(t: HashedSymbol<T>) {
Expand Down
65 changes: 42 additions & 23 deletions packages/node/src/sync/symbol.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
export interface Symbol<T> {
XOR(t2: T): T;
Hash(): number;
}
import * as crypto from "node:crypto";


export class HashedSymbol<T extends Symbol<T>> {
Symbol: T;
Hash: number;
class HashedSymbol {
sum: Uint8Array;
checksum: Uint8Array;

constructor(symbol: T, hash: number) {
this.Symbol = symbol;
this.Hash = hash;
constructor(sum: Uint8Array, checksum?: Uint8Array) {
this.sum = new Uint8Array(sum);
if (checksum !== undefined) {
this.checksum = new Uint8Array(checksum);
} else {
this.checksum = crypto.createHmac("sha1", "").update(sum).digest();
}
}
}

export class CodedSymbol<T extends Symbol<T>> extends HashedSymbol<T> {
Count: number;
XOR(s: HashedSymbol) {
for (let i = 0; i < this.sum.length; i++) {
this.sum[i] ^= s.sum[i];
}
for (let i = 0; i < this.checksum.length; i++) {
this.sum[i] ^= s.sum[i];
}
}

constructor(symbol: T, hash: number, count = 0) {
super(symbol, hash);
this.Count = count;
isPure(): boolean {
const checksum = crypto.createHmac("sha1", "").update(this.sum).digest();
if (checksum.length !== this.checksum.length) {
return false;
}
for (let i = 0; i < checksum.length; i++) {
if (checksum[i] !== this.checksum[i]) {
return false;
}
}
return true;
}
}

export class CodedSymbol extends HashedSymbol {
count: number;

static readonly ADD = 1;
static readonly REMOVE = -1;
constructor(sum: Uint8Array, checksum?: Uint8Array, count = 1) {
super(sum, checksum);
this.count = count;
}

apply(s: HashedSymbol<T>, direction: number): CodedSymbol<T> {
this.Symbol = this.Symbol.XOR(s.Symbol);
this.Hash ^= s.Hash;
this.Count += direction;
return this;
apply(s: HashedSymbol, direction: number) {
this.XOR(s);
this.count += direction;
}
}

0 comments on commit 9e52d50

Please sign in to comment.