LAL: Linear Arrangement Library 23.01.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
linear_queue.hpp
1/*********************************************************************
2 *
3 * Linear Arrangement Library - A library that implements a collection
4 * algorithms for linear arrangments of graphs.
5 *
6 * Copyright (C) 2019 - 2023
7 *
8 * This file is part of Linear Arrangement Library. The full code is available
9 * at:
10 * https://github.com/LAL-project/linear-arrangement-library.git
11 *
12 * Linear Arrangement Library is free software: you can redistribute it
13 * and/or modify it under the terms of the GNU Affero General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * Linear Arrangement Library is distributed in the hope that it will be
18 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with Linear Arrangement Library. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * Contact:
26 *
27 * LluĂ­s Alemany Puig (lalemany@cs.upc.edu)
28 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
29 * CQL (Complexity and Quantitative Linguistics Lab)
30 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
31 * Webpage: https://cqllab.upc.edu/people/lalemany/
32 *
33 * Ramon Ferrer i Cancho (rferrericancho@cs.upc.edu)
34 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
35 * CQL (Complexity and Quantitative Linguistics Lab)
36 * Office S124, Omega building
37 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
38 * Webpage: https://cqllab.upc.edu/people/rferrericancho/
39 *
40 ********************************************************************/
41
42#pragma once
43
44// C++ includes
45#if defined DEBUG
46#include <cassert>
47#endif
48
49// lal includes
50#include <lal/detail/data_array.hpp>
51
52namespace lal {
53namespace detail {
54
68template <class T>
70public:
72 void init(std::size_t n) noexcept {
73 m_queue.resize(n);
74 m_left = 0;
75 m_right = 0;
76 }
77
79 void push(const T& v) noexcept {
80#if defined DEBUG
81 assert(not is_full());
82#endif
83 m_queue[m_right++] = v;
84 }
86 void push(T&& v) noexcept {
87#if defined DEBUG
88 assert(not is_exhausted());
89#endif
90 m_queue[m_right++] = std::move(v);
91 }
98 T&& pop() noexcept {
99#if defined DEBUG
100 assert(m_left < m_queue.size());
101#endif
102 return std::move(m_queue[m_left++]);
103 }
104
106 T& front() noexcept {
107#if defined DEBUG
108 assert(not is_exhausted());
109#endif
110 return m_queue[m_left];
111 }
112
114 const T& front() const noexcept {
115#if defined DEBUG
116 assert(not is_exhausted());
117#endif
118 return m_queue[m_left];
119 }
121 std::size_t size() const noexcept {
122 return m_right - m_left;
123 }
124
131 void reset() noexcept {
132 m_left = m_right = 0;
133 }
134
142 bool is_exhausted() const noexcept { return m_left == m_queue.size(); }
143
151 bool is_full() const noexcept { return m_right == m_queue.size(); }
152
154 T *begin() noexcept { return m_queue.begin() + m_left; }
156 const T *begin() const noexcept { return m_queue.begin() + m_left; }
158 T *end() noexcept { return m_queue.begin() + m_right; }
160 const T *end() const noexcept { return m_queue.begin() + m_right; }
161
162private:
165
167 std::size_t m_left;
169 std::size_t m_right;
170};
171
172} // -- namespace detail
173} // -- namespace lal
Simple array-like fixed-size queue.
Definition: linear_queue.hpp:69
T && pop() noexcept
Pops the first element of the queue.
Definition: linear_queue.hpp:98
void reset() noexcept
Makes the queue usable again.
Definition: linear_queue.hpp:131
std::size_t m_right
Right pointer to m_queue.
Definition: linear_queue.hpp:169
std::size_t size() const noexcept
Returns the size of the queue.
Definition: linear_queue.hpp:121
const T * end() const noexcept
Constant pointer to end.
Definition: linear_queue.hpp:160
void push(const T &v) noexcept
Insert a new element to the queue.
Definition: linear_queue.hpp:79
bool is_full() const noexcept
Is the queue full?
Definition: linear_queue.hpp:151
T & front() noexcept
Returns a reference to the front element.
Definition: linear_queue.hpp:106
void init(std::size_t n) noexcept
Initializes the queue to hold n elements.
Definition: linear_queue.hpp:72
const T * begin() const noexcept
Constant pointer to begin.
Definition: linear_queue.hpp:156
void push(T &&v) noexcept
Insert a new element to the queue.
Definition: linear_queue.hpp:86
std::size_t m_left
Left pointer to m_queue.
Definition: linear_queue.hpp:167
T * begin() noexcept
Pointer to begin.
Definition: linear_queue.hpp:154
const T & front() const noexcept
Returns a constant reference to the front element.
Definition: linear_queue.hpp:114
bool is_exhausted() const noexcept
Has the queue exhausted its resources?
Definition: linear_queue.hpp:142
data_array< T > m_queue
Data (array) of the queue.
Definition: linear_queue.hpp:164
T * end() noexcept
Pointer to end.
Definition: linear_queue.hpp:158
Main namespace of the library.
Definition: basic_types.hpp:50
Wrapper of a C array for autmatic deallocation of memory.
Definition: data_array.hpp:59