Skip to content

Commit 14dd3ab

Browse files
yoshielpil
authored andcommitted
rename transient functions
1 parent 1c72223 commit 14dd3ab

File tree

2 files changed

+51
-38
lines changed

2 files changed

+51
-38
lines changed

src/dict.mjs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { isEqual, Result$Error, Result$Ok } from "./gleam.mjs";
88
// -- HASH --------------------------------------------------------------------
99

1010
const referenceMap = /* @__PURE__ */ new WeakMap();
11-
const tempDataView = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(8));
11+
const tempDataView = /* @__PURE__ */ new DataView(
12+
/* @__PURE__ */ new ArrayBuffer(8),
13+
);
1214
let referenceUID = 0;
1315
/**
1416
* hash the object by reference using a weak map and incrementing uid
@@ -299,7 +301,7 @@ export function make() {
299301
export function from(iterable) {
300302
let transient = toTransient(emptyDict);
301303
for (const [key, value] of iterable) {
302-
transient = put(key, value, transient);
304+
transient = transientInsert(key, value, transient);
303305
}
304306
return fromTransient(transient);
305307
}
@@ -432,7 +434,7 @@ export function insert(dict, key, value) {
432434
globalTransient.generation = nextGeneration(dict);
433435
globalTransient.size = dict.size;
434436

435-
const root = doPut(globalTransient, dict.root, key, value, getHash(key), 0);
437+
const root = doInsert(globalTransient, dict.root, key, value, getHash(key), 0);
436438
if (root === dict.root) {
437439
return dict;
438440
}
@@ -446,13 +448,30 @@ export function insert(dict, key, value) {
446448
*
447449
* Returns a new transient.
448450
*/
449-
export function put(key, value, transient) {
451+
export function transientInsert(key, value, transient) {
450452
const hash = getHash(key);
451-
transient.root = doPut(transient, transient.root, key, value, hash, 0);
453+
transient.root = doInsert(transient, transient.root, key, value, hash, 0);
452454
return transient;
453455
}
454456

455-
function doPut(transient, node, key, value, hash, shift) {
457+
/**
458+
* Consume a transient, writing a new key/value pair if the key doesn't exist or updating
459+
* the existing value with a function if it does.
460+
*
461+
* Returns a new transient.
462+
*/
463+
export function transientUpdateWith(key, fun, value, transient) {
464+
const hash = getHash(key);
465+
466+
const existing = lookup(transient.root, key, hash);
467+
if (existing !== noElementMarker) {
468+
value = fun(existing);
469+
}
470+
transient.root = doInsert(transient, transient.root, key, value, hash, 0);
471+
return transient;
472+
}
473+
474+
function doInsert(transient, node, key, value, hash, shift) {
456475
const data = node.data;
457476
const generation = transient.generation;
458477

@@ -476,7 +495,7 @@ function doPut(transient, node, key, value, hash, shift) {
476495
if (node.nodemap & bit) {
477496
const nodeidx = data.length - 1 - index(node.nodemap, bit);
478497
const child = data[nodeidx];
479-
const newChild = doPut(transient, child, key, value, hash, shift + bits);
498+
const newChild = doInsert(transient, child, key, value, hash, shift + bits);
480499
return copyAndSet(node, generation, nodeidx, newChild);
481500
}
482501

@@ -503,8 +522,8 @@ function doPut(transient, node, key, value, hash, shift) {
503522
const childShift = shift + bits;
504523

505524
let child = emptyNode;
506-
child = doPut(transient, child, key, value, hash, childShift);
507-
child = doPut(transient, child, otherKey, otherVal, otherHash, childShift);
525+
child = doInsert(transient, child, key, value, hash, childShift);
526+
child = doInsert(transient, child, otherKey, otherVal, otherHash, childShift);
508527

509528
// we inserted 2 elements, but implicitely deleted the one we pushed down from the datamap.
510529
transient.size -= 1;
@@ -529,16 +548,17 @@ function doPut(transient, node, key, value, hash, shift) {
529548

530549
return makeNode(generation, node.datamap ^ bit, node.nodemap | bit, newData);
531550
}
551+
532552
/**
533553
* Consume a transient, removing a key if it exists.
534554
* Returns a new transient.
535555
*/
536-
export function remove(key, transient) {
537-
transient.root = doRemove(transient, transient.root, key, getHash(key), 0);
556+
export function transientDelete(key, transient) {
557+
transient.root = doDelete(transient, transient.root, key, getHash(key), 0);
538558
return transient;
539559
}
540560

541-
function doRemove(transient, node, key, hash, shift) {
561+
function doDelete(transient, node, key, hash, shift) {
542562
const data = node.data;
543563
const generation = transient.generation;
544564

@@ -564,7 +584,7 @@ function doRemove(transient, node, key, hash, shift) {
564584
const nodeidx = data.length - 1 - index(node.nodemap, bit);
565585

566586
const oldChild = data[nodeidx];
567-
const newChild = doRemove(transient, oldChild, key, hash, shift + bits);
587+
const newChild = doDelete(transient, oldChild, key, hash, shift + bits);
568588

569589
// the node did change, so let's copy to incorporate that change.
570590
if (newChild.nodemap !== 0 || newChild.data.length > 2) {
@@ -602,18 +622,6 @@ function doRemove(transient, node, key, hash, shift) {
602622
return copyAndRemovePair(node, generation, bit, dataidx);
603623
}
604624

605-
export function update_with(key, fun, value, transient) {
606-
const hash = getHash(key);
607-
608-
const existing = lookup(transient.root, key, hash);
609-
if (existing !== noElementMarker) {
610-
value = fun(existing);
611-
}
612-
613-
transient.root = doPut(transient, transient.root, key, value, hash, 0);
614-
return transient;
615-
}
616-
617625
export function map(dict, fun) {
618626
// map can never modify the structure, so we can walk the dictionary directly,
619627
// but still move to a new generation to make sure we get a new copy of every node.

src/gleam/dict.gleam

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ fn from_list_loop(
115115
) -> Dict(k, v) {
116116
case list {
117117
[] -> from_transient(transient)
118-
[#(key, value), ..rest] -> from_list_loop(put(key, value, transient), rest)
118+
[#(key, value), ..rest] ->
119+
from_list_loop(transient_insert(key, value, transient), rest)
119120
}
120121
}
121122

@@ -194,8 +195,12 @@ pub fn insert(into dict: Dict(k, v), for key: k, insert value: v) -> Dict(k, v)
194195
fn do_insert(key: k, value: v, dict: Dict(k, v)) -> Dict(k, v)
195196

196197
@external(erlang, "maps", "put")
197-
@external(javascript, "../dict.mjs", "put")
198-
fn put(key: k, value: v, transient: TransientDict(k, v)) -> TransientDict(k, v)
198+
@external(javascript, "../dict.mjs", "transientInsert")
199+
fn transient_insert(
200+
key: k,
201+
value: v,
202+
transient: TransientDict(k, v),
203+
) -> TransientDict(k, v)
199204

200205
/// Updates all values in a given dict by calling a given function on each key
201206
/// and value.
@@ -281,7 +286,7 @@ fn do_filter(f: fn(k, v) -> Bool, dict: Dict(k, v)) -> Dict(k, v) {
281286
to_transient(new())
282287
|> fold(over: dict, with: fn(transient, key, value) {
283288
case f(key, value) {
284-
True -> put(key, value, transient)
289+
True -> transient_insert(key, value, transient)
285290
False -> transient
286291
}
287292
})
@@ -323,7 +328,7 @@ fn do_take_loop(
323328
[] -> from_transient(acc)
324329
[key, ..rest] ->
325330
case get(dict, key) {
326-
Ok(value) -> do_take_loop(dict, rest, put(key, value, acc))
331+
Ok(value) -> do_take_loop(dict, rest, transient_insert(key, value, acc))
327332
Error(_) -> do_take_loop(dict, rest, acc)
328333
}
329334
}
@@ -364,12 +369,12 @@ pub fn merge(into dict: Dict(k, v), from new_entries: Dict(k, v)) -> Dict(k, v)
364369
/// ```
365370
///
366371
pub fn delete(from dict: Dict(k, v), delete key: k) -> Dict(k, v) {
367-
to_transient(dict) |> remove(key, _) |> from_transient
372+
to_transient(dict) |> transient_delete(key, _) |> from_transient
368373
}
369374

370375
@external(erlang, "maps", "remove")
371-
@external(javascript, "../dict.mjs", "remove")
372-
fn remove(a: k, b: TransientDict(k, v)) -> TransientDict(k, v)
376+
@external(javascript, "../dict.mjs", "transientDelete")
377+
fn transient_delete(a: k, b: TransientDict(k, v)) -> TransientDict(k, v)
373378

374379
/// Creates a new dict from a given dict with all the same entries except any with
375380
/// keys found in a given list.
@@ -406,7 +411,7 @@ fn drop_loop(
406411
) -> Dict(k, v) {
407412
case disallowed_keys {
408413
[] -> from_transient(transient)
409-
[key, ..rest] -> drop_loop(remove(key, transient), rest)
414+
[key, ..rest] -> drop_loop(transient_delete(key, transient), rest)
410415
}
411416
}
412417

@@ -547,14 +552,14 @@ fn do_combine(
547552
to_transient(big)
548553
|> fold(over: small, with: fn(transient, key, value) {
549554
let update = fn(existing) { combine(key, existing, value) }
550-
update_with(key, update, value, transient)
555+
transient_update_with(key, update, value, transient)
551556
})
552557
|> from_transient
553558
}
554559

555560
@external(erlang, "maps", "update_with")
556-
@external(javascript, "../dict.mjs", "update_with")
557-
fn update_with(
561+
@external(javascript, "../dict.mjs", "transientUpdateWith")
562+
fn transient_update_with(
558563
key: k,
559564
fun: fn(v) -> v,
560565
init: v,
@@ -578,7 +583,7 @@ fn group_loop(
578583
let update = fn(existing) { [value, ..existing] }
579584

580585
transient
581-
|> update_with(key, update, [value], _)
586+
|> transient_update_with(key, update, [value], _)
582587
|> group_loop(to_key, rest)
583588
}
584589
}

0 commit comments

Comments
 (0)