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.

323 lines
6.2 KiB

9 years ago
9 years ago
9 years ago
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 && o == nil {
  91. return nil
  92. }
  93. if bA == nil && o != nil {
  94. return o.Copy()
  95. }
  96. if o == nil {
  97. return bA.Copy()
  98. }
  99. bA.mtx.Lock()
  100. defer bA.mtx.Unlock()
  101. c := bA.copyBits(MaxInt(bA.Bits, o.Bits))
  102. for i := 0; i < len(c.Elems); i++ {
  103. c.Elems[i] |= o.Elems[i]
  104. }
  105. return c
  106. }
  107. // Returns a BitArray of smaller bit size.
  108. func (bA *BitArray) And(o *BitArray) *BitArray {
  109. if bA == nil || o == nil {
  110. return nil
  111. }
  112. bA.mtx.Lock()
  113. defer bA.mtx.Unlock()
  114. return bA.and(o)
  115. }
  116. func (bA *BitArray) and(o *BitArray) *BitArray {
  117. c := bA.copyBits(MinInt(bA.Bits, o.Bits))
  118. for i := 0; i < len(c.Elems); i++ {
  119. c.Elems[i] &= o.Elems[i]
  120. }
  121. return c
  122. }
  123. func (bA *BitArray) Not() *BitArray {
  124. if bA == nil {
  125. return nil // Degenerate
  126. }
  127. bA.mtx.Lock()
  128. defer bA.mtx.Unlock()
  129. c := bA.copy()
  130. for i := 0; i < len(c.Elems); i++ {
  131. c.Elems[i] = ^c.Elems[i]
  132. }
  133. return c
  134. }
  135. func (bA *BitArray) Sub(o *BitArray) *BitArray {
  136. if bA == nil || o == nil {
  137. // TODO: Decide if we should do 1's complement here?
  138. return nil
  139. }
  140. bA.mtx.Lock()
  141. defer bA.mtx.Unlock()
  142. if bA.Bits > o.Bits {
  143. c := bA.copy()
  144. for i := 0; i < len(o.Elems)-1; i++ {
  145. c.Elems[i] &= ^c.Elems[i]
  146. }
  147. i := len(o.Elems) - 1
  148. if i >= 0 {
  149. for idx := i * 64; idx < o.Bits; idx++ {
  150. // NOTE: each individual GetIndex() call to o is safe.
  151. c.setIndex(idx, c.getIndex(idx) && !o.GetIndex(idx))
  152. }
  153. }
  154. return c
  155. } else {
  156. return bA.and(o.Not()) // Note degenerate case where o == nil
  157. }
  158. }
  159. func (bA *BitArray) IsEmpty() bool {
  160. if bA == nil {
  161. return true // should this be opposite?
  162. }
  163. bA.mtx.Lock()
  164. defer bA.mtx.Unlock()
  165. for _, e := range bA.Elems {
  166. if e > 0 {
  167. return false
  168. }
  169. }
  170. return true
  171. }
  172. func (bA *BitArray) IsFull() bool {
  173. if bA == nil {
  174. return true
  175. }
  176. bA.mtx.Lock()
  177. defer bA.mtx.Unlock()
  178. // Check all elements except the last
  179. for _, elem := range bA.Elems[:len(bA.Elems)-1] {
  180. if (^elem) != 0 {
  181. return false
  182. }
  183. }
  184. // Check that the last element has (lastElemBits) 1's
  185. lastElemBits := (bA.Bits+63)%64 + 1
  186. lastElem := bA.Elems[len(bA.Elems)-1]
  187. return (lastElem+1)&((uint64(1)<<uint(lastElemBits))-1) == 0
  188. }
  189. func (bA *BitArray) PickRandom() (int, bool) {
  190. if bA == nil {
  191. return 0, false
  192. }
  193. bA.mtx.Lock()
  194. defer bA.mtx.Unlock()
  195. length := len(bA.Elems)
  196. if length == 0 {
  197. return 0, false
  198. }
  199. randElemStart := RandIntn(length)
  200. for i := 0; i < length; i++ {
  201. elemIdx := ((i + randElemStart) % length)
  202. if elemIdx < length-1 {
  203. if bA.Elems[elemIdx] > 0 {
  204. randBitStart := RandIntn(64)
  205. for j := 0; j < 64; j++ {
  206. bitIdx := ((j + randBitStart) % 64)
  207. if (bA.Elems[elemIdx] & (uint64(1) << uint(bitIdx))) > 0 {
  208. return 64*elemIdx + bitIdx, true
  209. }
  210. }
  211. PanicSanity("should not happen")
  212. }
  213. } else {
  214. // Special case for last elem, to ignore straggler bits
  215. elemBits := bA.Bits % 64
  216. if elemBits == 0 {
  217. elemBits = 64
  218. }
  219. randBitStart := RandIntn(elemBits)
  220. for j := 0; j < elemBits; j++ {
  221. bitIdx := ((j + randBitStart) % elemBits)
  222. if (bA.Elems[elemIdx] & (uint64(1) << uint(bitIdx))) > 0 {
  223. return 64*elemIdx + bitIdx, true
  224. }
  225. }
  226. }
  227. }
  228. return 0, false
  229. }
  230. func (bA *BitArray) String() string {
  231. if bA == nil {
  232. return "nil-BitArray"
  233. }
  234. bA.mtx.Lock()
  235. defer bA.mtx.Unlock()
  236. return bA.stringIndented("")
  237. }
  238. func (bA *BitArray) StringIndented(indent string) string {
  239. if bA == nil {
  240. return "nil-BitArray"
  241. }
  242. bA.mtx.Lock()
  243. defer bA.mtx.Unlock()
  244. return bA.stringIndented(indent)
  245. }
  246. func (bA *BitArray) stringIndented(indent string) string {
  247. lines := []string{}
  248. bits := ""
  249. for i := 0; i < bA.Bits; i++ {
  250. if bA.getIndex(i) {
  251. bits += "X"
  252. } else {
  253. bits += "_"
  254. }
  255. if i%100 == 99 {
  256. lines = append(lines, bits)
  257. bits = ""
  258. }
  259. if i%10 == 9 {
  260. bits += " "
  261. }
  262. if i%50 == 49 {
  263. bits += " "
  264. }
  265. }
  266. if len(bits) > 0 {
  267. lines = append(lines, bits)
  268. }
  269. return fmt.Sprintf("BA{%v:%v}", bA.Bits, strings.Join(lines, indent))
  270. }
  271. func (bA *BitArray) Bytes() []byte {
  272. bA.mtx.Lock()
  273. defer bA.mtx.Unlock()
  274. numBytes := (bA.Bits + 7) / 8
  275. bytes := make([]byte, numBytes)
  276. for i := 0; i < len(bA.Elems); i++ {
  277. elemBytes := [8]byte{}
  278. binary.LittleEndian.PutUint64(elemBytes[:], bA.Elems[i])
  279. copy(bytes[i*8:], elemBytes[:])
  280. }
  281. return bytes
  282. }
  283. // NOTE: other bitarray o is not locked when reading,
  284. // so if necessary, caller must copy or lock o prior to calling Update.
  285. // If bA is nil, does nothing.
  286. func (bA *BitArray) Update(o *BitArray) {
  287. if bA == nil || o == nil {
  288. return
  289. }
  290. bA.mtx.Lock()
  291. defer bA.mtx.Unlock()
  292. copy(bA.Elems, o.Elems)
  293. }