-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from modern-dev/queue
feat: queue implementation, extend deque API. #patch
- Loading branch information
Showing
4 changed files
with
215 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2020. The GTL Authors. All rights reserved. | ||
// https://github.com/modern-dev/gtl | ||
// Use of this source code is governed by the MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
package gtl | ||
|
||
// Queue is a container adapter that gives the programmer the functionality of a queue | ||
// - specifically, a FIFO (first-in, first-out) data structure. | ||
type Queue[T any] struct { | ||
deque *Deque[T] | ||
} | ||
|
||
// NewQueue creates new empty Queue of given type | ||
func NewQueue[T any]() *Queue[T] { | ||
return &Queue[T]{deque: NewDeque[T]()} | ||
} | ||
|
||
// Len returns number of elements in queue | ||
// Complexity O(1) | ||
func (q *Queue[T]) Len() int { | ||
return q.deque.Len() | ||
} | ||
|
||
// IsEmpty checks if Queue has no element | ||
// Complexity O(1) | ||
func (q *Queue[T]) IsEmpty() bool { | ||
return q.deque.IsEmpty() | ||
} | ||
|
||
// NotEmpty checks if Queue has elements | ||
// Complexity O(1) | ||
func (q *Queue[T]) NotEmpty() bool { | ||
return q.deque.NotEmpty() | ||
} | ||
|
||
// Peek returns value of the first element in Queue | ||
// Complexity O(1) | ||
func (q *Queue[T]) Peek() T { | ||
return q.deque.Front() | ||
} | ||
|
||
// Enqueue add element in front of queue | ||
// Complexity O(1) | ||
func (q *Queue[T]) Enqueue(element T) { | ||
q.deque.EnqueueFront(element) | ||
} | ||
|
||
// Enqueue removes and returns first element of queue | ||
// Complexity O(1) | ||
func (q *Queue[T]) Dequeue() T { | ||
return q.deque.DequeueFront() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package gtl | ||
|
||
import "testing" | ||
|
||
const queueEnqueuesCount = 300 | ||
|
||
func TestNewQueue(t *testing.T) { | ||
structQueue := NewQueue[struct{}]() | ||
intQueue := NewQueue[int]() | ||
sliceQueue := NewQueue[[]float64]() | ||
|
||
checkQueueSize(structQueue, 0, t) | ||
checkQueueSize(intQueue, 0, t) | ||
checkQueueSize(sliceQueue, 0, t) | ||
} | ||
|
||
func TestEnqueue(t *testing.T) { | ||
queue := NewQueue[int]() | ||
|
||
for i := 0; i < queueEnqueuesCount; i++ { | ||
queue.Enqueue(i) | ||
checkQueueSize(queue, i+1, t) | ||
} | ||
} | ||
|
||
func TestDequeue(t *testing.T) { | ||
queue := NewQueue[int]() | ||
|
||
queue.Enqueue(1) | ||
el := queue.Dequeue() | ||
|
||
checkQueueSize(queue, 0, t) | ||
if el != 1 { | ||
t.Errorf("Expected to get %d, got %d", 1, el) | ||
} | ||
} | ||
|
||
func TestPeek(t *testing.T) { | ||
queue := NewQueue[int]() | ||
|
||
queue.Enqueue(1) | ||
el := queue.Peek() | ||
|
||
checkQueueSize(queue, 1, t) | ||
if el != 1 { | ||
t.Errorf("Expected to get %d, got %d", 1, el) | ||
} | ||
} | ||
|
||
func TestQueue(t *testing.T) { | ||
queue := NewQueue[float64]() | ||
|
||
for i := 0; i < queueEnqueuesCount; i++ { | ||
queue.Enqueue(float64(i)) | ||
} | ||
|
||
checkQueueSize(queue, queueEnqueuesCount, t) | ||
|
||
if queue.NotEmpty() != true { | ||
t.Errorf("Queue should not be empty") | ||
} | ||
|
||
for i := queueEnqueuesCount; i > 0; i-- { | ||
queue.Dequeue() | ||
} | ||
|
||
if queue.IsEmpty() != true { | ||
t.Errorf("Deque should be empty") | ||
} | ||
} | ||
|
||
func checkQueueSize[T any](queue *Queue[T], expected int, t *testing.T) { | ||
if queue.Len() != expected { | ||
t.Errorf("Queue should have size %d but got %d", expected, queue.Len()) | ||
} | ||
} |