WIP: stage all uncommitted work — sprints 46-221, graduation headers, specialist fleet, test steps 909-1988
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
36
examples/priority_queue/PriorityQueue.h
Normal file
36
examples/priority_queue/PriorityQueue.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
#include <queue>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include "WorkItem.h"
|
||||
|
||||
struct WorkItemComparator {
|
||||
bool operator()(const WorkItem& a, const WorkItem& b) const {
|
||||
return a.priority < b.priority; // max-heap: higher priority first
|
||||
}
|
||||
};
|
||||
|
||||
class PriorityQueue {
|
||||
public:
|
||||
void enqueue(WorkItem item) {
|
||||
heap_.push(std::move(item));
|
||||
}
|
||||
|
||||
WorkItem dequeue() {
|
||||
if (heap_.empty()) throw std::underflow_error("queue is empty");
|
||||
WorkItem top = heap_.top();
|
||||
heap_.pop();
|
||||
return top;
|
||||
}
|
||||
|
||||
const WorkItem& peek() const {
|
||||
if (heap_.empty()) throw std::underflow_error("queue is empty");
|
||||
return heap_.top();
|
||||
}
|
||||
|
||||
std::size_t size() const { return heap_.size(); }
|
||||
bool empty() const { return heap_.empty(); }
|
||||
|
||||
private:
|
||||
std::priority_queue<WorkItem, std::vector<WorkItem>, WorkItemComparator> heap_;
|
||||
};
|
||||
27
examples/priority_queue/WorkItem.h
Normal file
27
examples/priority_queue/WorkItem.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
struct WorkItem {
|
||||
std::string job_id;
|
||||
std::string payload;
|
||||
int priority;
|
||||
};
|
||||
|
||||
inline void to_json(nlohmann::json& j, const WorkItem& v) {
|
||||
j = nlohmann::json::object();
|
||||
j["job_id"] = v.job_id;
|
||||
j["payload"] = v.payload;
|
||||
j["priority"] = v.priority;
|
||||
}
|
||||
|
||||
inline void from_json(const nlohmann::json& j, WorkItem& v) {
|
||||
if (j.contains("job_id")) j.at("job_id").get_to(v.job_id);
|
||||
if (j.contains("payload")) j.at("payload").get_to(v.payload);
|
||||
if (j.contains("priority")) j.at("priority").get_to(v.priority);
|
||||
}
|
||||
|
||||
inline bool validateWorkItem(const WorkItem& v) {
|
||||
(void)v;
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user