Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/algorithms/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './sort/index.js'
export * from './sort/index.js'
export * from './packnumber.js'
42 changes: 42 additions & 0 deletions src/algorithms/packnumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 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
}

/**
* 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]
}
46 changes: 46 additions & 0 deletions src/algorithms/tests/packNumbers.test.js
Original file line number Diff line number Diff line change
@@ -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])

})
})