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
// src/stack.rs
use pyo3::prelude::*;
/// A stack data structure for `i32` that supports typical stack operations.
#[pyclass]
pub struct StackI32 {
items: Vec<i32>,
}
#[pymethods]
impl StackI32 {
/// Creates a new, empty stack.
#[new]
fn new() -> Self {
StackI32 { items: Vec::new() }
}
/// Pushes an item onto the top of the stack.
///
/// # Arguments
///
/// * `item` - The item to be pushed onto the stack.
fn push(&mut self, item: i32) {
self.items.push(item);
}
/// Removes and returns the item at the top of the stack, or `None` if the stack is empty.
fn pop(&mut self) -> Option<i32> {
self.items.pop()
}
/// Returns a reference to the item at the top of the stack without removing it, or `None` if the stack is empty.
fn peek(&self) -> Option<i32> {
self.items.last().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 stack.
fn print(&self) {
println!("Stack: {:?}", self.items);
}
}
/// A stack data structure for `f64` that supports typical stack operations.
#[pyclass]
pub struct StackF64 {
items: Vec<f64>,
}
#[pymethods]
impl StackF64 {
/// Creates a new, empty stack.
#[new]
fn new() -> Self {
StackF64 { items: Vec::new() }
}
/// Pushes an item onto the top of the stack.
///
/// # Arguments
///
/// * `item` - The item to be pushed onto the stack.
fn push(&mut self, item: f64) {
self.items.push(item);
}
/// Removes and returns the item at the top of the stack, or `None` if the stack is empty.
fn pop(&mut self) -> Option<f64> {
self.items.pop()
}
/// Returns a reference to the item at the top of the stack without removing it, or `None` if the stack is empty.
fn peek(&self) -> Option<f64> {
self.items.last().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 stack.
fn print(&self) {
println!("Stack: {:?}", self.items);
}
}