Skip to content

Commit

Permalink
alias macro
Browse files Browse the repository at this point in the history
  • Loading branch information
crepererum committed Aug 21, 2016
1 parent 97fad15 commit 367f425
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 148 deletions.
22 changes: 1 addition & 21 deletions src/char.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
use template::RdxSortTemplate;

impl RdxSortTemplate for char {
#[inline]
fn cfg_nbuckets() -> usize {
<u32 as RdxSortTemplate>::cfg_nbuckets()
}

#[inline]
fn cfg_nrounds() -> usize {
<u32 as RdxSortTemplate>::cfg_nrounds()
}

#[inline]
fn get_bucket(&self, round: usize) -> usize {
(*self as u32).get_bucket(round)
}

#[inline]
fn reverse(round: usize, bucket: usize) -> bool {
<u32 as RdxSortTemplate>::reverse(round, bucket)
}
}
rdxsort_template_alias!(char = u32);
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,13 @@ pub trait RdxSort {
fn rdxsort(&mut self);
}

#[macro_use] mod template;

mod array;
mod bool;
mod char;
mod floats;
mod signed_integer;
mod template;
mod tuple;
mod unsigned_integer;

Expand Down
66 changes: 3 additions & 63 deletions src/signed_integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,70 +47,10 @@ impl_rdxsort!(i32, u32, i32::min_value(), 0i32);
impl_rdxsort!(i64, u64, i64::min_value(), 0i64);

#[cfg(target_pointer_width = "16")]
impl RdxSortTemplate for isize {
#[inline]
fn cfg_nbuckets() -> usize {
<i16 as RdxSortTemplate>::cfg_nbuckets()
}

#[inline]
fn cfg_nrounds() -> usize {
<i16 as RdxSortTemplate>::cfg_nrounds()
}

#[inline]
fn get_bucket(&self, round: usize) -> usize {
(*self as i16).get_bucket(round)
}

#[inline]
fn reverse(round: usize, bucket: usize) -> bool {
<i16 as RdxSortTemplate>::reverse(round, bucket)
}
}
rdxsort_template_alias!(isize = i16);

#[cfg(target_pointer_width = "32")]
impl RdxSortTemplate for isize {
#[inline]
fn cfg_nbuckets() -> usize {
<i32 as RdxSortTemplate>::cfg_nbuckets()
}

#[inline]
fn cfg_nrounds() -> usize {
<i32 as RdxSortTemplate>::cfg_nrounds()
}

#[inline]
fn get_bucket(&self, round: usize) -> usize {
(*self as i32).get_bucket(round)
}

#[inline]
fn reverse(round: usize, bucket: usize) -> bool {
<i32 as RdxSortTemplate>::reverse(round, bucket)
}
}
rdxsort_template_alias!(isize = i32);

#[cfg(target_pointer_width = "64")]
impl RdxSortTemplate for isize {
#[inline]
fn cfg_nbuckets() -> usize {
<i64 as RdxSortTemplate>::cfg_nbuckets()
}

#[inline]
fn cfg_nrounds() -> usize {
<i64 as RdxSortTemplate>::cfg_nrounds()
}

#[inline]
fn get_bucket(&self, round: usize) -> usize {
(*self as i64).get_bucket(round)
}

#[inline]
fn reverse(round: usize, bucket: usize) -> bool {
<i64 as RdxSortTemplate>::reverse(round, bucket)
}
}
rdxsort_template_alias!(isize = i64);
29 changes: 29 additions & 0 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,35 @@ pub trait RdxSortTemplate {
fn reverse(round: usize, bucket: usize) -> bool;
}

/// Implements `t1` as alias of `t2`, e.g `usize = u64` on platforms that have 64 bit pointers.
#[macro_export]
macro_rules! rdxsort_template_alias {
($t1:ty = $t2:ty) => {
impl RdxSortTemplate for $t1 {
#[inline]
fn cfg_nbuckets() -> usize {
<$t2 as RdxSortTemplate>::cfg_nbuckets()
}

#[inline]
fn cfg_nrounds() -> usize {
<$t2 as RdxSortTemplate>::cfg_nrounds()
}

#[inline]
fn get_bucket(&self, round: usize) -> usize {
(*self as $t2).get_bucket(round)
}


#[inline]
fn reverse(round: usize, bucket: usize) -> bool {
<$t2 as RdxSortTemplate>::reverse(round, bucket)
}
}
}
}

#[inline]
fn helper_bucket<T, I>(buckets_b: &mut Vec<Vec<T>>, iter: I, cfg_nbuckets: usize, round: usize)
where T: RdxSortTemplate,
Expand Down
66 changes: 3 additions & 63 deletions src/unsigned_integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,70 +93,10 @@ impl RdxSortTemplate for u64 {
}

#[cfg(target_pointer_width = "16")]
impl RdxSortTemplate for usize {
#[inline]
fn cfg_nbuckets() -> usize {
<u16 as RdxSortTemplate>::cfg_nbuckets()
}

#[inline]
fn cfg_nrounds() -> usize {
<u16 as RdxSortTemplate>::cfg_nrounds()
}

#[inline]
fn get_bucket(&self, round: usize) -> usize {
(*self as u16).get_bucket(round)
}

#[inline]
fn reverse(round: usize, bucket: usize) -> bool {
<u16 as RdxSortTemplate>::reverse(round, bucket)
}
}
rdxsort_template_alias!(usize = u16);

#[cfg(target_pointer_width = "32")]
impl RdxSortTemplate for usize {
#[inline]
fn cfg_nbuckets() -> usize {
<u32 as RdxSortTemplate>::cfg_nbuckets()
}

#[inline]
fn cfg_nrounds() -> usize {
<u32 as RdxSortTemplate>::cfg_nrounds()
}

#[inline]
fn get_bucket(&self, round: usize) -> usize {
(*self as u32).get_bucket(round)
}

#[inline]
fn reverse(round: usize, bucket: usize) -> bool {
<u32 as RdxSortTemplate>::reverse(round, bucket)
}
}
rdxsort_template_alias!(usize = u32);

#[cfg(target_pointer_width = "64")]
impl RdxSortTemplate for usize {
#[inline]
fn cfg_nbuckets() -> usize {
<u64 as RdxSortTemplate>::cfg_nbuckets()
}

#[inline]
fn cfg_nrounds() -> usize {
<u64 as RdxSortTemplate>::cfg_nrounds()
}

#[inline]
fn get_bucket(&self, round: usize) -> usize {
(*self as u64).get_bucket(round)
}

#[inline]
fn reverse(round: usize, bucket: usize) -> bool {
<u64 as RdxSortTemplate>::reverse(round, bucket)
}
}
rdxsort_template_alias!(usize = u64);

0 comments on commit 367f425

Please sign in to comment.