-
Notifications
You must be signed in to change notification settings - Fork 342
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
linhpn99
wants to merge
38
commits into
gnolang:master
Choose a base branch
from
linhpn99:add-package-queue
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+520
−0
Open
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 8794582
Merge branch 'master' into add-package-queue
linhpn99 1de521d
Merge branch 'master' into add-package-queue
linhpn99 80b5d8d
Merge branch 'master' into add-package-queue
linhpn99 99d3833
Merge branch 'master' into add-package-queue
linhpn99 de784db
Merge branch 'master' into add-package-queue
linhpn99 42bfef5
Merge branch 'master' into add-package-queue
linhpn99 c4c53b8
Merge branch 'master' into add-package-queue
linhpn99 e39ac20
Merge branch 'master' into add-package-queue
linhpn99 81f45ef
add new line
linhpn99 450f3ae
make tidy
linhpn99 f151ea5
Merge branch 'master' into add-package-queue
linhpn99 af806bd
Merge branch 'master' into add-package-queue
linhpn99 a115de0
use avl.Tree & seqid
linhpn99 b1bc590
Merge branch 'add-package-queue' of https://github.com/linhpn99/gno i…
linhpn99 721f8f0
gno mod tidy
linhpn99 b8ae075
Merge branch 'master' into add-package-queue
linhpn99 d793941
Merge branch 'master' into add-package-queue
linhpn99 6291841
Merge branch 'master' into add-package-queue
linhpn99 743be21
Merge branch 'master' into add-package-queue
linhpn99 9e506a1
Merge branch 'master' into add-package-queue
linhpn99 390bbe7
Merge branch 'master' into add-package-queue
linhpn99 d0c27b1
Merge branch 'master' into add-package-queue
linhpn99 3d11284
Merge branch 'master' into add-package-queue
linhpn99 e9b29e3
Merge branch 'master' into add-package-queue
linhpn99 85f825c
Merge branch 'master' into add-package-queue
linhpn99 a57a513
Merge branch 'master' into add-package-queue
linhpn99 019b5e2
Merge branch 'master' into add-package-queue
linhpn99 318a3ed
Merge branch 'master' into add-package-queue
linhpn99 358515c
Merge branch 'master' into add-package-queue
linhpn99 11e0dc0
Merge branch 'master' into add-package-queue
linhpn99 8e25944
rename to dequeue
linhpn99 f4cf44e
Merge branch 'master' into add-package-queue
linhpn99 df770d6
remove queue
linhpn99 72cbc36
Merge branch 'add-package-queue' of https://github.com/linhpn99/gno i…
linhpn99 5f23f51
Merge branch 'master' into add-package-queue
linhpn99 efb1088
Merge branch 'master' into add-package-queue
linhpn99 f024b7e
Merge branch 'master' into add-package-queue
linhpn99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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 | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 equalmax.Uint64
and does not cause any other runtime errors