Skip to content

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

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

Closed
wants to merge 49 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
58 changes: 58 additions & 0 deletions examples/gno.land/p/demo/bitmap/bitmap.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package bitmap

import (
"gno.land/p/demo/ufmt"
)

// 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),
}
}

// MustSet sets the bit at the given index (0-based) and returns an error if out of bounds
func (b *Bitmap) Set(index uint64) error {
if index >= uint64(len(b.data))*8 {
return ufmt.Errorf("Index out of bounds")
}

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

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

return nil
}

// Set sets the bit at the given index (0-based)
func (b *Bitmap) MustSet(index uint64) {
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
}
96 changes: 96 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,96 @@
package bitmap

import (
"testing"
)

func TestBitmap_New(t *testing.T) {
size := uint64(16)
bm := New(size)

if len(bm.data)*8 != int(size) {
t.Errorf("Expected bitmap size: %d, got: %d", size, len(bm.data)*8)
}
}

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

// Test setting and getting bits within bounds
err := bm.Set(2)
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}

if !bm.Get(2) {
t.Errorf("Expected bit at index 2 to be set")
}

err = bm.Set(15)
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}

if !bm.Get(15) {
t.Errorf("Expected bit at index 15 to be set")
}

// Test setting and getting bits out of bounds
err = bm.Set(16)
if err == nil {
t.Errorf("Expected error, got: nil")
}
}

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

// Test setting bits within bounds
defer func() {
if r := recover(); r != nil {
t.Errorf("Expected no panic, but got panic: %v", r)
}
}()

bm.MustSet(2)
if !bm.Get(2) {
t.Errorf("Expected bit at index 2 to be set")
}

bm.MustSet(15)
if !bm.Get(15) {
t.Errorf("Expected bit at index 15 to be set")
}

// Test setting bits out of bounds
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic, but no panic occurred")
}
}()

bm.MustSet(16 * 8)
}

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

// Test getting bits within bounds
bm.MustSet(2)
if !bm.Get(2) {
t.Errorf("Expected bit at index 2 to be set")
}

if bm.Get(3) {
t.Errorf("Expected bit at index 3 to be unset")
}

// Test getting bits out of bounds
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic, but no panic occurred")
}
}()

bm.Get(16 * 8)
}
3 changes: 3 additions & 0 deletions examples/gno.land/p/demo/bitmap/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module gno.land/p/demo/bitmap

require gno.land/p/demo/ufmt v0.0.0-latest
Loading