-
Notifications
You must be signed in to change notification settings - Fork 381
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 & p/demo/ratelimiter #2170
Open
linhpn99
wants to merge
85
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.
+683
−0
Open
Changes from 41 commits
Commits
Show all changes
85 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 425fd8c
Merge branch 'master' into add-package-queue
linhpn99 04e47ed
Merge branch 'master' into add-package-queue
linhpn99 67b0983
Merge branch 'master' into add-package-queue
linhpn99 49d32e3
rename NewDequeue to New
linhpn99 bbc583d
Merge branch 'add-package-queue' of https://github.com/linhpn99/gno i…
linhpn99 7c78d84
fix CI
linhpn99 d19fb98
Merge branch 'master' into add-package-queue
linhpn99 2d8b933
Merge branch 'master' into add-package-queue
linhpn99 8529ef5
Merge branch 'master' into add-package-queue
linhpn99 5b3cef9
Merge branch 'master' into add-package-queue
linhpn99 6a408a2
Merge branch 'master' into add-package-queue
linhpn99 b411a04
Merge branch 'master' into add-package-queue
linhpn99 ae24809
Merge branch 'master' into add-package-queue
linhpn99 3357201
Merge branch 'master' into add-package-queue
linhpn99 bf6a058
Merge branch 'master' into add-package-queue
linhpn99 abadda7
Merge branch 'master' into add-package-queue
linhpn99 aaec913
Merge branch 'master' into add-package-queue
linhpn99 6f99eb4
Merge branch 'master' into add-package-queue
linhpn99 1d40d46
Merge branch 'master' into add-package-queue
linhpn99 eb6b3f6
Merge branch 'master' into add-package-queue
linhpn99 509e189
add OrderBook package implement dequeue
linhpn99 9a423e9
make tidy
linhpn99 3f9f75d
Merge branch 'master' into add-package-queue
linhpn99 c758b91
fix
linhpn99 232de6c
Merge branch 'add-package-queue' of https://github.com/linhpn99/gno i…
linhpn99 03ecaa9
fix
linhpn99 de3b51c
fixup
linhpn99 fa510b9
fixup
linhpn99 a44cc69
use interface for OrderBook
linhpn99 400fb21
update comment
linhpn99 12eb8d7
Merge branch 'master' into add-package-queue
linhpn99 b87114c
implement rate_limiter package
linhpn99 12de6ea
Merge branch 'master' into add-package-queue
linhpn99 46b690e
remove imports
linhpn99 b9535d0
Merge branch 'add-package-queue' of https://github.com/linhpn99/gno i…
linhpn99 396aef2
mod tidy
linhpn99 03b1cbf
fixup
linhpn99 016e7a4
fixup
linhpn99 bb98f23
update go.mod
linhpn99 cc406a2
rename
linhpn99 0416249
new functions
linhpn99 c6ad96c
add unit tests
linhpn99 9f50532
Merge branch 'master' into add-package-queue
linhpn99 ed1838a
Merge branch 'master' into add-package-queue
linhpn99 e3daf92
Merge branch 'master' into add-package-queue
linhpn99 cfb690f
Merge branch 'master' into add-package-queue
linhpn99 2b4c729
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,154 @@ | ||
package dequeue | ||
|
||
import ( | ||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/seqid" | ||
) | ||
|
||
// Dequeue is a Dequeue (double-ended queue) data structure that stores elements of any type. | ||
type Dequeue struct { | ||
begin seqid.ID // Index of the front element | ||
end seqid.ID // Index after the last element | ||
data *avl.Tree // Storage for Dequeue elements | ||
} | ||
|
||
// NewDequeue creates and initializes a new Dequeue. | ||
func NewDequeue() *Dequeue { | ||
return &Dequeue{ | ||
begin: seqid.ID(0), | ||
end: seqid.ID(0), | ||
data: avl.NewTree(), | ||
} | ||
} | ||
|
||
// PushBack adds an element to the end of the Dequeue. | ||
// Returns an error if the Dequeue is full. | ||
func (q *Dequeue) 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 Dequeue. | ||
// Returns an error if the Dequeue is empty. | ||
func (q *Dequeue) 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 Dequeue. | ||
// Returns an error if the Dequeue is full. | ||
func (q *Dequeue) 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 Dequeue. | ||
// Returns an error if the Dequeue is empty. | ||
func (q *Dequeue) 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 Dequeue without removing it. | ||
// Returns an error if the Dequeue is empty. | ||
func (q *Dequeue) 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 Dequeue without removing it. | ||
// Returns an error if the Dequeue is empty. | ||
func (q *Dequeue) 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 Dequeue. | ||
// Returns an error if the index is out of bounds. | ||
func (q *Dequeue) 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 Dequeue. | ||
func (q *Dequeue) Clear() { | ||
q.begin = 0 | ||
q.end = 0 | ||
q.data = avl.NewTree() | ||
} | ||
|
||
// Length returns the number of elements in the Dequeue. | ||
func (q *Dequeue) Length() uint64 { | ||
return uint64(q.end - q.begin) | ||
} | ||
|
||
// Empty returns true if the Dequeue is empty, false otherwise. | ||
func (q *Dequeue) 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.
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.
49d32e3