-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib): Replace shuffling lib with own implementation (#174)
- Loading branch information
Showing
7 changed files
with
91 additions
and
32 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -71,8 +71,5 @@ | |
}, | ||
"peerDependencies": { | ||
"@tensorflow/tfjs": "^2.0.1" | ||
}, | ||
"dependencies": { | ||
"shuffle-seed": "^1.1.6" | ||
} | ||
} |
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,51 @@ | ||
const mulberry32 = (a: number) => () => { | ||
let t = (a += 0x6d2b79f5); | ||
t = Math.imul(t ^ (t >>> 15), t | 1); | ||
t ^= t + Math.imul(t ^ (t >>> 7), t | 61); | ||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296; | ||
}; | ||
|
||
const cyrb53 = (str: string, seed = 0) => { | ||
let h1 = 0xdeadbeef ^ seed, | ||
h2 = 0x41c6ce57 ^ seed; | ||
for (let i = 0, ch; i < str.length; i++) { | ||
ch = str.charCodeAt(i); | ||
h1 = Math.imul(h1 ^ ch, 2654435761); | ||
h2 = Math.imul(h2 ^ ch, 1597334677); | ||
} | ||
h1 = | ||
Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ | ||
Math.imul(h2 ^ (h2 >>> 13), 3266489909); | ||
h2 = | ||
Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ | ||
Math.imul(h1 ^ (h1 >>> 13), 3266489909); | ||
return 4294967296 * (2097151 & h2) + (h1 >>> 0); | ||
}; | ||
|
||
function shuffle<T>(array: T[], seed: number | string = 0) { | ||
if (typeof seed === 'string') { | ||
seed = cyrb53(seed); | ||
} | ||
const random = mulberry32(seed); | ||
|
||
const output = new Array(array.length); | ||
|
||
for (let i = 0; i < array.length; i++) { | ||
output[i] = array[i]; | ||
} | ||
|
||
let m = output.length; | ||
|
||
while (m) { | ||
const i = Math.floor(random() * m--); | ||
|
||
const t = output[m]; | ||
output[m] = output[i]; | ||
output[i] = t; | ||
++seed; | ||
} | ||
|
||
return output; | ||
} | ||
|
||
export default shuffle; |
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,24 @@ | ||
import shuffle from '../src/shuffle'; | ||
|
||
const data = [1, 2, 3, 4]; | ||
|
||
test('Shuffling without a seed should change order', () => { | ||
expect(shuffle(data)).toEqual([4, 3, 1, 2]); | ||
}); | ||
|
||
test('Shuffling should not modify the original array', () => { | ||
expect(shuffle(data)).not.toEqual(data); | ||
}); | ||
|
||
test('Shuffling with a number seed should change order', () => { | ||
expect(shuffle(data, 7)).toEqual([3, 2, 4, 1]); | ||
}); | ||
|
||
test('Shuffling with a string seed should change order', () => { | ||
expect(shuffle(data, 'hello')).toEqual([2, 4, 3, 1]); | ||
}); | ||
|
||
test('Shuffling with different seeds should produce different results', () => { | ||
const results = [shuffle(data, 7), shuffle(data, 'hello')]; | ||
expect(results[0]).not.toEqual(results[2]); | ||
}); |