bitset.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // go-qrcode
  2. // Copyright 2014 Tom Harwood
  3. // Package bitset implements an append only bit array.
  4. //
  5. // To create a Bitset and append some bits:
  6. // // Bitset Contents
  7. // b := bitset.New() // {}
  8. // b.AppendBools(true, true, false) // {1, 1, 0}
  9. // b.AppendBools(true) // {1, 1, 0, 1}
  10. // b.AppendValue(0x02, 4) // {1, 1, 0, 1, 0, 0, 1, 0}
  11. //
  12. // To read values:
  13. //
  14. // len := b.Len() // 8
  15. // v := b.At(0) // 1
  16. // v = b.At(1) // 1
  17. // v = b.At(2) // 0
  18. // v = b.At(8) // 0
  19. package bitset
  20. import (
  21. "bytes"
  22. "fmt"
  23. "log"
  24. )
  25. const (
  26. b0 = false
  27. b1 = true
  28. )
  29. // Bitset stores an array of bits.
  30. type Bitset struct {
  31. // The number of bits stored.
  32. numBits int
  33. // Storage for individual bits.
  34. bits []byte
  35. }
  36. // New returns an initialised Bitset with optional initial bits v.
  37. func New(v ...bool) *Bitset {
  38. b := &Bitset{numBits: 0, bits: make([]byte, 0)}
  39. b.AppendBools(v...)
  40. return b
  41. }
  42. func Clone(from *Bitset) *Bitset {
  43. return &Bitset{numBits: from.numBits, bits: from.bits[:]}
  44. }
  45. func (b *Bitset) Substr(start int, end int) *Bitset {
  46. if start > end || end > b.numBits {
  47. log.Panicf("Out of range start=%d end=%d numBits=%d", start, end, b.numBits)
  48. }
  49. result := New()
  50. result.ensureCapacity(end - start)
  51. for i := start; i < end; i++ {
  52. if b.At(i) {
  53. result.bits[result.numBits/8] |= 0x80 >> uint(result.numBits%8)
  54. }
  55. result.numBits++
  56. }
  57. return result
  58. }
  59. func NewFromBase2String(b2string string) *Bitset {
  60. b := &Bitset{numBits: 0, bits: make([]byte, 0)}
  61. for _, c := range b2string {
  62. switch c {
  63. case '1':
  64. b.AppendBools(true)
  65. case '0':
  66. b.AppendBools(false)
  67. case ' ':
  68. default:
  69. log.Panicf("Invalid char %c in NewFromBase2String", c)
  70. }
  71. }
  72. return b
  73. }
  74. // AppendBytes appends a list of whole bytes.
  75. func (b *Bitset) AppendBytes(data []byte) {
  76. for _, d := range data {
  77. b.AppendByte(d, 8)
  78. }
  79. }
  80. // AppendBytes appends the |numBits| least significant bits from |byte|.
  81. func (b *Bitset) AppendByte(bits byte, numBits int) {
  82. b.ensureCapacity(numBits)
  83. if numBits > 8 {
  84. log.Panicf("numBits %d out of range 0-8", numBits)
  85. }
  86. for i := numBits - 1; i >= 0; i-- {
  87. if bits&(1<<uint(i)) != 0 {
  88. b.bits[b.numBits/8] |= 0x80 >> uint(b.numBits%8)
  89. }
  90. b.numBits++
  91. }
  92. }
  93. // AppendValue appends the |numBits| least significant bits from |value|.
  94. func (b *Bitset) AppendUint32(value uint32, numBits int) {
  95. b.ensureCapacity(numBits)
  96. if numBits > 32 {
  97. log.Panicf("numBits %d out of range 0-32", numBits)
  98. }
  99. for i := numBits - 1; i >= 0; i-- {
  100. if value&(1<<uint(i)) != 0 {
  101. b.bits[b.numBits/8] |= 0x80 >> uint(b.numBits%8)
  102. }
  103. b.numBits++
  104. }
  105. }
  106. // ensureCapacity ensures the Bitset can store an additional |numBits|.
  107. //
  108. // The underlying array is expanded if necessary. To prevent frequent
  109. // reallocation, expanding the underlying array at least doubles its capacity.
  110. func (b *Bitset) ensureCapacity(numBits int) {
  111. numBits += b.numBits
  112. newNumBytes := numBits / 8
  113. if numBits%8 != 0 {
  114. newNumBytes++
  115. }
  116. if len(b.bits) >= newNumBytes {
  117. return
  118. }
  119. b.bits = append(b.bits, make([]byte, newNumBytes+2*len(b.bits))...)
  120. }
  121. // Append bits copied from |other|.
  122. //
  123. // The new length is b.Len() + other.Len().
  124. func (b *Bitset) Append(other *Bitset) {
  125. b.ensureCapacity(other.numBits)
  126. for i := 0; i < other.numBits; i++ {
  127. if other.At(i) {
  128. b.bits[b.numBits/8] |= 0x80 >> uint(b.numBits%8)
  129. }
  130. b.numBits++
  131. }
  132. }
  133. // AppendBools appends bits to the Bitset.
  134. func (b *Bitset) AppendBools(bits ...bool) {
  135. b.ensureCapacity(len(bits))
  136. for _, v := range bits {
  137. if v {
  138. b.bits[b.numBits/8] |= 0x80 >> uint(b.numBits%8)
  139. }
  140. b.numBits++
  141. }
  142. }
  143. // AppendBools appends bits to the Bitset.
  144. func (b *Bitset) AppendNumBools(num int, value bool) {
  145. for i := 0; i < num; i++ {
  146. b.AppendBools(value)
  147. }
  148. }
  149. // String returns a human readable representation of the Bitset's contents.
  150. func (b *Bitset) String() string {
  151. var bitString string
  152. for i := 0; i < b.numBits; i++ {
  153. if (i % 8) == 0 {
  154. bitString += " "
  155. }
  156. if (b.bits[i/8] & (0x80 >> byte(i%8))) != 0 {
  157. bitString += "1"
  158. } else {
  159. bitString += "0"
  160. }
  161. }
  162. return fmt.Sprintf("numBits=%d, bits=%s", b.numBits, bitString)
  163. }
  164. // Len returns the length of the Bitset in bits.
  165. func (b *Bitset) Len() int {
  166. return b.numBits
  167. }
  168. // Bits returns the contents of the Bitset.
  169. func (b *Bitset) Bits() []bool {
  170. result := make([]bool, b.numBits)
  171. var i int
  172. for i = 0; i < b.numBits; i++ {
  173. result[i] = (b.bits[i/8] & (0x80 >> byte(i%8))) != 0
  174. }
  175. return result
  176. }
  177. // At returns the value of the bit at |index|.
  178. func (b *Bitset) At(index int) bool {
  179. if index >= b.numBits {
  180. log.Panicf("Index %d out of range", index)
  181. }
  182. return (b.bits[index/8] & (0x80 >> byte(index%8))) != 0
  183. }
  184. func (b *Bitset) Equals(other *Bitset) bool {
  185. if b.numBits != other.numBits {
  186. return false
  187. }
  188. if !bytes.Equal(b.bits[0:b.numBits/8], other.bits[0:b.numBits/8]) {
  189. return false
  190. }
  191. for i := 8 * (b.numBits / 8); i < b.numBits; i++ {
  192. a := (b.bits[i/8] & (0x80 >> byte(i%8)))
  193. b := (other.bits[i/8] & (0x80 >> byte(i%8)))
  194. if a != b {
  195. return false
  196. }
  197. }
  198. return true
  199. }
  200. func (b *Bitset) ByteAt(index int) byte {
  201. if index >= b.numBits {
  202. log.Panicf("Index %d out of range", index)
  203. }
  204. var result byte = 0
  205. for i := index; i < index+8 && i < b.numBits; i++ {
  206. result <<= 1
  207. if b.At(i) {
  208. result |= 1
  209. }
  210. }
  211. return result
  212. }