本站源代码
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

983 rindas
24KB

  1. package roaring
  2. import (
  3. "fmt"
  4. "unsafe"
  5. )
  6. //go:generate msgp -unexported
  7. type bitmapContainer struct {
  8. cardinality int
  9. bitmap []uint64
  10. }
  11. func (bc bitmapContainer) String() string {
  12. var s string
  13. for it := bc.getShortIterator(); it.hasNext(); {
  14. s += fmt.Sprintf("%v, ", it.next())
  15. }
  16. return s
  17. }
  18. func newBitmapContainer() *bitmapContainer {
  19. p := new(bitmapContainer)
  20. size := (1 << 16) / 64
  21. p.bitmap = make([]uint64, size, size)
  22. return p
  23. }
  24. func newBitmapContainerwithRange(firstOfRun, lastOfRun int) *bitmapContainer {
  25. bc := newBitmapContainer()
  26. bc.cardinality = lastOfRun - firstOfRun + 1
  27. if bc.cardinality == maxCapacity {
  28. fill(bc.bitmap, uint64(0xffffffffffffffff))
  29. } else {
  30. firstWord := firstOfRun / 64
  31. lastWord := lastOfRun / 64
  32. zeroPrefixLength := uint64(firstOfRun & 63)
  33. zeroSuffixLength := uint64(63 - (lastOfRun & 63))
  34. fillRange(bc.bitmap, firstWord, lastWord+1, uint64(0xffffffffffffffff))
  35. bc.bitmap[firstWord] ^= ((uint64(1) << zeroPrefixLength) - 1)
  36. blockOfOnes := (uint64(1) << zeroSuffixLength) - 1
  37. maskOnLeft := blockOfOnes << (uint64(64) - zeroSuffixLength)
  38. bc.bitmap[lastWord] ^= maskOnLeft
  39. }
  40. return bc
  41. }
  42. func (bc *bitmapContainer) minimum() uint16 {
  43. for i := 0; i < len(bc.bitmap); i++ {
  44. w := bc.bitmap[i]
  45. if w != 0 {
  46. r := countTrailingZeros(w)
  47. return uint16(r + i*64)
  48. }
  49. }
  50. return MaxUint16
  51. }
  52. // i should be non-zero
  53. func clz(i uint64) int {
  54. n := 1
  55. x := uint32(i >> 32)
  56. if x == 0 {
  57. n += 32
  58. x = uint32(i)
  59. }
  60. if x>>16 == 0 {
  61. n += 16
  62. x = x << 16
  63. }
  64. if x>>24 == 0 {
  65. n += 8
  66. x = x << 8
  67. }
  68. if x>>28 == 0 {
  69. n += 4
  70. x = x << 4
  71. }
  72. if x>>30 == 0 {
  73. n += 2
  74. x = x << 2
  75. }
  76. return n - int(x>>31)
  77. }
  78. func (bc *bitmapContainer) maximum() uint16 {
  79. for i := len(bc.bitmap); i > 0; i-- {
  80. w := bc.bitmap[i-1]
  81. if w != 0 {
  82. r := clz(w)
  83. return uint16((i-1)*64 + 63 - r)
  84. }
  85. }
  86. return uint16(0)
  87. }
  88. type bitmapContainerShortIterator struct {
  89. ptr *bitmapContainer
  90. i int
  91. }
  92. func (bcsi *bitmapContainerShortIterator) next() uint16 {
  93. j := bcsi.i
  94. bcsi.i = bcsi.ptr.NextSetBit(bcsi.i + 1)
  95. return uint16(j)
  96. }
  97. func (bcsi *bitmapContainerShortIterator) hasNext() bool {
  98. return bcsi.i >= 0
  99. }
  100. func newBitmapContainerShortIterator(a *bitmapContainer) *bitmapContainerShortIterator {
  101. return &bitmapContainerShortIterator{a, a.NextSetBit(0)}
  102. }
  103. func (bc *bitmapContainer) getShortIterator() shortIterable {
  104. return newBitmapContainerShortIterator(bc)
  105. }
  106. type bitmapContainerManyIterator struct {
  107. ptr *bitmapContainer
  108. base int
  109. bitset uint64
  110. }
  111. func (bcmi *bitmapContainerManyIterator) nextMany(hs uint32, buf []uint32) int {
  112. n := 0
  113. base := bcmi.base
  114. bitset := bcmi.bitset
  115. for n < len(buf) {
  116. if bitset == 0 {
  117. base += 1
  118. if base >= len(bcmi.ptr.bitmap) {
  119. bcmi.base = base
  120. bcmi.bitset = bitset
  121. return n
  122. }
  123. bitset = bcmi.ptr.bitmap[base]
  124. continue
  125. }
  126. t := bitset & -bitset
  127. buf[n] = uint32(((base * 64) + int(popcount(t-1)))) | hs
  128. n = n + 1
  129. bitset ^= t
  130. }
  131. bcmi.base = base
  132. bcmi.bitset = bitset
  133. return n
  134. }
  135. func newBitmapContainerManyIterator(a *bitmapContainer) *bitmapContainerManyIterator {
  136. return &bitmapContainerManyIterator{a, -1, 0}
  137. }
  138. func (bc *bitmapContainer) getManyIterator() manyIterable {
  139. return newBitmapContainerManyIterator(bc)
  140. }
  141. func (bc *bitmapContainer) getSizeInBytes() int {
  142. return len(bc.bitmap) * 8 // + bcBaseBytes
  143. }
  144. func (bc *bitmapContainer) serializedSizeInBytes() int {
  145. //return bc.Msgsize()// NOO! This breaks GetSerializedSizeInBytes
  146. return len(bc.bitmap) * 8
  147. }
  148. const bcBaseBytes = int(unsafe.Sizeof(bitmapContainer{}))
  149. // bitmapContainer doesn't depend on card, always fully allocated
  150. func bitmapContainerSizeInBytes() int {
  151. return bcBaseBytes + (1<<16)/8
  152. }
  153. func bitmapEquals(a, b []uint64) bool {
  154. if len(a) != len(b) {
  155. //p("bitmaps differ on length. len(a)=%v; len(b)=%v", len(a), len(b))
  156. return false
  157. }
  158. for i, v := range a {
  159. if v != b[i] {
  160. //p("bitmaps differ on element i=%v", i)
  161. return false
  162. }
  163. }
  164. //p("bitmapEquals returning true")
  165. return true
  166. }
  167. func (bc *bitmapContainer) fillLeastSignificant16bits(x []uint32, i int, mask uint32) {
  168. // TODO: should be written as optimized assembly
  169. pos := i
  170. base := mask
  171. for k := 0; k < len(bc.bitmap); k++ {
  172. bitset := bc.bitmap[k]
  173. for bitset != 0 {
  174. t := bitset & -bitset
  175. x[pos] = base + uint32(popcount(t-1))
  176. pos++
  177. bitset ^= t
  178. }
  179. base += 64
  180. }
  181. }
  182. func (bc *bitmapContainer) equals(o container) bool {
  183. srb, ok := o.(*bitmapContainer)
  184. if ok {
  185. //p("bitmapContainers.equals: both are bitmapContainers")
  186. if srb.cardinality != bc.cardinality {
  187. //p("bitmapContainers.equals: card differs: %v vs %v", srb.cardinality, bc.cardinality)
  188. return false
  189. }
  190. return bitmapEquals(bc.bitmap, srb.bitmap)
  191. }
  192. // use generic comparison
  193. if bc.getCardinality() != o.getCardinality() {
  194. return false
  195. }
  196. ait := o.getShortIterator()
  197. bit := bc.getShortIterator()
  198. for ait.hasNext() {
  199. if bit.next() != ait.next() {
  200. return false
  201. }
  202. }
  203. return true
  204. }
  205. func (bc *bitmapContainer) iaddReturnMinimized(i uint16) container {
  206. bc.iadd(i)
  207. if bc.isFull() {
  208. return newRunContainer16Range(0, MaxUint16)
  209. }
  210. return bc
  211. }
  212. func (bc *bitmapContainer) iadd(i uint16) bool {
  213. x := int(i)
  214. previous := bc.bitmap[x/64]
  215. mask := uint64(1) << (uint(x) % 64)
  216. newb := previous | mask
  217. bc.bitmap[x/64] = newb
  218. bc.cardinality += int((previous ^ newb) >> (uint(x) % 64))
  219. return newb != previous
  220. }
  221. func (bc *bitmapContainer) iremoveReturnMinimized(i uint16) container {
  222. if bc.iremove(i) {
  223. if bc.cardinality == arrayDefaultMaxSize {
  224. return bc.toArrayContainer()
  225. }
  226. }
  227. return bc
  228. }
  229. // iremove returns true if i was found.
  230. func (bc *bitmapContainer) iremove(i uint16) bool {
  231. /* branchless code
  232. w := bc.bitmap[i>>6]
  233. mask := uint64(1) << (i % 64)
  234. neww := w &^ mask
  235. bc.cardinality -= int((w ^ neww) >> (i % 64))
  236. bc.bitmap[i>>6] = neww */
  237. if bc.contains(i) {
  238. bc.cardinality--
  239. bc.bitmap[i/64] &^= (uint64(1) << (i % 64))
  240. return true
  241. }
  242. return false
  243. }
  244. func (bc *bitmapContainer) isFull() bool {
  245. return bc.cardinality == int(MaxUint16)+1
  246. }
  247. func (bc *bitmapContainer) getCardinality() int {
  248. return bc.cardinality
  249. }
  250. func (bc *bitmapContainer) clone() container {
  251. ptr := bitmapContainer{bc.cardinality, make([]uint64, len(bc.bitmap))}
  252. copy(ptr.bitmap, bc.bitmap[:])
  253. return &ptr
  254. }
  255. // add all values in range [firstOfRange,lastOfRange)
  256. func (bc *bitmapContainer) iaddRange(firstOfRange, lastOfRange int) container {
  257. bc.cardinality += setBitmapRangeAndCardinalityChange(bc.bitmap, firstOfRange, lastOfRange)
  258. return bc
  259. }
  260. // remove all values in range [firstOfRange,lastOfRange)
  261. func (bc *bitmapContainer) iremoveRange(firstOfRange, lastOfRange int) container {
  262. bc.cardinality += resetBitmapRangeAndCardinalityChange(bc.bitmap, firstOfRange, lastOfRange)
  263. if bc.getCardinality() <= arrayDefaultMaxSize {
  264. return bc.toArrayContainer()
  265. }
  266. return bc
  267. }
  268. // flip all values in range [firstOfRange,endx)
  269. func (bc *bitmapContainer) inot(firstOfRange, endx int) container {
  270. p("bc.inot() called with [%v, %v)", firstOfRange, endx)
  271. if endx-firstOfRange == maxCapacity {
  272. //p("endx-firstOfRange == maxCapacity")
  273. flipBitmapRange(bc.bitmap, firstOfRange, endx)
  274. bc.cardinality = maxCapacity - bc.cardinality
  275. //p("bc.cardinality is now %v", bc.cardinality)
  276. } else if endx-firstOfRange > maxCapacity/2 {
  277. //p("endx-firstOfRange > maxCapacity/2")
  278. flipBitmapRange(bc.bitmap, firstOfRange, endx)
  279. bc.computeCardinality()
  280. } else {
  281. bc.cardinality += flipBitmapRangeAndCardinalityChange(bc.bitmap, firstOfRange, endx)
  282. }
  283. if bc.getCardinality() <= arrayDefaultMaxSize {
  284. return bc.toArrayContainer()
  285. }
  286. return bc
  287. }
  288. // flip all values in range [firstOfRange,endx)
  289. func (bc *bitmapContainer) not(firstOfRange, endx int) container {
  290. answer := bc.clone()
  291. return answer.inot(firstOfRange, endx)
  292. }
  293. func (bc *bitmapContainer) or(a container) container {
  294. switch x := a.(type) {
  295. case *arrayContainer:
  296. return bc.orArray(x)
  297. case *bitmapContainer:
  298. return bc.orBitmap(x)
  299. case *runContainer16:
  300. if x.isFull() {
  301. return x.clone()
  302. }
  303. return x.orBitmapContainer(bc)
  304. }
  305. panic("unsupported container type")
  306. }
  307. func (bc *bitmapContainer) orCardinality(a container) int {
  308. switch x := a.(type) {
  309. case *arrayContainer:
  310. return bc.orArrayCardinality(x)
  311. case *bitmapContainer:
  312. return bc.orBitmapCardinality(x)
  313. case *runContainer16:
  314. return x.orBitmapContainerCardinality(bc)
  315. }
  316. panic("unsupported container type")
  317. }
  318. func (bc *bitmapContainer) ior(a container) container {
  319. switch x := a.(type) {
  320. case *arrayContainer:
  321. return bc.iorArray(x)
  322. case *bitmapContainer:
  323. return bc.iorBitmap(x)
  324. case *runContainer16:
  325. if x.isFull() {
  326. return x.clone()
  327. }
  328. for i := range x.iv {
  329. bc.iaddRange(int(x.iv[i].start), int(x.iv[i].last())+1)
  330. }
  331. if bc.isFull() {
  332. return newRunContainer16Range(0, MaxUint16)
  333. }
  334. //bc.computeCardinality()
  335. return bc
  336. }
  337. panic(fmt.Errorf("unsupported container type %T", a))
  338. }
  339. func (bc *bitmapContainer) lazyIOR(a container) container {
  340. switch x := a.(type) {
  341. case *arrayContainer:
  342. return bc.lazyIORArray(x)
  343. case *bitmapContainer:
  344. return bc.lazyIORBitmap(x)
  345. case *runContainer16:
  346. if x.isFull() {
  347. return x.clone()
  348. }
  349. // Manually inlined setBitmapRange function
  350. bitmap := bc.bitmap
  351. for _, iv := range x.iv {
  352. start := int(iv.start)
  353. end := int(iv.last()) + 1
  354. if start >= end {
  355. continue
  356. }
  357. firstword := start / 64
  358. endword := (end - 1) / 64
  359. if firstword == endword {
  360. bitmap[firstword] |= (^uint64(0) << uint(start%64)) & (^uint64(0) >> (uint(-end) % 64))
  361. continue
  362. }
  363. bitmap[firstword] |= ^uint64(0) << uint(start%64)
  364. for i := firstword + 1; i < endword; i++ {
  365. bitmap[i] = ^uint64(0)
  366. }
  367. bitmap[endword] |= ^uint64(0) >> (uint(-end) % 64)
  368. }
  369. bc.cardinality = invalidCardinality
  370. return bc
  371. }
  372. panic("unsupported container type")
  373. }
  374. func (bc *bitmapContainer) lazyOR(a container) container {
  375. switch x := a.(type) {
  376. case *arrayContainer:
  377. return bc.lazyORArray(x)
  378. case *bitmapContainer:
  379. return bc.lazyORBitmap(x)
  380. case *runContainer16:
  381. if x.isFull() {
  382. return x.clone()
  383. }
  384. // TODO: implement lazy OR
  385. return x.orBitmapContainer(bc)
  386. }
  387. panic("unsupported container type")
  388. }
  389. func (bc *bitmapContainer) orArray(value2 *arrayContainer) container {
  390. answer := bc.clone().(*bitmapContainer)
  391. c := value2.getCardinality()
  392. for k := 0; k < c; k++ {
  393. v := value2.content[k]
  394. i := uint(v) >> 6
  395. bef := answer.bitmap[i]
  396. aft := bef | (uint64(1) << (v % 64))
  397. answer.bitmap[i] = aft
  398. answer.cardinality += int((bef - aft) >> 63)
  399. }
  400. return answer
  401. }
  402. func (bc *bitmapContainer) orArrayCardinality(value2 *arrayContainer) int {
  403. answer := 0
  404. c := value2.getCardinality()
  405. for k := 0; k < c; k++ {
  406. // branchless:
  407. v := value2.content[k]
  408. i := uint(v) >> 6
  409. bef := bc.bitmap[i]
  410. aft := bef | (uint64(1) << (v % 64))
  411. answer += int((bef - aft) >> 63)
  412. }
  413. return answer
  414. }
  415. func (bc *bitmapContainer) orBitmap(value2 *bitmapContainer) container {
  416. answer := newBitmapContainer()
  417. for k := 0; k < len(answer.bitmap); k++ {
  418. answer.bitmap[k] = bc.bitmap[k] | value2.bitmap[k]
  419. }
  420. answer.computeCardinality()
  421. if answer.isFull() {
  422. return newRunContainer16Range(0, MaxUint16)
  423. }
  424. return answer
  425. }
  426. func (bc *bitmapContainer) orBitmapCardinality(value2 *bitmapContainer) int {
  427. return int(popcntOrSlice(bc.bitmap, value2.bitmap))
  428. }
  429. func (bc *bitmapContainer) andBitmapCardinality(value2 *bitmapContainer) int {
  430. return int(popcntAndSlice(bc.bitmap, value2.bitmap))
  431. }
  432. func (bc *bitmapContainer) computeCardinality() {
  433. bc.cardinality = int(popcntSlice(bc.bitmap))
  434. }
  435. func (bc *bitmapContainer) iorArray(ac *arrayContainer) container {
  436. for k := range ac.content {
  437. vc := ac.content[k]
  438. i := uint(vc) >> 6
  439. bef := bc.bitmap[i]
  440. aft := bef | (uint64(1) << (vc % 64))
  441. bc.bitmap[i] = aft
  442. bc.cardinality += int((bef - aft) >> 63)
  443. }
  444. if bc.isFull() {
  445. return newRunContainer16Range(0, MaxUint16)
  446. }
  447. return bc
  448. }
  449. func (bc *bitmapContainer) iorBitmap(value2 *bitmapContainer) container {
  450. answer := bc
  451. answer.cardinality = 0
  452. for k := 0; k < len(answer.bitmap); k++ {
  453. answer.bitmap[k] = bc.bitmap[k] | value2.bitmap[k]
  454. }
  455. answer.computeCardinality()
  456. if bc.isFull() {
  457. return newRunContainer16Range(0, MaxUint16)
  458. }
  459. return answer
  460. }
  461. func (bc *bitmapContainer) lazyIORArray(value2 *arrayContainer) container {
  462. answer := bc
  463. c := value2.getCardinality()
  464. for k := 0; k < c; k++ {
  465. vc := value2.content[k]
  466. i := uint(vc) >> 6
  467. answer.bitmap[i] = answer.bitmap[i] | (uint64(1) << (vc % 64))
  468. }
  469. answer.cardinality = invalidCardinality
  470. return answer
  471. }
  472. func (bc *bitmapContainer) lazyORArray(value2 *arrayContainer) container {
  473. answer := bc.clone().(*bitmapContainer)
  474. return answer.lazyIORArray(value2)
  475. }
  476. func (bc *bitmapContainer) lazyIORBitmap(value2 *bitmapContainer) container {
  477. answer := bc
  478. for k := 0; k < len(answer.bitmap); k++ {
  479. answer.bitmap[k] = bc.bitmap[k] | value2.bitmap[k]
  480. }
  481. bc.cardinality = invalidCardinality
  482. return answer
  483. }
  484. func (bc *bitmapContainer) lazyORBitmap(value2 *bitmapContainer) container {
  485. answer := bc.clone().(*bitmapContainer)
  486. return answer.lazyIORBitmap(value2)
  487. }
  488. func (bc *bitmapContainer) xor(a container) container {
  489. switch x := a.(type) {
  490. case *arrayContainer:
  491. return bc.xorArray(x)
  492. case *bitmapContainer:
  493. return bc.xorBitmap(x)
  494. case *runContainer16:
  495. return x.xorBitmap(bc)
  496. }
  497. panic("unsupported container type")
  498. }
  499. func (bc *bitmapContainer) xorArray(value2 *arrayContainer) container {
  500. answer := bc.clone().(*bitmapContainer)
  501. c := value2.getCardinality()
  502. for k := 0; k < c; k++ {
  503. vc := value2.content[k]
  504. index := uint(vc) >> 6
  505. abi := answer.bitmap[index]
  506. mask := uint64(1) << (vc % 64)
  507. answer.cardinality += 1 - 2*int((abi&mask)>>(vc%64))
  508. answer.bitmap[index] = abi ^ mask
  509. }
  510. if answer.cardinality <= arrayDefaultMaxSize {
  511. return answer.toArrayContainer()
  512. }
  513. return answer
  514. }
  515. func (bc *bitmapContainer) rank(x uint16) int {
  516. // TODO: rewrite in assembly
  517. leftover := (uint(x) + 1) & 63
  518. if leftover == 0 {
  519. return int(popcntSlice(bc.bitmap[:(uint(x)+1)/64]))
  520. }
  521. return int(popcntSlice(bc.bitmap[:(uint(x)+1)/64]) + popcount(bc.bitmap[(uint(x)+1)/64]<<(64-leftover)))
  522. }
  523. func (bc *bitmapContainer) selectInt(x uint16) int {
  524. remaining := x
  525. for k := 0; k < len(bc.bitmap); k++ {
  526. w := popcount(bc.bitmap[k])
  527. if uint16(w) > remaining {
  528. return k*64 + selectBitPosition(bc.bitmap[k], int(remaining))
  529. }
  530. remaining -= uint16(w)
  531. }
  532. return -1
  533. }
  534. func (bc *bitmapContainer) xorBitmap(value2 *bitmapContainer) container {
  535. newCardinality := int(popcntXorSlice(bc.bitmap, value2.bitmap))
  536. if newCardinality > arrayDefaultMaxSize {
  537. answer := newBitmapContainer()
  538. for k := 0; k < len(answer.bitmap); k++ {
  539. answer.bitmap[k] = bc.bitmap[k] ^ value2.bitmap[k]
  540. }
  541. answer.cardinality = newCardinality
  542. if answer.isFull() {
  543. return newRunContainer16Range(0, MaxUint16)
  544. }
  545. return answer
  546. }
  547. ac := newArrayContainerSize(newCardinality)
  548. fillArrayXOR(ac.content, bc.bitmap, value2.bitmap)
  549. ac.content = ac.content[:newCardinality]
  550. return ac
  551. }
  552. func (bc *bitmapContainer) and(a container) container {
  553. switch x := a.(type) {
  554. case *arrayContainer:
  555. return bc.andArray(x)
  556. case *bitmapContainer:
  557. return bc.andBitmap(x)
  558. case *runContainer16:
  559. if x.isFull() {
  560. return bc.clone()
  561. }
  562. return x.andBitmapContainer(bc)
  563. }
  564. panic("unsupported container type")
  565. }
  566. func (bc *bitmapContainer) andCardinality(a container) int {
  567. switch x := a.(type) {
  568. case *arrayContainer:
  569. return bc.andArrayCardinality(x)
  570. case *bitmapContainer:
  571. return bc.andBitmapCardinality(x)
  572. case *runContainer16:
  573. return x.andBitmapContainerCardinality(bc)
  574. }
  575. panic("unsupported container type")
  576. }
  577. func (bc *bitmapContainer) intersects(a container) bool {
  578. switch x := a.(type) {
  579. case *arrayContainer:
  580. return bc.intersectsArray(x)
  581. case *bitmapContainer:
  582. return bc.intersectsBitmap(x)
  583. case *runContainer16:
  584. return x.intersects(bc)
  585. }
  586. panic("unsupported container type")
  587. }
  588. func (bc *bitmapContainer) iand(a container) container {
  589. switch x := a.(type) {
  590. case *arrayContainer:
  591. return bc.iandArray(x)
  592. case *bitmapContainer:
  593. return bc.iandBitmap(x)
  594. case *runContainer16:
  595. if x.isFull() {
  596. return bc.clone()
  597. }
  598. return bc.iandRun16(x)
  599. }
  600. panic("unsupported container type")
  601. }
  602. func (bc *bitmapContainer) iandRun16(rc *runContainer16) container {
  603. rcb := newBitmapContainerFromRun(rc)
  604. return bc.iandBitmap(rcb)
  605. }
  606. func (bc *bitmapContainer) iandArray(ac *arrayContainer) container {
  607. acb := ac.toBitmapContainer()
  608. return bc.iandBitmap(acb)
  609. }
  610. func (bc *bitmapContainer) andArray(value2 *arrayContainer) *arrayContainer {
  611. answer := newArrayContainerCapacity(len(value2.content))
  612. answer.content = answer.content[:cap(answer.content)]
  613. c := value2.getCardinality()
  614. pos := 0
  615. for k := 0; k < c; k++ {
  616. v := value2.content[k]
  617. answer.content[pos] = v
  618. pos += int(bc.bitValue(v))
  619. }
  620. answer.content = answer.content[:pos]
  621. return answer
  622. }
  623. func (bc *bitmapContainer) andArrayCardinality(value2 *arrayContainer) int {
  624. c := value2.getCardinality()
  625. pos := 0
  626. for k := 0; k < c; k++ {
  627. v := value2.content[k]
  628. pos += int(bc.bitValue(v))
  629. }
  630. return pos
  631. }
  632. func (bc *bitmapContainer) getCardinalityInRange(start, end uint) int {
  633. if start >= end {
  634. return 0
  635. }
  636. firstword := start / 64
  637. endword := (end - 1) / 64
  638. const allones = ^uint64(0)
  639. if firstword == endword {
  640. return int(popcount(bc.bitmap[firstword] & ((allones << (start % 64)) & (allones >> ((64 - end) & 63)))))
  641. }
  642. answer := popcount(bc.bitmap[firstword] & (allones << (start % 64)))
  643. answer += popcntSlice(bc.bitmap[firstword+1 : endword])
  644. answer += popcount(bc.bitmap[endword] & (allones >> ((64 - end) & 63)))
  645. return int(answer)
  646. }
  647. func (bc *bitmapContainer) andBitmap(value2 *bitmapContainer) container {
  648. newcardinality := int(popcntAndSlice(bc.bitmap, value2.bitmap))
  649. if newcardinality > arrayDefaultMaxSize {
  650. answer := newBitmapContainer()
  651. for k := 0; k < len(answer.bitmap); k++ {
  652. answer.bitmap[k] = bc.bitmap[k] & value2.bitmap[k]
  653. }
  654. answer.cardinality = newcardinality
  655. return answer
  656. }
  657. ac := newArrayContainerSize(newcardinality)
  658. fillArrayAND(ac.content, bc.bitmap, value2.bitmap)
  659. ac.content = ac.content[:newcardinality] //not sure why i need this
  660. return ac
  661. }
  662. func (bc *bitmapContainer) intersectsArray(value2 *arrayContainer) bool {
  663. c := value2.getCardinality()
  664. for k := 0; k < c; k++ {
  665. v := value2.content[k]
  666. if bc.contains(v) {
  667. return true
  668. }
  669. }
  670. return false
  671. }
  672. func (bc *bitmapContainer) intersectsBitmap(value2 *bitmapContainer) bool {
  673. for k := 0; k < len(bc.bitmap); k++ {
  674. if (bc.bitmap[k] & value2.bitmap[k]) != 0 {
  675. return true
  676. }
  677. }
  678. return false
  679. }
  680. func (bc *bitmapContainer) iandBitmap(value2 *bitmapContainer) container {
  681. newcardinality := int(popcntAndSlice(bc.bitmap, value2.bitmap))
  682. for k := 0; k < len(bc.bitmap); k++ {
  683. bc.bitmap[k] = bc.bitmap[k] & value2.bitmap[k]
  684. }
  685. bc.cardinality = newcardinality
  686. if newcardinality <= arrayDefaultMaxSize {
  687. return newArrayContainerFromBitmap(bc)
  688. }
  689. return bc
  690. }
  691. func (bc *bitmapContainer) andNot(a container) container {
  692. switch x := a.(type) {
  693. case *arrayContainer:
  694. return bc.andNotArray(x)
  695. case *bitmapContainer:
  696. return bc.andNotBitmap(x)
  697. case *runContainer16:
  698. return bc.andNotRun16(x)
  699. }
  700. panic("unsupported container type")
  701. }
  702. func (bc *bitmapContainer) andNotRun16(rc *runContainer16) container {
  703. rcb := rc.toBitmapContainer()
  704. return bc.andNotBitmap(rcb)
  705. }
  706. func (bc *bitmapContainer) iandNot(a container) container {
  707. //p("bitmapContainer.iandNot() starting")
  708. switch x := a.(type) {
  709. case *arrayContainer:
  710. return bc.iandNotArray(x)
  711. case *bitmapContainer:
  712. return bc.iandNotBitmapSurely(x)
  713. case *runContainer16:
  714. return bc.iandNotRun16(x)
  715. }
  716. panic("unsupported container type")
  717. }
  718. func (bc *bitmapContainer) iandNotArray(ac *arrayContainer) container {
  719. acb := ac.toBitmapContainer()
  720. return bc.iandNotBitmapSurely(acb)
  721. }
  722. func (bc *bitmapContainer) iandNotRun16(rc *runContainer16) container {
  723. rcb := rc.toBitmapContainer()
  724. return bc.iandNotBitmapSurely(rcb)
  725. }
  726. func (bc *bitmapContainer) andNotArray(value2 *arrayContainer) container {
  727. answer := bc.clone().(*bitmapContainer)
  728. c := value2.getCardinality()
  729. for k := 0; k < c; k++ {
  730. vc := value2.content[k]
  731. i := uint(vc) >> 6
  732. oldv := answer.bitmap[i]
  733. newv := oldv &^ (uint64(1) << (vc % 64))
  734. answer.bitmap[i] = newv
  735. answer.cardinality -= int((oldv ^ newv) >> (vc % 64))
  736. }
  737. if answer.cardinality <= arrayDefaultMaxSize {
  738. return answer.toArrayContainer()
  739. }
  740. return answer
  741. }
  742. func (bc *bitmapContainer) andNotBitmap(value2 *bitmapContainer) container {
  743. newCardinality := int(popcntMaskSlice(bc.bitmap, value2.bitmap))
  744. if newCardinality > arrayDefaultMaxSize {
  745. answer := newBitmapContainer()
  746. for k := 0; k < len(answer.bitmap); k++ {
  747. answer.bitmap[k] = bc.bitmap[k] &^ value2.bitmap[k]
  748. }
  749. answer.cardinality = newCardinality
  750. return answer
  751. }
  752. ac := newArrayContainerSize(newCardinality)
  753. fillArrayANDNOT(ac.content, bc.bitmap, value2.bitmap)
  754. return ac
  755. }
  756. func (bc *bitmapContainer) iandNotBitmapSurely(value2 *bitmapContainer) *bitmapContainer {
  757. newCardinality := int(popcntMaskSlice(bc.bitmap, value2.bitmap))
  758. for k := 0; k < len(bc.bitmap); k++ {
  759. bc.bitmap[k] = bc.bitmap[k] &^ value2.bitmap[k]
  760. }
  761. bc.cardinality = newCardinality
  762. return bc
  763. }
  764. func (bc *bitmapContainer) contains(i uint16) bool { //testbit
  765. x := uint(i)
  766. w := bc.bitmap[x>>6]
  767. mask := uint64(1) << (x & 63)
  768. return (w & mask) != 0
  769. }
  770. func (bc *bitmapContainer) bitValue(i uint16) uint64 {
  771. x := uint(i)
  772. w := bc.bitmap[x>>6]
  773. return (w >> (x & 63)) & 1
  774. }
  775. func (bc *bitmapContainer) loadData(arrayContainer *arrayContainer) {
  776. bc.cardinality = arrayContainer.getCardinality()
  777. c := arrayContainer.getCardinality()
  778. for k := 0; k < c; k++ {
  779. x := arrayContainer.content[k]
  780. i := int(x) / 64
  781. bc.bitmap[i] |= (uint64(1) << uint(x%64))
  782. }
  783. }
  784. func (bc *bitmapContainer) toArrayContainer() *arrayContainer {
  785. ac := &arrayContainer{}
  786. ac.loadData(bc)
  787. return ac
  788. }
  789. func (bc *bitmapContainer) fillArray(container []uint16) {
  790. //TODO: rewrite in assembly
  791. pos := 0
  792. base := 0
  793. for k := 0; k < len(bc.bitmap); k++ {
  794. bitset := bc.bitmap[k]
  795. for bitset != 0 {
  796. t := bitset & -bitset
  797. container[pos] = uint16((base + int(popcount(t-1))))
  798. pos = pos + 1
  799. bitset ^= t
  800. }
  801. base += 64
  802. }
  803. }
  804. func (bc *bitmapContainer) NextSetBit(i int) int {
  805. x := i / 64
  806. if x >= len(bc.bitmap) {
  807. return -1
  808. }
  809. w := bc.bitmap[x]
  810. w = w >> uint(i%64)
  811. if w != 0 {
  812. return i + countTrailingZeros(w)
  813. }
  814. x++
  815. for ; x < len(bc.bitmap); x++ {
  816. if bc.bitmap[x] != 0 {
  817. return (x * 64) + countTrailingZeros(bc.bitmap[x])
  818. }
  819. }
  820. return -1
  821. }
  822. // reference the java implementation
  823. // https://github.com/RoaringBitmap/RoaringBitmap/blob/master/src/main/java/org/roaringbitmap/BitmapContainer.java#L875-L892
  824. //
  825. func (bc *bitmapContainer) numberOfRuns() int {
  826. if bc.cardinality == 0 {
  827. return 0
  828. }
  829. var numRuns uint64
  830. nextWord := bc.bitmap[0]
  831. for i := 0; i < len(bc.bitmap)-1; i++ {
  832. word := nextWord
  833. nextWord = bc.bitmap[i+1]
  834. numRuns += popcount((^word)&(word<<1)) + ((word >> 63) &^ nextWord)
  835. }
  836. word := nextWord
  837. numRuns += popcount((^word) & (word << 1))
  838. if (word & 0x8000000000000000) != 0 {
  839. numRuns++
  840. }
  841. return int(numRuns)
  842. }
  843. // convert to run or array *if needed*
  844. func (bc *bitmapContainer) toEfficientContainer() container {
  845. numRuns := bc.numberOfRuns()
  846. sizeAsRunContainer := runContainer16SerializedSizeInBytes(numRuns)
  847. sizeAsBitmapContainer := bitmapContainerSizeInBytes()
  848. card := bc.getCardinality()
  849. sizeAsArrayContainer := arrayContainerSizeInBytes(card)
  850. if sizeAsRunContainer <= minOfInt(sizeAsBitmapContainer, sizeAsArrayContainer) {
  851. return newRunContainer16FromBitmapContainer(bc)
  852. }
  853. if card <= arrayDefaultMaxSize {
  854. return bc.toArrayContainer()
  855. }
  856. return bc
  857. }
  858. func newBitmapContainerFromRun(rc *runContainer16) *bitmapContainer {
  859. if len(rc.iv) == 1 {
  860. return newBitmapContainerwithRange(int(rc.iv[0].start), int(rc.iv[0].last()))
  861. }
  862. bc := newBitmapContainer()
  863. for i := range rc.iv {
  864. setBitmapRange(bc.bitmap, int(rc.iv[i].start), int(rc.iv[i].last())+1)
  865. bc.cardinality += int(rc.iv[i].last()) + 1 - int(rc.iv[i].start)
  866. }
  867. //bc.computeCardinality()
  868. return bc
  869. }
  870. func (bc *bitmapContainer) containerType() contype {
  871. return bitmapContype
  872. }
上海开阖软件有限公司 沪ICP备12045867号-1