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
use pyo3::prelude::*;
/// A queue data structure specifically for `f64` items.
#[pyclass]
pub struct QueueF64 {
items: Vec<f64>,
}
#[pymethods]
impl QueueF64 {
/// Creates a new, empty queue.
#[new]
fn new() -> Self {
QueueF64 { items: Vec::new() }
}
/// Adds an item to the end of the queue.
///
/// # Arguments
///
/// * `item` - The item to be added to the queue.
fn push(&mut self, item: f64) {
self.items.push(item);
}
/// Removes and returns the item at the front of the queue, or `None` if the queue is empty.
fn pop(&mut self) -> Option<f64> {
if self.is_empty() {
None
} else {
Some(self.items.remove(0))
}
}
/// Returns a reference to the item at the front of the queue without removing it,
/// or `None` if the queue is empty.
fn peek(&self) -> Option<f64> {
self.items.first().copied()
}
/// Returns `true` if the stack is empty, and `false` otherwise.
fn is_empty(&self) -> bool {
self.items.is_empty()
}
/// Returns the number of items in the stack.
fn size(&self) -> usize {
self.items.len()
}
/// Prints the contents of the queue.
fn print(&self) {
println!("Queue: {:?}", self.items);
}
/// Returns the number of items in the queue.
fn len(&self) -> usize {
self.items.len()
}
}
/// A queue data structure specifically for `i32` items.
#[pyclass]
pub struct QueueI32 {
items: Vec<i32>,
}
#[pymethods]
impl QueueI32 {
/// Creates a new, empty queue.
#[new]
fn new() -> Self {
QueueI32 { items: Vec::new() }
}
/// Adds an item to the end of the queue.
///
/// # Arguments
///
/// * `item` - The item to be added to the queue.
fn push(&mut self, item: i32) {
self.items.push(item);
}
/// Removes and returns the item at the front of the queue, or `None` if the queue is empty.
fn pop(&mut self) -> Option<i32> {
if self.is_empty() {
None
} else {
Some(self.items.remove(0))
}
}
/// Returns a reference to the item at the front of the queue without removing it,
/// or `None` if the queue is empty.
fn peek(&self) -> Option<i32> {
self.items.first().copied()
}
/// Returns `true` if the stack is empty, and `false` otherwise.
fn is_empty(&self) -> bool {
self.items.is_empty()
}
/// Returns the number of items in the stack.
fn size(&self) -> usize {
self.items.len()
}
/// Prints the contents of the queue.
fn print(&self) {
println!("Queue: {:?}", self.items);
}
/// Returns the number of items in the queue.
fn len(&self) -> usize {
self.items.len()
}
}