functionalscript 0.11.9 → 0.11.10

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.11.9",
3
+ "version": "0.11.10",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "**/*.js",
@@ -162,6 +162,7 @@ export type BitOrder = {
162
162
  */
163
163
  readonly cmp: (a: Vec) => (b: Vec) => Sign;
164
164
  readonly unpackSplit: (len: bigint) => (u: Unpacked) => readonly [bigint, bigint];
165
+ readonly unpackConcat: (a: Unpacked) => (b: Unpacked) => Unpacked;
165
166
  };
166
167
  /**
167
168
  * Implements operations for handling vectors in a least-significant-bit (LSb) first order.
@@ -186,7 +187,7 @@ export declare const msb: BitOrder;
186
187
  * @param list The list of unsigned 8-bit integers to be converted.
187
188
  * @returns The resulting vector based on the provided bit order.
188
189
  */
189
- export declare const u8ListToVec: ({ concat }: BitOrder) => (list: List<number>) => Vec;
190
+ export declare const u8ListToVec: ({ unpackConcat }: BitOrder) => (list: List<number>) => Vec;
190
191
  /**
191
192
  * Converts a bit vector to a list of unsigned 8-bit integers based on the provided bit order.
192
193
  *
@@ -107,7 +107,7 @@ const op = (norm) => (op) => ap => bp => {
107
107
  const { a, b } = norm(au)(bu)(len);
108
108
  return vec(len)(op(a)(b));
109
109
  };
110
- const bo = ({ front, removeFront, concat, norm, uintCmp, unpackSplit }) => {
110
+ const bo = ({ front, removeFront, norm, uintCmp, unpackSplit, unpackConcatUint }) => {
111
111
  const unpackPopFront = (len) => {
112
112
  const m = mask(len);
113
113
  const us = unpackSplit(len);
@@ -116,10 +116,17 @@ const bo = ({ front, removeFront, concat, norm, uintCmp, unpackSplit }) => {
116
116
  return [uint & m, { length: v.length - len, uint: rest }];
117
117
  };
118
118
  };
119
+ const unpackConcat = (a) => (b) => ({
120
+ length: a.length + b.length, uint: unpackConcatUint(a)(b)
121
+ });
119
122
  return {
120
123
  front,
121
124
  removeFront,
122
- concat,
125
+ concat: a => b => {
126
+ const au = unpack(a);
127
+ const bu = unpack(b);
128
+ return pack(unpackConcat(au)(bu));
129
+ },
123
130
  xor: op(norm)(xor),
124
131
  unpackPopFront,
125
132
  popFront: len => {
@@ -140,8 +147,10 @@ const bo = ({ front, removeFront, concat, norm, uintCmp, unpackSplit }) => {
140
147
  return c === 0 ? cmp(al)(bl) : c;
141
148
  },
142
149
  unpackSplit,
150
+ unpackConcat,
143
151
  };
144
152
  };
153
+ const lsbUnpackConcatUint = ({ uint: a, length }) => ({ uint: b }) => (b << length) | a;
145
154
  /**
146
155
  * Implements operations for handling vectors in a least-significant-bit (LSb) first order.
147
156
  *
@@ -158,17 +167,13 @@ export const lsb = bo({
158
167
  const { length, uint } = unpack(v);
159
168
  return vec(length - len)(uint >> len);
160
169
  },
161
- concat: (a) => (b) => {
162
- const { length: al, uint: au } = unpack(a);
163
- const { length: bl, uint: bu } = unpack(b);
164
- return vec(al + bl)((bu << al) | au);
165
- },
166
170
  norm: ({ uint: a }) => ({ uint: b }) => () => ({ a, b }),
167
171
  uintCmp: a => b => {
168
172
  const diff = a ^ b;
169
173
  return diff === 0n ? 0 : (a & (diff & -diff)) === 0n ? -1 : 1;
170
174
  },
171
- unpackSplit: len => ({ uint }) => [uint, uint >> len]
175
+ unpackSplit: len => ({ uint }) => [uint, uint >> len],
176
+ unpackConcatUint: lsbUnpackConcatUint
172
177
  });
173
178
  /**
174
179
  * Implements operations for handling vectors in a most-significant-bit (MSb) first order.
@@ -189,11 +194,12 @@ export const msb = bo({
189
194
  const { length, uint } = unpack(v);
190
195
  return vec(length - len)(uint);
191
196
  },
192
- concat: flip(lsb.concat),
193
197
  norm: ({ length: al, uint: a }) => ({ length: bl, uint: b }) => len => ({ a: a << (len - al), b: b << (len - bl) }),
194
198
  uintCmp: cmp,
195
- unpackSplit: len => ({ length, uint }) => [uint >> (length - len), uint]
199
+ unpackSplit: len => ({ length, uint }) => [uint >> (length - len), uint],
200
+ unpackConcatUint: flip(lsbUnpackConcatUint),
196
201
  });
202
+ const unpackEmpty = { length: 0n, uint: 0n };
197
203
  /**
198
204
  * Converts a list of unsigned 8-bit integers to a bit vector using the provided bit order.
199
205
  *
@@ -201,16 +207,10 @@ export const msb = bo({
201
207
  * @param list The list of unsigned 8-bit integers to be converted.
202
208
  * @returns The resulting vector based on the provided bit order.
203
209
  */
204
- export const u8ListToVec = ({ concat }) => (list) => {
205
- // much faster than: `fold(appendU8(bo))(empty)(list)`
206
- // where `appendU8` is defined as
207
- // ```
208
- // const appendU8 = ({ concat }: BitOrder) => (u8: number) => (a: Vec) =>
209
- // concat(a)(vec8(BigInt(u8)))
210
- // ```
210
+ export const u8ListToVec = ({ unpackConcat }) => (list) => {
211
211
  let result = [];
212
212
  for (const b of iterable(list)) {
213
- let v = vec8(BigInt(b));
213
+ let v = { length: 8n, uint: BigInt(b) };
214
214
  let i = 0;
215
215
  while (true) {
216
216
  if (result.length <= i) {
@@ -218,16 +218,16 @@ export const u8ListToVec = ({ concat }) => (list) => {
218
218
  break;
219
219
  }
220
220
  const old = result[i];
221
- if (old === empty) {
221
+ if (old.length === 0n) {
222
222
  result = result.toSpliced(i, 1, v);
223
223
  break;
224
224
  }
225
- result = result.toSpliced(i, 1, empty);
226
- v = concat(old)(v);
225
+ result = result.toSpliced(i, 1, unpackEmpty);
226
+ v = unpackConcat(old)(v);
227
227
  i++;
228
228
  }
229
229
  }
230
- return result.reduce((p, c) => concat(c)(p), empty);
230
+ return pack(result.reduce((p, c) => unpackConcat(c)(p), unpackEmpty));
231
231
  };
232
232
  /**
233
233
  * Converts a bit vector to a list of unsigned 8-bit integers based on the provided bit order.