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/dequeue #2170

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
96eb7f1
implement logic and unit tests
linhpn99 May 23, 2024
8794582
Merge branch 'master' into add-package-queue
linhpn99 May 23, 2024
1de521d
Merge branch 'master' into add-package-queue
linhpn99 May 23, 2024
80b5d8d
Merge branch 'master' into add-package-queue
linhpn99 May 23, 2024
99d3833
Merge branch 'master' into add-package-queue
linhpn99 May 24, 2024
de784db
Merge branch 'master' into add-package-queue
linhpn99 May 24, 2024
42bfef5
Merge branch 'master' into add-package-queue
linhpn99 May 25, 2024
c4c53b8
Merge branch 'master' into add-package-queue
linhpn99 May 26, 2024
e39ac20
Merge branch 'master' into add-package-queue
linhpn99 May 26, 2024
81f45ef
add new line
linhpn99 May 26, 2024
450f3ae
make tidy
linhpn99 May 26, 2024
f151ea5
Merge branch 'master' into add-package-queue
linhpn99 May 26, 2024
af806bd
Merge branch 'master' into add-package-queue
linhpn99 May 26, 2024
a115de0
use avl.Tree & seqid
linhpn99 May 27, 2024
b1bc590
Merge branch 'add-package-queue' of https://github.com/linhpn99/gno i…
linhpn99 May 27, 2024
721f8f0
gno mod tidy
linhpn99 May 27, 2024
b8ae075
Merge branch 'master' into add-package-queue
linhpn99 May 27, 2024
d793941
Merge branch 'master' into add-package-queue
linhpn99 May 27, 2024
6291841
Merge branch 'master' into add-package-queue
linhpn99 May 27, 2024
743be21
Merge branch 'master' into add-package-queue
linhpn99 May 28, 2024
9e506a1
Merge branch 'master' into add-package-queue
linhpn99 May 28, 2024
390bbe7
Merge branch 'master' into add-package-queue
linhpn99 May 29, 2024
d0c27b1
Merge branch 'master' into add-package-queue
linhpn99 May 29, 2024
3d11284
Merge branch 'master' into add-package-queue
linhpn99 May 30, 2024
e9b29e3
Merge branch 'master' into add-package-queue
linhpn99 Jun 1, 2024
85f825c
Merge branch 'master' into add-package-queue
linhpn99 Jun 3, 2024
a57a513
Merge branch 'master' into add-package-queue
linhpn99 Jun 3, 2024
019b5e2
Merge branch 'master' into add-package-queue
linhpn99 Jun 6, 2024
318a3ed
Merge branch 'master' into add-package-queue
linhpn99 Jun 8, 2024
358515c
Merge branch 'master' into add-package-queue
linhpn99 Jun 11, 2024
11e0dc0
Merge branch 'master' into add-package-queue
linhpn99 Jun 19, 2024
8e25944
rename to dequeue
linhpn99 Jun 25, 2024
f4cf44e
Merge branch 'master' into add-package-queue
linhpn99 Jun 25, 2024
df770d6
remove queue
linhpn99 Jun 25, 2024
72cbc36
Merge branch 'add-package-queue' of https://github.com/linhpn99/gno i…
linhpn99 Jun 25, 2024
5f23f51
Merge branch 'master' into add-package-queue
linhpn99 Jun 26, 2024
efb1088
Merge branch 'master' into add-package-queue
linhpn99 Jun 27, 2024
f024b7e
Merge branch 'master' into add-package-queue
linhpn99 Jun 29, 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
14 changes: 14 additions & 0 deletions examples/gno.land/p/demo/queue/errors.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package queue

import (
"errors"
)

// Error definitions for various edge cases
var (
ErrResourceError = errors.New("resource error: queue is full")
ErrEmptyArrayPop = errors.New("empty array pop: queue is empty")
ErrArrayOutOfBounds = errors.New("array out of bounds")

ErrNonExistedValue = errors.New("non-existed value")
)
6 changes: 6 additions & 0 deletions examples/gno.land/p/demo/queue/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module gno.land/p/demo/queue

