aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/build/packages/gap/src/gap_ffi.mjs
blob: 235c80bec4837c8cb9768da1b9aaee4c8784f043 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import {
  Error,
  List,
  Ok,
  inspect,
  toList,
  makeError,
  isEqual,
} from "./gleam.mjs";
import * as $option from "../gleam_stdlib/gleam/option.mjs";

const HASHCODE_CACHE = new WeakMap();
const Nil = undefined;

class MutableMap {
  static #hashcode_cache = new WeakMap();

  static hash(value) {
    let existing = this.#hashcode_cache.get(value);
    if (existing) {
      return existing;
    } else if (value instanceof Object) {
      let hashcode = inspect(value);
      HASHCODE_CACHE.set(value, hashcode);
      return hashcode;
    } else {
      return value.toString();
    }
  }

  constructor() {
    this.entries = new globalThis.Map();
  }

  get size() {
    return this.entries.size;
  }

  inspect() {
    let entries = [...this.entries.values()]
      .map((pair) => inspect(pair))
      .join(", ");
    return `map.from_list([${entries}])`;
  }

  toList() {
    return List.fromArray([...this.entries.values()]);
  }

  insert(k, v) {
    this.entries.set(MutableMap.hash(k), [k, v]);
    return this;
  }

  delete(k) {
    this.entries.delete(MutableMap.hash(k));
    return this;
  }

  get(key) {
    let code = MutableMap.hash(key);
    if (this.entries.has(code)) {
      return new Ok(this.entries.get(code)[1]);
    } else {
      return new Error(Nil);
    }
  }
}

export function new_mutable_map() {
  return new MutableMap();
}

export function mutable_map_size(map) {
  return map.size;
}

export function mutable_map_to_list(map) {
  return map.toList();
}

export function mutable_map_remove(k, map) {
  return map.delete(k);
}

export function mutable_map_get(map, key) {
  return map.get(key);
}

export function mutable_map_insert(key, value, map) {
  return map.insert(key, value);
}

// From map.mjs

export function size(map) {
  return mutable_map_size(map);
}

export function to_list(map) {
  return mutable_map_to_list(map);
}

export function new$() {
  return new_mutable_map();
}

export function get(from, get) {
  return mutable_map_get(from, get);
}

function do_has_key(key, map) {
  return !isEqual(get(map, key), new Error(undefined));
}

export function has_key(map, key) {
  return do_has_key(key, map);
}

export function insert(map, key, value) {
  return mutable_map_insert(key, value, map);
}

function insert_pair(map, pair) {
  return insert(map, pair[0], pair[1]);
}

export function update(map, key, fun) {
  let _pipe = map;
  let _pipe$1 = get(_pipe, key);
  let _pipe$2 = $option.from_result(_pipe$1);
  let _pipe$3 = fun(_pipe$2);
  return ((_capture) => {
    return insert(map, key, _capture);
  })(_pipe$3);
}

export function delete$(map, key) {
  return mutable_map_remove(key, map);
}

function fold_list_of_pair(loop$list, loop$initial) {
  while (true) {
    let list = loop$list;
    let initial = loop$initial;
    if (list.hasLength(0)) {
      return initial;
    } else if (list.atLeastLength(1)) {
      let x = list.head;
      let rest = list.tail;
      loop$list = rest;
      loop$initial = insert(initial, x[0], x[1]);
    } else {
      throw makeError(
        "case_no_match",
        "gleam/map",
        98,
        "fold_list_of_pair",
        "No case clause matched",
        { values: [list] }
      );
    }
  }
}

function do_from_list(list) {
  return fold_list_of_pair(list, new$());
}

export function from_list(list) {
  return do_from_list(list);
}

function do_fold(loop$list, loop$initial, loop$fun) {
  while (true) {
    let list = loop$list;
    let initial = loop$initial;
    let fun = loop$fun;
    if (list.hasLength(0)) {
      return initial;
    } else if (list.atLeastLength(1)) {
      let k = list.head[0];
      let v = list.head[1];
      let tail = list.tail;
      loop$list = tail;
      loop$initial = fun(initial, k, v);
      loop$fun = fun;
    } else {
      throw makeError(
        "case_no_match",
        "gleam/map",
        558,
        "do_fold",
        "No case clause matched",
        { values: [list] }
      );
    }
  }
}

export function fold(map, initial, fun) {
  let _pipe = map;
  let _pipe$1 = to_list(_pipe);
  return do_fold(_pipe$1, initial, fun);
}

function do_map_values(f, map) {
  let f$1 = (map, k, v) => {
    return insert(map, k, f(k, v));
  };
  let _pipe = map;
  return fold(_pipe, new$(), f$1);
}

export function map_values(map, fun) {
  return do_map_values(fun, map);
}

function do_filter(f, map) {
  let insert$1 = (map, k, v) => {
    let $ = f(k, v);
    if ($) {
      return insert(map, k, v);
    } else {
      return map;
    }
  };
  let _pipe = map;
  return fold(_pipe, new$(), insert$1);
}

