bitlist.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package utils
  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. // appends all bits in the bool-slice to the end of the list
  72. func (bl *BitList) AddRange(bits []bool) {
  73. for _, b := range bits {
  74. bl.AddBit(b)
  75. }
  76. }
  77. // returns all bits of the BitList as a []byte
  78. func (bl *BitList) GetBytes() []byte {
  79. len := bl.count >> 3
  80. if (bl.count % 8) != 0 {
  81. len += 1
  82. }
  83. result := make([]byte, len)
  84. for i := 0; i < len; i++ {
  85. shift := (3 - (i % 4)) * 8
  86. result[i] = (byte)((bl.data[i/4] >> uint(shift)) & 0xFF)
  87. }
  88. return result
  89. }
  90. // iterates through all bytes contained in the BitList
  91. func (bl *BitList) IterateBytes() <-chan byte {
  92. res := make(chan byte)
  93. go func() {
  94. c := bl.count
  95. shift := 24
  96. i := 0
  97. for c > 0 {
  98. res <- byte((bl.data[i] >> uint(shift)) & 0xFF)
  99. shift -= 8
  100. if shift < 0 {
  101. shift = 24
  102. i += 1
  103. }
  104. c -= 8
  105. }
  106. close(res)
  107. }()
  108. return res
  109. }