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 & p/demo/ratelimiter #2170

Open
wants to merge 85 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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 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
425fd8c
Merge branch 'master' into add-package-queue
linhpn99 Jul 3, 2024
04e47ed
Merge branch 'master' into add-package-queue
linhpn99 Jul 3, 2024
67b0983
Merge branch 'master' into add-package-queue
linhpn99 Jul 4, 2024
49d32e3
rename NewDequeue to New
linhpn99 Jul 4, 2024
bbc583d
Merge branch 'add-package-queue' of https://github.com/linhpn99/gno i…
linhpn99 Jul 4, 2024
7c78d84
fix CI
linhpn99 Jul 4, 2024
d19fb98
Merge branch 'master' into add-package-queue
linhpn99 Jul 4, 2024
2d8b933
Merge branch 'master' into add-package-queue
linhpn99 Jul 5, 2024
8529ef5
Merge branch 'master' into add-package-queue
linhpn99 Jul 5, 2024
5b3cef9
Merge branch 'master' into add-package-queue
linhpn99 Jul 5, 2024
6a408a2
Merge branch 'master' into add-package-queue
linhpn99 Jul 6, 2024
b411a04
Merge branch 'master' into add-package-queue
linhpn99 Jul 6, 2024
ae24809
Merge branch 'master' into add-package-queue
linhpn99 Jul 8, 2024
3357201
Merge branch 'master' into add-package-queue
linhpn99 Jul 8, 2024
bf6a058
Merge branch 'master' into add-package-queue
linhpn99 Jul 8, 2024
abadda7
Merge branch 'master' into add-package-queue
linhpn99 Jul 9, 2024
aaec913
Merge branch 'master' into add-package-queue
linhpn99 Jul 9, 2024
6f99eb4
Merge branch 'master' into add-package-queue
linhpn99 Jul 9, 2024
1d40d46
Merge branch 'master' into add-package-queue
linhpn99 Jul 10, 2024
eb6b3f6
Merge branch 'master' into add-package-queue
linhpn99 Jul 14, 2024
509e189
add OrderBook package implement dequeue
linhpn99 Jul 14, 2024
9a423e9
make tidy
linhpn99 Jul 14, 2024
3f9f75d
Merge branch 'master' into add-package-queue
linhpn99 Jul 14, 2024
c758b91
fix
linhpn99 Jul 14, 2024
232de6c
Merge branch 'add-package-queue' of https://github.com/linhpn99/gno i…
linhpn99 Jul 14, 2024
03ecaa9
fix
linhpn99 Jul 14, 2024
de3b51c
fixup
linhpn99 Jul 14, 2024
fa510b9
fixup
linhpn99 Jul 14, 2024
a44cc69
use interface for OrderBook
linhpn99 Jul 14, 2024
400fb21
update comment
linhpn99 Jul 15, 2024
12eb8d7
Merge branch 'master' into add-package-queue
linhpn99 Jul 22, 2024
b87114c
implement rate_limiter package
linhpn99 Aug 21, 2024
12de6ea
Merge branch 'master' into add-package-queue
linhpn99 Aug 21, 2024
46b690e
remove imports
linhpn99 Aug 21, 2024
b9535d0
Merge branch 'add-package-queue' of https://github.com/linhpn99/gno i…
linhpn99 Aug 21, 2024
396aef2
mod tidy
linhpn99 Aug 21, 2024
03b1cbf
fixup
linhpn99 Aug 21, 2024
016e7a4
fixup
linhpn99 Aug 21, 2024
bb98f23
update go.mod
linhpn99 Aug 21, 2024
cc406a2
rename
linhpn99 Aug 21, 2024
0416249
new functions
linhpn99 Aug 21, 2024
c6ad96c
add unit tests
linhpn99 Aug 21, 2024
9f50532
Merge branch 'master' into add-package-queue
linhpn99 Aug 22, 2024
ed1838a
Merge branch 'master' into add-package-queue
linhpn99 Aug 23, 2024
e3daf92
Merge branch 'master' into add-package-queue
linhpn99 Aug 26, 2024
cfb690f
Merge branch 'master' into add-package-queue
linhpn99 Aug 28, 2024
2b4c729
Merge branch 'master' into add-package-queue
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
154 changes: 154 additions & 0 deletions examples/gno.land/p/demo/dequeue/dequeue.gno
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 {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func NewDequeue() *Dequeue {
func New() *Dequeue {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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
}
Loading
Loading