1 /*========================== begin_copyright_notice ============================
2 
3 Copyright (C) 2019-2021 Intel Corporation
4 
5 SPDX-License-Identifier: MIT
6 
7 ============================= end_copyright_notice ===========================*/
8 
9 #pragma once
10 
11 #include "LinkedList.h"
12 
13 namespace iSTD
14 {
15 
16 /*****************************************************************************\
17 Template Parameters
18 \*****************************************************************************/
19 #define QueueTemplateList   class Type, class CAllocatorType
20 #define CQueueType          CQueue<Type,CAllocatorType>
21 
22 /*****************************************************************************\
23 
24 Class:
25     CQueue
26 
27 Description:
28     Implements an linked-list-based queue
29 
30 \*****************************************************************************/
31 template<QueueTemplateList>
32 class CQueue : public CLinkedListType
33 {
34 public:
35 
36     bool    Push( const Type element );
37     Type    Pop( void );
38     Type    Top( void ) const;
39 };
40 
41 /*****************************************************************************\
42 
43 Function:
44     CQueue::Push
45 
46 Description:
47     Pushes an element on the queue
48 
49 Input:
50     const Type element
51 
52 Output:
53     bool - success or fail
54 
55 \*****************************************************************************/
56 template<QueueTemplateList>
Push(const Type element)57 bool CQueueType::Push( const Type element )
58 {
59     // Add element to the top of list
60     return this->Add( element );
61 }
62 
63 /*****************************************************************************\
64 
65 Function:
66     CQueue::Pop
67 
68 Description:
69     Pops an element off the queue
70 
71 Input:
72     none
73 
74 Output:
75     Type - element
76 
77 \*****************************************************************************/
78 template<QueueTemplateList>
Pop(void)79 Type CQueueType::Pop( void )
80 {
81     Type element = {0};
82 
83     if( this->IsEmpty() )
84     {
85         ASSERT(0);
86     }
87     else
88     {
89         // Get the last element on the list
90         typename CQueue::CIterator end = this->End();
91         --end;
92 
93         element = *end;
94 
95         // Remove the last element
96         this->Remove( end );
97     }
98 
99     return element;
100 }
101 
102 /*****************************************************************************\
103 
104 Function:
105     CQueue::Top
106 
107 Description:
108     Returns the top element of the queue
109 
110 Input:
111     none
112 
113 Output:
114     Type - element
115 
116 \*****************************************************************************/
117 template<QueueTemplateList>
Top(void)118 Type CQueueType::Top( void ) const
119 {
120     Type element = {0};
121 
122     if( this->IsEmpty() )
123     {
124         ASSERT(0);
125     }
126     else
127     {
128         // Get the last element on the list
129         typename CQueueType::CConstIterator end = this->End();
130         --end;
131 
132         element = *end;
133     }
134 
135     return element;
136 }
137 
138 } // iSTD
139