forked from tunabay/go-bitarray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitarray_shift.go
117 lines (101 loc) · 2.54 KB
/
bitarray_shift.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package bitarray
import (
"math/bits"
)
// Reverse returns the bit array with its bits in reversed order.
func (ba *BitArray) Reverse() *BitArray {
switch {
case ba.IsZero():
return zeroBitArray
case ba.Len() == 1, ba.b == nil:
return ba
}
buf := make([]byte, len(ba.b))
for i, o := range ba.b {
buf[len(ba.b)-1-i] = bits.Reverse8(o)
}
return NewFromBytes(buf, ba.NumPadding(), ba.nBits)
}
// ShiftLeft returns the bit array of shifted left by k bits.
// To shift to right, call ShiftLeft(-k).
func (ba *BitArray) ShiftLeft(k int) *BitArray {
switch {
case ba.IsZero():
return zeroBitArray
case ba.b == nil:
return ba
case ba.nBits <= k, ba.nBits <= -k:
return &BitArray{nBits: ba.nBits}
case 0 < k:
return ba.shiftLeft(k)
case k < 0:
return ba.shiftRight(-k)
}
return ba
}
func (ba *BitArray) shiftLeft(k int) *BitArray {
if k&7 == 0 {
buf := allocByteSlice(len(ba.b))
copy(buf, ba.b[k>>3:])
return &BitArray{b: buf, nBits: ba.nBits}
}
return ba.Slice(k, ba.nBits).append1(&BitArray{nBits: k})
}
func (ba *BitArray) shiftRight(k int) *BitArray {
if k&7 == 0 {
buf := allocByteSlice(len(ba.b))
copy(buf[k>>3:], ba.b)
if npad := ba.NumPadding(); npad != 0 {
mask := byte(0xff) << npad
buf[len(buf)-1] &= mask
}
return &BitArray{b: buf, nBits: ba.nBits}
}
return (&BitArray{nBits: k}).append1(ba.Slice(0, ba.nBits-k))
}
// RotateLeft returns the bit array of rotated left by k bits.
// To rotate to right, call RotateLeft(-k).
func (ba *BitArray) RotateLeft(k int) *BitArray {
switch {
case ba.IsZero():
return zeroBitArray
case ba.b == nil:
return ba
case 0 < k:
return ba.rotateLeft(k)
case k < 0:
return ba.rotateRight(-k)
}
return ba
}
func (ba *BitArray) rotateLeft(k int) *BitArray {
k %= ba.nBits
switch {
case k == 0:
return ba
case k&7 == 0 && ba.nBits&7 == 0:
buf := allocByteSlice(len(ba.b))
nbs := k >> 3
copy(buf, ba.b[nbs:])
copy(buf[len(buf)-nbs:], ba.b)
return &BitArray{b: buf, nBits: ba.nBits}
}
return ba.Slice(k, ba.nBits).append1(ba.Slice(0, k))
}
func (ba *BitArray) rotateRight(k int) *BitArray {
k %= ba.nBits
switch {
case k == 0:
return ba
case k&7 == 0 && ba.nBits&7 == 0:
buf := allocByteSlice(len(ba.b))
nbs := k >> 3
copy(buf[nbs:], ba.b)
copy(buf, ba.b[len(ba.b)-nbs:])
return &BitArray{b: buf, nBits: ba.nBits}
}
return ba.Slice(ba.nBits-k, ba.nBits).append1(ba.Slice(0, ba.nBits-k))
}