You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.1 KiB

  1. const fnv = require("fnv-plus");
  2. module.exports.fnvHash = fnvHash;
  3. module.exports.toArray32 = toArray32;
  4. module.exports.fromArray32 = fromArray32;
  5. module.exports.flatArray = flatArray;
  6. function toArray32(s,size) {
  7. const res = []; //new Uint32Array(size); //has no unshift
  8. let rem = BigInt(s);
  9. const radix = BigInt(0x100000000);
  10. while (rem) {
  11. res.unshift( Number(rem % radix));
  12. rem = rem / radix;
  13. }
  14. if (size) {
  15. var i = size - res.length;
  16. while (i>0) {
  17. res.unshift(0);
  18. i--;
  19. }
  20. }
  21. return res;
  22. }
  23. function fromArray32(arr) { //returns a BigInt
  24. var res = BigInt(0);
  25. const radix = BigInt(0x100000000);
  26. for (let i = 0; i<arr.length; i++) {
  27. res = res*radix + BigInt(arr[i]);
  28. }
  29. return res;
  30. }
  31. function flatArray(a) {
  32. var res = [];
  33. fillArray(res, a);
  34. return res;
  35. function fillArray(res, a) {
  36. if (Array.isArray(a)) {
  37. for (let i=0; i<a.length; i++) {
  38. fillArray(res, a[i]);
  39. }
  40. } else {
  41. res.push(a);
  42. }
  43. }
  44. }
  45. function fnvHash(str) {
  46. return fnv.hash(str, 64).hex();
  47. }