Skip to content

Commit

Permalink
feat: add dropped array, find next function in bitset
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangquocvietuet committed Nov 1, 2024
1 parent 8df13eb commit 8010bb3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 50 deletions.
18 changes: 18 additions & 0 deletions packages/object/src/hashgraph/bitset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,22 @@ export class BitSet {
.map((int) => int.toString(2).padStart(32, "0"))
.join("");
}

findNext(index: number, bit: number): number {
let byteIndex = (index / 32) | 0;
let bitIndex = index % 32;
let mask = 1 << bitIndex;
while (byteIndex < this.data.length) {
while (bitIndex < 32) {
if ((this.data[byteIndex] & mask) === bit)
return byteIndex * 32 + bitIndex;
mask <<= 1;
bitIndex++;
}
byteIndex++;
bitIndex = 0;
mask = 1;
}
return -1;
}
}
33 changes: 21 additions & 12 deletions packages/object/src/linearize/pairSemantics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@ import {

export function linearizePair(hashGraph: HashGraph): Operation[] {
const order = hashGraph.topologicalSort(true);
const dropped = new Array(order.length).fill(false);
const result: Operation[] = [];
let i = 0;

while (i < order.length) {
if (dropped[i]) {
i++;
continue;
}
const anchor = order[i];
let j = i + 1;
let shouldIncrementI = true;

while (j < order.length) {
if (dropped[i]) break;
if (dropped[j]) {
j++;
continue;
}
const moving = order[j];

if (!hashGraph.areCausallyRelatedUsingBitsets(anchor, moving)) {
Expand All @@ -29,16 +38,14 @@ export function linearizePair(hashGraph: HashGraph): Operation[] {

switch (action) {
case ActionType.DropLeft:
order.splice(i, 1);
j = order.length; // Break out of inner loop
shouldIncrementI = false;
continue; // Continue outer loop without incrementing i
dropped[i] = true;
break;
case ActionType.DropRight:
order.splice(j, 1);
continue; // Continue with the same j
dropped[j] = true;
break;
case ActionType.Swap:
[order[i], order[j]] = [order[j], order[i]];
j = order.length; // Break out of inner loop
j = i + 1;
break;
case ActionType.Nop:
j++;
Expand All @@ -48,12 +55,14 @@ export function linearizePair(hashGraph: HashGraph): Operation[] {
j++;
}
}

if (shouldIncrementI) {
const op = hashGraph.vertices.get(order[i])?.operation;
if (op && op.value !== null) result.push(op);
if (dropped[i]) {
i++;
continue;
}

const op = hashGraph.vertices.get(order[i])?.operation;
if (op && op.value !== null) result.push(op);
i++;
}

return result;
Expand Down
76 changes: 38 additions & 38 deletions packages/object/tests/hashgraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ describe("HashGraph for AddWinSet tests", () => {

test("Test: Add Two Vertices", () => {
/*
V1:NOP <- V2:ADD(1) <- V2:REMOVE(1)
*/
V1:NOP <- V2:ADD(1) <- V2:REMOVE(1)
*/

const cro1 = obj1.cro as AddWinsSet<number>;
cro1.add(1);
Expand All @@ -33,10 +33,10 @@ describe("HashGraph for AddWinSet tests", () => {

test("Test: Add Two Concurrent Vertices With Same Value", () => {
/*
_ V2:REMOVE(1)
V1:ADD(1) /
\ _ V3:ADD(1)
*/
_ V2:REMOVE(1)
V1:ADD(1) /
\ _ V3:ADD(1)
*/

const cro1 = obj1.cro as AddWinsSet<number>;
const cro2 = obj2.cro as AddWinsSet<number>;
Expand All @@ -61,10 +61,10 @@ describe("HashGraph for AddWinSet tests", () => {

test("Test: Add Two Concurrent Vertices With Different Values", () => {
/*
_ V2:REMOVE(1)
V1:ADD(1) /
\ _ V3:ADD(2)
*/
_ V2:REMOVE(1)
V1:ADD(1) /
\ _ V3:ADD(2)
*/

const cro1 = obj1.cro as AddWinsSet<number>;
const cro2 = obj2.cro as AddWinsSet<number>;
Expand All @@ -91,10 +91,10 @@ describe("HashGraph for AddWinSet tests", () => {

test("Test: Tricky Case", () => {
/*
___ V2:REMOVE(1) <- V4:ADD(10)
V1:ADD(1) /
\ ___ V3:ADD(1) <- V5:REMOVE(5)
*/
___ V2:REMOVE(1) <- V4:ADD(10)
V1:ADD(1) /
\ ___ V3:ADD(1) <- V5:REMOVE(5)
*/

const cro1 = obj1.cro as AddWinsSet<number>;
const cro2 = obj2.cro as AddWinsSet<number>;
Expand Down Expand Up @@ -125,10 +125,10 @@ describe("HashGraph for AddWinSet tests", () => {

test("Test: Yuta Papa's Case", () => {
/*
___ V2:REMOVE(1) <- V4:ADD(2)
V1:ADD(1) /
\ ___ V3:REMOVE(2) <- V5:ADD(1)
*/
___ V2:REMOVE(1) <- V4:ADD(2)
V1:ADD(1) /
\ ___ V3:REMOVE(2) <- V5:ADD(1)
*/

const cro1 = obj1.cro as AddWinsSet<number>;
const cro2 = obj2.cro as AddWinsSet<number>;
Expand Down Expand Up @@ -157,14 +157,14 @@ describe("HashGraph for AddWinSet tests", () => {

test("Test: Mega Complex Case", () => {
/*
__ V6:ADD(3)
/
___ V2:ADD(1) <-- V3:RM(2) <-- V7:RM(1) <-- V8:RM(3)
/ ______________/
V1:ADD(1)/ /
\ /
\ ___ V4:RM(2) <-- V5:ADD(2) <-- V9:RM(1)
*/
__ V6:ADD(3)
/
___ V2:ADD(1) <-- V3:RM(2) <-- V7:RM(1) <-- V8:RM(3)
/ ______________/
V1:ADD(1)/ /
\ /
\ ___ V4:RM(2) <-- V5:ADD(2) <-- V9:RM(1)
*/

const cro1 = obj1.cro as AddWinsSet<number>;
const cro2 = obj2.cro as AddWinsSet<number>;
Expand Down Expand Up @@ -212,14 +212,14 @@ describe("HashGraph for AddWinSet tests", () => {

test("Test: Mega Complex Case 1", () => {
/*
__ V5:ADD(3)
/
___ V2:ADD(1) <-- V3:RM(2) <-- V6:RM(1) <-- V8:RM(3)
/ ^
V1:ADD(1)/ \
\ \
\ ___ V4:RM(2) <-------------------- V7:ADD(2) <-- V9:RM(1)
*/
__ V5:ADD(3)
/
___ V2:ADD(1) <-- V3:RM(2) <-- V6:RM(1) <-- V8:RM(3)
/ ^
V1:ADD(1)/ \
\ \
\ ___ V4:RM(2) <-------------------- V7:ADD(2) <-- V9:RM(1)
*/

const cro1 = obj1.cro as AddWinsSet<number>;
const cro2 = obj2.cro as AddWinsSet<number>;
Expand Down Expand Up @@ -269,10 +269,10 @@ describe("HashGraph for AddWinSet tests", () => {

test("Test: Joao's latest brain teaser", () => {
/*
__ V2:Add(2) <------------\
V1:Add(1) / \ - V5:RM(2)
\__ V3:RM(2) <- V4:RM(2) <--/
*/
__ V2:Add(2) <------------\
V1:Add(1) / \ - V5:RM(2)
\__ V3:RM(2) <- V4:RM(2) <--/
*/

const cro1 = obj1.cro as AddWinsSet<number>;
const cro2 = obj2.cro as AddWinsSet<number>;
Expand Down

0 comments on commit 8010bb3

Please sign in to comment.