Beispiel #1
0
func (b *bitmapIndexedHmnode) assoc2(shift uint32, hash uint32, key interface{}, val interface{}) (hmnode, bool) {
	bit := bitpos(hash, shift)
	idx := b.index(bit)
	if (b.bitmap & bit) != 0 {
		keyOrNil := b.array[2*idx]
		valOrNode := b.array[2*idx+1]
		if keyOrNil == nil {
			n, ok := valOrNode.(hmnode)
			if !ok {
				panic("Unexpected node type")
			}
			n, addedLeaf := n.assoc2(shift+5, hash, key, val)
			if n == valOrNode {
				return b, false
			}
			return &bitmapIndexedHmnode{b.bitmap, cloneAndSetObjectSlice(b.array, 2*idx+1, n)}, addedLeaf
		}
		if sequtil.Equiv(key, keyOrNil) {
			if val == valOrNode {
				return b, false
			}
			return &bitmapIndexedHmnode{b.bitmap, cloneAndSetObjectSlice(b.array, 2*idx+1, val)}, false
		}
		return &bitmapIndexedHmnode{b.bitmap, cloneAndSetObjectSlice2(b.array, 2*idx, nil, 2*idx+1, createNode(shift+5, keyOrNil, valOrNode, hash, key, val))}, true
	}

	n := sequtil.BitCountU32(b.bitmap)
	if n >= 16 {
		nodes := make([]hmnode, 32)
		jdx := imask(hash, shift)
		nodes[jdx] = emptyBitmapIndexedHmnode.assoc(shift+5, hash, key, val)
		for i, j := 0, 0; i < 32; i++ {
			if ((b.bitmap >> uint(i)) & 1) != 0 {
				if b.array[j] == nil {
					if nn, ok := b.array[j+1].(hmnode); ok {
						nodes[i] = nn
					} else {
						panic("Unexpected node type")
					}
				} else {
					nodes[i] = emptyBitmapIndexedHmnode.assoc(shift+5, Hash(b.array[j]), b.array[j], b.array[j+1])
				}
				j += 2
			}
		}
		return &arrayHmnode{n + 1, nodes}, true
	}

	newArray := make([]interface{}, 2*(n+1))
	copy(newArray, b.array[0:2*idx])
	newArray[2*idx] = key
	newArray[2*idx+1] = val
	copy(newArray[2*(idx+1):], b.array[2*idx:])
	return &bitmapIndexedHmnode{b.bitmap | bit, newArray}, true
}
Beispiel #2
0
func (b *bitmapIndexedHmnode) index(bit uint32) int {
	return sequtil.BitCountU32(b.bitmap & (bit - 1))
}