-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
71 lines (61 loc) · 1.55 KB
/
queue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package goaster
import (
"fmt"
"strings"
)
type Queue struct {
elements []Toast
}
func NewQueue() *Queue {
return &Queue{elements: make([]Toast, 0)}
}
// Enqueue adds an element to the end of the queue.
func (q *Queue) Enqueue(toast Toast) {
q.elements = append(q.elements, toast)
}
// Dequeue removes and returns the first element of the queue.
// If the queue is empty, it returns -1 and an error.
func (q *Queue) Dequeue() (Toast, error) {
if len(q.elements) == 0 {
return Toast{}, fmt.Errorf("queue is empty")
}
element := q.elements[0]
q.elements = q.elements[1:]
return element, nil
}
// IsEmpty checks if the queue is empty.
func (q *Queue) IsEmpty() bool {
return len(q.elements) == 0
}
// Size returns the number of elements in the queue.
func (q *Queue) Size() int {
return len(q.elements)
}
// String returns a string representation of the queue.
func (q *Queue) String() string {
var sb strings.Builder
sb.WriteString("Queue: front [")
for i, e := range q.elements {
sb.WriteString(fmt.Sprintf("%v", e))
if i < len(q.elements)-1 {
sb.WriteString(", ")
}
}
sb.WriteString("] rear")
return sb.String()
}
// GetMessagesAndDequeue returns a slice of Toast messages from the queue.
// It dequeues messages from the queue until it's empty.
// **NOTE**: in a-h/templ only `for...range` is a valid `for`loop.
func (q *Queue) GetMessagesAndDequeue() []Toast {
var messages []Toast
for q.Size() > 0 {
item, err := q.Dequeue()
if err != nil {
fmt.Println("Error:", err)
break
}
messages = append(messages, item)
}
return messages
}