MCPcopy Create free account
hub / github.com/RoaringBitmap/roaring / Add

Method Add

runcontainer.go:1021–1099  ·  view source on GitHub ↗

Add adds a single value k to the set.

(k uint16)

Source from the content-addressed store, hash-verified

1019
1020// Add adds a single value k to the set.
1021func (rc *runContainer16) Add(k uint16) (wasNew bool) {
1022 // TODO comment from runContainer16.java:
1023 // it might be better and simpler to do return
1024 // toBitmapOrArrayContainer(getCardinality()).add(k)
1025 // but note that some unit tests use this method to build up test
1026 // runcontainers without calling runOptimize
1027
1028 k64 := int(k)
1029
1030 index, present, _ := rc.search(k64)
1031 if present {
1032 return // already there
1033 }
1034 wasNew = true
1035
1036 n := len(rc.iv)
1037 if index == -1 {
1038 // we may need to extend the first run
1039 if n > 0 {
1040 if rc.iv[0].start == k+1 {
1041 rc.iv[0].start = k
1042 rc.iv[0].length++
1043 return
1044 }
1045 }
1046 // nope, k stands alone, starting the new first interval16.
1047 rc.iv = append([]interval16{newInterval16Range(k, k)}, rc.iv...)
1048 return
1049 }
1050
1051 // are we off the end? handle both index == n and index == n-1:
1052 if index >= n-1 {
1053 if int(rc.iv[n-1].last())+1 == k64 {
1054 rc.iv[n-1].length++
1055 return
1056 }
1057 rc.iv = append(rc.iv, newInterval16Range(k, k))
1058 return
1059 }
1060
1061 // INVAR: index and index+1 both exist, and k goes between them.
1062 //
1063 // Now: add k into the middle,
1064 // possibly fusing with index or index+1 interval16
1065 // and possibly resulting in fusing of two interval16s
1066 // that had a one integer gap.
1067
1068 left := index
1069 right := index + 1
1070
1071 // are we fusing left and right by adding k?
1072 if int(rc.iv[left].last())+1 == k64 && int(rc.iv[right].start) == k64+1 {
1073 // fuse into left
1074 rc.iv[left].length = rc.iv[right].last() - rc.iv[left].start
1075 // remove redundant right
1076 rc.iv = append(rc.iv[:left+1], rc.iv[right+1:]...)
1077 return
1078 }

Callers 5

iaddReturnMinimizedMethod · 0.95
iaddMethod · 0.95
inplaceUnionMethod · 0.95
XorMethod · 0.45
OrMethod · 0.45

Calls 3

searchMethod · 0.95
newInterval16RangeFunction · 0.85
lastMethod · 0.80

Tested by

no test coverage detected