MCPcopy Create free account
hub / github.com/benbjohnson/immutable / mergeIntoNode

Function mergeIntoNode

immutable.go:1412–1428  ·  view source on GitHub ↗

mergeIntoNode merges a key/value pair into an existing node. Caller must verify that node's keyHash is not equal to keyHash.

(node mapLeafNode[K, V], shift uint, keyHash uint32, key K, value V)

Source from the content-addressed store, hash-verified

1410// mergeIntoNode merges a key/value pair into an existing node.
1411// Caller must verify that node's keyHash is not equal to keyHash.
1412func mergeIntoNode[K, V any](node mapLeafNode[K, V], shift uint, keyHash uint32, key K, value V) mapNode[K, V] {
1413 idx1 := (node.keyHashValue() >> shift) & mapNodeMask
1414 idx2 := (keyHash >> shift) & mapNodeMask
1415
1416 // Recursively build branch nodes to combine the node and its key.
1417 other := &mapBitmapIndexedNode[K, V]{bitmap: (1 << idx1) | (1 << idx2)}
1418 if idx1 == idx2 {
1419 other.nodes = []mapNode[K, V]{mergeIntoNode(node, shift+mapNodeBits, keyHash, key, value)}
1420 } else {
1421 if newNode := newMapValueNode(keyHash, key, value); idx1 < idx2 {
1422 other.nodes = []mapNode[K, V]{node, newNode}
1423 } else {
1424 other.nodes = []mapNode[K, V]{newNode, node}
1425 }
1426 }
1427 return other
1428}
1429
1430// mapEntry represents a single key/value pair.
1431type mapEntry[K, V any] struct {

Callers 1

setMethod · 0.85

Calls 2

newMapValueNodeFunction · 0.85
keyHashValueMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…