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

refactor: addr_set.gno modification proposal #1947

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
39 changes: 14 additions & 25 deletions gnovm/stdlibs/std/addr_set.gno
Original file line number Diff line number Diff line change
@@ -1,47 +1,36 @@
package std

import "errors"
import (
"errors"

//----------------------------------------
// AddressSet
"gno.land/p/demo/avl"
)

type AddressSet interface {
Size() int
AddAddress(Address) error
HasAddress(Address) bool
}

//----------------------------------------
// AddressList implements AddressSet.
// TODO implement AddressTree with avl.

type AddressList []Address
type AddressList struct {
list *avl.Tree
}

func NewAddressList() *AddressList {
return &AddressList{}
return &AddressList{list: avl.NewTree()}
}

func (alist *AddressList) Size() int {
return len(*alist)
func (alist AddressList) Size() int {
return alist.list.Size()
}

func (alist *AddressList) AddAddress(newAddr Address) error {
// TODO optimize with binary algorithm
for _, addr := range *alist {
if addr == newAddr {
return errors.New("address already exists")
}
if alist.list.Set(newAddr.String(), newAddr) {
return errors.New("address already exists")
}
*alist = append(*alist, newAddr)
return nil
}

func (alist *AddressList) HasAddress(newAddr Address) bool {
// TODO optimize with binary algorithm
for _, addr := range *alist {
if addr == newAddr {
return true
}
}
return false
func (alist AddressList) HasAddress(newAddr Address) bool {
return alist.list.Has(newAddr.String())
}
27 changes: 27 additions & 0 deletions gnovm/stdlibs/std/addr_set_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package std

import (
"testing"

"gno.land/p/demo/testutils"
)

func TestAddrSet(t *testing.T) {
addresslist := NewAddressList()
if addresslist.Size() != 0 {
t.Errorf("Expected size 0, got %d", addresslist.Size())
}
test1 := testutils.TestAddress("test1")
if err := addresslist.AddAddress(test1); err != nil {
t.Errorf("Expected no error, got %v", err)
}
if !addresslist.HasAddress(test1) {
t.Errorf("Address doesnt exist: %v", addresslist.HasAddress(test1))
}
if addresslist.Size() != 1 {
t.Errorf("Expected size 1, got %d", addresslist.Size())
}
if err := addresslist.AddAddress(test1); err.Error() != "address already exists" {
t.Errorf("Expected \"address already exists\", got %v", err)
}
}
2 changes: 1 addition & 1 deletion gnovm/tests/files/zrealm_std1.gno
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
}

// Output:
// (slice[ref(1ed29bd278d735e20e296bd4afe927501941392f:4)] std.AddressList)
// (struct{(&(ref(1ed29bd278d735e20e296bd4afe927501941392f:5) gno.land/p/demo/avl.Tree) *gno.land/p/demo/avl.Tree)} std.AddressList)
// error: address already exists
// has: true
// has: false
2 changes: 1 addition & 1 deletion gnovm/tests/files/zrealm_std2.gno
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
}

// Output:
// (slice[ref(1ed29bd278d735e20e296bd4afe927501941392f:4)] std.AddressList)
// (struct{(&(ref(1ed29bd278d735e20e296bd4afe927501941392f:5) gno.land/p/demo/avl.Tree) *gno.land/p/demo/avl.Tree)} std.AddressList)
// error: address already exists
// has: true
// has: false
Loading