bitlist.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package barcode
  2. // utility class that contains bits
  3. type BitList struct {
  4. count int
  5. data []int32
  6. }
  7. // returns a new BitList with the given length
  8. // all bits are initialize with false
  9. func NewBitList(capacity int) *BitList {
  10. bl := new(BitList)
  11. bl.count = capacity
  12. x := 0
  13. if capacity%32 != 0 {
  14. x = 1
  15. }
  16. bl.data = make([]int32, capacity/32+x)
  17. return bl
  18. }
  19. // returns the number of contained bits
  20. func (bl *BitList) Len() int {
  21. return bl.count
  22. }
  23. func (bl *BitList) grow() {
  24. growBy := len(bl.data)
  25. if growBy < 128 {
  26. growBy = 128
  27. } else if growBy >= 1024 {
  28. growBy = 1024
  29. }
  30. nd := make([]int32, len(bl.data)+growBy)
  31. copy(nd, bl.data)
  32. bl.data = nd
  33. }
  34. // appends the given bit to the end of the list
  35. func (bl *BitList) AddBit(bit bool) {
  36. itmIndex := bl.count / 32
  37. for itmIndex >= len(bl.data) {
  38. bl.grow()
  39. }
  40. bl.SetBit(bl.count, bit)
  41. bl.count++
  42. }
  43. // sets the bit at the given index to the given value
  44. func (bl *BitList) SetBit(index int, value bool) {
  45. itmIndex := index / 32
  46. itmBitShift := 31 - (index % 32)
  47. if value {
  48. bl.data[itmIndex] = bl.data[itmIndex] | 1<<uint(itmBitShift)
  49. } else {
  50. bl.data[itmIndex] = bl.data[itmIndex] & ^(1 << uint(itmBitShift))
  51. }
  52. }
  53. // returns the bit at the given index
  54. func (bl *BitList) GetBit(index int) bool {
  55. itmIndex := index / 32
  56. itmBitShift := 31 - (index % 32)
  57. return ((bl.data[itmIndex] >> uint(itmBitShift)) & 1) == 1
  58. }
  59. // appends all 8 bits of the given byte to the end of the list
  60. func (bl *BitList) AddByte(b byte) {
  61. for i := 7; i >= 0; i-- {
  62. bl.AddBit(((b >> uint(i)) & 1) == 1)
  63. }
  64. }
  65. // appends the last (LSB) 'count' bits of 'b' the the end of the list
  66. func (bl *BitList) AddBits(b int, count byte) {
  67. for i := int(count - 1); i >= 0; i-- {
  68. bl.AddBit(((b >> uint(i)) & 1) == 1)
  69. }
  70. }
  71. // returns all bits of the BitList as a []byte
  72. func (bl *BitList) GetBytes() []byte {
  73. len := bl.count >> 3
  74. if (bl.count % 8) != 0 {
  75. len += 1
  76. }
  77. result := make([]byte, len)
  78. for i := 0; i < len; i++ {
  79. shift := (3 - (i % 4)) * 8
  80. result[i] = (byte)((bl.data[i/4] >> uint(shift)) & 0xFF)
  81. }
  82. return result
  83. }
  84. // iterates through all bytes contained in the BitList
  85. func (bl *BitList) IterateBytes() <-chan byte {
  86. res := make(chan byte)
  87. go func() {
  88. c := bl.count
  89. shift := 24
  90. i := 0
  91. for c > 0 {
  92. res <- byte((bl.data[i] >> uint(shift)) & 0xFF)
  93. shift -= 8
  94. if shift < 0 {
  95. shift = 24
  96. i += 1
  97. }
  98. c -= 8
  99. }
  100. close(res)
  101. }()
  102. return res
  103. }