Skip to content

Commit da429eb

Browse files
authored
Merge pull request #202 from emirpasic/development
Implements queues, LinkedListQueue and ArrayQueue
2 parents 7815e7d + 5b23854 commit da429eb

17 files changed

+1447
-7
lines changed

README.md

+92-7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ Implementation of various data structures and algorithms in Go.
3636
- [AVLTree](#avltree)
3737
- [BTree](#btree)
3838
- [BinaryHeap](#binaryheap)
39+
- [Queues](#queues)
40+
- [LinkedListQueue](#linkedlistqueue)
41+
- [ArrayQueue](#arrayqueue)
3942
- [Functions](#functions)
4043
- [Comparator](#comparator)
4144
- [Iterator](#iterator)
@@ -94,6 +97,9 @@ Containers are either ordered or unordered. All ordered containers provide [stat
9497
| | [AVLTree](#avltree) | yes | yes* | no | key |
9598
| | [BTree](#btree) | yes | yes* | no | key |
9699
| | [BinaryHeap](#binaryheap) | yes | yes* | no | index |
100+
| [Queues](#queues) |
101+
| | [LinkedListQueue](#linkedlistqueue) | yes | yes | no | index |
102+
| | [ArrayQueue](#arrayqueue) | yes | yes* | no | index |
97103
| | | | <sub><sup>*reversible</sup></sub> | | <sub><sup>*bidirectional</sup></sub> |
98104

99105
### Lists
@@ -126,7 +132,7 @@ type List interface {
126132

127133
A [list](#lists) backed by a dynamic array that grows and shrinks implicitly.
128134

129-
Implements [List](#lists), [IteratorWithIndex](#iteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
135+
Implements [List](#lists), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
130136

131137
```go
132138
package main
@@ -200,7 +206,7 @@ func main() {
200206

201207
A [list](#lists) where each element points to the next and previous elements in the list.
202208

203-
Implements [List](#lists), [IteratorWithIndex](#iteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
209+
Implements [List](#lists), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
204210

205211
```go
206212
package main
@@ -290,7 +296,7 @@ func main() {
290296

291297
A [set](#sets) backed by a [red-black tree](#redblacktree) to keep the elements ordered with respect to the [comparator](#comparator).
292298

293-
Implements [Set](#sets), [IteratorWithIndex](#iteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
299+
Implements [Set](#sets), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
294300

295301
```go
296302
package main
@@ -317,7 +323,7 @@ func main() {
317323

318324
A [set](#sets) that preserves insertion-order. Data structure is backed by a hash table to store values and [doubly-linked list](#doublylinkedlist) to store insertion ordering.
319325

320-
Implements [Set](#sets), [IteratorWithIndex](#iteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
326+
Implements [Set](#sets), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [EnumerableWithIndex](#enumerablewithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
321327

322328
```go
323329
package main
@@ -479,7 +485,7 @@ func main() {
479485

480486
A [map](#maps) based on [red-black tree](#redblacktree). Keys are ordered with respect to the [comparator](#comparator).
481487

482-
Implements [Map](#maps), [IteratorWithKey](#iteratorwithkey), [EnumerableWithKey](#enumerablewithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
488+
Implements [Map](#maps), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [EnumerableWithKey](#enumerablewithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
483489

484490
```go
485491
package main
@@ -510,7 +516,7 @@ func main() {
510516

511517
A [map](#maps) that preserves insertion-order. It is backed by a hash table to store values and [doubly-linked list](doublylinkedlist) to store ordering.
512518

513-
Implements [Map](#maps), [IteratorWithKey](#iteratorwithkey), [EnumerableWithKey](#enumerablewithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
519+
Implements [Map](#maps), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [EnumerableWithKey](#enumerablewithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
514520

515521
```go
516522
package main
@@ -567,7 +573,7 @@ func main() {
567573

568574
A [map](#maps) based on red-black tree. This map guarantees that the map will be in both ascending key and value order. Other than key and value ordering, the goal with this structure is to avoid duplication of elements (unlike in [HashBidiMap](#hashbidimap)), which can be significant if contained elements are large.
569575

570-
Implements [BidiMap](#maps), [IteratorWithKey](#iteratorwithkey), [EnumerableWithKey](#enumerablewithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
576+
Implements [BidiMap](#maps), [ReverseIteratorWithIndex](#reverseiteratorwithindex), [EnumerableWithKey](#enumerablewithkey), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
571577

572578
```go
573579
package main
@@ -864,6 +870,85 @@ func main() {
864870
}
865871
```
866872

873+
### Queues
874+
875+
A queue that represents a first-in-first-out (FIFO) data structure. The usual enqueue and dequeue operations are provided, as well as a method to peek at the first item in the queue.
876+
877+
<p align="center"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Data_Queue.svg/300px-Data_Queue.svg.png" width="200px" height="120px" /></p>
878+
879+
Implements [Container](#containers) interface.
880+
881+
```go
882+
type Queue interface {
883+
Enqueue(value interface{})
884+
Dequeue() (value interface{}, ok bool)
885+
Peek() (value interface{}, ok bool)
886+
887+
containers.Container
888+
// Empty() bool
889+
// Size() int
890+
// Clear()
891+
// Values() []interface{}
892+
// String() string
893+
}
894+
```
895+
896+
#### LinkedListQueue
897+
898+
A [queue](#queues) based on a [linked list](#singlylinkedlist).
899+
900+
Implements [Queue](#queues), [IteratorWithIndex](#iteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
901+
902+
```go
903+
package main
904+
905+
import llq "github.com/emirpasic/gods/queues/linkedlistqueue"
906+
907+
// LinkedListQueueExample to demonstrate basic usage of LinkedListQueue
908+
func main() {
909+
queue := llq.New() // empty
910+
queue.Enqueue(1) // 1
911+
queue.Enqueue(2) // 1, 2
912+
_ = queue.Values() // 1, 2 (FIFO order)
913+
_, _ = queue.Peek() // 1,true
914+
_, _ = queue.Dequeue() // 1, true
915+
_, _ = queue.Dequeue() // 2, true
916+
_, _ = queue.Dequeue() // nil, false (nothing to deque)
917+
queue.Enqueue(1) // 1
918+
queue.Clear() // empty
919+
queue.Empty() // true
920+
_ = queue.Size() // 0
921+
}
922+
```
923+
924+
#### ArrayQueue
925+
926+
A [queue](#queues) based on a [array list](#arraylist).
927+
928+
Implements [Queue](#queues), [ReverseIteratorWithIndex](#iteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
929+
930+
```go
931+
package main
932+
933+
import aq "github.com/emirpasic/gods/queues/arrayqueue"
934+
935+
// ArrayQueueExample to demonstrate basic usage of ArrayQueue
936+
func main() {
937+
queue := aq.New() // empty
938+
queue.Enqueue(1) // 1
939+
queue.Enqueue(2) // 1, 2
940+
_ = queue.Values() // 1, 2 (FIFO order)
941+
_, _ = queue.Peek() // 1,true
942+
_, _ = queue.Dequeue() // 1, true
943+
_, _ = queue.Dequeue() // 2, true
944+
_, _ = queue.Dequeue() // nil, false (nothing to deque)
945+
queue.Enqueue(1) // 1
946+
queue.Clear() // empty
947+
queue.Empty() // true
948+
_ = queue.Size() // 0
949+
}
950+
```
951+
867952
## Functions
868953

869954
Various helper functions used throughout the library.

examples/arrayqueue/arrayqqueue.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2015, Emir Pasic. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import aq "github.com/emirpasic/gods/queues/arrayqueue"
8+
9+
// ArrayQueueExample to demonstrate basic usage of ArrayQueue
10+
func main() {
11+
queue := aq.New() // empty
12+
queue.Enqueue(1) // 1
13+
queue.Enqueue(2) // 1, 2
14+
_ = queue.Values() // 1, 2 (FIFO order)
15+
_, _ = queue.Peek() // 1,true
16+
_, _ = queue.Dequeue() // 1, true
17+
_, _ = queue.Dequeue() // 2, true
18+
_, _ = queue.Dequeue() // nil, false (nothing to deque)
19+
queue.Enqueue(1) // 1
20+
queue.Clear() // empty
21+
queue.Empty() // true
22+
_ = queue.Size() // 0
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2015, Emir Pasic. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import llq "github.com/emirpasic/gods/queues/linkedlistqueue"
8+
9+
// LinkedListQueueExample to demonstrate basic usage of LinkedListQueue
10+
func main() {
11+
queue := llq.New() // empty
12+
queue.Enqueue(1) // 1
13+
queue.Enqueue(2) // 1, 2
14+
_ = queue.Values() // 1, 2 (FIFO order)
15+
_, _ = queue.Peek() // 1,true
16+
_, _ = queue.Dequeue() // 1, true
17+
_, _ = queue.Dequeue() // 2, true
18+
_, _ = queue.Dequeue() // nil, false (nothing to deque)
19+
queue.Enqueue(1) // 1
20+
queue.Clear() // empty
21+
queue.Empty() // true
22+
_ = queue.Size() // 0
23+
}

lists/lists.go

+1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ type List interface {
3030
// Size() int
3131
// Clear()
3232
// Values() []interface{}
33+
// String() string
3334
}

maps/maps.go

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Map interface {
2929
// Size() int
3030
// Clear()
3131
// Values() []interface{}
32+
// String() string
3233
}
3334

3435
// BidiMap interface that all bidirectional maps implement (extends the Map interface)

queues/arrayqueue/arrayqueue.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) 2021, Aryan Ahadinia. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package arrayqueue implements a queue backed by array list.
6+
//
7+
// Structure is not thread safe.
8+
//
9+
// Reference: https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
10+
package arrayqueue
11+
12+
import (
13+
"fmt"
14+
"strings"
15+
16+
"github.com/emirpasic/gods/lists/arraylist"
17+
"github.com/emirpasic/gods/queues"
18+
)
19+
20+
// Assert Queue implementation
21+
var _ queues.Queue = (*Queue)(nil)
22+
23+
// Queue holds elements in an array-list
24+
type Queue struct {
25+
list *arraylist.List
26+
}
27+
28+
// New instantiates a new empty queue
29+
func New() *Queue {
30+
return &Queue{list: arraylist.New()}
31+
}
32+
33+
// Enqueue adds a value to the end of the queue
34+
func (queue *Queue) Enqueue(value interface{}) {
35+
queue.list.Add(value)
36+
}
37+
38+
// Dequeue removes first element of the queue and returns it, or nil if queue is empty.
39+
// Second return parameter is true, unless the queue was empty and there was nothing to dequeue.
40+
func (queue *Queue) Dequeue() (value interface{}, ok bool) {
41+
value, ok = queue.list.Get(0)
42+
if ok {
43+
queue.list.Remove(0)
44+
}
45+
return
46+
}
47+
48+
// Peek returns first element of the queue without removing it, or nil if queue is empty.
49+
// Second return parameter is true, unless the queue was empty and there was nothing to peek.
50+
func (queue *Queue) Peek() (value interface{}, ok bool) {
51+
return queue.list.Get(0)
52+
}
53+
54+
// Empty returns true if queue does not contain any elements.
55+
func (queue *Queue) Empty() bool {
56+
return queue.list.Empty()
57+
}
58+
59+
// Size returns number of elements within the queue.
60+
func (queue *Queue) Size() int {
61+
return queue.list.Size()
62+
}
63+
64+
// Clear removes all elements from the queue.
65+
func (queue *Queue) Clear() {
66+
queue.list.Clear()
67+
}
68+
69+
// Values returns all elements in the queue (FIFO order).
70+
func (queue *Queue) Values() []interface{} {
71+
return queue.list.Values()
72+
}
73+
74+
// String returns a string representation of container
75+
func (queue *Queue) String() string {
76+
str := "ArrayQueue\n"
77+
values := []string{}
78+
for _, value := range queue.list.Values() {
79+
values = append(values, fmt.Sprintf("%v", value))
80+
}
81+
str += strings.Join(values, ", ")
82+
return str
83+
}
84+
85+
// Check that the index is within bounds of the list
86+
func (queue *Queue) withinRange(index int) bool {
87+
return index >= 0 && index < queue.list.Size()
88+
}

0 commit comments

Comments
 (0)