Skip to content

Commit

Permalink
Add variadic macro invocation macro (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisOSRM committed Apr 18, 2024
1 parent 31170bc commit dc387ef
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 28 deletions.
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ pub mod unidirectional_dijkstra;
pub mod union_find;
pub mod unsafe_slice;
pub mod wgs84;

#[macro_export]
macro_rules! invoke_macro_for_types {
($macro:ident, $($args:ident),*) => {
$( $macro!($args); )*
}
}
18 changes: 5 additions & 13 deletions src/rdx_sort.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub mod radix {
use core::mem;

use crate::invoke_macro_for_types;

pub trait RadixType: Clone + Copy + Default {
// RadixTypes are sortable by rdx_sort(.)
const IS_SIGNED: bool;
Expand Down Expand Up @@ -56,19 +58,9 @@ pub mod radix {
}

// define built-in number types (integers, floats, bool) as RadixType
radix_type!(u8);
radix_type!(u16);
radix_type!(u32);
radix_type!(u64);
radix_type!(u128);
radix_type!(usize);

radix_type!(i8);
radix_type!(i16);
radix_type!(i32);
radix_type!(i64);
radix_type!(i128);
radix_type!(isize);
invoke_macro_for_types!(
radix_type, u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize
);

radix_type!(bool, u8);

Expand Down
19 changes: 4 additions & 15 deletions src/top_k.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::invoke_macro_for_types;

pub trait ComparisonValue {
type Integral: Default;
fn value(&self) -> Self::Integral;
}

macro_rules! comparison_value {
macro_rules! cv {
// short-hand to add a default ComparisonValue implementation for the
// given input type. Works with built-in types like integers.
($a:ident) => {
Expand All @@ -17,20 +19,7 @@ macro_rules! comparison_value {
};
}

// define built-in number types (integers, floats, bool)
comparison_value!(u8);
comparison_value!(u16);
comparison_value!(u32);
comparison_value!(u64);
comparison_value!(u128);
comparison_value!(usize);

comparison_value!(i8);
comparison_value!(i16);
comparison_value!(i32);
comparison_value!(i64);
comparison_value!(i128);
comparison_value!(isize);
invoke_macro_for_types!(cv, u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);

pub fn top_k<T: ComparisonValue + Copy + std::cmp::Ord>(
input: impl IntoIterator<Item = T>,
Expand Down

0 comments on commit dc387ef

Please sign in to comment.