roaring.go 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  1. // Package roaring is an implementation of Roaring Bitmaps in Go.
  2. // They provide fast compressed bitmap data structures (also called bitset).
  3. // They are ideally suited to represent sets of integers over
  4. // relatively small ranges.
  5. // See http://roaringbitmap.org for details.
  6. package roaring
  7. import (
  8. "bytes"
  9. "encoding/base64"
  10. "fmt"
  11. "io"
  12. "strconv"
  13. "github.com/RoaringBitmap/roaring/internal"
  14. )
  15. // Bitmap represents a compressed bitmap where you can add integers.
  16. type Bitmap struct {
  17. highlowcontainer roaringArray
  18. }
  19. // ToBase64 serializes a bitmap as Base64
  20. func (rb *Bitmap) ToBase64() (string, error) {
  21. buf := new(bytes.Buffer)
  22. _, err := rb.WriteTo(buf)
  23. return base64.StdEncoding.EncodeToString(buf.Bytes()), err
  24. }
  25. // FromBase64 deserializes a bitmap from Base64
  26. func (rb *Bitmap) FromBase64(str string) (int64, error) {
  27. data, err := base64.StdEncoding.DecodeString(str)
  28. if err != nil {
  29. return 0, err
  30. }
  31. buf := bytes.NewBuffer(data)
  32. return rb.ReadFrom(buf)
  33. }
  34. // WriteTo writes a serialized version of this bitmap to stream.
  35. // The format is compatible with other RoaringBitmap
  36. // implementations (Java, C) and is documented here:
  37. // https://github.com/RoaringBitmap/RoaringFormatSpec
  38. func (rb *Bitmap) WriteTo(stream io.Writer) (int64, error) {
  39. return rb.highlowcontainer.writeTo(stream)
  40. }
  41. // ToBytes returns an array of bytes corresponding to what is written
  42. // when calling WriteTo
  43. func (rb *Bitmap) ToBytes() ([]byte, error) {
  44. return rb.highlowcontainer.toBytes()
  45. }
  46. // Checksum computes a hash (currently FNV-1a) for a bitmap that is suitable for
  47. // using bitmaps as elements in hash sets or as keys in hash maps, as well as
  48. // generally quicker comparisons.
  49. // The implementation is biased towards efficiency in little endian machines, so
  50. // expect some extra CPU cycles and memory to be used if your machine is big endian.
  51. // Likewise, don't use this to verify integrity unless you're certain you'll load
  52. // the bitmap on a machine with the same endianess used to create it.
  53. func (rb *Bitmap) Checksum() uint64 {
  54. const (
  55. offset = 14695981039346656037
  56. prime = 1099511628211
  57. )
  58. var bytes []byte
  59. hash := uint64(offset)
  60. bytes = uint16SliceAsByteSlice(rb.highlowcontainer.keys)
  61. for _, b := range bytes {
  62. hash ^= uint64(b)
  63. hash *= prime
  64. }
  65. for _, c := range rb.highlowcontainer.containers {
  66. // 0 separator
  67. hash ^= 0
  68. hash *= prime
  69. switch c := c.(type) {
  70. case *bitmapContainer:
  71. bytes = uint64SliceAsByteSlice(c.bitmap)
  72. case *arrayContainer:
  73. bytes = uint16SliceAsByteSlice(c.content)
  74. case *runContainer16:
  75. bytes = interval16SliceAsByteSlice(c.iv)
  76. default:
  77. panic("invalid container type")
  78. }
  79. if len(bytes) == 0 {
  80. panic("empty containers are not supported")
  81. }
  82. for _, b := range bytes {
  83. hash ^= uint64(b)
  84. hash *= prime
  85. }
  86. }
  87. return hash
  88. }
  89. // ReadFrom reads a serialized version of this bitmap from stream.
  90. // The format is compatible with other RoaringBitmap
  91. // implementations (Java, C) and is documented here:
  92. // https://github.com/RoaringBitmap/RoaringFormatSpec
  93. // Since io.Reader is regarded as a stream and cannot be read twice.
  94. // So add cookieHeader to accept the 4-byte data that has been read in roaring64.ReadFrom.
  95. // It is not necessary to pass cookieHeader when call roaring.ReadFrom to read the roaring32 data directly.
  96. func (rb *Bitmap) ReadFrom(reader io.Reader, cookieHeader ...byte) (p int64, err error) {
  97. stream := internal.ByteInputAdapterPool.Get().(*internal.ByteInputAdapter)
  98. stream.Reset(reader)
  99. p, err = rb.highlowcontainer.readFrom(stream, cookieHeader...)
  100. internal.ByteInputAdapterPool.Put(stream)
  101. return
  102. }
  103. // FromBuffer creates a bitmap from its serialized version stored in buffer
  104. //
  105. // The format specification is available here:
  106. // https://github.com/RoaringBitmap/RoaringFormatSpec
  107. //
  108. // The provided byte array (buf) is expected to be a constant.
  109. // The function makes the best effort attempt not to copy data.
  110. // You should take care not to modify buff as it will
  111. // likely result in unexpected program behavior.
  112. //
  113. // Resulting bitmaps are effectively immutable in the following sense:
  114. // a copy-on-write marker is used so that when you modify the resulting
  115. // bitmap, copies of selected data (containers) are made.
  116. // You should *not* change the copy-on-write status of the resulting
  117. // bitmaps (SetCopyOnWrite).
  118. //
  119. // If buf becomes unavailable, then a bitmap created with
  120. // FromBuffer would be effectively broken. Furthermore, any
  121. // bitmap derived from this bitmap (e.g., via Or, And) might
  122. // also be broken. Thus, before making buf unavailable, you should
  123. // call CloneCopyOnWriteContainers on all such bitmaps.
  124. //
  125. func (rb *Bitmap) FromBuffer(buf []byte) (p int64, err error) {
  126. stream := internal.ByteBufferPool.Get().(*internal.ByteBuffer)
  127. stream.Reset(buf)
  128. p, err = rb.highlowcontainer.readFrom(stream)
  129. internal.ByteBufferPool.Put(stream)
  130. return
  131. }
  132. // RunOptimize attempts to further compress the runs of consecutive values found in the bitmap
  133. func (rb *Bitmap) RunOptimize() {
  134. rb.highlowcontainer.runOptimize()
  135. }
  136. // HasRunCompression returns true if the bitmap benefits from run compression
  137. func (rb *Bitmap) HasRunCompression() bool {
  138. return rb.highlowcontainer.hasRunCompression()
  139. }
  140. // MarshalBinary implements the encoding.BinaryMarshaler interface for the bitmap
  141. // (same as ToBytes)
  142. func (rb *Bitmap) MarshalBinary() ([]byte, error) {
  143. return rb.ToBytes()
  144. }
  145. // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface for the bitmap
  146. func (rb *Bitmap) UnmarshalBinary(data []byte) error {
  147. r := bytes.NewReader(data)
  148. _, err := rb.ReadFrom(r)
  149. return err
  150. }
  151. // NewBitmap creates a new empty Bitmap (see also New)
  152. func NewBitmap() *Bitmap {
  153. return &Bitmap{}
  154. }
  155. // New creates a new empty Bitmap (same as NewBitmap)
  156. func New() *Bitmap {
  157. return &Bitmap{}
  158. }
  159. // Clear resets the Bitmap to be logically empty, but may retain
  160. // some memory allocations that may speed up future operations
  161. func (rb *Bitmap) Clear() {
  162. rb.highlowcontainer.clear()
  163. }
  164. // ToArray creates a new slice containing all of the integers stored in the Bitmap in sorted order
  165. func (rb *Bitmap) ToArray() []uint32 {
  166. array := make([]uint32, rb.GetCardinality())
  167. pos := 0
  168. pos2 := 0
  169. for pos < rb.highlowcontainer.size() {
  170. hs := uint32(rb.highlowcontainer.getKeyAtIndex(pos)) << 16
  171. c := rb.highlowcontainer.getContainerAtIndex(pos)
  172. pos++
  173. pos2 = c.fillLeastSignificant16bits(array, pos2, hs)
  174. }
  175. return array
  176. }
  177. // GetSizeInBytes estimates the memory usage of the Bitmap. Note that this
  178. // might differ slightly from the amount of bytes required for persistent storage
  179. func (rb *Bitmap) GetSizeInBytes() uint64 {
  180. size := uint64(8)
  181. for _, c := range rb.highlowcontainer.containers {
  182. size += uint64(2) + uint64(c.getSizeInBytes())
  183. }
  184. return size
  185. }
  186. // GetSerializedSizeInBytes computes the serialized size in bytes
  187. // of the Bitmap. It should correspond to the
  188. // number of bytes written when invoking WriteTo. You can expect
  189. // that this function is much cheaper computationally than WriteTo.
  190. func (rb *Bitmap) GetSerializedSizeInBytes() uint64 {
  191. return rb.highlowcontainer.serializedSizeInBytes()
  192. }
  193. // BoundSerializedSizeInBytes returns an upper bound on the serialized size in bytes
  194. // assuming that one wants to store "cardinality" integers in [0, universe_size)
  195. func BoundSerializedSizeInBytes(cardinality uint64, universeSize uint64) uint64 {
  196. contnbr := (universeSize + uint64(65535)) / uint64(65536)
  197. if contnbr > cardinality {
  198. contnbr = cardinality
  199. // we can't have more containers than we have values
  200. }
  201. headermax := 8*contnbr + 4
  202. if 4 > (contnbr+7)/8 {
  203. headermax += 4
  204. } else {
  205. headermax += (contnbr + 7) / 8
  206. }
  207. valsarray := uint64(arrayContainerSizeInBytes(int(cardinality)))
  208. valsbitmap := contnbr * uint64(bitmapContainerSizeInBytes())
  209. valsbest := valsarray
  210. if valsbest > valsbitmap {
  211. valsbest = valsbitmap
  212. }
  213. return valsbest + headermax
  214. }
  215. // IntIterable allows you to iterate over the values in a Bitmap
  216. type IntIterable interface {
  217. HasNext() bool
  218. Next() uint32
  219. }
  220. // IntPeekable allows you to look at the next value without advancing and
  221. // advance as long as the next value is smaller than minval
  222. type IntPeekable interface {
  223. IntIterable
  224. // PeekNext peeks the next value without advancing the iterator
  225. PeekNext() uint32
  226. // AdvanceIfNeeded advances as long as the next value is smaller than minval
  227. AdvanceIfNeeded(minval uint32)
  228. }
  229. type intIterator struct {
  230. pos int
  231. hs uint32
  232. iter shortPeekable
  233. highlowcontainer *roaringArray
  234. // These embedded iterators per container type help reduce load in the GC.
  235. // This way, instead of making up-to 64k allocations per full iteration
  236. // we get a single allocation and simply reinitialize the appropriate
  237. // iterator and point to it in the generic `iter` member on each key bound.
  238. shortIter shortIterator
  239. runIter runIterator16
  240. bitmapIter bitmapContainerShortIterator
  241. }
  242. // HasNext returns true if there are more integers to iterate over
  243. func (ii *intIterator) HasNext() bool {
  244. return ii.pos < ii.highlowcontainer.size()
  245. }
  246. func (ii *intIterator) init() {
  247. if ii.highlowcontainer.size() > ii.pos {
  248. ii.hs = uint32(ii.highlowcontainer.getKeyAtIndex(ii.pos)) << 16
  249. c := ii.highlowcontainer.getContainerAtIndex(ii.pos)
  250. switch t := c.(type) {
  251. case *arrayContainer:
  252. ii.shortIter = shortIterator{t.content, 0}
  253. ii.iter = &ii.shortIter
  254. case *runContainer16:
  255. ii.runIter = runIterator16{rc: t, curIndex: 0, curPosInIndex: 0}
  256. ii.iter = &ii.runIter
  257. case *bitmapContainer:
  258. ii.bitmapIter = bitmapContainerShortIterator{t, t.NextSetBit(0)}
  259. ii.iter = &ii.bitmapIter
  260. }
  261. }
  262. }
  263. // Next returns the next integer
  264. func (ii *intIterator) Next() uint32 {
  265. x := uint32(ii.iter.next()) | ii.hs
  266. if !ii.iter.hasNext() {
  267. ii.pos = ii.pos + 1
  268. ii.init()
  269. }
  270. return x
  271. }
  272. // PeekNext peeks the next value without advancing the iterator
  273. func (ii *intIterator) PeekNext() uint32 {
  274. return uint32(ii.iter.peekNext()&maxLowBit) | ii.hs
  275. }
  276. // AdvanceIfNeeded advances as long as the next value is smaller than minval
  277. func (ii *intIterator) AdvanceIfNeeded(minval uint32) {
  278. to := minval & 0xffff0000
  279. for ii.HasNext() && ii.hs < to {
  280. ii.pos++
  281. ii.init()
  282. }
  283. if ii.HasNext() && ii.hs == to {
  284. ii.iter.advanceIfNeeded(lowbits(minval))
  285. if !ii.iter.hasNext() {
  286. ii.pos++
  287. ii.init()
  288. }
  289. }
  290. }
  291. // IntIterator is meant to allow you to iterate through the values of a bitmap, see Initialize(a *Bitmap)
  292. type IntIterator = intIterator
  293. // Initialize configures the existing iterator so that it can iterate through the values of
  294. // the provided bitmap.
  295. // The iteration results are undefined if the bitmap is modified (e.g., with Add or Remove).
  296. func (p *intIterator) Initialize(a *Bitmap) {
  297. p.pos = 0
  298. p.highlowcontainer = &a.highlowcontainer
  299. p.init()
  300. }
  301. type intReverseIterator struct {
  302. pos int
  303. hs uint32
  304. iter shortIterable
  305. highlowcontainer *roaringArray
  306. shortIter reverseIterator
  307. runIter runReverseIterator16
  308. bitmapIter reverseBitmapContainerShortIterator
  309. }
  310. // HasNext returns true if there are more integers to iterate over
  311. func (ii *intReverseIterator) HasNext() bool {
  312. return ii.pos >= 0
  313. }
  314. func (ii *intReverseIterator) init() {
  315. if ii.pos >= 0 {
  316. ii.hs = uint32(ii.highlowcontainer.getKeyAtIndex(ii.pos)) << 16
  317. c := ii.highlowcontainer.getContainerAtIndex(ii.pos)
  318. switch t := c.(type) {
  319. case *arrayContainer:
  320. ii.shortIter = reverseIterator{t.content, len(t.content) - 1}
  321. ii.iter = &ii.shortIter
  322. case *runContainer16:
  323. index := int(len(t.iv)) - 1
  324. pos := uint16(0)
  325. if index >= 0 {
  326. pos = t.iv[index].length
  327. }
  328. ii.runIter = runReverseIterator16{rc: t, curIndex: index, curPosInIndex: pos}
  329. ii.iter = &ii.runIter
  330. case *bitmapContainer:
  331. pos := -1
  332. if t.cardinality > 0 {
  333. pos = int(t.maximum())
  334. }
  335. ii.bitmapIter = reverseBitmapContainerShortIterator{t, pos}
  336. ii.iter = &ii.bitmapIter
  337. }
  338. } else {
  339. ii.iter = nil
  340. }
  341. }
  342. // Next returns the next integer
  343. func (ii *intReverseIterator) Next() uint32 {
  344. x := uint32(ii.iter.next()) | ii.hs
  345. if !ii.iter.hasNext() {
  346. ii.pos = ii.pos - 1
  347. ii.init()
  348. }
  349. return x
  350. }
  351. // IntReverseIterator is meant to allow you to iterate through the values of a bitmap, see Initialize(a *Bitmap)
  352. type IntReverseIterator = intReverseIterator
  353. // Initialize configures the existing iterator so that it can iterate through the values of
  354. // the provided bitmap.
  355. // The iteration results are undefined if the bitmap is modified (e.g., with Add or Remove).
  356. func (p *intReverseIterator) Initialize(a *Bitmap) {
  357. p.highlowcontainer = &a.highlowcontainer
  358. p.pos = a.highlowcontainer.size() - 1
  359. p.init()
  360. }
  361. // ManyIntIterable allows you to iterate over the values in a Bitmap
  362. type ManyIntIterable interface {
  363. // NextMany fills buf up with values, returns how many values were returned
  364. NextMany(buf []uint32) int
  365. // NextMany64 fills up buf with 64 bit values, uses hs as a mask (OR), returns how many values were returned
  366. NextMany64(hs uint64, buf []uint64) int
  367. }
  368. type manyIntIterator struct {
  369. pos int
  370. hs uint32
  371. iter manyIterable
  372. highlowcontainer *roaringArray
  373. shortIter shortIterator
  374. runIter runIterator16
  375. bitmapIter bitmapContainerManyIterator
  376. }
  377. func (ii *manyIntIterator) init() {
  378. if ii.highlowcontainer.size() > ii.pos {
  379. ii.hs = uint32(ii.highlowcontainer.getKeyAtIndex(ii.pos)) << 16
  380. c := ii.highlowcontainer.getContainerAtIndex(ii.pos)
  381. switch t := c.(type) {
  382. case *arrayContainer:
  383. ii.shortIter = shortIterator{t.content, 0}
  384. ii.iter = &ii.shortIter
  385. case *runContainer16:
  386. ii.runIter = runIterator16{rc: t, curIndex: 0, curPosInIndex: 0}
  387. ii.iter = &ii.runIter
  388. case *bitmapContainer:
  389. ii.bitmapIter = bitmapContainerManyIterator{t, -1, 0}
  390. ii.iter = &ii.bitmapIter
  391. }
  392. } else {
  393. ii.iter = nil
  394. }
  395. }
  396. func (ii *manyIntIterator) NextMany(buf []uint32) int {
  397. n := 0
  398. for n < len(buf) {
  399. if ii.iter == nil {
  400. break
  401. }
  402. moreN := ii.iter.nextMany(ii.hs, buf[n:])
  403. n += moreN
  404. if moreN == 0 {
  405. ii.pos = ii.pos + 1
  406. ii.init()
  407. }
  408. }
  409. return n
  410. }
  411. func (ii *manyIntIterator) NextMany64(hs64 uint64, buf []uint64) int {
  412. n := 0
  413. for n < len(buf) {
  414. if ii.iter == nil {
  415. break
  416. }
  417. hs := uint64(ii.hs) | hs64
  418. moreN := ii.iter.nextMany64(hs, buf[n:])
  419. n += moreN
  420. if moreN == 0 {
  421. ii.pos = ii.pos + 1
  422. ii.init()
  423. }
  424. }
  425. return n
  426. }
  427. // ManyIntIterator is meant to allow you to iterate through the values of a bitmap, see Initialize(a *Bitmap)
  428. type ManyIntIterator = manyIntIterator
  429. // Initialize configures the existing iterator so that it can iterate through the values of
  430. // the provided bitmap.
  431. // The iteration results are undefined if the bitmap is modified (e.g., with Add or Remove).
  432. func (p *manyIntIterator) Initialize(a *Bitmap) {
  433. p.pos = 0
  434. p.highlowcontainer = &a.highlowcontainer
  435. p.init()
  436. }
  437. // String creates a string representation of the Bitmap
  438. func (rb *Bitmap) String() string {
  439. // inspired by https://github.com/fzandona/goroar/
  440. var buffer bytes.Buffer
  441. start := []byte("{")
  442. buffer.Write(start)
  443. i := rb.Iterator()
  444. counter := 0
  445. if i.HasNext() {
  446. counter = counter + 1
  447. buffer.WriteString(strconv.FormatInt(int64(i.Next()), 10))
  448. }
  449. for i.HasNext() {
  450. buffer.WriteString(",")
  451. counter = counter + 1
  452. // to avoid exhausting the memory
  453. if counter > 0x40000 {
  454. buffer.WriteString("...")
  455. break
  456. }
  457. buffer.WriteString(strconv.FormatInt(int64(i.Next()), 10))
  458. }
  459. buffer.WriteString("}")
  460. return buffer.String()
  461. }
  462. // Iterate iterates over the bitmap, calling the given callback with each value in the bitmap. If the callback returns
  463. // false, the iteration is halted.
  464. // The iteration results are undefined if the bitmap is modified (e.g., with Add or Remove).
  465. // There is no guarantee as to what order the values will be iterated.
  466. func (rb *Bitmap) Iterate(cb func(x uint32) bool) {
  467. for i := 0; i < rb.highlowcontainer.size(); i++ {
  468. hs := uint32(rb.highlowcontainer.getKeyAtIndex(i)) << 16
  469. c := rb.highlowcontainer.getContainerAtIndex(i)
  470. var shouldContinue bool
  471. // This is hacky but it avoids allocations from invoking an interface method with a closure
  472. switch t := c.(type) {
  473. case *arrayContainer:
  474. shouldContinue = t.iterate(func(x uint16) bool {
  475. return cb(uint32(x) | hs)
  476. })
  477. case *runContainer16:
  478. shouldContinue = t.iterate(func(x uint16) bool {
  479. return cb(uint32(x) | hs)
  480. })
  481. case *bitmapContainer:
  482. shouldContinue = t.iterate(func(x uint16) bool {
  483. return cb(uint32(x) | hs)
  484. })
  485. }
  486. if !shouldContinue {
  487. break
  488. }
  489. }
  490. }
  491. // Iterator creates a new IntPeekable to iterate over the integers contained in the bitmap, in sorted order;
  492. // the iterator becomes invalid if the bitmap is modified (e.g., with Add or Remove).
  493. func (rb *Bitmap) Iterator() IntPeekable {
  494. p := new(intIterator)
  495. p.Initialize(rb)
  496. return p
  497. }
  498. // ReverseIterator creates a new IntIterable to iterate over the integers contained in the bitmap, in sorted order;
  499. // the iterator becomes invalid if the bitmap is modified (e.g., with Add or Remove).
  500. func (rb *Bitmap) ReverseIterator() IntIterable {
  501. p := new(intReverseIterator)
  502. p.Initialize(rb)
  503. return p
  504. }
  505. // ManyIterator creates a new ManyIntIterable to iterate over the integers contained in the bitmap, in sorted order;
  506. // the iterator becomes invalid if the bitmap is modified (e.g., with Add or Remove).
  507. func (rb *Bitmap) ManyIterator() ManyIntIterable {
  508. p := new(manyIntIterator)
  509. p.Initialize(rb)
  510. return p
  511. }
  512. // Clone creates a copy of the Bitmap
  513. func (rb *Bitmap) Clone() *Bitmap {
  514. ptr := new(Bitmap)
  515. ptr.highlowcontainer = *rb.highlowcontainer.clone()
  516. return ptr
  517. }
  518. // Minimum get the smallest value stored in this roaring bitmap, assumes that it is not empty
  519. func (rb *Bitmap) Minimum() uint32 {
  520. if len(rb.highlowcontainer.containers) == 0 {
  521. panic("Empty bitmap")
  522. }
  523. return uint32(rb.highlowcontainer.containers[0].minimum()) | (uint32(rb.highlowcontainer.keys[0]) << 16)
  524. }
  525. // Maximum get the largest value stored in this roaring bitmap, assumes that it is not empty
  526. func (rb *Bitmap) Maximum() uint32 {
  527. if len(rb.highlowcontainer.containers) == 0 {
  528. panic("Empty bitmap")
  529. }
  530. lastindex := len(rb.highlowcontainer.containers) - 1
  531. return uint32(rb.highlowcontainer.containers[lastindex].maximum()) | (uint32(rb.highlowcontainer.keys[lastindex]) << 16)
  532. }
  533. // Contains returns true if the integer is contained in the bitmap
  534. func (rb *Bitmap) Contains(x uint32) bool {
  535. hb := highbits(x)
  536. c := rb.highlowcontainer.getContainer(hb)
  537. return c != nil && c.contains(lowbits(x))
  538. }
  539. // ContainsInt returns true if the integer is contained in the bitmap (this is a convenience method, the parameter is casted to uint32 and Contains is called)
  540. func (rb *Bitmap) ContainsInt(x int) bool {
  541. return rb.Contains(uint32(x))
  542. }
  543. // Equals returns true if the two bitmaps contain the same integers
  544. func (rb *Bitmap) Equals(o interface{}) bool {
  545. srb, ok := o.(*Bitmap)
  546. if ok {
  547. return srb.highlowcontainer.equals(rb.highlowcontainer)
  548. }
  549. return false
  550. }
  551. // AddOffset adds the value 'offset' to each and every value in a bitmap, generating a new bitmap in the process
  552. func AddOffset(x *Bitmap, offset uint32) (answer *Bitmap) {
  553. return AddOffset64(x, int64(offset))
  554. }
  555. // AddOffset64 adds the value 'offset' to each and every value in a bitmap, generating a new bitmap in the process
  556. // If offset + element is outside of the range [0,2^32), that the element will be dropped
  557. func AddOffset64(x *Bitmap, offset int64) (answer *Bitmap) {
  558. // we need "offset" to be a long because we want to support values
  559. // between -0xFFFFFFFF up to +-0xFFFFFFFF
  560. var containerOffset64 int64
  561. if offset < 0 {
  562. containerOffset64 = (offset - (1 << 16) + 1) / (1 << 16)
  563. } else {
  564. containerOffset64 = offset >> 16
  565. }
  566. answer = New()
  567. if containerOffset64 >= (1<<16) || containerOffset64 < -(1<<16) {
  568. return answer
  569. }
  570. containerOffset := int32(containerOffset64)
  571. inOffset := (uint16)(offset - containerOffset64*(1<<16))
  572. if inOffset == 0 {
  573. for pos := 0; pos < x.highlowcontainer.size(); pos++ {
  574. key := int32(x.highlowcontainer.getKeyAtIndex(pos))
  575. key += containerOffset
  576. if key >= 0 && key <= MaxUint16 {
  577. c := x.highlowcontainer.getContainerAtIndex(pos).clone()
  578. answer.highlowcontainer.appendContainer(uint16(key), c, false)
  579. }
  580. }
  581. } else {
  582. for pos := 0; pos < x.highlowcontainer.size(); pos++ {
  583. key := int32(x.highlowcontainer.getKeyAtIndex(pos))
  584. key += containerOffset
  585. if key+1 < 0 || key > MaxUint16 {
  586. continue
  587. }
  588. c := x.highlowcontainer.getContainerAtIndex(pos)
  589. lo, hi := c.addOffset(inOffset)
  590. if lo != nil && key >= 0 {
  591. curSize := answer.highlowcontainer.size()
  592. lastkey := int32(0)
  593. if curSize > 0 {
  594. lastkey = int32(answer.highlowcontainer.getKeyAtIndex(curSize - 1))
  595. }
  596. if curSize > 0 && lastkey == key {
  597. prev := answer.highlowcontainer.getContainerAtIndex(curSize - 1)
  598. orresult := prev.ior(lo)
  599. answer.highlowcontainer.setContainerAtIndex(curSize-1, orresult)
  600. } else {
  601. answer.highlowcontainer.appendContainer(uint16(key), lo, false)
  602. }
  603. }
  604. if hi != nil && key+1 <= MaxUint16 {
  605. answer.highlowcontainer.appendContainer(uint16(key+1), hi, false)
  606. }
  607. }
  608. }
  609. return answer
  610. }
  611. // Add the integer x to the bitmap
  612. func (rb *Bitmap) Add(x uint32) {
  613. hb := highbits(x)
  614. ra := &rb.highlowcontainer
  615. i := ra.getIndex(hb)
  616. if i >= 0 {
  617. var c container
  618. c = ra.getWritableContainerAtIndex(i).iaddReturnMinimized(lowbits(x))
  619. rb.highlowcontainer.setContainerAtIndex(i, c)
  620. } else {
  621. newac := newArrayContainer()
  622. rb.highlowcontainer.insertNewKeyValueAt(-i-1, hb, newac.iaddReturnMinimized(lowbits(x)))
  623. }
  624. }
  625. // add the integer x to the bitmap, return the container and its index
  626. func (rb *Bitmap) addwithptr(x uint32) (int, container) {
  627. hb := highbits(x)
  628. ra := &rb.highlowcontainer
  629. i := ra.getIndex(hb)
  630. var c container
  631. if i >= 0 {
  632. c = ra.getWritableContainerAtIndex(i).iaddReturnMinimized(lowbits(x))
  633. rb.highlowcontainer.setContainerAtIndex(i, c)
  634. return i, c
  635. }
  636. newac := newArrayContainer()
  637. c = newac.iaddReturnMinimized(lowbits(x))
  638. rb.highlowcontainer.insertNewKeyValueAt(-i-1, hb, c)
  639. return -i - 1, c
  640. }
  641. // CheckedAdd adds the integer x to the bitmap and return true if it was added (false if the integer was already present)
  642. func (rb *Bitmap) CheckedAdd(x uint32) bool {
  643. // TODO: add unit tests for this method
  644. hb := highbits(x)
  645. i := rb.highlowcontainer.getIndex(hb)
  646. if i >= 0 {
  647. C := rb.highlowcontainer.getWritableContainerAtIndex(i)
  648. oldcard := C.getCardinality()
  649. C = C.iaddReturnMinimized(lowbits(x))
  650. rb.highlowcontainer.setContainerAtIndex(i, C)
  651. return C.getCardinality() > oldcard
  652. }
  653. newac := newArrayContainer()
  654. rb.highlowcontainer.insertNewKeyValueAt(-i-1, hb, newac.iaddReturnMinimized(lowbits(x)))
  655. return true
  656. }
  657. // AddInt adds the integer x to the bitmap (convenience method: the parameter is casted to uint32 and we call Add)
  658. func (rb *Bitmap) AddInt(x int) {
  659. rb.Add(uint32(x))
  660. }
  661. // Remove the integer x from the bitmap
  662. func (rb *Bitmap) Remove(x uint32) {
  663. hb := highbits(x)
  664. i := rb.highlowcontainer.getIndex(hb)
  665. if i >= 0 {
  666. c := rb.highlowcontainer.getWritableContainerAtIndex(i).iremoveReturnMinimized(lowbits(x))
  667. rb.highlowcontainer.setContainerAtIndex(i, c)
  668. if rb.highlowcontainer.getContainerAtIndex(i).isEmpty() {
  669. rb.highlowcontainer.removeAtIndex(i)
  670. }
  671. }
  672. }
  673. // CheckedRemove removes the integer x from the bitmap and return true if the integer was effectively removed (and false if the integer was not present)
  674. func (rb *Bitmap) CheckedRemove(x uint32) bool {
  675. // TODO: add unit tests for this method
  676. hb := highbits(x)
  677. i := rb.highlowcontainer.getIndex(hb)
  678. if i >= 0 {
  679. C := rb.highlowcontainer.getWritableContainerAtIndex(i)
  680. oldcard := C.getCardinality()
  681. C = C.iremoveReturnMinimized(lowbits(x))
  682. rb.highlowcontainer.setContainerAtIndex(i, C)
  683. if rb.highlowcontainer.getContainerAtIndex(i).isEmpty() {
  684. rb.highlowcontainer.removeAtIndex(i)
  685. return true
  686. }
  687. return C.getCardinality() < oldcard
  688. }
  689. return false
  690. }
  691. // IsEmpty returns true if the Bitmap is empty (it is faster than doing (GetCardinality() == 0))
  692. func (rb *Bitmap) IsEmpty() bool {
  693. return rb.highlowcontainer.size() == 0
  694. }
  695. // GetCardinality returns the number of integers contained in the bitmap
  696. func (rb *Bitmap) GetCardinality() uint64 {
  697. size := uint64(0)
  698. for _, c := range rb.highlowcontainer.containers {
  699. size += uint64(c.getCardinality())
  700. }
  701. return size
  702. }
  703. // Rank returns the number of integers that are smaller or equal to x (Rank(infinity) would be GetCardinality()).
  704. // If you pass the smallest value, you get the value 1. If you pass a value that is smaller than the smallest
  705. // value, you get 0. Note that this function differs in convention from the Select function since it
  706. // return 1 and not 0 on the smallest value.
  707. func (rb *Bitmap) Rank(x uint32) uint64 {
  708. size := uint64(0)
  709. for i := 0; i < rb.highlowcontainer.size(); i++ {
  710. key := rb.highlowcontainer.getKeyAtIndex(i)
  711. if key > highbits(x) {
  712. return size
  713. }
  714. if key < highbits(x) {
  715. size += uint64(rb.highlowcontainer.getContainerAtIndex(i).getCardinality())
  716. } else {
  717. return size + uint64(rb.highlowcontainer.getContainerAtIndex(i).rank(lowbits(x)))
  718. }
  719. }
  720. return size
  721. }
  722. // Select returns the xth integer in the bitmap. If you pass 0, you get
  723. // the smallest element. Note that this function differs in convention from
  724. // the Rank function which returns 1 on the smallest value.
  725. func (rb *Bitmap) Select(x uint32) (uint32, error) {
  726. remaining := x
  727. for i := 0; i < rb.highlowcontainer.size(); i++ {
  728. c := rb.highlowcontainer.getContainerAtIndex(i)
  729. card := uint32(c.getCardinality())
  730. if remaining >= card {
  731. remaining -= card
  732. } else {
  733. key := rb.highlowcontainer.getKeyAtIndex(i)
  734. return uint32(key)<<16 + uint32(c.selectInt(uint16(remaining))), nil
  735. }
  736. }
  737. return 0, fmt.Errorf("can't find %dth integer in a bitmap with only %d items", x, rb.GetCardinality())
  738. }
  739. // And computes the intersection between two bitmaps and stores the result in the current bitmap
  740. func (rb *Bitmap) And(x2 *Bitmap) {
  741. pos1 := 0
  742. pos2 := 0
  743. intersectionsize := 0
  744. length1 := rb.highlowcontainer.size()
  745. length2 := x2.highlowcontainer.size()
  746. main:
  747. for {
  748. if pos1 < length1 && pos2 < length2 {
  749. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  750. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  751. for {
  752. if s1 == s2 {
  753. c1 := rb.highlowcontainer.getWritableContainerAtIndex(pos1)
  754. c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
  755. diff := c1.iand(c2)
  756. if !diff.isEmpty() {
  757. rb.highlowcontainer.replaceKeyAndContainerAtIndex(intersectionsize, s1, diff, false)
  758. intersectionsize++
  759. }
  760. pos1++
  761. pos2++
  762. if (pos1 == length1) || (pos2 == length2) {
  763. break main
  764. }
  765. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  766. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  767. } else if s1 < s2 {
  768. pos1 = rb.highlowcontainer.advanceUntil(s2, pos1)
  769. if pos1 == length1 {
  770. break main
  771. }
  772. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  773. } else { //s1 > s2
  774. pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
  775. if pos2 == length2 {
  776. break main
  777. }
  778. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  779. }
  780. }
  781. } else {
  782. break
  783. }
  784. }
  785. rb.highlowcontainer.resize(intersectionsize)
  786. }
  787. // OrCardinality returns the cardinality of the union between two bitmaps, bitmaps are not modified
  788. func (rb *Bitmap) OrCardinality(x2 *Bitmap) uint64 {
  789. pos1 := 0
  790. pos2 := 0
  791. length1 := rb.highlowcontainer.size()
  792. length2 := x2.highlowcontainer.size()
  793. answer := uint64(0)
  794. main:
  795. for {
  796. if (pos1 < length1) && (pos2 < length2) {
  797. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  798. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  799. for {
  800. if s1 < s2 {
  801. answer += uint64(rb.highlowcontainer.getContainerAtIndex(pos1).getCardinality())
  802. pos1++
  803. if pos1 == length1 {
  804. break main
  805. }
  806. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  807. } else if s1 > s2 {
  808. answer += uint64(x2.highlowcontainer.getContainerAtIndex(pos2).getCardinality())
  809. pos2++
  810. if pos2 == length2 {
  811. break main
  812. }
  813. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  814. } else {
  815. // TODO: could be faster if we did not have to materialize the container
  816. answer += uint64(rb.highlowcontainer.getContainerAtIndex(pos1).or(x2.highlowcontainer.getContainerAtIndex(pos2)).getCardinality())
  817. pos1++
  818. pos2++
  819. if (pos1 == length1) || (pos2 == length2) {
  820. break main
  821. }
  822. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  823. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  824. }
  825. }
  826. } else {
  827. break
  828. }
  829. }
  830. for ; pos1 < length1; pos1++ {
  831. answer += uint64(rb.highlowcontainer.getContainerAtIndex(pos1).getCardinality())
  832. }
  833. for ; pos2 < length2; pos2++ {
  834. answer += uint64(x2.highlowcontainer.getContainerAtIndex(pos2).getCardinality())
  835. }
  836. return answer
  837. }
  838. // AndCardinality returns the cardinality of the intersection between two bitmaps, bitmaps are not modified
  839. func (rb *Bitmap) AndCardinality(x2 *Bitmap) uint64 {
  840. pos1 := 0
  841. pos2 := 0
  842. answer := uint64(0)
  843. length1 := rb.highlowcontainer.size()
  844. length2 := x2.highlowcontainer.size()
  845. main:
  846. for {
  847. if pos1 < length1 && pos2 < length2 {
  848. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  849. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  850. for {
  851. if s1 == s2 {
  852. c1 := rb.highlowcontainer.getContainerAtIndex(pos1)
  853. c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
  854. answer += uint64(c1.andCardinality(c2))
  855. pos1++
  856. pos2++
  857. if (pos1 == length1) || (pos2 == length2) {
  858. break main
  859. }
  860. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  861. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  862. } else if s1 < s2 {
  863. pos1 = rb.highlowcontainer.advanceUntil(s2, pos1)
  864. if pos1 == length1 {
  865. break main
  866. }
  867. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  868. } else { //s1 > s2
  869. pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
  870. if pos2 == length2 {
  871. break main
  872. }
  873. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  874. }
  875. }
  876. } else {
  877. break
  878. }
  879. }
  880. return answer
  881. }
  882. // IntersectsWithInterval checks whether a bitmap 'rb' and an open interval '[x,y)' intersect.
  883. func (rb *Bitmap) IntersectsWithInterval(x, y uint64) bool {
  884. if x >= y {
  885. return false
  886. }
  887. if x > MaxUint32 {
  888. return false
  889. }
  890. it := intIterator{}
  891. it.Initialize(rb)
  892. it.AdvanceIfNeeded(uint32(x))
  893. if !it.HasNext() {
  894. return false
  895. }
  896. if uint64(it.Next()) >= y {
  897. return false
  898. }
  899. return true
  900. }
  901. // Intersects checks whether two bitmap intersects, bitmaps are not modified
  902. func (rb *Bitmap) Intersects(x2 *Bitmap) bool {
  903. pos1 := 0
  904. pos2 := 0
  905. length1 := rb.highlowcontainer.size()
  906. length2 := x2.highlowcontainer.size()
  907. main:
  908. for {
  909. if pos1 < length1 && pos2 < length2 {
  910. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  911. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  912. for {
  913. if s1 == s2 {
  914. c1 := rb.highlowcontainer.getContainerAtIndex(pos1)
  915. c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
  916. if c1.intersects(c2) {
  917. return true
  918. }
  919. pos1++
  920. pos2++
  921. if (pos1 == length1) || (pos2 == length2) {
  922. break main
  923. }
  924. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  925. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  926. } else if s1 < s2 {
  927. pos1 = rb.highlowcontainer.advanceUntil(s2, pos1)
  928. if pos1 == length1 {
  929. break main
  930. }
  931. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  932. } else { //s1 > s2
  933. pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
  934. if pos2 == length2 {
  935. break main
  936. }
  937. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  938. }
  939. }
  940. } else {
  941. break
  942. }
  943. }
  944. return false
  945. }
  946. // Xor computes the symmetric difference between two bitmaps and stores the result in the current bitmap
  947. func (rb *Bitmap) Xor(x2 *Bitmap) {
  948. pos1 := 0
  949. pos2 := 0
  950. length1 := rb.highlowcontainer.size()
  951. length2 := x2.highlowcontainer.size()
  952. for {
  953. if (pos1 < length1) && (pos2 < length2) {
  954. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  955. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  956. if s1 < s2 {
  957. pos1 = rb.highlowcontainer.advanceUntil(s2, pos1)
  958. if pos1 == length1 {
  959. break
  960. }
  961. } else if s1 > s2 {
  962. c := x2.highlowcontainer.getWritableContainerAtIndex(pos2)
  963. rb.highlowcontainer.insertNewKeyValueAt(pos1, x2.highlowcontainer.getKeyAtIndex(pos2), c)
  964. length1++
  965. pos1++
  966. pos2++
  967. } else {
  968. // TODO: couple be computed in-place for reduced memory usage
  969. c := rb.highlowcontainer.getContainerAtIndex(pos1).xor(x2.highlowcontainer.getContainerAtIndex(pos2))
  970. if !c.isEmpty() {
  971. rb.highlowcontainer.setContainerAtIndex(pos1, c)
  972. pos1++
  973. } else {
  974. rb.highlowcontainer.removeAtIndex(pos1)
  975. length1--
  976. }
  977. pos2++
  978. }
  979. } else {
  980. break
  981. }
  982. }
  983. if pos1 == length1 {
  984. rb.highlowcontainer.appendCopyMany(x2.highlowcontainer, pos2, length2)
  985. }
  986. }
  987. // Or computes the union between two bitmaps and stores the result in the current bitmap
  988. func (rb *Bitmap) Or(x2 *Bitmap) {
  989. pos1 := 0
  990. pos2 := 0
  991. length1 := rb.highlowcontainer.size()
  992. length2 := x2.highlowcontainer.size()
  993. main:
  994. for (pos1 < length1) && (pos2 < length2) {
  995. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  996. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  997. for {
  998. if s1 < s2 {
  999. pos1++
  1000. if pos1 == length1 {
  1001. break main
  1002. }
  1003. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  1004. } else if s1 > s2 {
  1005. rb.highlowcontainer.insertNewKeyValueAt(pos1, s2, x2.highlowcontainer.getContainerAtIndex(pos2).clone())
  1006. pos1++
  1007. length1++
  1008. pos2++
  1009. if pos2 == length2 {
  1010. break main
  1011. }
  1012. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  1013. } else {
  1014. rb.highlowcontainer.replaceKeyAndContainerAtIndex(pos1, s1, rb.highlowcontainer.getUnionedWritableContainer(pos1, x2.highlowcontainer.getContainerAtIndex(pos2)), false)
  1015. pos1++
  1016. pos2++
  1017. if (pos1 == length1) || (pos2 == length2) {
  1018. break main
  1019. }
  1020. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  1021. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  1022. }
  1023. }
  1024. }
  1025. if pos1 == length1 {
  1026. rb.highlowcontainer.appendCopyMany(x2.highlowcontainer, pos2, length2)
  1027. }
  1028. }
  1029. // AndNot computes the difference between two bitmaps and stores the result in the current bitmap
  1030. func (rb *Bitmap) AndNot(x2 *Bitmap) {
  1031. pos1 := 0
  1032. pos2 := 0
  1033. intersectionsize := 0
  1034. length1 := rb.highlowcontainer.size()
  1035. length2 := x2.highlowcontainer.size()
  1036. main:
  1037. for {
  1038. if pos1 < length1 && pos2 < length2 {
  1039. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  1040. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  1041. for {
  1042. if s1 == s2 {
  1043. c1 := rb.highlowcontainer.getWritableContainerAtIndex(pos1)
  1044. c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
  1045. diff := c1.iandNot(c2)
  1046. if !diff.isEmpty() {
  1047. rb.highlowcontainer.replaceKeyAndContainerAtIndex(intersectionsize, s1, diff, false)
  1048. intersectionsize++
  1049. }
  1050. pos1++
  1051. pos2++
  1052. if (pos1 == length1) || (pos2 == length2) {
  1053. break main
  1054. }
  1055. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  1056. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  1057. } else if s1 < s2 {
  1058. c1 := rb.highlowcontainer.getContainerAtIndex(pos1)
  1059. mustCopyOnWrite := rb.highlowcontainer.needsCopyOnWrite(pos1)
  1060. rb.highlowcontainer.replaceKeyAndContainerAtIndex(intersectionsize, s1, c1, mustCopyOnWrite)
  1061. intersectionsize++
  1062. pos1++
  1063. if pos1 == length1 {
  1064. break main
  1065. }
  1066. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  1067. } else { //s1 > s2
  1068. pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
  1069. if pos2 == length2 {
  1070. break main
  1071. }
  1072. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  1073. }
  1074. }
  1075. } else {
  1076. break
  1077. }
  1078. }
  1079. // TODO:implement as a copy
  1080. for pos1 < length1 {
  1081. c1 := rb.highlowcontainer.getContainerAtIndex(pos1)
  1082. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  1083. mustCopyOnWrite := rb.highlowcontainer.needsCopyOnWrite(pos1)
  1084. rb.highlowcontainer.replaceKeyAndContainerAtIndex(intersectionsize, s1, c1, mustCopyOnWrite)
  1085. intersectionsize++
  1086. pos1++
  1087. }
  1088. rb.highlowcontainer.resize(intersectionsize)
  1089. }
  1090. // Or computes the union between two bitmaps and returns the result
  1091. func Or(x1, x2 *Bitmap) *Bitmap {
  1092. answer := NewBitmap()
  1093. pos1 := 0
  1094. pos2 := 0
  1095. length1 := x1.highlowcontainer.size()
  1096. length2 := x2.highlowcontainer.size()
  1097. main:
  1098. for (pos1 < length1) && (pos2 < length2) {
  1099. s1 := x1.highlowcontainer.getKeyAtIndex(pos1)
  1100. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  1101. for {
  1102. if s1 < s2 {
  1103. answer.highlowcontainer.appendCopy(x1.highlowcontainer, pos1)
  1104. pos1++
  1105. if pos1 == length1 {
  1106. break main
  1107. }
  1108. s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
  1109. } else if s1 > s2 {
  1110. answer.highlowcontainer.appendCopy(x2.highlowcontainer, pos2)
  1111. pos2++
  1112. if pos2 == length2 {
  1113. break main
  1114. }
  1115. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  1116. } else {
  1117. answer.highlowcontainer.appendContainer(s1, x1.highlowcontainer.getContainerAtIndex(pos1).or(x2.highlowcontainer.getContainerAtIndex(pos2)), false)
  1118. pos1++
  1119. pos2++
  1120. if (pos1 == length1) || (pos2 == length2) {
  1121. break main
  1122. }
  1123. s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
  1124. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  1125. }
  1126. }
  1127. }
  1128. if pos1 == length1 {
  1129. answer.highlowcontainer.appendCopyMany(x2.highlowcontainer, pos2, length2)
  1130. } else if pos2 == length2 {
  1131. answer.highlowcontainer.appendCopyMany(x1.highlowcontainer, pos1, length1)
  1132. }
  1133. return answer
  1134. }
  1135. // And computes the intersection between two bitmaps and returns the result
  1136. func And(x1, x2 *Bitmap) *Bitmap {
  1137. answer := NewBitmap()
  1138. pos1 := 0
  1139. pos2 := 0
  1140. length1 := x1.highlowcontainer.size()
  1141. length2 := x2.highlowcontainer.size()
  1142. main:
  1143. for pos1 < length1 && pos2 < length2 {
  1144. s1 := x1.highlowcontainer.getKeyAtIndex(pos1)
  1145. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  1146. for {
  1147. if s1 == s2 {
  1148. C := x1.highlowcontainer.getContainerAtIndex(pos1)
  1149. C = C.and(x2.highlowcontainer.getContainerAtIndex(pos2))
  1150. if !C.isEmpty() {
  1151. answer.highlowcontainer.appendContainer(s1, C, false)
  1152. }
  1153. pos1++
  1154. pos2++
  1155. if (pos1 == length1) || (pos2 == length2) {
  1156. break main
  1157. }
  1158. s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
  1159. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  1160. } else if s1 < s2 {
  1161. pos1 = x1.highlowcontainer.advanceUntil(s2, pos1)
  1162. if pos1 == length1 {
  1163. break main
  1164. }
  1165. s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
  1166. } else { // s1 > s2
  1167. pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
  1168. if pos2 == length2 {
  1169. break main
  1170. }
  1171. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  1172. }
  1173. }
  1174. }
  1175. return answer
  1176. }
  1177. // Xor computes the symmetric difference between two bitmaps and returns the result
  1178. func Xor(x1, x2 *Bitmap) *Bitmap {
  1179. answer := NewBitmap()
  1180. pos1 := 0
  1181. pos2 := 0
  1182. length1 := x1.highlowcontainer.size()
  1183. length2 := x2.highlowcontainer.size()
  1184. for {
  1185. if (pos1 < length1) && (pos2 < length2) {
  1186. s1 := x1.highlowcontainer.getKeyAtIndex(pos1)
  1187. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  1188. if s1 < s2 {
  1189. answer.highlowcontainer.appendCopy(x1.highlowcontainer, pos1)
  1190. pos1++
  1191. } else if s1 > s2 {
  1192. answer.highlowcontainer.appendCopy(x2.highlowcontainer, pos2)
  1193. pos2++
  1194. } else {
  1195. c := x1.highlowcontainer.getContainerAtIndex(pos1).xor(x2.highlowcontainer.getContainerAtIndex(pos2))
  1196. if !c.isEmpty() {
  1197. answer.highlowcontainer.appendContainer(s1, c, false)
  1198. }
  1199. pos1++
  1200. pos2++
  1201. }
  1202. } else {
  1203. break
  1204. }
  1205. }
  1206. if pos1 == length1 {
  1207. answer.highlowcontainer.appendCopyMany(x2.highlowcontainer, pos2, length2)
  1208. } else if pos2 == length2 {
  1209. answer.highlowcontainer.appendCopyMany(x1.highlowcontainer, pos1, length1)
  1210. }
  1211. return answer
  1212. }
  1213. // AndNot computes the difference between two bitmaps and returns the result
  1214. func AndNot(x1, x2 *Bitmap) *Bitmap {
  1215. answer := NewBitmap()
  1216. pos1 := 0
  1217. pos2 := 0
  1218. length1 := x1.highlowcontainer.size()
  1219. length2 := x2.highlowcontainer.size()
  1220. main:
  1221. for {
  1222. if pos1 < length1 && pos2 < length2 {
  1223. s1 := x1.highlowcontainer.getKeyAtIndex(pos1)
  1224. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  1225. for {
  1226. if s1 < s2 {
  1227. answer.highlowcontainer.appendCopy(x1.highlowcontainer, pos1)
  1228. pos1++
  1229. if pos1 == length1 {
  1230. break main
  1231. }
  1232. s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
  1233. } else if s1 == s2 {
  1234. c1 := x1.highlowcontainer.getContainerAtIndex(pos1)
  1235. c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
  1236. diff := c1.andNot(c2)
  1237. if !diff.isEmpty() {
  1238. answer.highlowcontainer.appendContainer(s1, diff, false)
  1239. }
  1240. pos1++
  1241. pos2++
  1242. if (pos1 == length1) || (pos2 == length2) {
  1243. break main
  1244. }
  1245. s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
  1246. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  1247. } else { //s1 > s2
  1248. pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
  1249. if pos2 == length2 {
  1250. break main
  1251. }
  1252. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  1253. }
  1254. }
  1255. } else {
  1256. break
  1257. }
  1258. }
  1259. if pos2 == length2 {
  1260. answer.highlowcontainer.appendCopyMany(x1.highlowcontainer, pos1, length1)
  1261. }
  1262. return answer
  1263. }
  1264. // AddMany add all of the values in dat
  1265. func (rb *Bitmap) AddMany(dat []uint32) {
  1266. if len(dat) == 0 {
  1267. return
  1268. }
  1269. prev := dat[0]
  1270. idx, c := rb.addwithptr(prev)
  1271. for _, i := range dat[1:] {
  1272. if highbits(prev) == highbits(i) {
  1273. c = c.iaddReturnMinimized(lowbits(i))
  1274. rb.highlowcontainer.setContainerAtIndex(idx, c)
  1275. } else {
  1276. idx, c = rb.addwithptr(i)
  1277. }
  1278. prev = i
  1279. }
  1280. }
  1281. // BitmapOf generates a new bitmap filled with the specified integers
  1282. func BitmapOf(dat ...uint32) *Bitmap {
  1283. ans := NewBitmap()
  1284. ans.AddMany(dat)
  1285. return ans
  1286. }
  1287. // Flip negates the bits in the given range (i.e., [rangeStart,rangeEnd)), any integer present in this range and in the bitmap is removed,
  1288. // and any integer present in the range and not in the bitmap is added.
  1289. // The function uses 64-bit parameters even though a Bitmap stores 32-bit values because it is allowed and meaningful to use [0,uint64(0x100000000)) as a range
  1290. // while uint64(0x100000000) cannot be represented as a 32-bit value.
  1291. func (rb *Bitmap) Flip(rangeStart, rangeEnd uint64) {
  1292. if rangeEnd > MaxUint32+1 {
  1293. panic("rangeEnd > MaxUint32+1")
  1294. }
  1295. if rangeStart > MaxUint32+1 {
  1296. panic("rangeStart > MaxUint32+1")
  1297. }
  1298. if rangeStart >= rangeEnd {
  1299. return
  1300. }
  1301. hbStart := uint32(highbits(uint32(rangeStart)))
  1302. lbStart := uint32(lowbits(uint32(rangeStart)))
  1303. hbLast := uint32(highbits(uint32(rangeEnd - 1)))
  1304. lbLast := uint32(lowbits(uint32(rangeEnd - 1)))
  1305. var max uint32 = maxLowBit
  1306. for hb := hbStart; hb <= hbLast; hb++ {
  1307. var containerStart uint32
  1308. if hb == hbStart {
  1309. containerStart = uint32(lbStart)
  1310. }
  1311. containerLast := max
  1312. if hb == hbLast {
  1313. containerLast = uint32(lbLast)
  1314. }
  1315. i := rb.highlowcontainer.getIndex(uint16(hb))
  1316. if i >= 0 {
  1317. c := rb.highlowcontainer.getWritableContainerAtIndex(i).inot(int(containerStart), int(containerLast)+1)
  1318. if !c.isEmpty() {
  1319. rb.highlowcontainer.setContainerAtIndex(i, c)
  1320. } else {
  1321. rb.highlowcontainer.removeAtIndex(i)
  1322. }
  1323. } else { // *think* the range of ones must never be
  1324. // empty.
  1325. rb.highlowcontainer.insertNewKeyValueAt(-i-1, uint16(hb), rangeOfOnes(int(containerStart), int(containerLast)))
  1326. }
  1327. }
  1328. }
  1329. // FlipInt calls Flip after casting the parameters (convenience method)
  1330. func (rb *Bitmap) FlipInt(rangeStart, rangeEnd int) {
  1331. rb.Flip(uint64(rangeStart), uint64(rangeEnd))
  1332. }
  1333. // AddRange adds the integers in [rangeStart, rangeEnd) to the bitmap.
  1334. // The function uses 64-bit parameters even though a Bitmap stores 32-bit values because it is allowed and meaningful to use [0,uint64(0x100000000)) as a range
  1335. // while uint64(0x100000000) cannot be represented as a 32-bit value.
  1336. func (rb *Bitmap) AddRange(rangeStart, rangeEnd uint64) {
  1337. if rangeStart >= rangeEnd {
  1338. return
  1339. }
  1340. if rangeEnd-1 > MaxUint32 {
  1341. panic("rangeEnd-1 > MaxUint32")
  1342. }
  1343. hbStart := uint32(highbits(uint32(rangeStart)))
  1344. lbStart := uint32(lowbits(uint32(rangeStart)))
  1345. hbLast := uint32(highbits(uint32(rangeEnd - 1)))
  1346. lbLast := uint32(lowbits(uint32(rangeEnd - 1)))
  1347. var max uint32 = maxLowBit
  1348. for hb := hbStart; hb <= hbLast; hb++ {
  1349. containerStart := uint32(0)
  1350. if hb == hbStart {
  1351. containerStart = lbStart
  1352. }
  1353. containerLast := max
  1354. if hb == hbLast {
  1355. containerLast = lbLast
  1356. }
  1357. i := rb.highlowcontainer.getIndex(uint16(hb))
  1358. if i >= 0 {
  1359. c := rb.highlowcontainer.getWritableContainerAtIndex(i).iaddRange(int(containerStart), int(containerLast)+1)
  1360. rb.highlowcontainer.setContainerAtIndex(i, c)
  1361. } else { // *think* the range of ones must never be
  1362. // empty.
  1363. rb.highlowcontainer.insertNewKeyValueAt(-i-1, uint16(hb), rangeOfOnes(int(containerStart), int(containerLast)))
  1364. }
  1365. }
  1366. }
  1367. // RemoveRange removes the integers in [rangeStart, rangeEnd) from the bitmap.
  1368. // The function uses 64-bit parameters even though a Bitmap stores 32-bit values because it is allowed and meaningful to use [0,uint64(0x100000000)) as a range
  1369. // while uint64(0x100000000) cannot be represented as a 32-bit value.
  1370. func (rb *Bitmap) RemoveRange(rangeStart, rangeEnd uint64) {
  1371. if rangeStart >= rangeEnd {
  1372. return
  1373. }
  1374. if rangeEnd-1 > MaxUint32 {
  1375. // logically, we should assume that the user wants to
  1376. // remove all values from rangeStart to infinity
  1377. // see https://github.com/RoaringBitmap/roaring/issues/141
  1378. rangeEnd = uint64(0x100000000)
  1379. }
  1380. hbStart := uint32(highbits(uint32(rangeStart)))
  1381. lbStart := uint32(lowbits(uint32(rangeStart)))
  1382. hbLast := uint32(highbits(uint32(rangeEnd - 1)))
  1383. lbLast := uint32(lowbits(uint32(rangeEnd - 1)))
  1384. var max uint32 = maxLowBit
  1385. if hbStart == hbLast {
  1386. i := rb.highlowcontainer.getIndex(uint16(hbStart))
  1387. if i < 0 {
  1388. return
  1389. }
  1390. c := rb.highlowcontainer.getWritableContainerAtIndex(i).iremoveRange(int(lbStart), int(lbLast+1))
  1391. if !c.isEmpty() {
  1392. rb.highlowcontainer.setContainerAtIndex(i, c)
  1393. } else {
  1394. rb.highlowcontainer.removeAtIndex(i)
  1395. }
  1396. return
  1397. }
  1398. ifirst := rb.highlowcontainer.getIndex(uint16(hbStart))
  1399. ilast := rb.highlowcontainer.getIndex(uint16(hbLast))
  1400. if ifirst >= 0 {
  1401. if lbStart != 0 {
  1402. c := rb.highlowcontainer.getWritableContainerAtIndex(ifirst).iremoveRange(int(lbStart), int(max+1))
  1403. if !c.isEmpty() {
  1404. rb.highlowcontainer.setContainerAtIndex(ifirst, c)
  1405. ifirst++
  1406. }
  1407. }
  1408. } else {
  1409. ifirst = -ifirst - 1
  1410. }
  1411. if ilast >= 0 {
  1412. if lbLast != max {
  1413. c := rb.highlowcontainer.getWritableContainerAtIndex(ilast).iremoveRange(int(0), int(lbLast+1))
  1414. if !c.isEmpty() {
  1415. rb.highlowcontainer.setContainerAtIndex(ilast, c)
  1416. } else {
  1417. ilast++
  1418. }
  1419. } else {
  1420. ilast++
  1421. }
  1422. } else {
  1423. ilast = -ilast - 1
  1424. }
  1425. rb.highlowcontainer.removeIndexRange(ifirst, ilast)
  1426. }
  1427. // Flip negates the bits in the given range (i.e., [rangeStart,rangeEnd)), any integer present in this range and in the bitmap is removed,
  1428. // and any integer present in the range and not in the bitmap is added, a new bitmap is returned leaving
  1429. // the current bitmap unchanged.
  1430. // The function uses 64-bit parameters even though a Bitmap stores 32-bit values because it is allowed and meaningful to use [0,uint64(0x100000000)) as a range
  1431. // while uint64(0x100000000) cannot be represented as a 32-bit value.
  1432. func Flip(bm *Bitmap, rangeStart, rangeEnd uint64) *Bitmap {
  1433. if rangeStart >= rangeEnd {
  1434. return bm.Clone()
  1435. }
  1436. if rangeStart > MaxUint32 {
  1437. panic("rangeStart > MaxUint32")
  1438. }
  1439. if rangeEnd-1 > MaxUint32 {
  1440. panic("rangeEnd-1 > MaxUint32")
  1441. }
  1442. answer := NewBitmap()
  1443. hbStart := uint32(highbits(uint32(rangeStart)))
  1444. lbStart := uint32(lowbits(uint32(rangeStart)))
  1445. hbLast := uint32(highbits(uint32(rangeEnd - 1)))
  1446. lbLast := uint32(lowbits(uint32(rangeEnd - 1)))
  1447. // copy the containers before the active area
  1448. answer.highlowcontainer.appendCopiesUntil(bm.highlowcontainer, uint16(hbStart))
  1449. var max uint32 = maxLowBit
  1450. for hb := hbStart; hb <= hbLast; hb++ {
  1451. var containerStart uint32
  1452. if hb == hbStart {
  1453. containerStart = uint32(lbStart)
  1454. }
  1455. containerLast := max
  1456. if hb == hbLast {
  1457. containerLast = uint32(lbLast)
  1458. }
  1459. i := bm.highlowcontainer.getIndex(uint16(hb))
  1460. j := answer.highlowcontainer.getIndex(uint16(hb))
  1461. if i >= 0 {
  1462. c := bm.highlowcontainer.getContainerAtIndex(i).not(int(containerStart), int(containerLast)+1)
  1463. if !c.isEmpty() {
  1464. answer.highlowcontainer.insertNewKeyValueAt(-j-1, uint16(hb), c)
  1465. }
  1466. } else { // *think* the range of ones must never be
  1467. // empty.
  1468. answer.highlowcontainer.insertNewKeyValueAt(-j-1, uint16(hb),
  1469. rangeOfOnes(int(containerStart), int(containerLast)))
  1470. }
  1471. }
  1472. // copy the containers after the active area.
  1473. answer.highlowcontainer.appendCopiesAfter(bm.highlowcontainer, uint16(hbLast))
  1474. return answer
  1475. }
  1476. // SetCopyOnWrite sets this bitmap to use copy-on-write so that copies are fast and memory conscious
  1477. // if the parameter is true, otherwise we leave the default where hard copies are made
  1478. // (copy-on-write requires extra care in a threaded context).
  1479. // Calling SetCopyOnWrite(true) on a bitmap created with FromBuffer is unsafe.
  1480. func (rb *Bitmap) SetCopyOnWrite(val bool) {
  1481. rb.highlowcontainer.copyOnWrite = val
  1482. }
  1483. // GetCopyOnWrite gets this bitmap's copy-on-write property
  1484. func (rb *Bitmap) GetCopyOnWrite() (val bool) {
  1485. return rb.highlowcontainer.copyOnWrite
  1486. }
  1487. // CloneCopyOnWriteContainers clones all containers which have
  1488. // needCopyOnWrite set to true.
  1489. // This can be used to make sure it is safe to munmap a []byte
  1490. // that the roaring array may still have a reference to, after
  1491. // calling FromBuffer.
  1492. // More generally this function is useful if you call FromBuffer
  1493. // to construct a bitmap with a backing array buf
  1494. // and then later discard the buf array. Note that you should call
  1495. // CloneCopyOnWriteContainers on all bitmaps that were derived
  1496. // from the 'FromBuffer' bitmap since they map have dependencies
  1497. // on the buf array as well.
  1498. func (rb *Bitmap) CloneCopyOnWriteContainers() {
  1499. rb.highlowcontainer.cloneCopyOnWriteContainers()
  1500. }
  1501. // FlipInt calls Flip after casting the parameters (convenience method)
  1502. func FlipInt(bm *Bitmap, rangeStart, rangeEnd int) *Bitmap {
  1503. return Flip(bm, uint64(rangeStart), uint64(rangeEnd))
  1504. }
  1505. // Statistics provides details on the container types in use.
  1506. type Statistics struct {
  1507. Cardinality uint64
  1508. Containers uint64
  1509. ArrayContainers uint64
  1510. ArrayContainerBytes uint64
  1511. ArrayContainerValues uint64
  1512. BitmapContainers uint64
  1513. BitmapContainerBytes uint64
  1514. BitmapContainerValues uint64
  1515. RunContainers uint64
  1516. RunContainerBytes uint64
  1517. RunContainerValues uint64
  1518. }
  1519. // Stats returns details on container type usage in a Statistics struct.
  1520. func (rb *Bitmap) Stats() Statistics {
  1521. stats := Statistics{}
  1522. stats.Containers = uint64(len(rb.highlowcontainer.containers))
  1523. for _, c := range rb.highlowcontainer.containers {
  1524. stats.Cardinality += uint64(c.getCardinality())
  1525. switch c.(type) {
  1526. case *arrayContainer:
  1527. stats.ArrayContainers++
  1528. stats.ArrayContainerBytes += uint64(c.getSizeInBytes())
  1529. stats.ArrayContainerValues += uint64(c.getCardinality())
  1530. case *bitmapContainer:
  1531. stats.BitmapContainers++
  1532. stats.BitmapContainerBytes += uint64(c.getSizeInBytes())
  1533. stats.BitmapContainerValues += uint64(c.getCardinality())
  1534. case *runContainer16:
  1535. stats.RunContainers++
  1536. stats.RunContainerBytes += uint64(c.getSizeInBytes())
  1537. stats.RunContainerValues += uint64(c.getCardinality())
  1538. }
  1539. }
  1540. return stats
  1541. }