Skip to content

Commit

Permalink
refactor: Rename crate to "cheap"
Browse files Browse the repository at this point in the history
  • Loading branch information
mantono committed Jan 4, 2024
1 parent 5624dc1 commit 7a9929b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 24 deletions.
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
[package]
name = "prio"
name = "cheap"
version = "0.1.0"
authors = ["Anton Österberg <[email protected]>"]
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"

2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# prio
# cheap
A _channel_ heap in Rust, i.e. a fixed capacity binary heap (priority queue) over a channel.
7 changes: 4 additions & 3 deletions src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(capacity: usize) -> (Sender<T>, Receiver<T>)
where
T: Ord,
{
let buffer: PrioQueue<T> = PrioQueue::with_capacity(capacity);
let buffer: FixedHeap<T> = FixedHeap::with_capacity(capacity);
let shared = Shared {
buffer: Mutex::new(buffer),
receivers: Condvar::new(),
Expand Down Expand Up @@ -235,7 +236,7 @@ struct Shared<T>
where
T: Ord,
{
buffer: Mutex<PrioQueue<T>>,
buffer: Mutex<FixedHeap<T>>,
receivers: Condvar,
senders: Condvar,
sender_count: AtomicUsize,
Expand Down
32 changes: 16 additions & 16 deletions src/queue.rs → src/heap.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
use std::collections::BinaryHeap;

/// PrioQueue is a priority queue (heap) with a fixed capacity.
pub(crate) struct PrioQueue<T>
/// FixedHeap is a bianry heap (priority queue) with a fixed capacity.
pub(crate) struct FixedHeap<T>
where
T: Ord,
{
data: BinaryHeap<T>,
capacity: usize,
}

impl<T> PrioQueue<T>
impl<T> FixedHeap<T>
where
T: Ord,
{
pub fn new() -> PrioQueue<T> {
pub fn new() -> FixedHeap<T> {
Self::with_capacity(32)
}

pub fn with_capacity(capacity: usize) -> PrioQueue<T> {
pub fn with_capacity(capacity: usize) -> FixedHeap<T> {
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> {
Expand Down Expand Up @@ -56,7 +56,7 @@ where
}
}

impl<T> Default for PrioQueue<T>
impl<T> Default for FixedHeap<T>
where
T: Ord,
{
Expand All @@ -67,49 +67,49 @@ where

#[cfg(test)]
mod tests {
use super::PrioQueue;
use super::FixedHeap;

#[test]
fn test_new() {
let _: PrioQueue<char> = PrioQueue::new();
let _: FixedHeap<char> = FixedHeap::new();
}

#[test]
fn test_is_empty() {
let buffer: PrioQueue<char> = PrioQueue::new();
let buffer: FixedHeap<char> = FixedHeap::new();
assert!(buffer.is_empty());
}

#[test]
fn test_capacity() {
let buffer: PrioQueue<char> = PrioQueue::with_capacity(4);
let buffer: FixedHeap<char> = FixedHeap::with_capacity(4);
assert_eq!(4, buffer.capacity());
}

#[test]
fn test_offer() {
let mut buffer: PrioQueue<usize> = PrioQueue::with_capacity(1);
let mut buffer: FixedHeap<usize> = FixedHeap::with_capacity(1);
assert!(buffer.offer(0).is_ok());
}

#[test]
fn test_offer_full() {
let mut buffer: PrioQueue<usize> = PrioQueue::with_capacity(1);
let mut buffer: FixedHeap<usize> = 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<usize> = PrioQueue::with_capacity(1);
let mut buffer: FixedHeap<usize> = FixedHeap::with_capacity(1);
assert!(!buffer.is_full());
assert!(buffer.offer(0).is_ok());
assert!(buffer.is_full());
}

#[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());
Expand All @@ -119,7 +119,7 @@ mod tests {

#[test]
fn test_priority() {
let mut buffer: PrioQueue<usize> = PrioQueue::new();
let mut buffer: FixedHeap<usize> = FixedHeap::new();
buffer.offer(1).unwrap();
buffer.offer(3).unwrap();
buffer.offer(2).unwrap();
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod channel;
mod queue;
mod channel;
mod heap;

pub use channel::channel;

0 comments on commit 7a9929b

Please sign in to comment.