Deluge Firmware
1.3.0
Build date: 2026.07.29
Toggle main menu visibility
Loading...
Searching...
No Matches
cluster_priority_queue.h
1
/*
2
* Copyright © 2015-2023 Synthstrom Audible Limited
3
*
4
* This file is part of The Synthstrom Audible Deluge Firmware.
5
*
6
* The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7
* terms of the GNU General Public License as published by the Free Software Foundation,
8
* either version 3 of the License, or (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
* See the GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License along with this program.
15
* If not, see <https://www.gnu.org/licenses/>.
16
*/
17
18
#pragma once
19
20
#include "cluster.h"
21
#include "util/containers.h"
22
#include <cstdint>
23
#include <limits>
24
#include <ranges>
25
26
using
Priority = uint32_t;
27
using
qcluster = std::pair<Priority, Cluster*>;
28
29
// comparator returns true if elements need to be swapped
30
class
Compare
{
31
public
:
32
bool
operator()(
const
qcluster& first,
const
qcluster& second)
const
{
return
first.first > second.first; }
33
};
34
class
ClusterPriorityQueue
:
public
std::priority_queue<qcluster, deluge::fast_vector<qcluster>, Compare> {
35
36
public
:
37
Error enqueueCluster(
Cluster
& cluster, uint32_t priorityRating) {
38
this->push(std::pair(priorityRating, &cluster));
39
return
Error::NONE;
40
}
41
[[nodiscard]]
constexpr
Cluster
* front()
const
{
return
this->top().second; }
42
[[nodiscard]]
Cluster
* getNext() {
43
Cluster
* cluster =
nullptr
;
44
while
(!this->empty()) {
45
cluster = this->front();
46
this->pop();
47
if
(cluster !=
nullptr
) {
48
// if this cluster is still wanted then we'll return it
49
if
((!cluster->unloadable) && cluster->numReasonsToBeLoaded > 0) {
50
return
cluster;
51
}
52
// otherwise get rid of it and continue to the next one
53
cluster->destroy();
54
}
55
}
56
return
nullptr
;
57
}
58
59
/* This is currently a consequence of how priorities are calculated (using full 32bits) next-gen getPriorityRating
60
* should return an int32_t */
61
[[nodiscard]]
constexpr
bool
hasAnyLowestPriority()
const
{
62
return
!c.empty() && c.rbegin()->first == std::numeric_limits<uint32_t>::max();
63
}
64
65
bool
erase(
const
Cluster
* cluster) {
66
for
(
auto
& val : this->c | std::views::values) {
67
if
(val == cluster) {
68
val =
nullptr
;
69
return
true
;
70
}
71
}
72
return
false
;
73
};
74
};
ClusterPriorityQueue
Definition
cluster_priority_queue.h:34
Cluster
Definition
cluster.h:34
Compare
Definition
cluster_priority_queue.h:30