Deluge Firmware 1.3.0
Build date: 2026.11.28
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 "OSLikeStuff/timers_interrupts/timers_interrupts.h"
21#include "cluster.h"
22#include "util/containers.h"
23#include <algorithm>
24#include <cstdint>
25#include <optional>
26#include <ranges>
27
28using Priority = uint32_t;
29using qcluster = std::pair<Priority, Cluster*>;
30
37namespace ClusterPriority {
38constexpr Priority kSampleStart = 0xFFFFFFFD;
39constexpr Priority kSpeculative = 0xFFFFFFFE;
40constexpr Priority kRetryAfterCardError = 0xFFFFFFFF;
41
44constexpr Priority forAudioClip(VoicePriority voicePriority) {
45 return static_cast<Priority>(3 - util::to_underlying(voicePriority)) << 30;
46}
47} // namespace ClusterPriority
48
49// comparator returns true if elements need to be swapped
50class Compare {
51public:
52 bool operator()(const qcluster& first, const qcluster& second) const { return first.first > second.first; }
53};
54
57class ClusterPriorityQueue : private std::priority_queue<qcluster, deluge::fast_vector<qcluster>, Compare> {
58
59public:
60 Error enqueueCluster(Cluster& cluster, uint32_t priorityRating, const InterruptsDisabled&) {
61 this->push(std::pair(priorityRating, &cluster));
62 return Error::NONE;
63 }
64
66 [[nodiscard]] bool empty() const { return this->c.empty(); }
67
72 std::optional<qcluster> popForLoading(const InterruptsDisabled&) {
73 while (!this->c.empty()) {
74 qcluster entry = this->top();
75 this->pop();
76 Cluster* cluster = entry.second;
77 if (cluster != nullptr && !cluster->unloadable && cluster->numReasonsToBeLoaded > 0) {
78 return entry;
79 }
80 }
81 return std::nullopt;
82 }
83
86 [[nodiscard]] bool hasAnyWithPriority(Priority priority, const InterruptsDisabled&) const {
87 return std::ranges::any_of(
88 this->c, [priority](const qcluster& entry) { return entry.second != nullptr && entry.first == priority; });
89 }
90
95 bool raisePriority(const Cluster* cluster, Priority priorityRating, const InterruptsDisabled&) {
96 bool raised = false;
97 for (auto& [priority, val] : this->c) {
98 if (val == cluster && priorityRating < priority) {
99 priority = priorityRating;
100 raised = true;
101 }
102 }
103 if (raised) {
104 std::ranges::make_heap(this->c, this->comp);
105 }
106 return raised;
107 }
108
111 std::optional<Priority> take(const Cluster* cluster, const InterruptsDisabled&) {
112 std::optional<Priority> taken;
113 for (auto& [priority, val] : this->c) {
114 if (val == cluster) {
115 if (!taken || priority < *taken) {
116 taken = priority;
117 }
118 val = nullptr;
119 }
120 }
121 return taken;
122 }
123
124 bool erase(const Cluster* cluster, const InterruptsDisabled&) {
125 for (auto& val : this->c | std::views::values) {
126 if (val == cluster) {
127 val = nullptr;
128 return true;
129 }
130 }
131 return false;
132 };
133};
Definition cluster_priority_queue.h:57
std::optional< qcluster > popForLoading(const InterruptsDisabled &)
Takes the highest priority Cluster that still wants loading off the queue. For the SD interrupt,...
Definition cluster_priority_queue.h:72
bool empty() const
Not exact unless interrupts are disabled, as the SD interrupt may be taking one off.
Definition cluster_priority_queue.h:66
bool hasAnyWithPriority(Priority priority, const InterruptsDisabled &) const
Whether any Cluster is queued at exactly priority. Has to check every entry, as the heap only keeps t...
Definition cluster_priority_queue.h:86
bool raisePriority(const Cluster *cluster, Priority priorityRating, const InterruptsDisabled &)
If cluster is queued at a lower priority than priorityRating, moves it up to that.
Definition cluster_priority_queue.h:95
std::optional< Priority > take(const Cluster *cluster, const InterruptsDisabled &)
Takes cluster out of the queue, e.g. to load it right away.
Definition cluster_priority_queue.h:111
Definition cluster.h:34
Definition cluster_priority_queue.h:50
Definition cluster_priority_queue.h:37
constexpr Priority forAudioClip(VoicePriority voicePriority)
Definition cluster_priority_queue.h:44