Skip to content

Commit 2023901

Browse files
committed
docs: update README.md, add example tests and benchmarks for Circular Queue
1 parent 8b4b880 commit 2023901

File tree

4 files changed

+175
-9
lines changed

4 files changed

+175
-9
lines changed

README.md

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ The queue package provides two types of queues:
3131

3232
- Priority Queue: Order based on the less function provided at construction. Implemented using container/heap standard library package.
3333

34+
- Circular Queue: FIFO Ordering, implemented using a fixed size slice. When the queue is full, adding a new element to the queue overwrites the oldest element.
35+
3436
### Installation
3537
To add this package as a dependency to your project, run:
3638

@@ -145,15 +147,56 @@ func main() {
145147
}
146148
```
147149

150+
##### Circular Queue
151+
152+
```go
153+
package main
154+
155+
import (
156+
"fmt"
157+
158+
"github.com/adrianbrad/queue"
159+
)
160+
161+
func main() {
162+
elems := []int{2, 3, 4}
163+
164+
priorityQueue := queue.NewCircular(elems, 3)
165+
166+
containsTwo := priorityQueue.Contains(2)
167+
fmt.Println(containsTwo) // true
168+
169+
size := priorityQueue.Size()
170+
fmt.Println(size) // 3
171+
172+
empty := priorityQueue.IsEmpty()
173+
fmt.Println(empty) // false
174+
175+
if err := priorityQueue.Offer(1); err != nil {
176+
// handle err
177+
}
178+
179+
elem, err := priorityQueue.Get()
180+
if err != nil {
181+
// handle err
182+
}
183+
184+
fmt.Printf("elem: %d\n", elem) // elem: 1
185+
}
186+
```
187+
148188
## Benchmarks
149189

150-
Results as of 3rd of February 2023.
190+
Results as of April 2023.
151191

152192
```text
153-
BenchmarkBlockingQueue/Peek-12 63275360 19.44 ns/op 0 B/op 0 allocs/op
154-
BenchmarkBlockingQueue/Get_Offer-12 19066974 69.67 ns/op 40 B/op 0 allocs/op
155-
BenchmarkBlockingQueue/Offer-12 36569245 37.86 ns/op 41 B/op 0 allocs/op
156-
BenchmarkPriorityQueue/Peek-12 66765319 15.86 ns/op 0 B/op 0 allocs/op
157-
BenchmarkPriorityQueue/Get_Offer-12 16677442 71.33 ns/op 0 B/op 0 allocs/op
158-
BenchmarkPriorityQueue/Offer-12 20044909 58.58 ns/op 55 B/op 0 allocs/op
193+
BenchmarkBlockingQueue/Peek-12 72900492 18.92 ns/op 0 B/op 0 allocs/op
194+
BenchmarkBlockingQueue/Get_Offer-12 14937858 95.08 ns/op 41 B/op 0 allocs/op
195+
BenchmarkBlockingQueue/Offer-12 26680512 51.81 ns/op 45 B/op 0 allocs/op
196+
BenchmarkCircularQueue/Peek-12 73749498 16.24 ns/op 0 B/op 0 allocs/op
197+
BenchmarkCircularQueue/Get_Offer-12 18768650 63.02 ns/op 0 B/op 0 allocs/op
198+
BenchmarkCircularQueue/Offer-12 38328231 37.57 ns/op 0 B/op 0 allocs/op
199+
BenchmarkPriorityQueue/Peek-12 75156879 15.79 ns/op 0 B/op 0 allocs/op
200+
BenchmarkPriorityQueue/Get_Offer-12 17643837 68.65 ns/op 0 B/op 0 allocs/op
201+
BenchmarkPriorityQueue/Offer-12 20506784 57.43 ns/op 54 B/op 0 allocs/op
159202
```

circular_bench_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package queue_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/adrianbrad/queue"
7+
)
8+
9+
func BenchmarkCircularQueue(b *testing.B) {
10+
b.Run("Peek", func(b *testing.B) {
11+
circularQueue := queue.NewCircular([]int{1}, 1)
12+
13+
b.ReportAllocs()
14+
b.ResetTimer()
15+
16+
for i := 0; i <= b.N; i++ {
17+
_, _ = circularQueue.Peek()
18+
}
19+
})
20+
21+
b.Run("Get_Offer", func(b *testing.B) {
22+
circularQueue := queue.NewCircular([]int{1}, 1)
23+
24+
b.ReportAllocs()
25+
b.ResetTimer()
26+
27+
for i := 0; i <= b.N; i++ {
28+
_, _ = circularQueue.Get()
29+
30+
_ = circularQueue.Offer(1)
31+
}
32+
})
33+
34+
b.Run("Offer", func(b *testing.B) {
35+
circularQueue := queue.NewCircular[int](nil, 1)
36+
37+
b.ReportAllocs()
38+
b.ResetTimer()
39+
40+
for i := 0; i <= b.N; i++ {
41+
_ = circularQueue.Offer(i)
42+
}
43+
})
44+
}

example_circular_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package queue_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/adrianbrad/queue"
7+
)
8+
9+
func ExampleCircular() {
10+
elems := []int{1, 2, 3}
11+
12+
const capacity = 4
13+
14+
priorityQueue := queue.NewCircular(
15+
elems,
16+
capacity,
17+
)
18+
19+
containsTwo := priorityQueue.Contains(2)
20+
fmt.Println("Contains 2:", containsTwo)
21+
22+
size := priorityQueue.Size()
23+
fmt.Println("Size:", size)
24+
25+
if err := priorityQueue.Offer(4); err != nil {
26+
fmt.Println("Offer err: ", err)
27+
return
28+
}
29+
30+
nextElem, err := priorityQueue.Peek()
31+
if err != nil {
32+
fmt.Println("Peek err: ", err)
33+
return
34+
}
35+
36+
fmt.Println("Peek:", nextElem)
37+
38+
if err := priorityQueue.Offer(5); err != nil {
39+
fmt.Println("Offer err: ", err)
40+
return
41+
}
42+
43+
fmt.Println("Offered 5")
44+
45+
if err := priorityQueue.Offer(6); err != nil {
46+
fmt.Println("Offer err: ", err)
47+
return
48+
}
49+
50+
fmt.Println("Offered 6")
51+
52+
clearElems := priorityQueue.Clear()
53+
fmt.Println("Clear:", clearElems)
54+
55+
fmt.Println("Offered 7")
56+
57+
if err := priorityQueue.Offer(7); err != nil {
58+
fmt.Println("Offer err: ", err)
59+
return
60+
}
61+
62+
elem, err := priorityQueue.Get()
63+
if err != nil {
64+
fmt.Println("Get err: ", err)
65+
return
66+
}
67+
68+
fmt.Println("Get:", elem)
69+
70+
// Output:
71+
// Contains 2: true
72+
// Size: 3
73+
// Peek: 1
74+
// Offered 5
75+
// Offered 6
76+
// Clear: [5 6 3 4]
77+
// Offered 7
78+
// Get: 7
79+
}

example_priority_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func ExamplePriority() {
1818
)
1919

2020
containsTwo := priorityQueue.Contains(2)
21-
fmt.Println("Contains 3:", containsTwo)
21+
fmt.Println("Contains 2:", containsTwo)
2222

2323
size := priorityQueue.Size()
2424
fmt.Println("Size:", size)
@@ -51,7 +51,7 @@ func ExamplePriority() {
5151
fmt.Println("Get:", elem)
5252

5353
// Output:
54-
// Contains 3: true
54+
// Contains 2: true
5555
// Size: 3
5656
// Empty before clear: false
5757
// Clear: [1 2 3 4]

0 commit comments

Comments
 (0)