1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#pragma once
//priority queue implementation using binary min-heap array:
//O(1) find
//O(log n) insert
//O(log n) remove(first)
//O(n) remove(event)
#include <nall/function.hpp>
#include <nall/serializer.hpp>
namespace nall {
template<typename T> struct priority_queue;
template<typename T, u32 Size>
struct priority_queue<T[Size]> {
explicit operator bool() const {
return size != 0;
}
auto reset() -> void {
clock = 0;
size = 0;
}
template<typename F>
auto step(u32 clocks, const F& callback) -> void {
clock += clocks;
while(size && ge(clock, heap[0].clock)) {
if(auto event = remove()) callback(*event);
}
}
auto insert(const T& event, u32 clock) -> bool {
if(size >= Size) return false;
u32 child = size++;
clock += this->clock;
while(child) {
u32 parent = (child - 1) >> 1;
if(ge(clock, heap[parent].clock)) break;
heap[child].clock = heap[parent].clock;
heap[child].event = heap[parent].event;
heap[child].valid = heap[parent].valid;
child = parent;
}
heap[child].clock = clock;
heap[child].event = event;
heap[child].valid = true;
return true;
}
auto remove() -> maybe<T> {
T event = heap[0].event;
bool valid = heap[0].valid;
u32 parent = 0;
u32 clock = heap[--size].clock;
while(true) {
u32 child = (parent << 1) + 1;
if(child >= size) break;
if(child + 1 < size && ge(heap[child].clock, heap[child + 1].clock)) child++;
if(ge(heap[child].clock, clock)) break;
heap[parent].clock = heap[child].clock;
heap[parent].event = heap[child].event;
heap[parent].valid = heap[child].valid;
parent = child;
}
heap[parent].clock = clock;
heap[parent].event = heap[size].event;
heap[parent].valid = heap[size].valid;
if(valid) return event;
return nothing;
}
auto remove(const T& event) -> void {
for(auto& entry : heap) {
if(entry.event == event) entry.valid = false;
}
}
auto serialize(serializer& s) -> void {
s(clock);
s(size);
for(auto& entry : heap) {
s(entry.clock);
s(entry.event);
s(entry.valid);
}
}
private:
//returns true if x is greater than or equal to y
auto ge(u32 x, u32 y) -> bool {
return x - y < 0x7fffffff;
}
u32 clock = 0;
u32 size = 0;
struct Entry {
u32 clock;
T event;
bool valid;
} heap[Size];
};
}
|