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.

316 lines
6.1 KiB

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