-
Notifications
You must be signed in to change notification settings - Fork 25
/
Queue.hxx
executable file
·119 lines (76 loc) · 2.96 KB
/
Queue.hxx
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
#pragma once
#include <type_traits> // move() ?
#include <cstddef> // size_t
#include <stdexcept> // out_of_range
#include <string> // to_string()
#include "Queue.hpp"
/*******************************************************************************
** Primary implementation
*******************************************************************************/
template<typename T, class UnderlyingContainer>
void Queue<T, UnderlyingContainer>::push( const T & element )
{
collection.prepend( element ); // insert at the front
}
template<typename T, class UnderlyingContainer>
T Queue<T, UnderlyingContainer>::pop()
{
// Note: zyBook returns the value popped, the C++ standard template library does not.
// popping an element from an empty queue error handling is handled by the underlying container
auto element = collection.back();
collection.removeBack();
return element;
}
template<typename T, class UnderlyingContainer>
T & Queue<T, UnderlyingContainer>::top()
{
// Note: viewing an element from an empty queue error handling is handled by the underlying container
return collection.back();
}
template<typename T, class UnderlyingContainer>
bool Queue<T, UnderlyingContainer>::empty()
{
return collection.empty();
}
template<typename T, class UnderlyingContainer>
std::size_t Queue<T, UnderlyingContainer>::size()
{
return collection.size();
}
/*******************************************************************************
** Partial Specialization for a fixed sized Queue over a fixed sized standard array
*******************************************************************************/
template<typename T, std::size_t CAPACITY>
void Queue<T, std::array<T, CAPACITY>>::push( const T & element )
{
if( _size >= CAPACITY ) throw std::out_of_range( "ERROR: Attempt to add to an already full queue of " + std::to_string( CAPACITY ) + " elements." );
collection[_rear] = element;
_rear = ( _rear + 1 ) % CAPACITY;
++_size;
}
template<typename T, std::size_t CAPACITY>
T Queue<T, std::array<T, CAPACITY>>::pop()
{
if( empty() ) throw std::out_of_range( "ERROR: Attempt to remove an element from an empty queue" );
auto temp = std::move( collection[_front] );
_front = ( _front + 1 ) % CAPACITY;
--_size;
// Note, zyBook returns the value popped, the C++ standard template library does not.
return temp;
}
template<typename T, std::size_t CAPACITY>
T & Queue<T, std::array<T, CAPACITY>>::top()
{
if( empty() ) throw std::out_of_range( "ERROR: Attempt to view an element from an empty queue" );
return collection[_front];
}
template<typename T, std::size_t CAPACITY>
bool Queue<T, std::array<T, CAPACITY>>::empty()
{
return _size == 0;
}
template<typename T, std::size_t CAPACITY>
std::size_t Queue<T, std::array<T, CAPACITY>>::size()
{
return _size;
}