require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/demo/seqid v0.0.0-latest
)
154 changes: 154 additions & 0 deletions examples/gno.land/p/demo/queue/queue.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package queue

import (
"gno.land/p/demo/avl"
"gno.land/p/demo/seqid"
)

// Queue is a queue data structure that stores elements of any type.
type Queue struct {
begin seqid.ID // Index of the front element
end seqid.ID // Index after the last element
data *avl.Tree // Storage for queue elements
}

// NewQueue creates and initializes a new queue.
func NewQueue() *Queue {
return &Queue{
begin: seqid.ID(0),
end: seqid.ID(0),
data: avl.NewTree(),
}
}

// PushBack adds an element to the end of the queue.
// Returns an error if the queue is full.
func (q *Queue) PushBack(value interface{}) error {
if q.end+1 == q.begin {
return ErrResourceError
}

q.data.Set(q.end.String(), value)

q.end++

return nil
}

// PopBack removes and returns the element at the end of the queue.
// Returns an error if the queue is empty.
func (q *Queue) PopBack() (interface{}, error) {
if q.begin == q.end {
return nil, ErrEmptyArrayPop
}

q.end--
value, ok := q.data.Get(q.end.String())
if !ok {
return nil, ErrNonExistedValue
}

q.data.Remove(q.end.String())

return value, nil
}

// PushFront adds an element to the front of the queue.
// Returns an error if the queue is full.
func (q *Queue) PushFront(value interface{}) error {
if q.begin-1 == q.end {
Copy link
Member

@moul moul May 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With q.begin (uint) set to 0 by default, you may encounter an issue here.

Copy link
Contributor Author

@linhpn99 linhpn99 May 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested this case, q.begin will equal max.Uint64 and does not cause any other runtime errors

return ErrResourceError
}

q.begin--
q.data.Set(q.begin.String(), value)

return nil
}

// PopFront removes and returns the element at the front of the queue.
// Returns an error if the queue is empty.
func (q *Queue) PopFront() (interface{}, error) {
if q.begin == q.end {
return nil, ErrEmptyArrayPop
}

value, ok := q.data.Get(q.begin.String())
if !ok {
return nil, ErrNonExistedValue
}

q.data.Remove(q.begin.String())

q.begin++

return value, nil
}

// Front returns the element at the front of the queue without removing it.
// Returns an error if the queue is empty.
func (q *Queue) Front() (interface{}, error) {
if q.Empty() {
return nil, ErrArrayOutOfBounds
}

id := q.begin

value, ok := q.data.Get(id.String())
if !ok {
return nil, ErrNonExistedValue
}

return value, nil
}

// Back returns the element at the end of the queue without removing it.
// Returns an error if the queue is empty.
func (q *Queue) Back() (interface{}, error) {
if q.Empty() {
return nil, ErrArrayOutOfBounds
}

id := q.end - 1

value, ok := q.data.Get(id.String())
if !ok {
return nil, ErrNonExistedValue
}

return value, nil
}

// At returns the element at the specified index in the queue.
// Returns an error if the index is out of bounds.
func (q *Queue) At(index uint64) (interface{}, error) {
if index >= q.Length() {
return nil, ErrArrayOutOfBounds
}

id := q.begin + seqid.ID(index)

value, ok := q.data.Get(id.String())
if !ok {
return nil, ErrNonExistedValue
}

return value, nil
}

// Clear removes all elements from the queue.
func (q *Queue) Clear() {
q.begin = 0
q.end = 0
q.data = avl.NewTree()
}

// Length returns the number of elements in the queue.
func (q *Queue) Length() uint64 {
return uint64(q.end - q.begin)
}

// Empty returns true if the queue is empty, false otherwise.
func (q *Queue) Empty() bool {
return q.end == q.begin
}
Loading
Loading