pub struct BoundedPriorityQueue<T> { /* private fields */ }Expand description
Bounded priority queue with capacity limiting
This matches C++ AGC’s CBoundedPQueue (queue.h:153-346):
template<typename T> class CBoundedPQueue {
typedef multimap<pair<size_t, size_t>, T> queue_t;
// ...
void Emplace(T&& data, size_t priority, size_t cost);
result_t PopLarge(T& data);
void MarkCompleted();
}Key features:
- Priority ordering: Higher priority items processed first
- Capacity limiting: Sum of costs must stay below max_cost
- Thread-safe: Multiple producers and consumers
- Completion signaling: MarkCompleted() when producer done
Example:
use ragc_core::priority_queue::{BoundedPriorityQueue, PopResult};
let queue = BoundedPriorityQueue::new(2, 1000); // 2 producers, 1000 max cost
// Producer thread
queue.emplace("task1".to_string(), 100, 50); // priority 100, cost 50
queue.mark_completed(); // This producer is done
// Consumer thread
loop {
match queue.pop_large() {
(PopResult::Normal, Some(data)) => {
// Process data
}
(PopResult::Empty, None) => {
continue; // Wait for more
}
(PopResult::Completed, None) => {
break; // All done
}
_ => {
// Handle other cases
}
}
}Implementations§
Source§impl<T> BoundedPriorityQueue<T>
impl<T> BoundedPriorityQueue<T>
Sourcepub fn new(n_producers: usize, max_cost: usize) -> Self
pub fn new(n_producers: usize, max_cost: usize) -> Self
Create a new bounded priority queue
Matches C++ AGC’s constructor (queue.h:174-180):
CBoundedPQueue(const int _n_producers, const size_t _max_cost);§Arguments
n_producers- Number of producer threadsmax_cost- Maximum total cost allowed in queue
Sourcepub fn emplace(&self, data: T, priority: usize, cost: usize)
pub fn emplace(&self, data: T, priority: usize, cost: usize)
Add an item to the queue (blocks if at capacity)
Matches C++ AGC’s Emplace (queue.h:238-251):
void Emplace(T&& data, const size_t priority, const size_t cost);§Arguments
data- Item to enqueuepriority- Priority (higher = processed first)cost- Memory cost (for capacity limiting)
Sourcepub fn emplace_many_no_cost(&self, data: T, priority: usize, n_items: usize)
pub fn emplace_many_no_cost(&self, data: T, priority: usize, n_items: usize)
Add multiple copies of an item with zero cost (for sync barriers)
Matches C++ AGC’s EmplaceManyNoCost (queue.h:270-280):
void EmplaceManyNoCost(T&& data, size_t priority, size_t n_items);This is used to send synchronization tokens to all workers.
§Arguments
data- Item to enqueue (will be cloned n_items times)priority- Priorityn_items- Number of copies to enqueue
Sourcepub fn pop_large(&self) -> (PopResult, Option<T>)
pub fn pop_large(&self) -> (PopResult, Option<T>)
Pop the highest priority item (blocks if empty)
Matches C++ AGC’s PopLarge (queue.h:284-313):
result_t PopLarge(T& data);Returns:
- (Normal, Some(data)): Successfully popped highest priority item
- (Empty, None): Queue empty but producers still active
- (Completed, None): Queue empty and no producers (exit)
Sourcepub fn mark_completed(&self)
pub fn mark_completed(&self)
Signal that a producer is done
Matches C++ AGC’s MarkCompleted (queue.h:212-219):
void MarkCompleted();When all producers call this, consumers will receive Completed.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if queue is empty
Matches C++ AGC’s IsEmpty (queue.h:197-201)
Sourcepub fn is_completed(&self) -> bool
pub fn is_completed(&self) -> bool
Check if queue is completed (empty and no producers)
Matches C++ AGC’s IsCompleted (queue.h:204-209)
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for BoundedPriorityQueue<T>
impl<T> RefUnwindSafe for BoundedPriorityQueue<T>
impl<T> Send for BoundedPriorityQueue<T>where
T: Send,
impl<T> Sync for BoundedPriorityQueue<T>where
T: Send,
impl<T> Unpin for BoundedPriorityQueue<T>
impl<T> UnwindSafe for BoundedPriorityQueue<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more