Concat concatenates two bit arrays.
(lhs, rhs BitArray)
| 358 | |
| 359 | // Concat concatenates two bit arrays. |
| 360 | func Concat(lhs, rhs BitArray) BitArray { |
| 361 | if lhs.lastBitsUsed == 0 { |
| 362 | return rhs |
| 363 | } |
| 364 | if rhs.lastBitsUsed == 0 { |
| 365 | return lhs |
| 366 | } |
| 367 | words := make([]word, (lhs.nonEmptyBitLen()+rhs.nonEmptyBitLen()+numBitsPerWord-1)/numBitsPerWord) |
| 368 | |
| 369 | // The first bits come from the lhs unchanged. |
| 370 | copy(words, lhs.words) |
| 371 | var lastBitsUsed uint8 |
| 372 | if lhs.lastBitsUsed == numBitsPerWord { |
| 373 | // Fast path. Just concatenate. |
| 374 | copy(words[len(lhs.words):], rhs.words) |
| 375 | lastBitsUsed = rhs.lastBitsUsed |
| 376 | } else { |
| 377 | // We need to shift all the words in the RHS |
| 378 | // by the lastBitsUsed of the LHS. |
| 379 | rhsShift := lhs.lastBitsUsed |
| 380 | targetWordIdx := len(lhs.words) - 1 |
| 381 | trailingBits := words[targetWordIdx] |
| 382 | for _, w := range rhs.words { |
| 383 | headingBits := w >> rhsShift |
| 384 | combinedBits := trailingBits | headingBits |
| 385 | words[targetWordIdx] = combinedBits |
| 386 | targetWordIdx++ |
| 387 | trailingBits = w << (numBitsPerWord - rhsShift) |
| 388 | } |
| 389 | lastBitsUsed = lhs.lastBitsUsed + rhs.lastBitsUsed |
| 390 | if lastBitsUsed > numBitsPerWord { |
| 391 | // Some bits from the RHS didn't fill a |
| 392 | // word, we need to fit them in the last word. |
| 393 | words[targetWordIdx] = trailingBits |
| 394 | } |
| 395 | |
| 396 | // Compute the final thing. |
| 397 | lastBitsUsed %= numBitsPerWord |
| 398 | if lastBitsUsed == 0 { |
| 399 | lastBitsUsed = numBitsPerWord |
| 400 | } |
| 401 | } |
| 402 | return BitArray{words: words, lastBitsUsed: lastBitsUsed} |
| 403 | } |
| 404 | |
| 405 | // Not computes the complement of a bit array. |
| 406 | func Not(d BitArray) BitArray { |
no test coverage detected
searching dependent graphs…