Skip to content

Commit

Permalink
code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
martial-plains committed Mar 6, 2024
1 parent 1f869cb commit a3c186a
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 27 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ authors = ["Allister Isaiah Harvey <[email protected]>"]
edition = "2021"

[features]
default = ["std"]
default = ["hashbrown"]
std = []
hashbrown = ["dep:hashbrown"]

[dependencies]
num = "0.4.0"
rand = "0.8.5"
hashbrown = { version = "0.14.0", optional = true }

[target.'cfg(not(target_feature = "std"))'.dependencies]
hashbrown = { version = "0.14.0" }

[dev-dependencies]
test-case = "3.0.0"
2 changes: 1 addition & 1 deletion benches/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use algorithms::math::fibonacci::*;
use test::Bencher;

// bench: find the `BENCH_SIZE` first terms of the fibonacci sequence
static BENCH_SIZE: usize = 1000;
static BENCH_SIZE: usize = 10;

#[bench]
fn recursive_fibonacci(b: &mut Bencher) {
Expand Down
12 changes: 8 additions & 4 deletions src/dynamic_programming/memoize.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use core::hash::Hash;

#[cfg(not(target_feature = "std"))]
#[cfg(not(feature = "std"))]
use hashbrown::HashMap;
#[cfg(target_feature = "std")]
#[cfg(feature = "std")]
use std::collections::HashMap;

/// Memoizes the result of a function to avoid redundant function calls.
Expand All @@ -20,7 +20,10 @@ use std::collections::HashMap;
/// # Example
///
/// ```
/// #[cfg(not(feature = "std"))]
/// use hashbrown::HashMap;
/// #[cfg(feature = "std")]
/// use std::collections::HashMap;
/// use algorithms::dynamic_programming::memoize;
///
/// fn fibonacci(cache: &mut HashMap<u32, u64>, n: u32) -> u64 {
Expand Down Expand Up @@ -61,11 +64,12 @@ use std::collections::HashMap;
/// # References
///
/// - [Wikipedia](https://en.wikipedia.org/wiki/Memoization)
pub fn memoize<A, R, F>(cache: &mut HashMap<A, R>, func: F, arg: A) -> R
pub fn memoize<A, R, F, S>(cache: &mut HashMap<A, R, S>, func: F, arg: A) -> R
where
A: Hash + Eq + Clone,
R: Clone,
F: Fn(&mut HashMap<A, R>, A) -> R,
F: Fn(&mut HashMap<A, R, S>, A) -> R,
S: core::hash::BuildHasher,
{
if let Some(result) = cache.get(&arg).cloned() {
result
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
while_true
)]
#![warn(clippy::all, clippy::pedantic, missing_debug_implementations)]
#![feature(decl_macro)]
#![feature(cfg_match, decl_macro)]
#![no_std]

#[cfg(target_feature = "std")]
#[cfg(feature = "std")]
extern crate std;

#[cfg(not(target_feature = "std"))]
extern crate alloc;

pub mod dynamic_programming;
Expand Down
4 changes: 2 additions & 2 deletions src/math/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//!
//! [Wikipedia](https://en.wikipedia.org/wiki/Fibonacci_number)
#[cfg(not(target_feature = "std"))]
#[cfg(not(feature = "std"))]
use hashbrown::HashMap;
#[cfg(target_feature = "std")]
#[cfg(feature = "std")]
use std::collections::HashMap;

use crate::dynamic_programming::memoize;
Expand Down
2 changes: 0 additions & 2 deletions src/math/greatest_common_divisor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::marker::Copy;

/// Computes the greatest common divisor (GCD) of a list of nonnegative integers.
///
/// # Examples
Expand Down
3 changes: 2 additions & 1 deletion src/sorts/bubble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ where

#[cfg(test)]
mod tests {
use alloc::vec;

use alloc::vec::Vec;
use alloc::vec;

use test_case::test_case;

Expand Down
3 changes: 2 additions & 1 deletion src/sorts/bucket.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloc::vec::Vec;
use alloc::vec;

/// A Rust bucket sort implementation
///
Expand All @@ -13,7 +14,7 @@ pub fn bucket<T>(arr: &mut [T])
where
T: Ord + Clone,
{
let mut bucket: Vec<Vec<T>> = alloc::vec![Vec::new(); arr.len()];
let mut bucket: Vec<Vec<T>> = vec![Vec::new(); arr.len()];
for i in 0..arr.len() {
bucket[i].push(arr[i].clone());
}
Expand Down
5 changes: 3 additions & 2 deletions src/sorts/counting.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::vec::Vec;
use alloc::vec;

use core::{cmp::Ord, ops::AddAssign};
use core::ops::AddAssign;

/// A counting sort implementation for all unsigned types
#[allow(clippy::pedantic)]
Expand All @@ -10,7 +11,7 @@ where
{
let max = arr.len();

let mut occurrences: Vec<usize> = alloc::vec![0; max + 1];
let mut occurrences: Vec<usize> = vec![0; max + 1];

for &data in arr.iter() {
occurrences[data.into() as usize] += 1;
Expand Down
2 changes: 1 addition & 1 deletion src/sorts/insertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ where
#[cfg(test)]
mod tests {
use test_case::test_case;

use alloc::vec;
use alloc::vec::Vec;

Expand Down
7 changes: 5 additions & 2 deletions src/sorts/wiggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ where
});
}

#[cfg(test)]
mod tests {

use alloc::vec;

#[test]
fn wiggle_sort_test() {
let mut vector = alloc::vec![0, 5, 3, 2, 2];
let expected = alloc::vec![0, 5, 2, 3, 2];
let mut vector = vec![0, 5, 3, 2, 2];
let expected = vec![0, 5, 2, 3, 2];
super::wiggle(&mut vector);
let actual = vector;
assert_eq!(expected, actual);
Expand Down
4 changes: 1 addition & 3 deletions src/strings/capitalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ pub fn capitalize(text: &str) -> String {
mod tests {
use super::*;

use alloc::string::String;

use test_case::test_case;

#[test_case("123 hello world", "123 hello world")]
#[test_case("hello world", "Hello world")]
#[test_case("a", "A")]
#[test_case("", "")]
fn does_it_capitalize(sentence: &str, expected: &str) {
let actual = capitalize(&String::from(sentence));
let actual = capitalize(sentence);
assert_eq!(expected, actual);
}
}
4 changes: 2 additions & 2 deletions src/strings/search/word_occurrences.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(not(target_feature = "std"))]
#[cfg(not(feature = "std"))]
use hashbrown::HashMap;
#[cfg(target_feature = "std")]
#[cfg(feature = "std")]
use std::collections::HashMap;

/// Create a map containing count of each word
Expand Down

0 comments on commit a3c186a

Please sign in to comment.