本站源代码
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

164 lines
3.6KB

  1. package roaring
  2. import (
  3. "fmt"
  4. )
  5. // common to rle32.go and rle16.go
  6. // rleVerbose controls whether p() prints show up.
  7. // The testing package sets this based on
  8. // testing.Verbose().
  9. var rleVerbose bool
  10. // p is a shorthand for fmt.Printf with beginning and
  11. // trailing newlines. p() makes it easy
  12. // to add diagnostic print statements.
  13. func p(format string, args ...interface{}) {
  14. if rleVerbose {
  15. fmt.Printf("\n"+format+"\n", args...)
  16. }
  17. }
  18. // MaxUint32 is the largest uint32 value.
  19. const MaxUint32 = 4294967295
  20. // MaxUint16 is the largest 16 bit unsigned int.
  21. // This is the largest value an interval16 can store.
  22. const MaxUint16 = 65535
  23. // searchOptions allows us to accelerate runContainer32.search with
  24. // prior knowledge of (mostly lower) bounds. This is used by Union
  25. // and Intersect.
  26. type searchOptions struct {
  27. // start here instead of at 0
  28. startIndex int64
  29. // upper bound instead of len(rc.iv);
  30. // endxIndex == 0 means ignore the bound and use
  31. // endxIndex == n ==len(rc.iv) which is also
  32. // naturally the default for search()
  33. // when opt = nil.
  34. endxIndex int64
  35. }
  36. // And finds the intersection of rc and b.
  37. func (rc *runContainer32) And(b *Bitmap) *Bitmap {
  38. out := NewBitmap()
  39. for _, p := range rc.iv {
  40. for i := p.start; i <= p.last; i++ {
  41. if b.Contains(i) {
  42. out.Add(i)
  43. }
  44. }
  45. }
  46. return out
  47. }
  48. // Xor returns the exclusive-or of rc and b.
  49. func (rc *runContainer32) Xor(b *Bitmap) *Bitmap {
  50. out := b.Clone()
  51. for _, p := range rc.iv {
  52. for v := p.start; v <= p.last; v++ {
  53. if out.Contains(v) {
  54. out.RemoveRange(uint64(v), uint64(v+1))
  55. } else {
  56. out.Add(v)
  57. }
  58. }
  59. }
  60. return out
  61. }
  62. // Or returns the union of rc and b.
  63. func (rc *runContainer32) Or(b *Bitmap) *Bitmap {
  64. out := b.Clone()
  65. for _, p := range rc.iv {
  66. for v := p.start; v <= p.last; v++ {
  67. out.Add(v)
  68. }
  69. }
  70. return out
  71. }
  72. // trial is used in the randomized testing of runContainers
  73. type trial struct {
  74. n int
  75. percentFill float64
  76. ntrial int
  77. // only in the union test
  78. // only subtract test
  79. percentDelete float64
  80. // only in 067 randomized operations
  81. // we do this + 1 passes
  82. numRandomOpsPass int
  83. // allow sampling range control
  84. // only recent tests respect this.
  85. srang *interval16
  86. }
  87. // And finds the intersection of rc and b.
  88. func (rc *runContainer16) And(b *Bitmap) *Bitmap {
  89. out := NewBitmap()
  90. for _, p := range rc.iv {
  91. plast := p.last()
  92. for i := p.start; i <= plast; i++ {
  93. if b.Contains(uint32(i)) {
  94. out.Add(uint32(i))
  95. }
  96. }
  97. }
  98. return out
  99. }
  100. // Xor returns the exclusive-or of rc and b.
  101. func (rc *runContainer16) Xor(b *Bitmap) *Bitmap {
  102. out := b.Clone()
  103. for _, p := range rc.iv {
  104. plast := p.last()
  105. for v := p.start; v <= plast; v++ {
  106. w := uint32(v)
  107. if out.Contains(w) {
  108. out.RemoveRange(uint64(w), uint64(w+1))
  109. } else {
  110. out.Add(w)
  111. }
  112. }
  113. }
  114. return out
  115. }
  116. // Or returns the union of rc and b.
  117. func (rc *runContainer16) Or(b *Bitmap) *Bitmap {
  118. out := b.Clone()
  119. for _, p := range rc.iv {
  120. plast := p.last()
  121. for v := p.start; v <= plast; v++ {
  122. out.Add(uint32(v))
  123. }
  124. }
  125. return out
  126. }
  127. //func (rc *runContainer32) and(container) container {
  128. // panic("TODO. not yet implemented")
  129. //}
  130. // serializedSizeInBytes returns the number of bytes of memory
  131. // required by this runContainer16. This is for the
  132. // Roaring format, as specified https://github.com/RoaringBitmap/RoaringFormatSpec/
  133. func (rc *runContainer16) serializedSizeInBytes() int {
  134. // number of runs in one uint16, then each run
  135. // needs two more uint16
  136. return 2 + len(rc.iv)*4
  137. }
  138. // serializedSizeInBytes returns the number of bytes of memory
  139. // required by this runContainer32.
  140. func (rc *runContainer32) serializedSizeInBytes() int {
  141. return 4 + len(rc.iv)*8
  142. }
上海开阖软件有限公司 沪ICP备12045867号-1