Skip to content

Commit

Permalink
support tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
crepererum committed Aug 18, 2016
1 parent f97bfdf commit fcc5585
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ mod char;
mod floats;
mod signed_integer;
mod template;
mod tuple;
mod unsigned_integer;

pub use template::RdxSortTemplate;
120 changes: 120 additions & 0 deletions src/tuple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use template::RdxSortTemplate;

use std::cmp;

impl RdxSortTemplate for () {
#[inline]
fn cfg_nbuckets() -> usize {
0
}

#[inline]
fn cfg_nrounds() -> usize {
0
}

#[inline]
fn get_bucket(&self, _round: usize) -> usize {
unreachable!()
}

#[inline]
fn reverse(_round: usize, _bucket: usize) -> bool {
unreachable!()
}
}

impl<A> RdxSortTemplate for (A,) where A: RdxSortTemplate
{
#[inline]
fn cfg_nbuckets() -> usize {
A::cfg_nbuckets()
}

#[inline]
fn cfg_nrounds() -> usize {
A::cfg_nrounds()
}

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

#[inline]
fn reverse(round: usize, bucket: usize) -> bool {
A::reverse(round, bucket)
}
}

impl<A, B> RdxSortTemplate for (A, B)
where A: RdxSortTemplate,
B: RdxSortTemplate
{
#[inline]
fn cfg_nbuckets() -> usize {
cmp::max(A::cfg_nbuckets(), B::cfg_nbuckets())
}

#[inline]
fn cfg_nrounds() -> usize {
A::cfg_nrounds() + B::cfg_nrounds()
}

#[inline]
fn get_bucket(&self, round: usize) -> usize {
if round < B::cfg_nrounds() {
self.1.get_bucket(round)
} else {
self.0.get_bucket(round - B::cfg_nrounds())
}
}

#[inline]
fn reverse(round: usize, bucket: usize) -> bool {
if round < B::cfg_nrounds() {
B::reverse(round, bucket)
} else {
A::reverse(round - B::cfg_nrounds(), bucket)
}
}
}

impl<A, B, C> RdxSortTemplate for (A, B, C)
where A: RdxSortTemplate,
B: RdxSortTemplate,
C: RdxSortTemplate
{
#[inline]
fn cfg_nbuckets() -> usize {
cmp::max(A::cfg_nbuckets(),
cmp::max(B::cfg_nbuckets(), C::cfg_nbuckets()))
}

#[inline]
fn cfg_nrounds() -> usize {
A::cfg_nrounds() + B::cfg_nrounds() + C::cfg_nrounds()
}

#[inline]
fn get_bucket(&self, round: usize) -> usize {
if round < C::cfg_nrounds() {
self.2.get_bucket(round)
} else if round < B::cfg_nrounds() + C::cfg_nrounds() {
self.1.get_bucket(round - C::cfg_nrounds())
} else {
self.0.get_bucket(round - B::cfg_nrounds() - C::cfg_nrounds())
}
}

#[inline]
fn reverse(round: usize, bucket: usize) -> bool {
if round < C::cfg_nrounds() {
C::reverse(round, bucket)
} else if round < B::cfg_nrounds() + C::cfg_nrounds() {
B::reverse(round - C::cfg_nrounds(), bucket)
} else {
A::reverse(round - B::cfg_nrounds() - C::cfg_nrounds(), bucket)
}
}
}
62 changes: 62 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ macro_rules! trivial_myhash {

trivial_myhash!([u8; 0]);
trivial_myhash!([u8; 4]);
trivial_myhash!((u8,));
trivial_myhash!((u8, i32));
trivial_myhash!((u8, i32, char));
trivial_myhash!(bool);
trivial_myhash!(char);
trivial_myhash!(i8);
Expand Down Expand Up @@ -443,3 +446,62 @@ mod sub_f64 {
test_single_generic::<f64>(3f64);
}
}

mod sub_tuple {
use super::*;

#[test]
fn test_empty_tuple0() {
test_empty_generic::<()>();
}

#[test]
fn test_single_tuple0() {
test_single_generic::<()>(());
}

#[test]
fn test_rnd_tuple1() {
test_rnd_generic::<(u8,)>(vec![]);
}

#[test]
fn test_empty_tuple1() {
test_empty_generic::<(u8,)>();
}

#[test]
fn test_single_tuple1() {
test_single_generic::<(u8,)>((1u8,));
}

#[test]
fn test_rnd_tuple2() {
test_rnd_generic::<(u8, i32)>(vec![]);
}

#[test]
fn test_empty_tuple2() {
test_empty_generic::<(u8, i32)>();
}

#[test]
fn test_single_tuple2() {
test_single_generic::<(u8, i32)>((1u8, 1337i32));
}

#[test]
fn test_rnd_tuple3() {
test_rnd_generic::<(u8, i32, char)>(vec![]);
}

#[test]
fn test_empty_tuple3() {
test_empty_generic::<(u8, i32, char)>();
}

#[test]
fn test_single_tuple3() {
test_single_generic::<(u8, i32, char)>((1u8, 1337i32, 'x'));
}
}

0 comments on commit fcc5585

Please sign in to comment.