SWMMVis  6.0.0-alpha.4
Qt6/C++ GIS-based graphical user interface for the SWMMVis engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
coarseningqueue.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
3#include <algorithm>
4#include <cstdint>
5#include <vector>
6
7namespace meshkit
8{
9// One live entry per vertex. Updating a neighbour cannot accumulate stale heap
10// entries. Tile order retains locality; lower importance/density comes first,
11// with vertex id as a deterministic final tie. Thread confined, no callbacks.
13{
14public:
15 struct Entry {
16 float importance = 0, density = 0;
17 int vertex = -1;
18 std::uint32_t tile = 0;
19 };
20 explicit CoarseningQueue(std::size_t vertices, std::size_t maximumEntries = 0)
21 : positions(vertices, -1)
22 {
23 heap.reserve(std::min(vertices, maximumEntries));
24 }
25 bool empty() const { return heap.empty(); }
26 std::size_t size() const { return heap.size(); }
27 std::size_t capacityBytes() const
28 {
29 return heap.capacity() * sizeof(Entry) + positions.capacity() * sizeof(int);
30 }
31 void update(Entry entry)
32 {
33 int at = positions[entry.vertex];
34 if (at < 0) {
35 at = int(heap.size());
36 heap.push_back(entry);
37 positions[entry.vertex] = at;
38 up(at);
39 } else {
40 heap[at] = entry;
41 at = up(at);
42 down(at);
43 }
44 }
46 {
47 const auto result = heap.front();
48 positions[result.vertex] = -1;
49 if (heap.size() == 1) {
50 heap.pop_back();
51 return result;
52 }
53 heap.front() = heap.back();
54 heap.pop_back();
55 positions[heap.front().vertex] = 0;
56 down(0);
57 return result;
58 }
59
60private:
61 std::vector<Entry> heap;
62 std::vector<int> positions;
63 static bool before(const Entry &a, const Entry &b)
64 {
65 if (a.tile != b.tile)
66 return a.tile < b.tile;
67 if (a.importance != b.importance)
68 return a.importance < b.importance;
69 if (a.density != b.density)
70 return a.density < b.density;
71 return a.vertex < b.vertex;
72 }
73 void exchange(int a, int b)
74 {
75 std::swap(heap[a], heap[b]);
76 positions[heap[a].vertex] = a;
77 positions[heap[b].vertex] = b;
78 }
79 int up(int at)
80 {
81 while (at > 0) {
82 const int parent = (at - 1) / 2;
83 if (!before(heap[at], heap[parent]))
84 break;
85 exchange(at, parent);
86 at = parent;
87 }
88 return at;
89 }
90 void down(int at)
91 {
92 for (;;) {
93 const int left = at * 2 + 1;
94 if (left >= int(heap.size()))
95 return;
96 const int right = left + 1;
97 const int next =
98 right < int(heap.size()) && before(heap[right], heap[left]) ? right : left;
99 if (!before(heap[next], heap[at]))
100 return;
101 exchange(at, next);
102 at = next;
103 }
104 }
105};
106}
Definition coarseningqueue.hpp:13
Entry pop()
Definition coarseningqueue.hpp:45
void update(Entry entry)
Definition coarseningqueue.hpp:31
bool empty() const
Definition coarseningqueue.hpp:25
std::size_t size() const
Definition coarseningqueue.hpp:26
std::size_t capacityBytes() const
Definition coarseningqueue.hpp:27
CoarseningQueue(std::size_t vertices, std::size_t maximumEntries=0)
Definition coarseningqueue.hpp:20
Definition qtcdtstorage.h:9
int right
Definition constraintaudit.cpp:162
int at
Definition constraintaudit.cpp:156
int left
Definition constraintaudit.cpp:162
int parent
Definition planning.cpp:199
Definition coarseningqueue.hpp:15
float importance
Definition coarseningqueue.hpp:16
std::uint32_t tile
Definition coarseningqueue.hpp:18
float density
Definition coarseningqueue.hpp:16
int vertex
Definition coarseningqueue.hpp:17
double a
Definition terrainerrorfield.cpp:42
double b
Definition terrainerrorfield.cpp:42