functionalscript 0.3.6 → 0.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/com/cpp/module.f.js +0 -1
  2. package/com/cs/module.f.js +5 -6
  3. package/com/rust/module.f.d.ts +3 -3
  4. package/com/rust/module.f.js +24 -29
  5. package/com/test/build.f.js +2 -2
  6. package/com/types/module.f.d.ts +1 -1
  7. package/com/types/module.f.js +1 -2
  8. package/com/types/testlib.f.js +0 -1
  9. package/commonjs/build/module.f.d.ts +2 -2
  10. package/commonjs/build/module.f.js +3 -6
  11. package/commonjs/build/test.f.js +0 -4
  12. package/commonjs/module/function/module.f.d.ts +1 -1
  13. package/commonjs/module/function/module.f.js +1 -1
  14. package/commonjs/module/module.f.d.ts +1 -1
  15. package/commonjs/module/module.f.js +2 -3
  16. package/commonjs/path/module.f.d.ts +5 -5
  17. package/commonjs/path/module.f.js +6 -8
  18. package/commonjs/path/test.f.js +1 -2
  19. package/crypto/prime_field/module.f.d.ts +2 -2
  20. package/crypto/prime_field/module.f.js +3 -4
  21. package/crypto/secp/module.f.d.ts +2 -2
  22. package/crypto/secp/module.f.js +4 -5
  23. package/crypto/sha2/module.f.d.ts +75 -12
  24. package/crypto/sha2/module.f.js +246 -139
  25. package/crypto/sha2/test.f.d.ts +22 -3
  26. package/crypto/sha2/test.f.js +120 -67
  27. package/dev/test/module.f.d.ts +1 -1
  28. package/dev/test/module.f.js +4 -5
  29. package/dev/test.f.js +0 -4
  30. package/djs/module.f.d.ts +1 -1
  31. package/djs/module.f.js +0 -1
  32. package/djs/parser/module.f.d.ts +1 -1
  33. package/djs/parser/module.f.js +0 -2
  34. package/djs/serializer/module.f.d.ts +2 -2
  35. package/djs/serializer/module.f.js +0 -2
  36. package/djs/tokenizer/module.f.js +0 -1
  37. package/fsc/test.f.js +0 -1
  38. package/html/module.f.d.ts +1 -1
  39. package/html/module.f.js +15 -11
  40. package/html/test.f.js +7 -7
  41. package/js/tokenizer/module.f.d.ts +2 -2
  42. package/js/tokenizer/module.f.js +1 -2
  43. package/json/parser/module.f.d.ts +2 -2
  44. package/json/parser/module.f.js +0 -3
  45. package/json/serializer/module.f.js +0 -2
  46. package/json/tokenizer/module.f.js +0 -1
  47. package/nanvm-lib/tests/test.f.d.ts +28 -0
  48. package/nanvm-lib/tests/test.f.js +45 -0
  49. package/package.json +1 -1
  50. package/text/ascii/module.f.d.ts +1 -1
  51. package/text/ascii/module.f.js +2 -3
  52. package/text/ascii/test.f.js +2 -4
  53. package/text/module.f.d.ts +18 -3
  54. package/text/module.f.js +22 -6
  55. package/text/sgr/module.f.js +1 -1
  56. package/text/test.f.d.ts +4 -1
  57. package/text/test.f.js +24 -16
  58. package/text/utf16/module.f.d.ts +5 -6
  59. package/text/utf16/module.f.js +9 -10
  60. package/text/utf16/test.f.js +29 -30
  61. package/text/utf8/module.f.d.ts +8 -5
  62. package/text/utf8/module.f.js +3 -5
  63. package/text/utf8/test.f.js +34 -35
  64. package/types/array/module.f.d.ts +12 -9
  65. package/types/array/module.f.js +10 -11
  66. package/types/array/test.f.d.ts +1 -0
  67. package/types/array/test.f.js +18 -17
  68. package/types/bigfloat/module.f.d.ts +1 -1
  69. package/types/bigfloat/module.f.js +7 -8
  70. package/types/bigfloat/test.f.js +1 -2
  71. package/types/bigint/module.f.d.ts +20 -11
  72. package/types/bigint/module.f.js +19 -20
  73. package/types/bigint/test.f.d.ts +1 -0
  74. package/types/bigint/test.f.js +7 -2
  75. package/types/bit_vec/module.f.d.ts +103 -83
  76. package/types/bit_vec/module.f.js +68 -110
  77. package/types/bit_vec/test.f.d.ts +20 -8
  78. package/types/bit_vec/test.f.js +110 -117
  79. package/types/btree/find/test.f.js +3 -4
  80. package/types/btree/module.f.d.ts +3 -3
  81. package/types/btree/module.f.js +2 -5
  82. package/types/btree/remove/test.f.js +2 -3
  83. package/types/btree/set/test.f.js +3 -6
  84. package/types/btree/test.f.js +0 -1
  85. package/types/btree/types/module.f.d.ts +3 -3
  86. package/types/byte_set/module.f.d.ts +2 -2
  87. package/types/byte_set/module.f.js +0 -2
  88. package/types/function/compare/module.f.d.ts +1 -1
  89. package/types/function/compare/module.f.js +0 -1
  90. package/types/function/compare/test.f.js +1 -2
  91. package/types/function/module.f.d.ts +1 -1
  92. package/types/function/module.f.js +1 -1
  93. package/types/function/operator/module.f.d.ts +2 -3
  94. package/types/function/operator/module.f.js +5 -5
  95. package/types/function/test.f.js +1 -2
  96. package/types/list/module.f.d.ts +1 -1
  97. package/types/list/module.f.js +9 -9
  98. package/types/list/test.f.js +2 -4
  99. package/types/map/module.f.d.ts +6 -6
  100. package/types/map/module.f.js +6 -15
  101. package/types/map/test.f.js +1 -2
  102. package/types/monoid/module.f.d.ts +73 -0
  103. package/types/monoid/module.f.js +42 -0
  104. package/types/monoid/test.f.d.ts +5 -0
  105. package/types/monoid/test.f.js +27 -0
  106. package/types/nibble_set/module.f.d.ts +2 -3
  107. package/types/nibble_set/test.f.js +1 -2
  108. package/types/number/test.f.js +1 -2
  109. package/types/object/module.f.d.ts +5 -5
  110. package/types/object/module.f.js +2 -4
  111. package/types/range_map/module.f.d.ts +4 -4
  112. package/types/range_map/module.f.js +3 -9
  113. package/types/result/module.f.d.ts +2 -3
  114. package/types/sorted_list/module.f.d.ts +6 -6
  115. package/types/sorted_list/module.f.js +2 -6
  116. package/types/sorted_list/test.f.js +4 -9
  117. package/types/sorted_set/module.f.d.ts +1 -1
  118. package/types/sorted_set/module.f.js +2 -5
  119. package/types/sorted_set/test.f.js +4 -9
  120. package/types/string/module.f.d.ts +3 -3
  121. package/types/string/module.f.js +1 -2
  122. package/types/string/test.f.js +2 -4
  123. package/types/string_set/module.f.d.ts +1 -1
  124. package/types/string_set/module.f.js +5 -11