export function filter(map, property) {
  return do_filter(property, map);
}

function do_keys_acc(loop$list, loop$acc) {
  while (true) {
    let list = loop$list;
    let acc = loop$acc;
    if (list.hasLength(0)) {
      return reverse_and_concat(acc, toList([]));
    } else if (list.atLeastLength(1)) {
      let x = list.head;
      let xs = list.tail;
      loop$list = xs;
      loop$acc = toList([x[0]], acc);
    } else {
      throw makeError(
        "case_no_match",
        "gleam/map",
        276,
        "do_keys_acc",
        "No case clause matched",
        { values: [list] }
      );
    }
  }
}

function do_keys(map) {
  let list_of_pairs = (() => {
    let _pipe = map;
    return to_list(_pipe);
  })();
  return do_keys_acc(list_of_pairs, toList([]));
}

export function keys(map) {
  return do_keys(map);
}

function reverse_and_concat(loop$remaining, loop$accumulator) {
  while (true) {
    let remaining = loop$remaining;
    let accumulator = loop$accumulator;
    if (remaining.hasLength(0)) {
      return accumulator;
    } else if (remaining.atLeastLength(1)) {
      let item = remaining.head;
      let rest = remaining.tail;
      loop$remaining = rest;
      loop$accumulator = toList([item], accumulator);
    } else {
      throw makeError(
        "case_no_match",
        "gleam/map",
        269,
        "reverse_and_concat",
        "No case clause matched",
        { values: [remaining] }
      );
    }
  }
}

function do_values_acc(loop$list, loop$acc) {
  while (true) {
    let list = loop$list;
    let acc = loop$acc;
    if (list.hasLength(0)) {
      return reverse_and_concat(acc, toList([]));
    } else if (list.atLeastLength(1)) {
      let x = list.head;
      let xs = list.tail;
      loop$list = xs;
      loop$acc = toList([x[1]], acc);
    } else {
      throw makeError(
        "case_no_match",
        "gleam/map",
        314,
        "do_values_acc",
        "No case clause matched",
        { values: [list] }
      );
    }
  }
}

function do_values(map) {
  let list_of_pairs = (() => {
    let _pipe = map;
    return to_list(_pipe);
  })();
  return do_values_acc(list_of_pairs, toList([]));
}

export function values(map) {
  return do_values(map);
}

function insert_taken(loop$map, loop$desired_keys, loop$acc) {
  while (true) {
    let map = loop$map;
    let desired_keys = loop$desired_keys;
    let acc = loop$acc;
    let insert$1 = (taken, key) => {
      let $ = get(map, key);
      if ($.isOk()) {
        let value = $[0];
        return insert(taken, key, value);
      } else {
        return taken;
      }
    };
    if (desired_keys.hasLength(0)) {
      return acc;
    } else if (desired_keys.atLeastLength(1)) {
      let x = desired_keys.head;
      let xs = desired_keys.tail;
      loop$map = map;
      loop$desired_keys = xs;
      loop$acc = insert$1(acc, x);
    } else {
      throw makeError(
        "case_no_match",
        "gleam/map",
        411,
        "insert_taken",
        "No case clause matched",
        { values: [desired_keys] }
      );
    }
  }
}

function do_take(desired_keys, map) {
  return insert_taken(map, desired_keys, new$());
}

export function take(map, desired_keys) {
  return do_take(desired_keys, map);
}

function fold_inserts(loop$new_entries, loop$map) {
  while (true) {
    let new_entries = loop$new_entries;
    let map = loop$map;
    if (new_entries.hasLength(0)) {
      return map;
    } else if (new_entries.atLeastLength(1)) {
      let x = new_entries.head;
      let xs = new_entries.tail;
      loop$new_entries = xs;
      loop$map = insert_pair(map, x);
    } else {
      throw makeError(
        "case_no_match",
        "gleam/map",
        451,
        "fold_inserts",
        "No case clause matched",
        { values: [new_entries] }
      );
    }
  }
}

function do_merge(map, new_entries) {
  let _pipe = new_entries;
  let _pipe$1 = to_list(_pipe);
  return fold_inserts(_pipe$1, map);
}

export function merge(map, new_entries) {
  return do_merge(map, new_entries);
}

export function drop(loop$map, loop$disallowed_keys) {
  while (true) {
    let map = loop$map;
    let disallowed_keys = loop$disallowed_keys;
    if (disallowed_keys.hasLength(0)) {
      return map;
    } else if (disallowed_keys.atLeastLength(1)) {
      let x = disallowed_keys.head;
      let xs = disallowed_keys.tail;
      loop$map = delete$(map, x);
      loop$disallowed_keys = xs;
    } else {
      throw makeError(
        "case_no_match",
        "gleam/map",
        514,
        "drop",
        "No case clause matched",
        { values: [disallowed_keys] }
      );
    }
  }
}