Deluge Firmware 1.3.0
Build date: 2025.04.16
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 "util/containers.h"
21#include <cstdint>
22#include <limits>
23
24class Cluster;
25
26class ClusterPriorityQueue {
27 using Priority = uint32_t;
28
29public:
30 ClusterPriorityQueue() = default;
31
32 Error push(Cluster& cluster, uint32_t priorityRating);
33 constexpr Cluster& front() const { return *priority_map_.begin()->second; }
34
35 constexpr void pop() {
36 auto it = priority_map_.begin();
37 priority_map_.erase(it);
38 queued_clusters_.erase(it->second);
39 }
40
41 constexpr bool empty() const { return queued_clusters_.empty() || priority_map_.empty(); }
42 constexpr size_t erase(Cluster& cluster) {
43 if (auto search = queued_clusters_.find(&cluster); search != queued_clusters_.end()) {
44 priority_map_.erase({search->second, &cluster});
45 queued_clusters_.erase(&cluster);
46 return 1;
47 }
48 return 0;
49 }
50
51 /* This is currently a consequence of how priorities are calculated (using full 32bits) next-gen getPriorityRating
52 * should return an int32_t */
53 constexpr bool hasAnyLowestPriority() const {
54 return !priority_map_.empty() && priority_map_.rbegin()->first == std::numeric_limits<uint32_t>::max();
55 }
56
57private:
58 deluge::fast_set<std::pair<Priority, Cluster*>> priority_map_;
59 deluge::fast_unordered_map<Cluster*, Priority> queued_clusters_;
60};
Definition cluster.h:34