-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlinked.go
211 lines (162 loc) · 4 KB
/
linked.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package queue
import (
"encoding/json"
"sync"
)
var _ Queue[any] = (*Linked[any])(nil)
// node is an individual element of the linked list.
type node[T any] struct {
value T
next *node[T]
}
// Linked represents a data structure representing a queue that uses a
// linked list for its internal storage.
type Linked[T comparable] struct {
head *node[T] // first node of the queue.
tail *node[T] // last node of the queue.
size int // number of elements in the queue.
// nolint: revive
initialElements []T // initial elements with which the queue was created, allowing for a reset to its original state if needed.
// synchronization
lock sync.RWMutex
}
// NewLinked creates a new Linked containing the given elements.
func NewLinked[T comparable](elements []T) *Linked[T] {
queue := &Linked[T]{
head: nil,
tail: nil,
size: 0,
initialElements: make([]T, len(elements)),
}
copy(queue.initialElements, elements)
for _, element := range elements {
_ = queue.offer(element)
}
return queue
}
// Get retrieves and removes the head of the queue.
func (lq *Linked[T]) Get() (elem T, _ error) {
lq.lock.Lock()
defer lq.lock.Unlock()
if lq.isEmpty() {
return elem, ErrNoElementsAvailable
}
value := lq.head.value
lq.head = lq.head.next
lq.size--
if lq.isEmpty() {
lq.tail = nil
}
return value, nil
}
// Offer inserts the element into the queue.
func (lq *Linked[T]) Offer(value T) error {
lq.lock.Lock()
defer lq.lock.Unlock()
return lq.offer(value)
}
// offer inserts the element into the queue.
func (lq *Linked[T]) offer(value T) error {
newNode := &node[T]{value: value}
if lq.isEmpty() {
lq.head = newNode
} else {
lq.tail.next = newNode
}
lq.tail = newNode
lq.size++
return nil
}
// Reset sets the queue to its initial state.
func (lq *Linked[T]) Reset() {
lq.lock.Lock()
defer lq.lock.Unlock()
lq.head = nil
lq.tail = nil
lq.size = 0
for _, element := range lq.initialElements {
_ = lq.offer(element)
}
}
// Contains returns true if the queue contains the element.
func (lq *Linked[T]) Contains(value T) bool {
lq.lock.RLock()
defer lq.lock.RUnlock()
current := lq.head
for current != nil {
if current.value == value {
return true
}
current = current.next
}
return false
}
// Peek retrieves but does not remove the head of the queue.
func (lq *Linked[T]) Peek() (elem T, _ error) {
lq.lock.RLock()
defer lq.lock.RUnlock()
if lq.isEmpty() {
return elem, ErrNoElementsAvailable
}
return lq.head.value, nil
}
// Size returns the number of elements in the queue.
func (lq *Linked[T]) Size() int {
lq.lock.RLock()
defer lq.lock.RUnlock()
return lq.size
}
// IsEmpty returns true if the queue is empty, false otherwise.
func (lq *Linked[T]) IsEmpty() bool {
lq.lock.RLock()
defer lq.lock.RUnlock()
return lq.isEmpty()
}
// IsEmpty returns true if the queue is empty, false otherwise.
func (lq *Linked[T]) isEmpty() bool {
return lq.size == 0
}
// Iterator returns a channel that will be filled with the elements.
// It removes the elements from the queue.
func (lq *Linked[T]) Iterator() <-chan T {
ch := make(chan T)
elems := lq.Clear()
go func() {
for _, e := range elems {
ch <- e
}
close(ch)
}()
return ch
}
// Clear removes and returns all elements from the queue.
func (lq *Linked[T]) Clear() []T {
lq.lock.Lock()
defer lq.lock.Unlock()
elements := make([]T, 0, lq.size)
current := lq.head
for current != nil {
elements = append(elements, current.value)
next := current.next
current = next
}
// Clear the queue
lq.head = nil
lq.tail = nil
lq.size = 0
return elements
}
// MarshalJSON serializes the Linked queue to JSON.
func (lq *Linked[T]) MarshalJSON() ([]byte, error) {
lq.lock.RLock()
defer lq.lock.RUnlock()
// Traverse the linked list and collect elements.
elements := make([]T, 0, lq.size)
current := lq.head
for current != nil {
elements = append(elements, current.value)
current = current.next
}
// Marshal the elements slice to JSON.
return json.Marshal(elements)
}