You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
5.6 KiB

9 years ago
9 years ago
  1. package common
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "math/rand"
  6. "strings"
  7. "sync"
  8. )
  9. type BitArray struct {
  10. mtx sync.Mutex
  11. Bits int `json:"bits"` // NOTE: persisted via reflect, must be exported
  12. Elems []uint64 `json:"elems"` // NOTE: persisted via reflect, must be exported
  13. }
  14. // There is no BitArray whose Size is 0. Use nil instead.
  15. func NewBitArray(bits int) *BitArray {
  16. if bits == 0 {
  17. return nil
  18. }
  19. return &BitArray{
  20. Bits: bits,
  21. Elems: make([]uint64, (bits+63)/64),
  22. }
  23. }
  24. func (bA *BitArray) Size() int {
  25. if bA == nil {
  26. return 0
  27. }
  28. return bA.Bits
  29. }
  30. // NOTE: behavior is undefined if i >= bA.Bits
  31. func (bA *BitArray) GetIndex(i int) bool {
  32. if bA == nil {
  33. return false
  34. }
  35. bA.mtx.Lock()
  36. defer bA.mtx.Unlock()
  37. return bA.getIndex(i)
  38. }
  39. func (bA *BitArray) getIndex(i int) bool {
  40. if i >= bA.Bits {
  41. return false
  42. }
  43. return bA.Elems[i/64]&(uint64(1)<<uint(i%64)) > 0
  44. }
  45. // NOTE: behavior is undefined if i >= bA.Bits
  46. func (bA *BitArray) SetIndex(i int, v bool) bool {
  47. if bA == nil {
  48. return false
  49. }
  50. bA.mtx.Lock()
  51. defer bA.mtx.Unlock()
  52. return bA.setIndex(i, v)
  53. }
  54. func (bA *BitArray) setIndex(i int, v bool) bool {
  55. if i >= bA.Bits {
  56. return false
  57. }
  58. if v {
  59. bA.Elems[i/64] |= (uint64(1) << uint(i%64))
  60. } else {
  61. bA.Elems[i/64] &= ^(uint64(1) << uint(i%64))
  62. }
  63. return true
  64. }
  65. func (bA *BitArray) Copy() *BitArray {
  66. if bA == nil {
  67. return nil
  68. }
  69. bA.mtx.Lock()
  70. defer bA.mtx.Unlock()
  71. return bA.copy()
  72. }
  73. func (bA *BitArray) copy() *BitArray {
  74. c := make([]uint64, len(bA.Elems))
  75. copy(c, bA.Elems)
  76. return &BitArray{
  77. Bits: bA.Bits,
  78. Elems: c,
  79. }
  80. }
  81. func (bA *BitArray) copyBits(bits int) *BitArray {
  82. c := make([]uint64, (bits+63)/64)
  83. copy(c, bA.Elems)
  84. return &BitArray{
  85. Bits: bits,
  86. Elems: c,
  87. }
  88. }
  89. // Returns a BitArray of larger bits size.
  90. func (bA *BitArray) Or(o *BitArray) *BitArray {
  91. if bA == nil {
  92. o.Copy()
  93. }
  94. bA.mtx.Lock()
  95. defer bA.mtx.Unlock()
  96. c := bA.copyBits(MaxInt(bA.Bits, o.Bits))
  97. for i := 0; i < len(c.Elems); i++ {
  98. c.Elems[i] |= o.Elems[i]
  99. }
  100. return c
  101. }
  102. // Returns a BitArray of smaller bit size.
  103. func (bA *BitArray) And(o *BitArray) *BitArray {
  104. if bA == nil {
  105. return nil
  106. }
  107. bA.mtx.Lock()
  108. defer bA.mtx.Unlock()
  109. return bA.and(o)
  110. }
  111. func (bA *BitArray) and(o *BitArray) *BitArray {
  112. c := bA.copyBits(MinInt(bA.Bits, o.Bits))
  113. for i := 0; i < len(c.Elems); i++ {
  114. c.Elems[i] &= o.Elems[i]
  115. }
  116. return c
  117. }
  118. func (bA *BitArray) Not() *BitArray {
  119. if bA == nil {
  120. return nil // Degenerate
  121. }
  122. bA.mtx.Lock()
  123. defer bA.mtx.Unlock()
  124. c := bA.copy()
  125. for i := 0; i < len(c.Elems); i++ {
  126. c.Elems[i] = ^c.Elems[i]
  127. }
  128. return c
  129. }
  130. func (bA *BitArray) Sub(o *BitArray) *BitArray {
  131. if bA == nil {
  132. return nil
  133. }
  134. bA.mtx.Lock()
  135. defer bA.mtx.Unlock()
  136. if bA.Bits > o.Bits {
  137. c := bA.copy()
  138. for i := 0; i < len(o.Elems)-1; i++ {
  139. c.Elems[i] &= ^c.Elems[i]
  140. }
  141. i := len(o.Elems) - 1
  142. if i >= 0 {
  143. for idx := i * 64; idx < o.Bits; idx++ {
  144. // NOTE: each individual GetIndex() call to o is safe.
  145. c.setIndex(idx, c.getIndex(idx) && !o.GetIndex(idx))
  146. }
  147. }
  148. return c
  149. } else {
  150. return bA.and(o.Not()) // Note degenerate case where o == nil
  151. }
  152. }
  153. func (bA *BitArray) IsFull() bool {
  154. if bA == nil {
  155. return true
  156. }
  157. bA.mtx.Lock()
  158. defer bA.mtx.Unlock()
  159. // Check all elements except the last
  160. for _, elem := range bA.Elems[:len(bA.Elems)-1] {
  161. if (^elem) != 0 {
  162. return false
  163. }
  164. }
  165. // Check that the last element has (lastElemBits) 1's
  166. lastElemBits := (bA.Bits+63)%64 + 1
  167. lastElem := bA.Elems[len(bA.Elems)-1]
  168. return (lastElem+1)&((uint64(1)<<uint(lastElemBits))-1) == 0
  169. }
  170. func (bA *BitArray) PickRandom() (int, bool) {
  171. if bA == nil {
  172. return 0, false
  173. }
  174. bA.mtx.Lock()
  175. defer bA.mtx.Unlock()
  176. length := len(bA.Elems)
  177. if length == 0 {
  178. return 0, false
  179. }
  180. randElemStart := rand.Intn(length)
  181. for i := 0; i < length; i++ {
  182. elemIdx := ((i + randElemStart) % length)
  183. if elemIdx < length-1 {
  184. if bA.Elems[elemIdx] > 0 {
  185. randBitStart := rand.Intn(64)
  186. for j := 0; j < 64; j++ {
  187. bitIdx := ((j + randBitStart) % 64)
  188. if (bA.Elems[elemIdx] & (uint64(1) << uint(bitIdx))) > 0 {
  189. return 64*elemIdx + bitIdx, true
  190. }
  191. }
  192. PanicSanity("should not happen")
  193. }
  194. } else {
  195. // Special case for last elem, to ignore straggler bits
  196. elemBits := bA.Bits % 64
  197. if elemBits == 0 {
  198. elemBits = 64
  199. }
  200. randBitStart := rand.Intn(elemBits)
  201. for j := 0; j < elemBits; j++ {
  202. bitIdx := ((j + randBitStart) % elemBits)
  203. if (bA.Elems[elemIdx] & (uint64(1) << uint(bitIdx))) > 0 {
  204. return 64*elemIdx + bitIdx, true
  205. }
  206. }
  207. }
  208. }
  209. return 0, false
  210. }
  211. func (bA *BitArray) String() string {
  212. if bA == nil {
  213. return "nil-BitArray"
  214. }
  215. bA.mtx.Lock()
  216. defer bA.mtx.Unlock()
  217. return bA.stringIndented("")
  218. }
  219. func (bA *BitArray) StringIndented(indent string) string {
  220. if bA == nil {
  221. return "nil-BitArray"
  222. }
  223. bA.mtx.Lock()
  224. defer bA.mtx.Unlock()
  225. return bA.stringIndented(indent)
  226. }
  227. func (bA *BitArray) stringIndented(indent string) string {
  228. lines := []string{}
  229. bits := ""
  230. for i := 0; i < bA.Bits; i++ {
  231. if bA.getIndex(i) {
  232. bits += "X"
  233. } else {
  234. bits += "_"
  235. }
  236. if i%100 == 99 {
  237. lines = append(lines, bits)
  238. bits = ""
  239. }
  240. if i%10 == 9 {
  241. bits += " "
  242. }
  243. if i%50 == 49 {
  244. bits += " "
  245. }
  246. }
  247. if len(bits) > 0 {
  248. lines = append(lines, bits)
  249. }
  250. return fmt.Sprintf("BA{%v:%v}", bA.Bits, strings.Join(lines, indent))
  251. }
  252. func (bA *BitArray) Bytes() []byte {
  253. bA.mtx.Lock()
  254. defer bA.mtx.Unlock()
  255. numBytes := (bA.Bits + 7) / 8
  256. bytes := make([]byte, numBytes)
  257. for i := 0; i < len(bA.Elems); i++ {
  258. elemBytes := [8]byte{}
  259. binary.LittleEndian.PutUint64(elemBytes[:], bA.Elems[i])
  260. copy(bytes[i*8:], elemBytes[:])
  261. }
  262. return bytes
  263. }