From a7d09b033059993db3347a905e6ae9989864d996 Mon Sep 17 00:00:00 2001 From: waynemwashuma <94756970+waynemwashuma@users.noreply.github.com> Date: Fri, 26 Sep 2025 03:44:00 +0300 Subject: [PATCH 1/4] Add `packInto64Int` --- src/algorithms/packnumber.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/algorithms/packnumber.js diff --git a/src/algorithms/packnumber.js b/src/algorithms/packnumber.js new file mode 100644 index 00000000..b32b1374 --- /dev/null +++ b/src/algorithms/packnumber.js @@ -0,0 +1,20 @@ +/** + * Packs two numbers into a single number. + * + * #### Note + * This operation is should be analogous to packing two 32 bit + * numbers into a 64 bit number in the high and low bits.Due + * to language limitations, the high bits of this implementation + * can only be up to 2 ^ 20, otherwise the output will be garbled + * nonsense. + * + * @param {number} low + * @param {number} high + * @returns + */ +export function packInto64Int(low, high) { + + // We cant use bit manipulation as javascript has a 32 bit limit on + // manipulating "integers" + return (high * 2 ** 32) + low +} \ No newline at end of file From 89720c16750b6e15b6f22fdedc3e6c6f1fd345cf Mon Sep 17 00:00:00 2001 From: waynemwashuma <94756970+waynemwashuma@users.noreply.github.com> Date: Fri, 26 Sep 2025 03:44:47 +0300 Subject: [PATCH 2/4] Add `unpackFrom64Int` --- src/algorithms/packnumber.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/algorithms/packnumber.js b/src/algorithms/packnumber.js index b32b1374..6b562f6f 100644 --- a/src/algorithms/packnumber.js +++ b/src/algorithms/packnumber.js @@ -17,4 +17,26 @@ export function packInto64Int(low, high) { // We cant use bit manipulation as javascript has a 32 bit limit on // manipulating "integers" return (high * 2 ** 32) + low +} + +/** + * Unpacks two numbers from a single number. + * + * #### Note + * This operation is should be analogous to unpacking two 32 bit + * numbers from 64 bit number in the high and low bits.Due + * to language limitations, the number passed into this function + * can only be up to 2 ^ 53, otherwise the output will be garbled + * nonsense. + * + * @param {number} value + */ +export function unpackFrom64Int(value) { + + // We cant use bit manipulation as javascript has a 32 bit limit on + // manipulating "integers" + const low = value % 2 ** 32 + const high = Math.floor(value / 2 ** 32) + + return [low, high] } \ No newline at end of file From 59b9f1e5e61c76c8fa677faaba6960f01c8dbf8f Mon Sep 17 00:00:00 2001 From: waynemwashuma <94756970+waynemwashuma@users.noreply.github.com> Date: Fri, 26 Sep 2025 03:45:07 +0300 Subject: [PATCH 3/4] Update algorithm exports --- src/algorithms/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/algorithms/index.js b/src/algorithms/index.js index e370abe7..39298245 100644 --- a/src/algorithms/index.js +++ b/src/algorithms/index.js @@ -1 +1,2 @@ -export * from './sort/index.js' \ No newline at end of file +export * from './sort/index.js' +export * from './packnumber.js' \ No newline at end of file From 7d1837855b4a37c92473b685f64e171aefdbcbe3 Mon Sep 17 00:00:00 2001 From: waynemwashuma <94756970+waynemwashuma@users.noreply.github.com> Date: Tue, 30 Sep 2025 06:45:11 +0300 Subject: [PATCH 4/4] Add unit tests --- src/algorithms/tests/packNumbers.test.js | 46 ++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/algorithms/tests/packNumbers.test.js diff --git a/src/algorithms/tests/packNumbers.test.js b/src/algorithms/tests/packNumbers.test.js new file mode 100644 index 00000000..10c4ad20 --- /dev/null +++ b/src/algorithms/tests/packNumbers.test.js @@ -0,0 +1,46 @@ +import { test, describe } from "node:test"; +import { deepStrictEqual, strictEqual } from "node:assert"; +import { packInto64Int, unpackFrom64Int } from "../packnumber.js"; + +describe("Testing pack and unpacking numbers", () => { + test('Testing packing number.', () => { + const num1 = packInto64Int(0, 0) + const num2 = packInto64Int(1, 0) + const num3 = packInto64Int(3, 0) + const num4 = packInto64Int(0, 1) + const num5 = packInto64Int(1, 1) + const num6 = packInto64Int(4, 1) + const num7 = packInto64Int(0, 3) + const num8 = packInto64Int(2, 3) + + strictEqual(num1, 0) + strictEqual(num2, 1) + strictEqual(num3, 3) + strictEqual(num4, 4294967296) + strictEqual(num5, 4294967297) + strictEqual(num6, 4294967300) + strictEqual(num7, 12884901888) + strictEqual(num8, 12884901890) + }) + + test('Testing unpacking number.', () => { + const num1 = unpackFrom64Int(0) + const num2 = unpackFrom64Int(1) + const num3 = unpackFrom64Int(3) + const num4 = unpackFrom64Int(4294967296) + const num5 = unpackFrom64Int(4294967297) + const num6 = unpackFrom64Int(4294967300) + const num7 = unpackFrom64Int(12884901888) + const num8 = unpackFrom64Int(12884901890) + + deepStrictEqual(num1, [0, 0]) + deepStrictEqual(num2, [1, 0]) + deepStrictEqual(num3, [3, 0]) + deepStrictEqual(num4, [0, 1]) + deepStrictEqual(num5, [1, 1]) + deepStrictEqual(num6, [4, 1]) + deepStrictEqual(num7, [0, 3]) + deepStrictEqual(num8, [2, 3]) + + }) +}) \ No newline at end of file