Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions .github/workflows/rust_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,37 @@ on:


jobs:
check:
name: Rust Check
std_check:
name: Rust std feature Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: cargo check
test:
name: Rust Test
- run: cargo check -F std
std_test:
name: Rust std feature Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: cargo test
- run: cargo test -F std
no_std_check:
name: Rust no_std feature Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: cargo check -F no_std
no_std_test:
name: Rust no_std feature Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: cargo test -F no_std
fmt:
name: Rust fmt Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: cargo-fmt --all --check
- run: cargo-fmt --all --check

# clippy:
# name: Rust Clippy
# runs-on: ubuntu-latest
Expand Down
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
[package]
name = "algori"
version = "0.12.0"
version = "0.13.0"
edition = "2021"
authors = ["Donjuan Platinum <[email protected]>"]
license = "GPL-2.0-only"
description = "Rust Algorithms"
repository = "https://github.com/donjuanplatinum/Algori"
readme = "README.md"
readme = "README.org"
documentation = "https://docs.rs/algori"
keywords = ["matrix","math","algorithm","sort","search"]
categories = ["algorithms"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[features]
default = ["std"]
std = []
no_std = []
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Copyright 2024 Barrensea. Licensed under GPL-2

// no_std
#![cfg_attr(feature = "no_std", no_std)]

/// Algori
/// This is a algorithms Library

/// Sortion Algorithms
pub mod sorting;

Expand Down
4 changes: 2 additions & 2 deletions src/math/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ impl<T> From<(T, T)> for Complex<T> {

#[cfg(test)]
mod max_substring {
use std::hash::Hasher;

use super::*;
#[test]
fn create_new_complex() {
Expand Down Expand Up @@ -185,10 +183,12 @@ mod max_substring {
let b: Complex<i32> = (992, 8770).into();
assert_eq!(Complex { re: 972, im: 8740 }, b - a);
}
#[cfg(not(feature = "no_std"))]
#[test]
fn hash() {
use std::collections::hash_map::DefaultHasher;
use std::hash::Hash;
use std::hash::Hasher;
fn calculate_hash<T: Hash>(t: &T) -> u64 {
let mut s = DefaultHasher::new();
t.hash(&mut s);
Expand Down
2 changes: 2 additions & 0 deletions src/math/dft.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(not(feature = "no_std"))]
use super::Complex;
/// Discrete Fourier Transform (DFT)
/// # Examples
Expand All @@ -8,6 +9,7 @@ use super::Complex;
/// println!("{:?}",result);
///
/// ```
#[cfg(not(feature = "no_std"))]
pub fn dft<T: Clone>(input: &[T]) -> Vec<Complex<f64>>
where
f64: From<T>,
Expand Down
1 change: 1 addition & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod dft;
mod pi;
pub use complex::*;
use core::ops::{Add, Div, Mul, Rem, Sub};
#[cfg(not(feature = "no_std"))]
pub use dft::*;
pub use pi::*;
pub trait NumOps<Rhs = Self, Output = Self>:
Expand Down
1 change: 1 addition & 0 deletions src/searching/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod binary_search;
mod two_sum;
pub use binary_search::*;
#[cfg(not(feature = "no_std"))]
pub use two_sum::*;
6 changes: 5 additions & 1 deletion src/searching/two_sum.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#[cfg(not(feature = "no_std"))]
use core::hash::Hash;
#[cfg(not(feature = "no_std"))]
use std::collections::HashMap;

/// 两数之和
/// 在给定序列中寻找和为目标值的两个数的下标
/// ```
/// use algori::searching::two_sum;
/// let a = [2,7,11,15];
/// assert_eq!(two_sum(&a,&9),Ok((0,1)));
/// ```
pub fn two_sum<T: Hash + Eq + Clone + std::ops::Sub<Output = T>>(
#[cfg(not(feature = "no_std"))]
pub fn two_sum<T: Hash + Eq + Clone + core::ops::Sub<Output = T>>(
array: &[T],
target: &T,
) -> Result<(usize, usize), ()> {
Expand Down
4 changes: 3 additions & 1 deletion src/sorting/merge_sort.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(not(feature = "no_std"))]
fn merge<T: Clone>(array: &mut [T], mid: usize, comparator: fn(&T, &T) -> bool) {
let left = array[..mid].to_vec();
let right = array[mid..].to_vec();
Expand Down Expand Up @@ -36,6 +37,7 @@ fn merge<T: Clone>(array: &mut [T], mid: usize, comparator: fn(&T, &T) -> bool)
/// merge_sort(&mut a ,|a,b| a<=b);
/// assert_eq!(is_sorted(&mut a ,|a,b| a<=b),true);
/// ```
#[cfg(not(feature = "no_std"))]
pub fn merge_sort<T: Clone>(array: &mut [T], comparator: fn(&T, &T) -> bool) {
if array.len() > 1 {
if array.len() > 1 {
Expand All @@ -46,7 +48,7 @@ pub fn merge_sort<T: Clone>(array: &mut [T], comparator: fn(&T, &T) -> bool) {
}
}
}

#[cfg(not(feature = "no_std"))]
#[cfg(test)]
mod tests {
use super::super::is_sorted;
Expand Down
5 changes: 4 additions & 1 deletion src/sorting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ mod selection_sort;
mod utils;
pub use self::bubble_sort::*;
pub use self::insertion_sort::*;
pub use self::merge_sort::*;

pub use self::selection_sort::*;
pub use self::utils::*;

#[cfg(not(feature = "no_std"))]
pub use self::merge_sort::*;
27 changes: 17 additions & 10 deletions src/structure/heap.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use core::ops::{Deref, DerefMut};
use std::fmt::Display;
#[cfg(not(feature = "no_std"))]
use std::ops::{Deref, DerefMut};

#[cfg(not(feature = "no_std"))]
macro_rules! left_child {
($parent:ident) => {
($parent << 1) + 1 as usize
};
}

#[cfg(not(feature = "no_std"))]
macro_rules! right_child {
($parent:ident) => {
($parent << 1) + 2 as usize
};
}

#[cfg(not(feature = "no_std"))]
macro_rules! parent {
($child:ident) => {
($child - 1) >> 1
Expand All @@ -33,6 +34,7 @@ macro_rules! parent {
/// a.push(1); a.push(100); a.push(40); a.push(0);
/// assert_eq!(a.pop().unwrap(),100); assert_eq!(a.pop().unwrap(),40);
/// ```
#[cfg(not(feature = "no_std"))]
#[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq)]
pub struct Heap<T> {
items: Vec<T>,
Expand All @@ -50,20 +52,22 @@ pub struct Heap<T> {
/// fn ps(a: &[i32])->(){};
/// ps(&a);
/// ```
#[cfg(not(feature = "no_std"))]
impl<T> Deref for Heap<T> {
type Target = [T];

fn deref(&self) -> &Self::Target {
return &self.items;
}
}

#[cfg(not(feature = "no_std"))]
impl<T> DerefMut for Heap<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
return &mut self.items;
}
}

#[cfg(not(feature = "no_std"))]
impl<T> Default for Heap<T>
where
T: PartialOrd,
Expand All @@ -75,15 +79,17 @@ where
};
}
}
impl<T> Display for Heap<T>
#[cfg(not(feature = "no_std"))]
impl<T> core::fmt::Display for Heap<T>
where
T: core::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
return write!(f, "{:?}", self.items);
}
}
/// from a [T;N]
#[cfg(not(feature = "no_std"))]
impl<T: PartialOrd, const N: usize> From<[T; N]> for Heap<T> {
fn from(array: [T; N]) -> Self {
let mut heap = Heap {
Expand All @@ -96,6 +102,7 @@ impl<T: PartialOrd, const N: usize> From<[T; N]> for Heap<T> {
}

/// from Vec<T>
#[cfg(not(feature = "no_std"))]
impl<T: PartialOrd> From<Vec<T>> for Heap<T> {
fn from(array: Vec<T>) -> Self {
let mut heap = Heap {
Expand All @@ -106,7 +113,7 @@ impl<T: PartialOrd> From<Vec<T>> for Heap<T> {
return heap;
}
}
#[allow(dead_code)]
#[cfg(not(feature = "no_std"))]
impl<T> Heap<T> {
/// # Create a Heap With a Comparator
/// ```
Expand Down Expand Up @@ -268,7 +275,7 @@ impl<T> Heap<T> {
return &self.items;
}
}

#[cfg(not(feature = "no_std"))]
impl<T> Heap<T> {
pub fn from_array<const N: usize>(array: [T; N], comparator: fn(&T, &T) -> bool) -> Self {
let mut heap = Heap {
Expand All @@ -279,7 +286,7 @@ impl<T> Heap<T> {
return heap;
}
}

#[cfg(not(feature = "no_std"))]
#[cfg(test)]
mod heap_tests {
use super::super::super::sorting::is_sorted;
Expand Down
Loading
Loading