BoundedPriorityQueue

Struct BoundedPriorityQueue 

Source
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>
where T: Clone + Eq,

Source

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 threads
  • max_cost - Maximum total cost allowed in queue
Source

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 enqueue
  • priority - Priority (higher = processed first)
  • cost - Memory cost (for capacity limiting)
Source

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 - Priority
  • n_items - Number of copies to enqueue
Source

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)
Source

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.

Source

pub fn is_empty(&self) -> bool

Check if queue is empty

Matches C++ AGC’s IsEmpty (queue.h:197-201)

Source

pub fn is_completed(&self) -> bool

Check if queue is completed (empty and no producers)

Matches C++ AGC’s IsCompleted (queue.h:204-209)

Source

pub fn get_size(&self) -> (usize, usize)

Get current queue size (items, total_cost)

Matches C++ AGC’s GetSize (queue.h:340-345)

Trait Implementations§

Source§

impl<T> Clone for BoundedPriorityQueue<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.