Uniot Core
0.8.1
Loading...
Searching...
No Matches
IterableQueue.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2024 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include "ClearQueue.h"
22
35template <typename T>
36class IterableQueue : public ClearQueue<T> {
37 public:
43 void begin() const;
44
51 bool isEnd() const;
52
61 const T& next() const;
62
68 const T& current() const;
69
81
82 protected:
90};
92
93template <typename T>
97
98template <typename T>
100 return !mCurrent;
101}
102
103template <typename T>
104const T& IterableQueue<T>::next() const {
105 auto prevCurrent = mCurrent;
106 mCurrent = mCurrent->next;
107 return prevCurrent->value;
108}
109
110template <typename T>
112 return mCurrent->value;
113}
114
115template <typename T>
117 if (!mCurrent) {
118 return false;
119 }
120
124 return true;
125 }
126
128 while (prev && prev->next != mCurrent) {
129 prev = prev->next;
130 }
131
132 if (!prev) {
133 return false;
134 }
135
136 auto nextNode = mCurrent->next;
137 prev->next = nextNode;
138
139 if (!nextNode) {
141 }
142
143 delete mCurrent;
144 mCurrent = nextNode;
145
146 return true;
147}
ClearQueue(ClearQueue const &)=delete
Deleted copy constructor to prevent copying.
T hardPop()
Removes and returns the element at the front of the queue.
Definition ClearQueue.h:221
struct ClearQueue::node * pnode
Node structure for the linked list implementation.
pnode mTail
Pointer to the last node in the queue.
Definition ClearQueue.h:182
pnode mHead
Pointer to the first node in the queue.
Definition ClearQueue.h:181
Definition IterableQueue.h:36
const T & next() const
Move to the next element and return the current element.
Definition IterableQueue.h:104
void begin() const
Initialize the iterator to the beginning of the queue.
Definition IterableQueue.h:94
bool isEnd() const
Check if the iterator has reached the end of the queue.
Definition IterableQueue.h:99
ClearQueue< T >::pnode mCurrent
Pointer to the current node during iteration.
Definition IterableQueue.h:89
const T & current() const
Access the current element without moving the iterator.
Definition IterableQueue.h:111
bool deleteCurrent()
Remove the current element from the queue.
Definition IterableQueue.h:116
node * next
Pointer to the next node.
Definition ClearQueue.h:178