From 7a9929bf0c895aa56b3f08fa491349ae499ec03b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20=C3=96sterberg?= Date: Thu, 4 Jan 2024 20:42:14 +0100 Subject: [PATCH] refactor: Rename crate to "cheap" --- Cargo.toml | 10 +++++++++- LICENSE.md | 2 +- README.md | 3 ++- src/channel.rs | 7 ++++--- src/{queue.rs => heap.rs} | 32 ++++++++++++++++---------------- src/lib.rs | 6 ++++-- 6 files changed, 36 insertions(+), 24 deletions(-) rename src/{queue.rs => heap.rs} (72%) diff --git a/Cargo.toml b/Cargo.toml index ee0dc6a..8aaff29 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,13 @@ [package] -name = "prio" +name = "cheap" version = "0.1.0" authors = ["Anton Österberg "] edition = "2021" +description = "A channel backed by a binary heap" +license = "MIT" +keywords = ["channel", "heap", "data-structure", "priority", "queue"] +categories = ["concurrency", "data-structures"] + +[lints.rust] +unsafe_code = "forbid" + diff --git a/LICENSE.md b/LICENSE.md index 3144457..31fe9d1 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Anton Österberg +Copyright (c) 2024 Anton Österberg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 8d23122..ebc2772 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -# prio +# cheap +A _channel_ heap in Rust, i.e. a fixed capacity binary heap (priority queue) over a channel. diff --git a/src/channel.rs b/src/channel.rs index 9ccdd1b..f37c890 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -3,13 +3,14 @@ use std::sync::atomic::Ordering::Relaxed; use std::sync::{atomic::AtomicUsize, Arc, Condvar, Mutex}; use std::time::Duration; -use crate::queue::PrioQueue; +use crate::heap::FixedHeap; +/// Create a channel with a fixed capacity that is backed by a heap pub fn channel(capacity: usize) -> (Sender, Receiver) where T: Ord, { - let buffer: PrioQueue = PrioQueue::with_capacity(capacity); + let buffer: FixedHeap = FixedHeap::with_capacity(capacity); let shared = Shared { buffer: Mutex::new(buffer), receivers: Condvar::new(), @@ -235,7 +236,7 @@ struct Shared where T: Ord, { - buffer: Mutex>, + buffer: Mutex>, receivers: Condvar, senders: Condvar, sender_count: AtomicUsize, diff --git a/src/queue.rs b/src/heap.rs similarity index 72% rename from src/queue.rs rename to src/heap.rs index 91717e0..d0a4838 100644 --- a/src/queue.rs +++ b/src/heap.rs @@ -1,7 +1,7 @@ use std::collections::BinaryHeap; -/// PrioQueue is a priority queue (heap) with a fixed capacity. -pub(crate) struct PrioQueue +/// FixedHeap is a bianry heap (priority queue) with a fixed capacity. +pub(crate) struct FixedHeap where T: Ord, { @@ -9,20 +9,20 @@ where capacity: usize, } -impl PrioQueue +impl FixedHeap where T: Ord, { - pub fn new() -> PrioQueue { + pub fn new() -> FixedHeap { Self::with_capacity(32) } - pub fn with_capacity(capacity: usize) -> PrioQueue { + pub fn with_capacity(capacity: usize) -> FixedHeap { if capacity == 0 { panic!("Tried to create an empty queue") } let data = BinaryHeap::with_capacity(capacity); - PrioQueue { data, capacity } + FixedHeap { data, capacity } } pub fn offer(&mut self, item: T) -> Result<(), T> { @@ -56,7 +56,7 @@ where } } -impl Default for PrioQueue +impl Default for FixedHeap where T: Ord, { @@ -67,41 +67,41 @@ where #[cfg(test)] mod tests { - use super::PrioQueue; + use super::FixedHeap; #[test] fn test_new() { - let _: PrioQueue = PrioQueue::new(); + let _: FixedHeap = FixedHeap::new(); } #[test] fn test_is_empty() { - let buffer: PrioQueue = PrioQueue::new(); + let buffer: FixedHeap = FixedHeap::new(); assert!(buffer.is_empty()); } #[test] fn test_capacity() { - let buffer: PrioQueue = PrioQueue::with_capacity(4); + let buffer: FixedHeap = FixedHeap::with_capacity(4); assert_eq!(4, buffer.capacity()); } #[test] fn test_offer() { - let mut buffer: PrioQueue = PrioQueue::with_capacity(1); + let mut buffer: FixedHeap = FixedHeap::with_capacity(1); assert!(buffer.offer(0).is_ok()); } #[test] fn test_offer_full() { - let mut buffer: PrioQueue = PrioQueue::with_capacity(1); + let mut buffer: FixedHeap = FixedHeap::with_capacity(1); assert!(buffer.offer(0).is_ok()); assert!(buffer.offer(0).is_err()); } #[test] fn test_is_full() { - let mut buffer: PrioQueue = PrioQueue::with_capacity(1); + let mut buffer: FixedHeap = FixedHeap::with_capacity(1); assert!(!buffer.is_full()); assert!(buffer.offer(0).is_ok()); assert!(buffer.is_full()); @@ -109,7 +109,7 @@ mod tests { #[test] fn test_size() { - let mut buffer: PrioQueue<()> = PrioQueue::new(); + let mut buffer: FixedHeap<()> = FixedHeap::new(); assert_eq!(0, buffer.size()); assert!(buffer.offer(()).is_ok()); assert_eq!(1, buffer.size()); @@ -119,7 +119,7 @@ mod tests { #[test] fn test_priority() { - let mut buffer: PrioQueue = PrioQueue::new(); + let mut buffer: FixedHeap = FixedHeap::new(); buffer.offer(1).unwrap(); buffer.offer(3).unwrap(); buffer.offer(2).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index d14b6f8..40e12be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,4 @@ -pub mod channel; -mod queue; +mod channel; +mod heap; + +pub use channel::channel;