Created
June 14, 2020 04:46
-
-
Save wwylele/36359873310c7c2a701ec55f95084ef5 to your computer and use it in GitHub Desktop.
Revisions
-
wwylele created this gist
Jun 14, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ 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])); }