SyntaxTutor
Educational app designed to help compiler students understand LL(1) and SLR(1) parsing algorithms.
Loading...
Searching...
No Matches
UniqueQueue.h
Go to the documentation of this file.
1/*
2 * SyntaxTutor - Interactive Tutorial About Syntax Analyzers
3 * Copyright (C) 2025 Jose R. (jose-rzm)
4 *
5 * This program is free software: you can redistribute it and/or modify it
6 * 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 <https://www.gnu.org/licenses/>.
17 */
18
19#ifndef UNIQUEQUEUE_H
20#define UNIQUEQUEUE_H
21#include <queue>
22#include <unordered_set>
23
37template <typename T> class UniqueQueue {
38 public:
43 void push(const T& value) {
44 if (seen_.insert(value).second) {
45 queue_.push(value);
46 }
47 }
48
52 void pop() {
53 if (!queue_.empty()) {
54 queue_.pop();
55 }
56 }
57
62 const T& front() const { return queue_.front(); }
63
68 bool empty() const { return queue_.empty(); }
69
73 void clear() {
74 while (!queue_.empty())
75 queue_.pop();
76 seen_.clear();
77 }
78
79 private:
80 std::queue<T> queue_;
81 std::unordered_set<T> seen_;
82};
83#endif // UNIQUEQUEUE_H
A queue that ensures each element is inserted only once.
Definition UniqueQueue.h:37
void clear()
Clears the queue and the set of seen elements.
Definition UniqueQueue.h:73
const T & front() const
Accesses the front element of the queue.
Definition UniqueQueue.h:62
void pop()
Removes the front element from the queue.
Definition UniqueQueue.h:52
void push(const T &value)
Pushes an element to the queue if it hasn't been inserted before.
Definition UniqueQueue.h:43
bool empty() const
Checks whether the queue is empty.
Definition UniqueQueue.h:68