-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To allow sorting arrays of xor values, add an xor compare function that compares the relative distance of two xor arrays.
- Loading branch information
1 parent
b7f9f21
commit 3313702
Showing
6 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Compares two Uint8Arrays representing two xor distances. Returns `-1` if `a` | ||
* is a lower distance, `1` if `b` is a lower distance or `0` if the distances | ||
* are equal. | ||
*/ | ||
export function xorCompare (a: Uint8Array, b: Uint8Array): -1 | 0 | 1 { | ||
if (a.byteLength !== b.byteLength) { | ||
throw new Error('Inputs should have the same length') | ||
} | ||
|
||
for (let i = 0; i < a.byteLength; i++) { | ||
if (a[i] === b[i]) { | ||
continue | ||
} | ||
|
||
return a[i] < b[i] ? -1 : 1 | ||
} | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { expect } from 'aegir/chai' | ||
import { xorCompare } from '../src/xor-compare.js' | ||
|
||
describe('xor-compare', () => { | ||
it('compare', () => { | ||
expect(xorCompare(Uint8Array.from([0, 0]), Uint8Array.from([0, 1]))).to.equal(-1) | ||
expect(xorCompare(Uint8Array.from([0, 1]), Uint8Array.from([0, 1]))).to.equal(0) | ||
expect(xorCompare(Uint8Array.from([1, 1]), Uint8Array.from([0, 1]))).to.equal(1) | ||
}) | ||
}) |