Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitmap.Or(shortBitmap, longBitmap) result length same as the first one #31

Open
slawdan opened this issue Sep 5, 2022 · 0 comments
Open

Comments

@slawdan
Copy link

slawdan commented Sep 5, 2022

When using multiple bitmaps as Bitmap.Or() parameters, the length of destination Bitmap will be same to the first bitmap, if first one is shorter than others, the result will be truncate to start bytes of the result.

Here is sample code to reproduce, see only in last case, call Or() one by one, get the right result.

package main

import (
	"fmt"
	"github.com/kelindar/bitmap"
)

func main() {
	bm := new(bitmap.Bitmap)
	bm1 := bitmap.FromBytes([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01})
	bm2 := bitmap.FromBytes([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01})
	bm3 := bitmap.FromBytes([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01})
	bm.Or(bm1, bm2, bm3)
	fmt.Println("Excepted:3, got:", bm.Count())
	bm.Clear()
	bm.Or(bm1, bm3, bm2)
	fmt.Println("Excepted:3, got:", bm.Count())
	bm.Clear()
	bm.Or(bm3, bm2, bm1)
	fmt.Println("Excepted:3, got:", bm.Count())
	fmt.Printf("bm:%+v\n", bm.ToBytes())
	bm.Clear()
	bm.Or(bm3, bm1, bm2)
	fmt.Println("Excepted:3, got:", bm.Count())
	fmt.Printf("bm:%+v\n", bm.ToBytes())
	bm.Clear()
	bm.Or(bm2, bm3, bm1)
	fmt.Println("Excepted:3, got:", bm.Count())
	bm.Clear()
	bm.Or(bm2, bm1, bm3)
	fmt.Println("Excepted:3, got:", bm.Count())
	bm.Clear()
	bm.Or(bm3)
	bm.Or(bm2)
	bm.Or(bm1)
	fmt.Println("Excepted:3, got:", bm.Count())
}

outputs,

Excepted:3, got: 1
Excepted:3, got: 1
Excepted:3, got: 32
bm:[0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 69 120 99 101 112 116 101 101]
Excepted:3, got: 32
bm:[0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 69 120 99 101 112 116 101 101]
Excepted:3, got: 2
Excepted:3, got: 2
Excepted:3, got: 3

Affected lib versions: v1.2.1, v1.4.1 (I found this issue on v1.2.1 and reproduced on v1.4.1)
Affected Go versions: go1.17.6 darwin/amd64, go1.18.5 darwin/amd64
Affected OS: macOS Monterey 12.5.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant