Skip to content

Instantly share code, notes, and snippets.

@wwylele
Created June 14, 2020 04:46
Show Gist options
  • Select an option

  • Save wwylele/36359873310c7c2a701ec55f95084ef5 to your computer and use it in GitHub Desktop.

Select an option

Save wwylele/36359873310c7c2a701ec55f95084ef5 to your computer and use it in GitHub Desktop.

Revisions

  1. wwylele created this gist Jun 14, 2020.
    36 changes: 36 additions & 0 deletions main.rs
    Original 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]));
    }