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.

134 lines
3.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. const fnv = require("fnv-plus");
  2. const bigInt = require("big-integer");
  3. module.exports.ident =ident;
  4. module.exports.extractSizes =extractSizes;
  5. module.exports.flatArray = flatArray;
  6. module.exports.csArr = csArr;
  7. module.exports.accSizes = accSizes;
  8. module.exports.fnvHash = fnvHash;
  9. module.exports.stringifyBigInts = stringifyBigInts;
  10. module.exports.unstringifyBigInts = unstringifyBigInts;
  11. module.exports.sameSizes = sameSizes;
  12. module.exports.isDefined = isDefined;
  13. module.exports.accSizes2Str = accSizes2Str;
  14. function ident(text) {
  15. if (typeof text === "string") {
  16. let lines = text.split("\n");
  17. for (let i=0; i<lines.length; i++) {
  18. if (lines[i]) lines[i] = " "+lines[i];
  19. }
  20. return lines.join("\n");
  21. } else if (Array.isArray(text)) {
  22. for (let i=0; i<text.length; i++ ) {
  23. text[i] = ident(text[i]);
  24. }
  25. return text;
  26. }
  27. }
  28. function extractSizes (o) {
  29. if (! Array.isArray(o)) return [];
  30. return [o.length, ...extractSizes(o[0])];
  31. }
  32. function flatArray(a) {
  33. var res = [];
  34. fillArray(res, a);
  35. return res;
  36. function fillArray(res, a) {
  37. if (Array.isArray(a)) {
  38. for (let i=0; i<a.length; i++) {
  39. fillArray(res, a[i]);
  40. }
  41. } else {
  42. res.push(bigInt(a));
  43. }
  44. }
  45. }
  46. // Input [1,2,3]
  47. // Returns " ,1 ,2, 3"
  48. function csArr(_arr) {
  49. let S = "";
  50. const arr = _arr || [];
  51. for (let i=0; i<arr.length; i++) {
  52. S = " ,"+arr[i];
  53. }
  54. return S;
  55. }
  56. function accSizes(_sizes) {
  57. const sizes = _sizes || [];
  58. const accSizes = [1, 0];
  59. for (let i=sizes.length-1; i>=0; i--) {
  60. accSizes.unshift(accSizes[0]*sizes[i]);
  61. }
  62. return accSizes;
  63. }
  64. function fnvHash(str) {
  65. return fnv.hash(str, 64).hex();
  66. }
  67. function stringifyBigInts(o) {
  68. if ((typeof(o) == "bigint") || o.eq !== undefined) {
  69. return o.toString(10);
  70. } else if (Array.isArray(o)) {
  71. return o.map(stringifyBigInts);
  72. } else if (typeof o == "object") {
  73. const res = {};
  74. for (let k in o) {
  75. res[k] = stringifyBigInts(o[k]);
  76. }
  77. return res;
  78. } else {
  79. return o;
  80. }
  81. }
  82. function unstringifyBigInts(o) {
  83. if ((typeof(o) == "string") && (/^[0-9]+$/.test(o) )) {
  84. return bigInt(o);
  85. } else if (Array.isArray(o)) {
  86. return o.map(unstringifyBigInts);
  87. } else if (typeof o == "object") {
  88. const res = {};
  89. for (let k in o) {
  90. res[k] = unstringifyBigInts(o[k]);
  91. }
  92. return res;
  93. } else {
  94. return bigInt(o);
  95. }
  96. }
  97. function sameSizes(s1, s2) {
  98. if (!Array.isArray(s1)) return false;
  99. if (!Array.isArray(s2)) return false;
  100. if (s1.length != s2.length) return false;
  101. for (let i=0; i<s1.length; i++) {
  102. if (s1[i] != s2[i]) return false;
  103. }
  104. return true;
  105. }
  106. function isDefined(v) {
  107. return ((typeof(v) != "undefined")&&(v != null));
  108. }
  109. function accSizes2Str(sizes) {
  110. if (sizes.length == 2) return "";
  111. return `[${sizes[0]/sizes[1]}]`+accSizes2Str(sizes.slice(1));
  112. }