Skip to content

Instantly share code, notes, and snippets.

@wwylele
Created June 14, 2020 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wwylele/36359873310c7c2a701ec55f95084ef5 to your computer and use it in GitHub Desktop.
Save wwylele/36359873310c7c2a701ec55f95084ef5 to your computer and use it in GitHub Desktop.
Rust template template parameter
use std::collections::*;
use std::hash::Hash;
use std::iter::FromIterator;
trait SetFunc<T> {
type Set: IntoIterator + FromIterator<T>;
}
struct BTreeSetF;
impl<T: Ord + Eq> SetFunc<T> for BTreeSetF {
type Set = BTreeSet<T>;
}
struct HashSetF;
impl<T: Eq + Hash> SetFunc<T> for HashSetF {
type Set = HashSet<T>;
}
fn uni<Set: SetFunc<T> + SetFunc<(T,T)>, T: Copy>(input: &[T]) -> usize {
input.iter().copied().collect::
< <Set as SetFunc<T>>::Set >
().into_iter().count() *
input.windows(2).map(|a|(a[0], a[1])).collect::
< <Set as SetFunc<(T,T)>>::Set >
().into_iter().count()
}
fn main() {
println!("{}", uni::<BTreeSetF, _>(&[1,2,3,3,2,3]));
println!("{}", uni::<HashSetF, _>(&[1,2,3,3,2,3]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment