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

feat(examples): Implement p/demo/bitmap #2115

Open
wants to merge 49 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
59b8a80
logic and test cases
linhpn99 May 15, 2024
db4ada1
Merge branch 'master' into add-package-bitmap
linhpn99 May 15, 2024
cf42789
add gno.mod
linhpn99 May 15, 2024
dad0e84
Merge branch 'add-package-bitmap' of https://github.com/linhpn99/gno …
linhpn99 May 15, 2024
2e876e6
pull latest
linhpn99 May 15, 2024
0e13a8a
add new line
linhpn99 May 16, 2024
d5b7141
Merge branch 'master' into add-package-bitmap
linhpn99 May 17, 2024
bb7957d
Merge branch 'master' into add-package-bitmap
linhpn99 May 19, 2024
ea66549
Merge branch 'master' into add-package-bitmap
linhpn99 May 22, 2024
ec6cd61
Merge branch 'master' into add-package-bitmap
linhpn99 May 22, 2024
daf2721
refactor code and unit tests
linhpn99 May 23, 2024
e885be0
remove require
linhpn99 May 23, 2024
b9cb538
Merge branch 'master' into add-package-bitmap
linhpn99 May 23, 2024
c697dd2
Merge branch 'master' into add-package-bitmap
linhpn99 May 23, 2024
41840b2
Merge branch 'master' into add-package-bitmap
linhpn99 May 24, 2024
df845a3
Merge branch 'master' into add-package-bitmap
linhpn99 May 24, 2024
37b19b8
Merge branch 'master' into add-package-bitmap
linhpn99 May 25, 2024
79517d8
Merge branch 'master' into add-package-bitmap
linhpn99 May 26, 2024
ba90d47
Merge branch 'master' into add-package-bitmap
linhpn99 May 26, 2024
e208020
add new line
linhpn99 May 26, 2024
4b8a0dc
new line
linhpn99 May 26, 2024
6447f11
Merge branch 'master' into add-package-bitmap
linhpn99 May 26, 2024
4210352
make tidy
linhpn99 May 26, 2024
4d3c4c5
Merge branch 'add-package-bitmap' of https://github.com/linhpn99/gno …
linhpn99 May 26, 2024
ae88381
Merge branch 'master' into add-package-bitmap
linhpn99 May 26, 2024
be1a85a
Merge branch 'master' into add-package-bitmap
linhpn99 May 27, 2024
f260c85
Merge branch 'master' into add-package-bitmap
linhpn99 May 27, 2024
f2b1529
Merge branch 'master' into add-package-bitmap
linhpn99 May 27, 2024
bea1eda
Merge branch 'master' into add-package-bitmap
linhpn99 May 28, 2024
8cac6f4
Merge branch 'master' into add-package-bitmap
linhpn99 May 28, 2024
6804a8e
Merge branch 'master' into add-package-bitmap
linhpn99 May 28, 2024
9821525
Merge branch 'master' into add-package-bitmap
linhpn99 May 29, 2024
a860abc
Merge branch 'master' into add-package-bitmap
linhpn99 May 30, 2024
760dbf5
Merge branch 'master' into add-package-bitmap
linhpn99 Jun 1, 2024
c9fbdab
Merge branch 'master' into add-package-bitmap
linhpn99 Jun 3, 2024
c78d7bc
Merge branch 'master' into add-package-bitmap
linhpn99 Jun 3, 2024
b6afc2d
Merge branch 'master' into add-package-bitmap
linhpn99 Jun 6, 2024
456c823
Merge branch 'master' into add-package-bitmap
linhpn99 Jun 8, 2024
83f18ce
Merge branch 'master' into add-package-bitmap
linhpn99 Jun 11, 2024
c56f674
Merge branch 'master' into add-package-bitmap
linhpn99 Jun 26, 2024
44858f5
Merge branch 'master' into add-package-bitmap
linhpn99 Jul 3, 2024
e67df15
Merge branch 'master' into add-package-bitmap
linhpn99 Jul 4, 2024
988c5cc
Merge branch 'master' into add-package-bitmap
linhpn99 Jul 5, 2024
156cb4c
Merge branch 'master' into add-package-bitmap
linhpn99 Jul 5, 2024
5eabe07
Merge branch 'master' into add-package-bitmap
linhpn99 Jul 6, 2024
d017bc4
Merge branch 'master' into add-package-bitmap
linhpn99 Jul 8, 2024
a9e0691
Merge branch 'master' into add-package-bitmap
linhpn99 Jul 8, 2024
7c97ccb
Merge branch 'master' into add-package-bitmap
linhpn99 Jul 9, 2024
94834d1
Merge branch 'master' into add-package-bitmap
linhpn99 Sep 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions examples/gno.land/p/demo/bitmap/bitmap.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package bitmap

// A simple implementation of Bitmap

// Bitmap represents a bitmap using a slice of bytes
type Bitmap struct {
data []byte
}

// NewBitmap creates a new Bitmap with a specific size (in bits)
func New(size uint64) *Bitmap {
byteSize := (size + 7) / 8 // Calculate the number of bytes needed
return &Bitmap{
data: make([]byte, byteSize),
}
}

// Set sets the bit at the given index (0-based)
func (b *Bitmap) Set(index uint64) {
linhpn99 marked this conversation as resolved.
Show resolved Hide resolved
if index >= uint64(len(b.data))*8 {
panic("Index out of bounds")
}

byteIndex := index / 8
bitIndex := index % 8

b.data[byteIndex] |= 1 << uint(bitIndex) // Set the corresponding bit using bitwise OR
}

// Get checks if the bit at the given index is set
func (b *Bitmap) Get(index uint64) bool {
if index >= uint64(len(b.data))*8 {
panic("Index out of bounds")
}

byteIndex := index / 8
bitIndex := index % 8

return b.data[byteIndex]&(1<<uint(bitIndex)) > 0 // Check if the corresponding bit is set using bitwise AND
}
62 changes: 62 additions & 0 deletions examples/gno.land/p/demo/bitmap/bitmap_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package bitmap

import (
"testing"
)

func TestNewBitmap(t *testing.T) {
// Test creating a bitmap with zero size
bm := New(0)
if len(bm.data) != 0 {
t.Errorf("Expected empty data slice for size 0, got %d bytes", len(bm.data))
}

// Test creating a bitmap with a small size
bm = New(8)
if len(bm.data) != 1 {
t.Errorf("Expected 1 byte for size 8, got %d bytes", len(bm.data))
}

// Test creating a bitmap with a size not divisible by 8
bm = New(10)
if len(bm.data) != 2 {
t.Errorf("Expected 2 bytes for size 10, got %d bytes", len(bm.data))
}
}

func TestSetGet(t *testing.T) {
bm := New(16)

// Test setting and getting a bit
bm.Set(5)
if !bm.Get(5) {
t.Errorf("Expected bit 5 to be set after setting")
}
if bm.Get(10) {
t.Errorf("Expected bit 10 to be unset")
}

// Test setting and getting out of bounds
defer func() {
if recover() == nil {
t.Errorf("Expected panic for out of bounds index")
}
}()
bm.Set(20) // Should panic

defer func() {
if recover() == nil {
t.Errorf("Expected panic for out of bounds index")
}
}()
bm.Get(20) // Should panic
}

func TestLargeBitmap(t *testing.T) {
// Test creating and setting a bit in a larger bitmap
bm := New(1024)
bm.Set(512)
if !bm.Get(512) {
t.Errorf("Expected bit 512 to be set in a large bitmap")
}
}
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/bitmap/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/bitmap
Loading