Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/angry-trainers-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tsplus/stdlib": patch
---

Fix HashMap Modify
25 changes: 13 additions & 12 deletions packages/stdlib/_src/collections/HashMap/_internal/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,23 @@ export class IndexedNode<K, V> {
const bit = toBitmap(frag)
const indx = fromBitmap(mask, bit)
const exists = mask & bit
const current = exists ? children[indx]! : new EmptyNode<K, V>()
const canEdit = canEditNode(this, edit)

if (!exists) {
const _newChild = new EmptyNode<K, V>().modify(edit, shift + SIZE, f, hash, key, size)
if (!_newChild) return this
return children.length >= MAX_INDEX_NODE ?
expand(edit, frag, _newChild, mask, children) :
new IndexedNode(edit, mask | bit, arraySpliceIn(canEdit, indx, _newChild, children))
}

const current = children[indx]!
const child = current.modify(edit, shift + SIZE, f, hash, key, size)

if (current === child) return this

const canEdit = canEditNode(this, edit)
let bitmap = mask
let newChildren
if (exists && isEmptyNode(child)) {
if (isEmptyNode(child)) {
// remove
bitmap &= ~bit
if (!bitmap) return new EmptyNode()
Expand All @@ -231,14 +239,6 @@ export class IndexedNode<K, V> {
}

newChildren = arraySpliceOut(canEdit, indx, children)
} else if (!exists && !isEmptyNode(child)) {
// add
if (children.length >= MAX_INDEX_NODE) {
return expand(edit, frag, child, mask, children)
}

bitmap |= bit
newChildren = arraySpliceIn(canEdit, indx, child, children)
} else {
// modify
newChildren = arrayUpdate(canEdit, indx, child, children)
Expand All @@ -249,6 +249,7 @@ export class IndexedNode<K, V> {
this.children = newChildren
return this
}

return new IndexedNode(edit, bitmap, newChildren)
}
}
Expand Down
16 changes: 12 additions & 4 deletions packages/stdlib/_test/Differ.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { it as it_ } from "vitest"

function diffLaws<Value, Patch>(
differ: Differ<Value, Patch>,
gen: () => Value,
equal: (a: Value, b: Value) => boolean
): void {
const it = (name: string, f: () => void) =>
it_(name, () => {
for (let i = 0; i < 100; i++) {
f()
}
})
describe.concurrent("differ laws", () => {
it("combining patches is associative", () => {
const value1 = gen()
Expand Down Expand Up @@ -57,9 +65,9 @@ function randomChunk(): Chunk<number> {
return Chunk.fill(20, smallInt)
}

// function randomHashMap(): HashMap<number, number> {
// return HashMap.from(Chunk.fill(20, smallInt).zip(Chunk.fill(20, smallInt)))
// }
function randomHashMap(): HashMap<number, number> {
return HashMap.from(Chunk.fill(2, smallInt).zip(Chunk.fill(2, smallInt)))
}

function randomHashSet(): HashSet<number> {
return HashSet.from(Chunk.fill(20, smallInt))
Expand Down Expand Up @@ -93,7 +101,7 @@ describe.concurrent("Differ", () => {
describe.concurrent("hashMap", () => {
diffLaws(
Differ.hashMap<number, number, (n: number) => number>(Differ.update<number>()),
() => HashMap(Tuple(1, 2), Tuple(3, 4), Tuple(5, 6)),
randomHashMap,
Equals.equals
)
})
Expand Down
5 changes: 5 additions & 0 deletions packages/stdlib/_test/HashMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ describe.concurrent("HashMap", () => {
assert.isTrue(result[key(1)] == Maybe.some(value("b")))
})

it("remove non existing key doesn't change the array", () => {
const map = HashMap(Tuple(13, 95), Tuple(90, 4))
assert.deepEqual(map.remove(75).keySet.toArray, map.keySet.toArray)
})

it("removeMany", () => {
const hashMap = HashMap(Tuple(key(0), value("a")), Tuple(key(1), value("b")))

Expand Down