Bumps the minor-and-patch group with 18 updates: | Package | From | To | | --- | --- | --- | | [github.com/blevesearch/bleve/v2](https://github.com/blevesearch/bleve) | `2.5.7` | `2.6.0` | | [github.com/davidbyttow/govips/v2](https://github.com/davidbyttow/govips) | `2.16.0` | `2.18.0` | | [github.com/go-chi/chi/v5](https://github.com/go-chi/chi) | `5.2.5` | `5.3.0` | | [github.com/gookit/config/v2](https://github.com/gookit/config) | `2.2.7` | `2.2.8` | | [github.com/grpc-ecosystem/grpc-gateway/v2](https://github.com/grpc-ecosystem/grpc-gateway) | `2.28.0` | `2.29.0` | | [github.com/kovidgoyal/imaging](https://github.com/kovidgoyal/imaging) | `1.8.19` | `1.8.21` | | [github.com/libregraph/lico](https://github.com/libregraph/lico) | `0.66.0` | `0.67.0` | | [github.com/nats-io/nats-server/v2](https://github.com/nats-io/nats-server) | `2.14.0` | `2.14.1` | | [github.com/nats-io/nats.go](https://github.com/nats-io/nats.go) | `1.51.0` | `1.52.0` | | [github.com/olekukonko/tablewriter](https://github.com/olekukonko/tablewriter) | `1.1.0` | `1.1.4` | | [github.com/onsi/gomega](https://github.com/onsi/gomega) | `1.40.0` | `1.41.0` | | [github.com/open-policy-agent/opa](https://github.com/open-policy-agent/opa) | `1.12.3` | `1.16.2` | | [github.com/tidwall/gjson](https://github.com/tidwall/gjson) | `1.18.0` | `1.19.0` | | [go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp](https://github.com/open-telemetry/opentelemetry-go-contrib) | `0.66.0` | `0.68.0` | | [go.opentelemetry.io/contrib/zpages](https://github.com/open-telemetry/opentelemetry-go-contrib) | `0.64.0` | `0.68.0` | | [golang.org/x/crypto](https://github.com/golang/crypto) | `0.51.0` | `0.52.0` | | [golang.org/x/image](https://github.com/golang/image) | `0.39.0` | `0.41.0` | | [google.golang.org/genproto/googleapis/api](https://github.com/googleapis/go-genproto) | `0.0.0-20260401024825-9d38bb4040a9` | `0.0.0-20260414002931-afd174a4e478` | Updates `github.com/blevesearch/bleve/v2` from 2.5.7 to 2.6.0 - [Release notes](https://github.com/blevesearch/bleve/releases) - [Commits](https://github.com/blevesearch/bleve/compare/v2.5.7...v2.6.0) Updates `github.com/davidbyttow/govips/v2` from 2.16.0 to 2.18.0 - [Release notes](https://github.com/davidbyttow/govips/releases) - [Commits](https://github.com/davidbyttow/govips/compare/v2.16.0...v2.18.0) Updates `github.com/go-chi/chi/v5` from 5.2.5 to 5.3.0 - [Release notes](https://github.com/go-chi/chi/releases) - [Changelog](https://github.com/go-chi/chi/blob/master/CHANGELOG.md) - [Commits](https://github.com/go-chi/chi/compare/v5.2.5...v5.3.0) Updates `github.com/gookit/config/v2` from 2.2.7 to 2.2.8 - [Release notes](https://github.com/gookit/config/releases) - [Commits](https://github.com/gookit/config/compare/v2.2.7...v2.2.8) Updates `github.com/grpc-ecosystem/grpc-gateway/v2` from 2.28.0 to 2.29.0 - [Release notes](https://github.com/grpc-ecosystem/grpc-gateway/releases) - [Commits](https://github.com/grpc-ecosystem/grpc-gateway/compare/v2.28.0...v2.29.0) Updates `github.com/kovidgoyal/imaging` from 1.8.19 to 1.8.21 - [Release notes](https://github.com/kovidgoyal/imaging/releases) - [Commits](https://github.com/kovidgoyal/imaging/compare/v1.8.19...v1.8.21) Updates `github.com/libregraph/lico` from 0.66.0 to 0.67.0 - [Changelog](https://github.com/libregraph/lico/blob/master/CHANGELOG.md) - [Commits](https://github.com/libregraph/lico/compare/v0.66.0...v0.67.0) Updates `github.com/nats-io/nats-server/v2` from 2.14.0 to 2.14.1 - [Release notes](https://github.com/nats-io/nats-server/releases) - [Changelog](https://github.com/nats-io/nats-server/blob/main/RELEASES.md) - [Commits](https://github.com/nats-io/nats-server/compare/v2.14.0...v2.14.1) Updates `github.com/nats-io/nats.go` from 1.51.0 to 1.52.0 - [Release notes](https://github.com/nats-io/nats.go/releases) - [Commits](https://github.com/nats-io/nats.go/compare/v1.51.0...v1.52.0) Updates `github.com/olekukonko/tablewriter` from 1.1.0 to 1.1.4 - [Release notes](https://github.com/olekukonko/tablewriter/releases) - [Commits](https://github.com/olekukonko/tablewriter/compare/v1.1.0...v1.1.4) Updates `github.com/onsi/gomega` from 1.40.0 to 1.41.0 - [Release notes](https://github.com/onsi/gomega/releases) - [Changelog](https://github.com/onsi/gomega/blob/master/CHANGELOG.md) - [Commits](https://github.com/onsi/gomega/compare/v1.40.0...v1.41.0) Updates `github.com/open-policy-agent/opa` from 1.12.3 to 1.16.2 - [Release notes](https://github.com/open-policy-agent/opa/releases) - [Changelog](https://github.com/open-policy-agent/opa/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-policy-agent/opa/compare/v1.12.3...v1.16.2) Updates `github.com/tidwall/gjson` from 1.18.0 to 1.19.0 - [Commits](https://github.com/tidwall/gjson/compare/v1.18.0...v1.19.0) Updates `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` from 0.66.0 to 0.68.0 - [Release notes](https://github.com/open-telemetry/opentelemetry-go-contrib/releases) - [Changelog](https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-telemetry/opentelemetry-go-contrib/compare/zpages/v0.66.0...zpages/v0.68.0) Updates `go.opentelemetry.io/contrib/zpages` from 0.64.0 to 0.68.0 - [Release notes](https://github.com/open-telemetry/opentelemetry-go-contrib/releases) - [Changelog](https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-telemetry/opentelemetry-go-contrib/compare/zpages/v0.64.0...zpages/v0.68.0) Updates `golang.org/x/crypto` from 0.51.0 to 0.52.0 - [Commits](https://github.com/golang/crypto/compare/v0.51.0...v0.52.0) Updates `golang.org/x/image` from 0.39.0 to 0.41.0 - [Commits](https://github.com/golang/image/compare/v0.39.0...v0.41.0) Updates `google.golang.org/genproto/googleapis/api` from 0.0.0-20260401024825-9d38bb4040a9 to 0.0.0-20260414002931-afd174a4e478 - [Commits](https://github.com/googleapis/go-genproto/commits) --- updated-dependencies: - dependency-name: github.com/blevesearch/bleve/v2 dependency-version: 2.6.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/davidbyttow/govips/v2 dependency-version: 2.18.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/go-chi/chi/v5 dependency-version: 5.3.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/gookit/config/v2 dependency-version: 2.2.8 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/grpc-ecosystem/grpc-gateway/v2 dependency-version: 2.29.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/kovidgoyal/imaging dependency-version: 1.8.21 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/libregraph/lico dependency-version: 0.67.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/nats-io/nats-server/v2 dependency-version: 2.14.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/nats-io/nats.go dependency-version: 1.52.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/olekukonko/tablewriter dependency-version: 1.1.4 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/onsi/gomega dependency-version: 1.41.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/open-policy-agent/opa dependency-version: 1.16.2 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/tidwall/gjson dependency-version: 1.19.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp dependency-version: 0.68.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: go.opentelemetry.io/contrib/zpages dependency-version: 0.68.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: golang.org/x/crypto dependency-version: 0.52.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: golang.org/x/image dependency-version: 0.41.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: google.golang.org/genproto/googleapis/api dependency-version: 0.0.0-20260414002931-afd174a4e478 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
710 lines
13 KiB
Go
710 lines
13 KiB
Go
package roaring
|
|
|
|
func difference(set1 []uint16, set2 []uint16, buffer []uint16) int {
|
|
if len(set2) == 0 {
|
|
buffer = buffer[:len(set1)]
|
|
copy(buffer, set1)
|
|
return len(set1)
|
|
}
|
|
if len(set1) == 0 {
|
|
return 0
|
|
}
|
|
pos := 0
|
|
k1 := 0
|
|
k2 := 0
|
|
buffer = buffer[:cap(buffer)]
|
|
s1 := set1[k1]
|
|
s2 := set2[k2]
|
|
for {
|
|
if s1 < s2 {
|
|
buffer[pos] = s1
|
|
pos++
|
|
k1++
|
|
if k1 >= len(set1) {
|
|
break
|
|
}
|
|
s1 = set1[k1]
|
|
} else if s1 == s2 {
|
|
k1++
|
|
k2++
|
|
if k1 >= len(set1) {
|
|
break
|
|
}
|
|
s1 = set1[k1]
|
|
if k2 >= len(set2) {
|
|
for ; k1 < len(set1); k1++ {
|
|
buffer[pos] = set1[k1]
|
|
pos++
|
|
}
|
|
break
|
|
}
|
|
s2 = set2[k2]
|
|
} else { // if (val1>val2)
|
|
k2++
|
|
if k2 >= len(set2) {
|
|
for ; k1 < len(set1); k1++ {
|
|
buffer[pos] = set1[k1]
|
|
pos++
|
|
}
|
|
break
|
|
}
|
|
s2 = set2[k2]
|
|
}
|
|
}
|
|
return pos
|
|
}
|
|
|
|
func exclusiveUnion2by2(set1 []uint16, set2 []uint16, buffer []uint16) int {
|
|
if 0 == len(set2) {
|
|
buffer = buffer[:len(set1)]
|
|
copy(buffer, set1[:])
|
|
return len(set1)
|
|
}
|
|
if 0 == len(set1) {
|
|
buffer = buffer[:len(set2)]
|
|
copy(buffer, set2[:])
|
|
return len(set2)
|
|
}
|
|
pos := 0
|
|
k1 := 0
|
|
k2 := 0
|
|
s1 := set1[k1]
|
|
s2 := set2[k2]
|
|
buffer = buffer[:cap(buffer)]
|
|
for {
|
|
if s1 < s2 {
|
|
buffer[pos] = s1
|
|
pos++
|
|
k1++
|
|
if k1 >= len(set1) {
|
|
for ; k2 < len(set2); k2++ {
|
|
buffer[pos] = set2[k2]
|
|
pos++
|
|
}
|
|
break
|
|
}
|
|
s1 = set1[k1]
|
|
} else if s1 == s2 {
|
|
k1++
|
|
k2++
|
|
if k1 >= len(set1) {
|
|
for ; k2 < len(set2); k2++ {
|
|
buffer[pos] = set2[k2]
|
|
pos++
|
|
}
|
|
break
|
|
}
|
|
if k2 >= len(set2) {
|
|
for ; k1 < len(set1); k1++ {
|
|
buffer[pos] = set1[k1]
|
|
pos++
|
|
}
|
|
break
|
|
}
|
|
s1 = set1[k1]
|
|
s2 = set2[k2]
|
|
} else { // if (val1>val2)
|
|
buffer[pos] = s2
|
|
pos++
|
|
k2++
|
|
if k2 >= len(set2) {
|
|
for ; k1 < len(set1); k1++ {
|
|
buffer[pos] = set1[k1]
|
|
pos++
|
|
}
|
|
break
|
|
}
|
|
s2 = set2[k2]
|
|
}
|
|
}
|
|
return pos
|
|
}
|
|
|
|
// union2by2Cardinality computes the cardinality of the union
|
|
func union2by2Cardinality(set1 []uint16, set2 []uint16) int {
|
|
pos := 0
|
|
k1 := 0
|
|
k2 := 0
|
|
if 0 == len(set2) {
|
|
return len(set1)
|
|
}
|
|
if 0 == len(set1) {
|
|
return len(set2)
|
|
}
|
|
s1 := set1[k1]
|
|
s2 := set2[k2]
|
|
for {
|
|
if s1 < s2 {
|
|
pos++
|
|
k1++
|
|
if k1 >= len(set1) {
|
|
pos += len(set2) - k2
|
|
break
|
|
}
|
|
s1 = set1[k1]
|
|
} else if s1 == s2 {
|
|
pos++
|
|
k1++
|
|
k2++
|
|
if k1 >= len(set1) {
|
|
pos += len(set2) - k2
|
|
break
|
|
}
|
|
if k2 >= len(set2) {
|
|
pos += len(set1) - k1
|
|
break
|
|
}
|
|
s1 = set1[k1]
|
|
s2 = set2[k2]
|
|
} else { // if (set1[k1]>set2[k2])
|
|
pos++
|
|
k2++
|
|
if k2 >= len(set2) {
|
|
pos += len(set1) - k1
|
|
break
|
|
}
|
|
s2 = set2[k2]
|
|
}
|
|
}
|
|
return pos
|
|
}
|
|
|
|
func intersection2by2(
|
|
set1 []uint16,
|
|
set2 []uint16,
|
|
buffer []uint16,
|
|
) int {
|
|
if len(set1)*64 < len(set2) {
|
|
return onesidedgallopingintersect2by2(set1, set2, buffer)
|
|
} else if len(set2)*64 < len(set1) {
|
|
return onesidedgallopingintersect2by2(set2, set1, buffer)
|
|
} else {
|
|
return localintersect2by2(set1, set2, buffer)
|
|
}
|
|
}
|
|
|
|
// intersection2by2Cardinality computes the cardinality of the intersection
|
|
func intersection2by2Cardinality(
|
|
set1 []uint16,
|
|
set2 []uint16,
|
|
) int {
|
|
if len(set1)*64 < len(set2) {
|
|
return onesidedgallopingintersect2by2Cardinality(set1, set2)
|
|
} else if len(set2)*64 < len(set1) {
|
|
return onesidedgallopingintersect2by2Cardinality(set2, set1)
|
|
} else {
|
|
return localintersect2by2Cardinality(set1, set2)
|
|
}
|
|
}
|
|
|
|
// intersects2by2 computes whether the two sets intersect
|
|
func intersects2by2(
|
|
set1 []uint16,
|
|
set2 []uint16,
|
|
) bool {
|
|
if (len(set1) == 0) || (len(set2) == 0) {
|
|
return false
|
|
}
|
|
if len(set1)*64 < len(set2) {
|
|
return onesidedgallopingintersect2by2Bool(set1, set2)
|
|
} else if len(set2)*64 < len(set1) {
|
|
return onesidedgallopingintersect2by2Bool(set2, set1)
|
|
} else {
|
|
return intersects2by2Bool(set1, set2)
|
|
}
|
|
}
|
|
|
|
func intersects2by2Bool(
|
|
set1 []uint16,
|
|
set2 []uint16,
|
|
) bool {
|
|
index1 := 0
|
|
index2 := 0
|
|
value1 := set1[index1]
|
|
value2 := set2[index2]
|
|
mainwhile:
|
|
for {
|
|
|
|
if value2 < value1 {
|
|
for {
|
|
index2++
|
|
if index2 == len(set2) {
|
|
break mainwhile
|
|
}
|
|
value2 = set2[index2]
|
|
if value2 >= value1 {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if value1 < value2 {
|
|
for {
|
|
index1++
|
|
if index1 == len(set1) {
|
|
break mainwhile
|
|
}
|
|
value1 = set1[index1]
|
|
if value1 >= value2 {
|
|
break
|
|
}
|
|
}
|
|
} else {
|
|
// (set2[k2] == set1[k1])
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func onesidedgallopingintersect2by2Bool(
|
|
smallset []uint16,
|
|
largeset []uint16,
|
|
) bool {
|
|
k1 := 0
|
|
k2 := 0
|
|
s1 := largeset[k1]
|
|
s2 := smallset[k2]
|
|
mainwhile:
|
|
for {
|
|
if s1 < s2 {
|
|
k1 = advanceUntil(largeset, k1, len(largeset), s2)
|
|
if k1 == len(largeset) {
|
|
break mainwhile
|
|
}
|
|
s1 = largeset[k1]
|
|
}
|
|
if s2 < s1 {
|
|
k2++
|
|
if k2 == len(smallset) {
|
|
break mainwhile
|
|
}
|
|
s2 = smallset[k2]
|
|
} else {
|
|
// (set2[k2] == set1[k1])
|
|
return true
|
|
}
|
|
|
|
}
|
|
return false
|
|
}
|
|
|
|
func localintersect2by2(
|
|
set1 []uint16,
|
|
set2 []uint16,
|
|
buffer []uint16,
|
|
) int {
|
|
if (len(set1) == 0) || (len(set2) == 0) {
|
|
return 0
|
|
}
|
|
k1 := 0
|
|
k2 := 0
|
|
pos := 0
|
|
buffer = buffer[:cap(buffer)]
|
|
s1 := set1[k1]
|
|
s2 := set2[k2]
|
|
mainwhile:
|
|
for {
|
|
if s2 < s1 {
|
|
for {
|
|
k2++
|
|
if k2 == len(set2) {
|
|
break mainwhile
|
|
}
|
|
s2 = set2[k2]
|
|
if s2 >= s1 {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if s1 < s2 {
|
|
for {
|
|
k1++
|
|
if k1 == len(set1) {
|
|
break mainwhile
|
|
}
|
|
s1 = set1[k1]
|
|
if s1 >= s2 {
|
|
break
|
|
}
|
|
}
|
|
} else {
|
|
// (set2[k2] == set1[k1])
|
|
buffer[pos] = s1
|
|
pos++
|
|
k1++
|
|
if k1 == len(set1) {
|
|
break
|
|
}
|
|
s1 = set1[k1]
|
|
k2++
|
|
if k2 == len(set2) {
|
|
break
|
|
}
|
|
s2 = set2[k2]
|
|
}
|
|
}
|
|
return pos
|
|
}
|
|
|
|
// / localintersect2by2Cardinality computes the cardinality of the intersection
|
|
func localintersect2by2Cardinality(
|
|
set1 []uint16,
|
|
set2 []uint16,
|
|
) int {
|
|
if (len(set1) == 0) || (len(set2) == 0) {
|
|
return 0
|
|
}
|
|
index1 := 0
|
|
index2 := 0
|
|
pos := 0
|
|
value1 := set1[index1]
|
|
value2 := set2[index2]
|
|
mainwhile:
|
|
for {
|
|
if value2 < value1 {
|
|
for {
|
|
index2++
|
|
if index2 == len(set2) {
|
|
break mainwhile
|
|
}
|
|
value2 = set2[index2]
|
|
if value2 >= value1 {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if value1 < value2 {
|
|
for {
|
|
index1++
|
|
if index1 == len(set1) {
|
|
break mainwhile
|
|
}
|
|
value1 = set1[index1]
|
|
if value1 >= value2 {
|
|
break
|
|
}
|
|
}
|
|
} else {
|
|
// (set2[k2] == set1[k1])
|
|
pos++
|
|
index1++
|
|
if index1 == len(set1) {
|
|
break
|
|
}
|
|
value1 = set1[index1]
|
|
index2++
|
|
if index2 == len(set2) {
|
|
break
|
|
}
|
|
value2 = set2[index2]
|
|
}
|
|
}
|
|
return pos
|
|
}
|
|
|
|
func advanceUntil(
|
|
array []uint16,
|
|
pos int,
|
|
length int,
|
|
min uint16,
|
|
) int {
|
|
lower := pos + 1
|
|
|
|
if lower >= length || array[lower] >= min {
|
|
return lower
|
|
}
|
|
|
|
spansize := 1
|
|
|
|
for lower+spansize < length && array[lower+spansize] < min {
|
|
spansize *= 2
|
|
}
|
|
var upper int
|
|
if lower+spansize < length {
|
|
upper = lower + spansize
|
|
} else {
|
|
upper = length - 1
|
|
}
|
|
|
|
if array[upper] == min {
|
|
return upper
|
|
}
|
|
|
|
if array[upper] < min {
|
|
// means
|
|
// array
|
|
// has no
|
|
// item
|
|
// >= min
|
|
// pos = array.length;
|
|
return length
|
|
}
|
|
|
|
// we know that the next-smallest span was too small
|
|
lower += (spansize >> 1)
|
|
|
|
mid := 0
|
|
for lower+1 != upper {
|
|
mid = (lower + upper) >> 1
|
|
if array[mid] == min {
|
|
return mid
|
|
} else if array[mid] < min {
|
|
lower = mid
|
|
} else {
|
|
upper = mid
|
|
}
|
|
}
|
|
return upper
|
|
}
|
|
|
|
func onesidedgallopingintersect2by2(
|
|
smallset []uint16,
|
|
largeset []uint16,
|
|
buffer []uint16,
|
|
) int {
|
|
if 0 == len(smallset) {
|
|
return 0
|
|
}
|
|
buffer = buffer[:cap(buffer)]
|
|
k1 := 0
|
|
k2 := 0
|
|
pos := 0
|
|
s1 := largeset[k1]
|
|
s2 := smallset[k2]
|
|
mainwhile:
|
|
|
|
for {
|
|
if s1 < s2 {
|
|
k1 = advanceUntil(largeset, k1, len(largeset), s2)
|
|
if k1 == len(largeset) {
|
|
break mainwhile
|
|
}
|
|
s1 = largeset[k1]
|
|
}
|
|
if s2 < s1 {
|
|
k2++
|
|
if k2 == len(smallset) {
|
|
break mainwhile
|
|
}
|
|
s2 = smallset[k2]
|
|
} else {
|
|
|
|
buffer[pos] = s2
|
|
pos++
|
|
k2++
|
|
if k2 == len(smallset) {
|
|
break
|
|
}
|
|
s2 = smallset[k2]
|
|
k1 = advanceUntil(largeset, k1, len(largeset), s2)
|
|
if k1 == len(largeset) {
|
|
break mainwhile
|
|
}
|
|
s1 = largeset[k1]
|
|
}
|
|
|
|
}
|
|
return pos
|
|
}
|
|
|
|
func onesidedgallopingintersect2by2Cardinality(
|
|
smallset []uint16,
|
|
largeset []uint16,
|
|
) int {
|
|
if 0 == len(smallset) {
|
|
return 0
|
|
}
|
|
k1 := 0
|
|
k2 := 0
|
|
pos := 0
|
|
s1 := largeset[k1]
|
|
s2 := smallset[k2]
|
|
mainwhile:
|
|
|
|
for {
|
|
if s1 < s2 {
|
|
k1 = advanceUntil(largeset, k1, len(largeset), s2)
|
|
if k1 == len(largeset) {
|
|
break mainwhile
|
|
}
|
|
s1 = largeset[k1]
|
|
}
|
|
if s2 < s1 {
|
|
k2++
|
|
if k2 == len(smallset) {
|
|
break mainwhile
|
|
}
|
|
s2 = smallset[k2]
|
|
} else {
|
|
|
|
pos++
|
|
k2++
|
|
if k2 == len(smallset) {
|
|
break
|
|
}
|
|
s2 = smallset[k2]
|
|
k1 = advanceUntil(largeset, k1, len(largeset), s2)
|
|
if k1 == len(largeset) {
|
|
break mainwhile
|
|
}
|
|
s1 = largeset[k1]
|
|
}
|
|
|
|
}
|
|
return pos
|
|
}
|
|
|
|
func binarySearch(array []uint16, ikey uint16) int {
|
|
low := 0
|
|
high := len(array) - 1
|
|
for low+16 <= high {
|
|
middleIndex := int(uint32(low+high) >> 1)
|
|
middleValue := array[middleIndex]
|
|
if middleValue < ikey {
|
|
low = middleIndex + 1
|
|
} else if middleValue > ikey {
|
|
high = middleIndex - 1
|
|
} else {
|
|
return middleIndex
|
|
}
|
|
}
|
|
for ; low <= high; low++ {
|
|
val := array[low]
|
|
if val >= ikey {
|
|
if val == ikey {
|
|
return low
|
|
}
|
|
break
|
|
}
|
|
}
|
|
return -(low + 1)
|
|
}
|
|
|
|
// searchResult provides information about a search request.
|
|
// The values will depend on the context of the search
|
|
type searchResult struct {
|
|
value uint16
|
|
index int
|
|
exactMatch bool
|
|
}
|
|
|
|
// notFound returns a bool depending the search context
|
|
// For cases `previousValue` and `nextValue` if target is present in the slice
|
|
// this function will return `true` otherwise `false`
|
|
// For `nextAbsentValue` and `previousAbsentValue` this will only return `False`
|
|
func (sr *searchResult) notFound() bool {
|
|
return !sr.exactMatch
|
|
}
|
|
|
|
// outOfBounds indicates whether the target was outside the lower and upper bounds of the container
|
|
func (sr *searchResult) outOfBounds() bool {
|
|
return sr.index <= -1
|
|
}
|
|
|
|
// binarySearchUntil is a helper function around binarySearchUntilWithBounds
|
|
// The user does not have to pass in the lower and upper bound
|
|
// The lower bound is taken to be `0` and the upper bound `len(array)-1`
|
|
func binarySearchUntil(array []uint16, target uint16) searchResult {
|
|
return binarySearchUntilWithBounds(array, target, 0, len(array)-1)
|
|
}
|
|
|
|
// binarySearchUntilWithBounds returns a `searchResult`.
|
|
// If an exact match is found the `searchResult{target, <index>, true}` will be returned, where `<index>` is
|
|
// `target`s index in `array`, and `result.notFound()` evaluates to `false`.
|
|
// If a match is not found, but `target` was in-bounds then the result.index will be the closest smaller value
|
|
// Example: [ 8,9,11,12] if the target was 10, then `searchResult{9, 1, false}` will be returned.
|
|
// If `target` was out of bounds `searchResult{0, -1, false}` will be returned.
|
|
func binarySearchUntilWithBounds(array []uint16, target uint16, lowIndex int, maxIndex int) searchResult {
|
|
highIndex := maxIndex
|
|
|
|
closestIndex := -1
|
|
|
|
if target < array[lowIndex] {
|
|
return searchResult{0, closestIndex, false}
|
|
}
|
|
|
|
if target > array[maxIndex] {
|
|
return searchResult{0, len(array), false}
|
|
}
|
|
|
|
for lowIndex <= highIndex {
|
|
middleIndex := (lowIndex + highIndex) / 2
|
|
middleValue := array[middleIndex]
|
|
|
|
if middleValue == target {
|
|
return searchResult{middleValue, middleIndex, true}
|
|
}
|
|
|
|
if target < middleValue {
|
|
|
|
if middleIndex > 0 && target > array[middleIndex-1] {
|
|
return searchResult{array[middleIndex-1], middleIndex - 1, false}
|
|
}
|
|
|
|
highIndex = middleIndex
|
|
} else {
|
|
if middleIndex < maxIndex && target < array[middleIndex+1] {
|
|
return searchResult{middleValue, middleIndex, false}
|
|
}
|
|
lowIndex = middleIndex + 1
|
|
}
|
|
|
|
}
|
|
|
|
return searchResult{array[closestIndex], closestIndex, false}
|
|
}
|
|
|
|
// binarySearchPast is a wrapper around binarySearchPastWithBounds
|
|
// The user does not have to pass in the lower and upper bound
|
|
// The lower bound is taken to be `0` and the upper bound `len(array)-1`
|
|
func binarySearchPast(array []uint16, target uint16) searchResult {
|
|
return binarySearchPastWithBounds(array, target, 0, len(array)-1)
|
|
}
|
|
|
|
// binarySearchPastWithBounds looks for the smallest value larger than or equal to `target`
|
|
// If `target` is out of bounds a `searchResult` indicating out of bounds is returned
|
|
// `target` does not have to exist in the slice.
|
|
//
|
|
// Example:
|
|
// Suppose the slice is [...10,13...] with `target` equal to 11
|
|
// The searchResult will have searchResult.value = 13
|
|
func binarySearchPastWithBounds(array []uint16, target uint16, lowIndex int, maxIndex int) searchResult {
|
|
highIndex := maxIndex
|
|
|
|
closestIndex := -1
|
|
|
|
if target < array[lowIndex] {
|
|
return searchResult{0, closestIndex, false}
|
|
}
|
|
|
|
if target > array[maxIndex] {
|
|
return searchResult{0, len(array), false}
|
|
}
|
|
|
|
for lowIndex <= highIndex {
|
|
middleIndex := (lowIndex + highIndex) / 2
|
|
middleValue := array[middleIndex]
|
|
|
|
if middleValue == target {
|
|
return searchResult{middleValue, middleIndex, true}
|
|
}
|
|
|
|
if target < middleValue {
|
|
|
|
if middleIndex > 0 && target > array[middleIndex-1] {
|
|
return searchResult{array[middleIndex], middleIndex, false}
|
|
}
|
|
|
|
highIndex = middleIndex
|
|
} else {
|
|
if middleIndex < maxIndex && target < array[middleIndex+1] {
|
|
return searchResult{array[middleIndex+1], middleIndex + 1, false}
|
|
}
|
|
lowIndex = middleIndex + 1
|
|
}
|
|
|
|
}
|
|
|
|
return searchResult{array[closestIndex], closestIndex, false}
|
|
}
|