From 7eef405eeff59c27691be294906e8381a76771e2 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Wed, 4 Feb 2015 17:38:49 +0100 Subject: Implemented the Sieve of Eratosthenes as a basic example --- example/prime/prime.cc | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 example/prime/prime.cc (limited to 'example/prime/prime.cc') diff --git a/example/prime/prime.cc b/example/prime/prime.cc new file mode 100644 index 0000000..9d10ff4 --- /dev/null +++ b/example/prime/prime.cc @@ -0,0 +1,63 @@ +#include + +#include "type.h" +#include "operation/math.h" + +#include "list/list.h" +#include "list/operation/higher/filter.h" +#include "list/generator/iota.h" + +#include "runtime/list/for_each.h" + +using candidates = tav::Iota, tav::Int<2>, tav::Int<1>>::type; + +template < + typename Candidate, + typename Base +> +using isMultipleOf = tav::EqualValue< + tav::Modulo, + tav::Int<0> +>; + +template < + typename Candidates, + typename Base +> +class removeMultiplesOf { + private: + template + using predicate_wrapper = isMultipleOf; + + public: + typedef typename tav::Remove< + predicate_wrapper, + Candidates + >::type type; +}; + +template +struct Sieve { + typedef tav::Cons< + tav::Head, + typename Sieve< + typename removeMultiplesOf< + tav::Tail, + tav::Head + >::type + >::type + > type; +}; + +template <> +struct Sieve { + typedef void type; +}; + +using primes = Sieve::type; + +int main(int, char **) { + tav::runtime::for_each([](const int x) { + std::cout << x << std::endl; + }); +} -- cgit v1.2.3