MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / ToWidth

Method ToWidth

pkg/util/bitarray/bitarray.go:111–139  ·  view source on GitHub ↗

ToWidth resizes the bit array to the specified size. If the specified width is shorter, bits on the right are truncated away. If the specified width is larger, zero bits are added on the right.

(desiredLen uint)

Source from the content-addressed store, hash-verified

109// If the specified width is shorter, bits on the right are truncated away.
110// If the specified width is larger, zero bits are added on the right.
111func (d BitArray) ToWidth(desiredLen uint) BitArray {
112 bitlen := d.BitLen()
113 if bitlen == desiredLen {
114 // Nothing to do; fast path.
115 return d
116 }
117 if desiredLen == 0 {
118 // Nothing to do; fast path.
119 return BitArray{}
120 }
121 if desiredLen < bitlen {
122 // Destructive, we have to copy.
123 words, lastBitsUsed := EncodingPartsForBitLen(desiredLen)
124 copy(words, d.words[:len(words)])
125 words[len(words)-1] &= (^word(0) << (numBitsPerWord - lastBitsUsed))
126 return mustFromEncodingParts(words, lastBitsUsed)
127 }
128
129 // New length is larger.
130 numWords, lastBitsUsed := SizesForBitLen(desiredLen)
131 var words []word
132 if numWords <= uint(cap(d.words)) {
133 words = d.words[0:numWords]
134 } else {
135 words = make([]word, numWords)
136 copy(words, d.words)
137 }
138 return mustFromEncodingParts(words, lastBitsUsed)
139}
140
141// Sizeof returns the size in bytes of the bit array and its components.
142func (d BitArray) Sizeof() uintptr {

Callers 1

FormatBitArrayToTypeFunction · 0.80

Calls 4

BitLenMethod · 0.95
EncodingPartsForBitLenFunction · 0.85
mustFromEncodingPartsFunction · 0.85
SizesForBitLenFunction · 0.85

Tested by

no test coverage detected