@@ -1,8 +1,7 @@
1
- import * as bi from "../bigint/module.f.js";
2
- const { abs, sign } = bi;
1
+ import { abs, sign } from "../bigint/module.f.js";
3
2
  const twoPow53 = 9007199254740992n;
4
3
  const twoPow54 = 18014398509481984n;
5
- const increaseMantissa = ([m, e]) => min => {
4
+ const increaseMantissa = ([m, e]) => (min) => {
6
5
  if (m === 0n) {
7
6
  return [m, e];
8
7
  }
@@ -16,7 +15,7 @@ const increaseMantissa = ([m, e]) => min => {
16
15
  e--;
17
16
  }
18
17
  };
19
- const decreaseMantissa = ([m, e]) => max => {
18
+ const decreaseMantissa = ([m, e]) => (max) => {
20
19
  if (m === 0n) {
21
20
  return [m, e];
22
21
  }
@@ -30,10 +29,10 @@ const decreaseMantissa = ([m, e]) => max => {
30
29
  e++;
31
30
  }
32
31
  };
33
- const pow = base => exp => base ** BigInt(exp);
32
+ const pow = (base) => (exp) => base ** BigInt(exp);
34
33
  const pow5 = pow(5n);
35
- export const multiply = ([m, e]) => mul => [m * mul, e];
36
- const divide = ([m, e]) => div => [[m / div, e], m % div];
34
+ export const multiply = ([m, e]) => (mul) => [m * mul, e];
35
+ const divide = ([m, e]) => (div) => [[m / div, e], m % div];
37
36
  const round53 = ([[m, e], r]) => {
38
37
  const mabs = abs(m);
39
38
  const s = BigInt(sign(m));
@@ -47,7 +46,7 @@ const round53 = ([[m, e], r]) => {
47
46
  }
48
47
  return multiply([m53 + o54, e53])(s);
49
48
  };
50
- export const decToBin = dec => {
49
+ export const decToBin = (dec) => {
51
50
  if (dec[0] === 0n) {
52
51
  return [0n, 0];
53
52
  }
@@ -1,5 +1,4 @@
1
- import * as _ from "./module.f.js";
2
- const { decToBin } = _;
1
+ import { decToBin } from "./module.f.js";
3
2
  export default {
4
3
  decToBin: [
5
4
  () => {
@@ -1,16 +1,13 @@
1
1
  import * as compare from '../function/compare/module.f.ts';
2
- import * as Operator from '../function/operator/module.f.ts';
2
+ import type * as Operator from '../function/operator/module.f.ts';
3
3
  import { type List } from '../list/module.f.ts';
4
- export declare const addition: (a: bigint) => (b: bigint) => bigint;
4
+ export type Unary = Operator.Unary<bigint, bigint>;
5
+ export type Reduce = Operator.Reduce<bigint>;
6
+ export declare const addition: Reduce;
5
7
  export declare const sum: (input: List<bigint>) => bigint;
6
- export declare const abs: (a: bigint) => bigint;
8
+ export declare const abs: Unary;
7
9
  export declare const sign: (a: bigint) => compare.Sign;
8
10
  export declare const serialize: (a: bigint) => string;
9
- type Additive<T> = {
10
- readonly 0: T;
11
- readonly add: Operator.Reduce<T>;
12
- };
13
- export declare const scalar_mul: <T>(a: Additive<T>) => (a: T) => (n: bigint) => T;
14
11
  /**
15
12
  * Calculates the base-2 logarithm (floor).
16
13
  *
@@ -47,8 +44,20 @@ export declare const log2: (v: bigint) => bigint;
47
44
  * @returns The bit length of the input BigInt.
48
45
  *
49
46
  * @remark
50
- * The function uses the `log2` function to calculate the position of the most significant bit(MSB)
51
- * and adds `1n` to account for the MSB itself.For negative numbers, the absolute value is used.
47
+ * The function uses the `log2` function to calculate the position of the most significant bit (MSB)
48
+ * and adds `1n` to account for the MSB itself. For negative numbers, the absolute value is used.
52
49
  */
53
50
  export declare const bitLength: (v: bigint) => bigint;
54
- export {};
51
+ /**
52
+ * Generates a bitmask with the specified number of bits set to 1.
53
+ *
54
+ * @param len - The number of bits to set in the mask. Must be a non-negative integer.
55
+ * @returns A bigint representing the bitmask, where the least significant `len` bits are 1.
56
+ *
57
+ * @example
58
+ *
59
+ * ```js
60
+ * const result = mask(3n) // 7n
61
+ * ```
62
+ */
63
+ export declare const mask: (len: bigint) => bigint;
@@ -1,27 +1,11 @@
1
1
  import * as compare from "../function/compare/module.f.js";
2
- import * as Operator from "../function/operator/module.f.js";
3
2
  const { unsafeCmp } = compare;
4
3
  import { reduce } from "../list/module.f.js";
5
4
  export const addition = a => b => a + b;
6
5
  export const sum = reduce(addition)(0n);
7
6
  export const abs = a => a >= 0 ? a : -a;
8
- export const sign = a => unsafeCmp(a)(0n);
9
- export const serialize = a => `${a}n`;
10
- export const scalar_mul = ({ 0: _0, add }) => a => n => {
11
- let ai = a;
12
- let ni = n;
13
- let result = _0;
14
- while (true) {
15
- if ((ni & 1n) === 1n) {
16
- result = add(result)(ai);
17
- }
18
- ni >>= 1n;
19
- if (ni === 0n) {
20
- return result;
21
- }
22
- ai = add(ai)(ai);
23
- }
24
- };
7
+ export const sign = (a) => unsafeCmp(a)(0n);
8
+ export const serialize = (a) => `${a}n`;
25
9
  /**
26
10
  * Calculates the base-2 logarithm (floor).
27
11
  *
@@ -41,6 +25,8 @@ export const scalar_mul = ({ 0: _0, add }) => a => n => {
41
25
  * determining the exact value of the logarithm.
42
26
  */
43
27
  export const log2 = (v) => {
28
+ // TODO: use step 32 and `Math.clz32()` at the end.
29
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/clz32
44
30
  if (v <= 0n) {
45
31
  return -1n;
46
32
  }
@@ -85,8 +71,8 @@ export const log2 = (v) => {
85
71
  * @returns The bit length of the input BigInt.
86
72
  *
87
73
  * @remark
88
- * The function uses the `log2` function to calculate the position of the most significant bit(MSB)
89
- * and adds `1n` to account for the MSB itself.For negative numbers, the absolute value is used.
74
+ * The function uses the `log2` function to calculate the position of the most significant bit (MSB)
75
+ * and adds `1n` to account for the MSB itself. For negative numbers, the absolute value is used.
90
76
  */
91
77
  export const bitLength = (v) => {
92
78
  if (v <= 0n) {
@@ -97,3 +83,16 @@ export const bitLength = (v) => {
97
83
  }
98
84
  return log2(v) + 1n;
99
85
  };
86
+ /**
87
+ * Generates a bitmask with the specified number of bits set to 1.
88
+ *
89
+ * @param len - The number of bits to set in the mask. Must be a non-negative integer.
90
+ * @returns A bigint representing the bitmask, where the least significant `len` bits are 1.
91
+ *
92
+ * @example
93
+ *
94
+ * ```js
95
+ * const result = mask(3n) // 7n
96
+ * ```
97
+ */
98
+ export const mask = (len) => (1n << len) - 1n;
@@ -1,4 +1,5 @@
1
1
  declare const _default: {
2
+ mask: () => void;
2
3
  sum: () => void;
3
4
  abs: (() => void)[];
4
5
  serialize: (() => void)[];
@@ -1,6 +1,11 @@
1
- import * as _ from "./module.f.js";
2
- const { sum, abs, serialize, log2, bitLength } = _;
1
+ import { sum, abs, serialize, log2, bitLength, mask } from "./module.f.js";
3
2
  export default {
3
+ mask: () => {
4
+ const result = mask(3n); // 7n
5
+ if (result !== 7n) {
6
+ throw result;
7
+ }
8
+ },
4
9
  sum: () => {
5
10
  const result = sum([2n, 3n, 4n, 5n]);
6
11
  if (result !== 14n) {
@@ -1,7 +1,8 @@
1
+ import { type List, type Thunk } from '../list/module.f.ts';
1
2
  /**
2
3
  * A vector of bits represented as a `bigint`.
3
4
  */
4
- type Vec = bigint;
5
+ export type Vec = bigint;
5
6
  /**
6
7
  * An empty vector of bits.
7
8
  */
@@ -22,6 +23,10 @@ export declare const length: (v: bigint) => bigint;
22
23
  * ```
23
24
  */
24
25
  export declare const vec: (len: bigint) => (ui: bigint) => Vec;
26
+ /**
27
+ * Creates an 8 bit vector from an unsigned integer.
28
+ */
29
+ export declare const vec8: (u: bigint) => Vec;
25
30
  /**
26
31
  * Returns the unsigned integer of the given vector by removing a stop bit.
27
32
  *
@@ -34,101 +39,116 @@ export declare const vec: (len: bigint) => (ui: bigint) => Vec;
34
39
  */
35
40
  export declare const uint: (v: Vec) => bigint;
36
41
  /**
37
- * Extract the least significant unsigned integer from the given vector.
38
- *
39
- * @example
42
+ * Represents operations for handling bit vectors with a specific bit order.
40
43
  *
41
- * ```js
42
- * const vector = vec(8n)(0xF5n) // 0x1F5n
43
- * const result = uintLsb(4n)(vector); // result is 5n
44
- * const result2 = uintLsb(16n)(vector); // result2 is 0xF5n
45
- * ```
44
+ * https://en.wikipedia.org/wiki/Bit_numbering
46
45
  */
47
- export declare const uintLsb: (len: bigint) => (v: Vec) => bigint;
46
+ export type BitOrder = {
47
+ /**
48
+ * Retrieves the first unsigned integer of the specified length from the given vector.
49
+ *
50
+ * @param len - The number of bits to read from the start of the vector.
51
+ * @returns A function that takes a vector and returns the extracted unsigned integer.
52
+ *
53
+ * @example
54
+ *
55
+ * ```js
56
+ * const vector = vec(8n)(0xF5n) // 0x1F5n
57
+ *
58
+ * const resultL0 = lsb.front(4n)(vector) // resultL0 is 5n
59
+ * const resultL1 = lsb.front(16n)(vector) // resultL1 is 0xF5n
60
+ *
61
+ * const resultM0 = msb.front(4n)(vector) // resultM0 is 0xFn
62
+ * const resultM1 = msb.front(16n)(vector) // resultM1 is 0xF500n
63
+ * ```
64
+ */
65
+ readonly front: (len: bigint) => (v: Vec) => bigint;
66
+ /**
67
+ * Removes a specified number of bits from the start of the given vector.
68
+ *
69
+ * @param len - The number of bits to remove from the vector.
70
+ * @returns A function that takes a vector and returns the remaining vector.
71
+ *
72
+ * @example
73
+ *
74
+ * ```js
75
+ * const v = vec(16n)(0x3456n) // 0x13456n
76
+ *
77
+ * const rL0 = lsb.removeFront(4n)(v) // 0x1345n
78
+ * const rL1 = lsb.removeFront(24n)(v) // 0x1n
79
+ *
80
+ * const rM0 = msb.removeFront(4n)(v) // 0x1456n
81
+ * const rM1 = msb.removeFront(24n)(v) // 0x1n
82
+ * ```
83
+ */
84
+ readonly removeFront: (len: bigint) => (v: Vec) => Vec;
85
+ /**
86
+ * Removes a specified number of bits from the start of the vector and returns
87
+ * the removed bits and the remaining vector.
88
+ *
89
+ * @param len - The number of bits to remove from the vector.
90
+ * @returns A function that takes a vector and returns
91
+ * a tuple containing the removed bits as an unsigned integer and the remaining vector.
92
+ *
93
+ * ```js
94
+ * const vector = vec(8n)(0xF5n) // 0x1F5n
95
+ *
96
+ * const [uL0, rL0] = lsb.popFront(4n)(vector) // [5n, 0x1Fn]
97
+ * const [uL1, rL1] = lsb.popFront(16n)(vector) // [0xF5n, 1n]
98
+ *
99
+ * const [uM0, rM0] = msb.popFront(4n)(vector) // [0xFn, 0x15n]
100
+ * const [uM1, rM1] = msb.popFront(16n)(vector) // [0xF500n, 1n]
101
+ * ```
102
+ */
103
+ readonly popFront: (len: bigint) => (v: Vec) => readonly [bigint, Vec];
104
+ /**
105
+ * Concatenates two vectors.
106
+ *
107
+ * @param a - The first vector.
108
+ * @returns A function that takes a second vector and returns the concatenated vector.
109
+ *
110
+ * @example
111
+ *
112
+ * ```js
113
+ * const u8 = vec(8n)
114
+ * const a = u8(0x45n) // 0x145n
115
+ * const b = u8(0x89n) // 0x189n
116
+ *
117
+ * const abL = lsb.concat(a)(b) // 0x18945n
118
+ * const abM = msb.concat(a)(b) // 0x14589n
119
+ * ```
120
+ */
121
+ readonly concat: (a: Vec) => (b: Vec) => Vec;
122
+ };
48
123
  /**
49
- * Removes the first `len` least significant bits from the given vector.
124
+ * Implements operations for handling vectors in a least-significant-bit (LSb) first order.
50
125
  *
51
- * @example
126
+ * https://en.wikipedia.org/wiki/Bit_numbering#LSb_0_bit_numbering
52
127
  *
53
- * ```js
54
- * const v = vec(16n)(0x3456n) // 0x13456n
55
- * const r = removeLsb(4n)(v) // 0x1345n
56
- * const r2 = removeLsb(24n)(v) // 0x1n
57
- * ```
128
+ * Usually associated with Little-Endian (LE) byte order.
58
129
  */
59
- export declare const removeLsb: (len: bigint) => (v: Vec) => Vec;
130
+ export declare const lsb: BitOrder;
60
131
  /**
61
- * Extracts the least significant unsigned integer and removes it from the vector.
132
+ * Implements operations for handling vectors in a most-significant-bit (MSb) first order.
62
133
  *
63
- * @example
134
+ * https://en.wikipedia.org/wiki/Bit_numbering#MSb_0_bit_numbering
64
135
  *
65
- * ```js
66
- * const vector = vec(8n)(0xF5n) // 0x1F5n
67
- * const [result, rest] = popUintLsb(4n)(vector); // result is 5n, rest is 0x1Fn
68
- * const [result2, rest2] = popUintLsb(16n)(vector); // result2 is 0xF5n, rest2 is 1n
69
- * ```
136
+ * Usually associated with Big-Endian (BE) byte order.
70
137
  */
71
- export declare const popUintLsb: (len: bigint) => (v: Vec) => readonly [bigint, Vec];
138
+ export declare const msb: BitOrder;
72
139
  /**
73
- * Extract the most significant unsigned integer of the given `len` from the given vector.
74
- *
75
- * @example
140
+ * Converts a list of unsigned 8-bit integers to a bit vector.
76
141
  *
77
- * ```js
78
- * const vector = vec(8n)(0xF5n) // 0x1F5n
79
- * const result = uintMsb(4n)(vector); // result is 0xFn
80
- * const result2 = uintMsb(16n)(vector); // result2 is 0xF500n
81
- * ```
142
+ * @param bo The bit order for the conversion
143
+ * @param list The list of unsigned 8-bit integers to be converted.
144
+ * @returns The resulting vector based on the provided bit order.
82
145
  */
83
- export declare const uintMsb: (len: bigint) => (v: Vec) => bigint;
146
+ export declare const u8ListToVec: (bo: BitOrder) => (list: List<number>) => Vec;
84
147
  /**
85
- * Removes the first `len` most significant bits from the given vector.
148
+ * Converts a bit vector to a list of unsigned 8-bit integers based on the provided bit order.
86
149
  *
87
- * @example
88
- *
89
- * ```js
90
- * const v = vec(16n)(0x3456n) // 0x13456n
91
- * const r = removeMsb(4n)(v) // 0x1456n
92
- * const r2 = removeMsb(24n)(v) // 0x1n
93
- * ```
94
- */
95
- export declare const removeMsb: (len: bigint) => (v: Vec) => Vec;
96
- /**
97
- * Extracts the most significant unsigned integer and removes it from the vector.
98
- *
99
- * @example
100
- *
101
- * ```js
102
- * const vector = vec(8n)(0xF5n) // 0x1F5n
103
- * const [result, rest] = popUintMsb(4n)(vector); // [0xFn, 0x15n]
104
- * const [result2, rest2] = popUintMsb(16n)(vector); // [0xF500n, 1n]
105
- * ```
106
- */
107
- export declare const popUintMsb: (len: bigint) => (v: Vec) => readonly [bigint, Vec];
108
- /**
109
- * Concat the given vectors of bits. The first vector is the least significant.
110
- *
111
- * @example
112
- *
113
- * ```js
114
- * const u8 = vec(8n)
115
- * const a = u8(0x45n) // 0x145n
116
- * const b = u8(0x89n) // 0x189n
117
- * const ab = concatLsb(a)(b) // 0x18945n
118
- * ```
119
- */
120
- export declare const concatLsb: (a: Vec) => (b: Vec) => Vec;
121
- /**
122
- * Concat the given vectors of bits. The first vector is the most significant.
123
- *
124
- * @example
125
- *
126
- * ```js
127
- * const u8 = vec(8n)
128
- * const a = u8(0x45n) // 0x145n
129
- * const b = u8(0x89n) // 0x189n
130
- * const ab = concatMsb(a)(b) // 0x14589n
131
- * ```
150
+ * @param bitOrder The bit order for the conversion.
151
+ * @param v The vector to be converted.
152
+ * @returns A thunk that produces a list of unsigned 8-bit integers.
132
153
  */
133
- export declare const concatMsb: (b: Vec) => (a: Vec) => Vec;
134
- export {};
154
+ export declare const u8List: ({ popFront }: BitOrder) => (v: Vec) => Thunk<number>;
@@ -1,5 +1,6 @@
1
- import { log2 } from "../bigint/module.f.js";
1
+ import { log2, mask } from "../bigint/module.f.js";
2
2
  import { flip } from "../function/module.f.js";
3
+ import { fold } from "../list/module.f.js";
3
4
  /**
4
5
  * An empty vector of bits.
5
6
  */
@@ -27,7 +28,10 @@ export const vec = (len) => {
27
28
  const mask = stop - 1n;
28
29
  return data => stop | (data & mask);
29
30
  };
30
- const mask = (len) => (1n << len) - 1n;
31
+ /**
32
+ * Creates an 8 bit vector from an unsigned integer.
33
+ */
34
+ export const vec8 = vec(8n);
31
35
  /**
32
36
  * Returns the unsigned integer of the given vector by removing a stop bit.
33
37
  *
@@ -40,128 +44,82 @@ const mask = (len) => (1n << len) - 1n;
40
44
  */
41
45
  export const uint = (v) => v ^ (1n << length(v));
42
46
  /**
43
- * Extract the least significant unsigned integer from the given vector.
47
+ * Implements operations for handling vectors in a least-significant-bit (LSb) first order.
44
48
  *
45
- * @example
49
+ * https://en.wikipedia.org/wiki/Bit_numbering#LSb_0_bit_numbering
46
50
  *
47
- * ```js
48
- * const vector = vec(8n)(0xF5n) // 0x1F5n
49
- * const result = uintLsb(4n)(vector); // result is 5n
50
- * const result2 = uintLsb(16n)(vector); // result2 is 0xF5n
51
- * ```
51
+ * Usually associated with Little-Endian (LE) byte order.
52
52
  */
53
- export const uintLsb = (len) => {
54
- const m = mask(len);
55
- return v => {
56
- const result = v & m;
57
- return result === v ? uint(v) : result;
58
- };
59
- };
60
- /**
61
- * Removes the first `len` least significant bits from the given vector.
62
- *
63
- * @example
64
- *
65
- * ```js
66
- * const v = vec(16n)(0x3456n) // 0x13456n
67
- * const r = removeLsb(4n)(v) // 0x1345n
68
- * const r2 = removeLsb(24n)(v) // 0x1n
69
- * ```
70
- */
71
- export const removeLsb = (len) => (v) => {
72
- const r = v >> len;
73
- return r === 0n ? empty : r;
74
- };
75
- /**
76
- * Extracts the least significant unsigned integer and removes it from the vector.
77
- *
78
- * @example
79
- *
80
- * ```js
81
- * const vector = vec(8n)(0xF5n) // 0x1F5n
82
- * const [result, rest] = popUintLsb(4n)(vector); // result is 5n, rest is 0x1Fn
83
- * const [result2, rest2] = popUintLsb(16n)(vector); // result2 is 0xF5n, rest2 is 1n
84
- * ```
85
- */
86
- export const popUintLsb = (len) => {
87
- const m = mask(len);
88
- return v => {
89
- const result = v & m;
90
- return result === v ? [uint(v), empty] : [result, v >> len];
91
- };
53
+ export const lsb = {
54
+ front: len => {
55
+ const m = mask(len);
56
+ return v => {
57
+ const result = v & m;
58
+ return result === v ? uint(v) : result;
59
+ };
60
+ },
61
+ removeFront: len => v => {
62
+ const r = v >> len;
63
+ return r === 0n ? empty : r;
64
+ },
65
+ popFront: len => {
66
+ const m = mask(len);
67
+ return v => {
68
+ const result = v & m;
69
+ return result === v ? [uint(v), empty] : [result, v >> len];
70
+ };
71
+ },
72
+ concat: a => {
73
+ const aLen = length(a);
74
+ const m = mask(aLen);
75
+ return b => (b << aLen) | (a & m);
76
+ }
92
77
  };
93
78
  /**
94
- * Extract the most significant unsigned integer of the given `len` from the given vector.
79
+ * Implements operations for handling vectors in a most-significant-bit (MSb) first order.
95
80
  *
96
- * @example
81
+ * https://en.wikipedia.org/wiki/Bit_numbering#MSb_0_bit_numbering
97
82
  *
98
- * ```js
99
- * const vector = vec(8n)(0xF5n) // 0x1F5n
100
- * const result = uintMsb(4n)(vector); // result is 0xFn
101
- * const result2 = uintMsb(16n)(vector); // result2 is 0xF500n
102
- * ```
83
+ * Usually associated with Big-Endian (BE) byte order.
103
84
  */
104
- export const uintMsb = (len) => {
105
- const m = mask(len);
106
- return v => (v >> (length(v) - len)) & m;
85
+ export const msb = {
86
+ front: len => {
87
+ const m = mask(len);
88
+ return v => (v >> (length(v) - len)) & m;
89
+ },
90
+ removeFront: len => v => vec(length(v) - len)(v),
91
+ popFront: len => {
92
+ const m = mask(len);
93
+ return v => {
94
+ const d = length(v) - len;
95
+ return [(v >> d) & m, vec(d)(v)];
96
+ };
97
+ },
98
+ concat: flip(lsb.concat)
107
99
  };
100
+ const appendU8 = ({ concat }) => (u8) => (a) => concat(a)(vec8(BigInt(u8)));
108
101
  /**
109
- * Removes the first `len` most significant bits from the given vector.
110
- *
111
- * @example
102
+ * Converts a list of unsigned 8-bit integers to a bit vector.
112
103
  *
113
- * ```js
114
- * const v = vec(16n)(0x3456n) // 0x13456n
115
- * const r = removeMsb(4n)(v) // 0x1456n
116
- * const r2 = removeMsb(24n)(v) // 0x1n
117
- * ```
104
+ * @param bo The bit order for the conversion
105
+ * @param list The list of unsigned 8-bit integers to be converted.
106
+ * @returns The resulting vector based on the provided bit order.
118
107
  */
119
- export const removeMsb = (len) => (v) => vec(length(v) - len)(v);
108
+ export const u8ListToVec = (bo) => fold(appendU8(bo))(empty);
120
109
  /**
121
- * Extracts the most significant unsigned integer and removes it from the vector.
110
+ * Converts a bit vector to a list of unsigned 8-bit integers based on the provided bit order.
122
111
  *
123
- * @example
124
- *
125
- * ```js
126
- * const vector = vec(8n)(0xF5n) // 0x1F5n
127
- * const [result, rest] = popUintMsb(4n)(vector); // [0xFn, 0x15n]
128
- * const [result2, rest2] = popUintMsb(16n)(vector); // [0xF500n, 1n]
129
- * ```
112
+ * @param bitOrder The bit order for the conversion.
113
+ * @param v The vector to be converted.
114
+ * @returns A thunk that produces a list of unsigned 8-bit integers.
130
115
  */
131
- export const popUintMsb = (len) => {
132
- const m = mask(len);
133
- return v => {
134
- const d = length(v) - len;
135
- return [(v >> d) & m, vec(d)(v)];
116
+ export const u8List = ({ popFront }) => {
117
+ const f = (v) => () => {
118
+ if (v === empty) {
119
+ return null;
120
+ }
121
+ const [first, tail] = popFront(8n)(v);
122
+ return { first: Number(first), tail: f(tail) };
136
123
  };
124
+ return f;
137
125
  };
138
- /**
139
- * Concat the given vectors of bits. The first vector is the least significant.
140
- *
141
- * @example
142
- *
143
- * ```js
144
- * const u8 = vec(8n)
145
- * const a = u8(0x45n) // 0x145n
146
- * const b = u8(0x89n) // 0x189n
147
- * const ab = concatLsb(a)(b) // 0x18945n
148
- * ```
149
- */
150
- export const concatLsb = (a) => {
151
- const aLen = length(a);
152
- const m = mask(aLen);
153
- return b => (b << aLen) | (a & m);
154
- };
155
- /**
156
- * Concat the given vectors of bits. The first vector is the most significant.
157
- *
158
- * @example
159
- *
160
- * ```js
161
- * const u8 = vec(8n)
162
- * const a = u8(0x45n) // 0x145n
163
- * const b = u8(0x89n) // 0x189n
164
- * const ab = concatMsb(a)(b) // 0x14589n
165
- * ```
166
- */
167
- export const concatMsb = flip(concatLsb);
@@ -2,14 +2,26 @@ declare const _default: {
2
2
  examples: {
3
3
  vec: () => void;
4
4
  uint: () => void;
5
- uintLsb: () => void;
6
- uintMsb: () => void;
7
- popUintLsb: () => void;
8
- popUintMsb: () => void;
9
- concatLsb: () => void;
10
- concatMsb: () => void;
11
- removeLsb: () => void;
12
- removeMsb: () => void;
5
+ front: () => void;
6
+ removeFront: () => void;
7
+ popFront: () => void;
8
+ concat: () => void;
9
+ };
10
+ front: {
11
+ lsbf: () => void;
12
+ msbf: () => void;
13
+ };
14
+ popFront: {
15
+ lsbm: () => void;
16
+ msbm: () => void;
17
+ };
18
+ removeFront: {
19
+ lsbm: () => void;
20
+ msbm: () => void;
21
+ };
22
+ concat: {
23
+ lsbm: () => void;
24
+ msbm: () => void;
13
25
  };
14
26
  uintLsb: () => void;
15
27
  uintSmall: () => void;