Ejemplo n.º 1
0
int main() {
    std::vector<int> ivec{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4};
    for (auto& i : dropwhile([] (int i) {return i < 5;}, ivec)) {
        std::cout << i << '\n';
        i = 69;
    }
    assert(ivec.at(0) == 1);
    assert(ivec.at(4) == 69);

    for (auto i : dropwhile([] (int i) {return i < 5;}, range(10))) {
        std::cout << i << '\n';
    }

    for (auto i : dropwhile([] (int i) {return i < 5;}, 
                {1, 2, 3, 4, 5, 6, 7, 8, 9})) {
        std::cout << i << '\n';
    }

    for (auto i : dropwhile([] (int i) {return i < 5;}, 
                std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9})) {
        std::cout << i << '\n';
    }

    return 0;
}
Ejemplo n.º 2
0
int main()
{
    std::vector<int> vec = {19, 45, 32, 10, 0, 90, 15, 1, 7, 5, 6, 69};
    for (auto i : sorted(vec)) {
        std::cout << i << '\n';
    }

    const std::vector<int> cvec(vec);
    for (auto i : sorted(cvec)) {
        std::cout << i << '\n';
    }

    std::cout << "Sort by first character only\n";
    std::vector<std::string> svec = {"hello", "everyone", "thanks", "for",
                                     "having", "me", "here", "today"};
    for (auto s : sorted(svec,
                [] (const std::string & s1, const std::string & s2) {
                    return s1[0] < s2[0]; })) {
        std::cout << s << '\n';
    }
        

    for (auto i : sorted(
                std::vector<int>{19, 45, 32, 10, 0, 90, 15, 1, 7, 5, 6, 69})) {
        std::cout << i << '\n';
    }
    return 0;
}
Ejemplo n.º 3
0
void Simplex::neiBottom(iter s) {
  if(s->getBottom()->whos(s) == 0)
    s->getBottom()->setBottom(s);
  else if(s->getBottom()->whos(s) == 1)
    s->getBottom()->setLeft(s);
  else
    s->getBottom()->setRight(s); 
}
Ejemplo n.º 4
0
int main() {
    std::vector<int> vec{1, 5, 6, 7, 2, 3, 8, 3, 2, 1};

    std::cout << "Greater than 4 (function pointer)\n";
    for (auto i : filter(greater_than_four, vec)) {
        std::cout << i << '\n';
    }

    std::cout << "Less than 4 (lambda)\n";
    for (auto i : filter([] (const int i) { return i < 4; }, vec)) {
        std::cout << i << '\n';
    }

    LessThanValue lv(4);
    std::cout << "Less than 4 (callable object)\n";
    for (auto i : filter(lv, vec)) {
        std::cout << i << '\n';
    }

    std::cout << "Nonzero ints filter(vec2)\n";
    std::vector<int> vec2 {0, 1, 2, 0, 3, 0, 0, 0, 4, 5, 0};
    for (auto i : filter(vec2)) {
        std::cout << i << '\n';
    }

    std::cout << "odd numbers in range(10) temp\n";
    for (auto i : filter([] (const int i) {return i % 2;}, iter::range(10))) {
        std::cout << i << '\n';
    }

    std::cout << "range(-1, 2)\n";
    for (auto i : filter(iter::range(-1, 2))) {
        std::cout << i << '\n';
    }


    std::cout << "ever numbers in initializer_list\n";
    for (auto i : filter([] (const int i) {return i % 2 == 0;},
                         {1, 2, 3, 4, 5, 6, 7}))
    {
        std::cout << i << '\n';
    }

    std::cout << "default in initialization_list\n";
    for (auto i : filter({-2, -1, 0, 0, 0, 1, 2})) {
        std::cout << i << '\n';
    }

    std::cout << "ever numbers in vector temporary\n";
    for (auto i : filter([] (const int i) {return i % 2 == 0;},
                         std::vector<int>{1, 2, 3, 4, 5, 6, 7}))
    {
        std::cout << i << '\n';
    }

    return 0;
}
Ejemplo n.º 5
0
  vec pgs::solve(const vec& q, iter it) const {

    vec res = vec::Zero(q.size());

    it.go([&] {

	real eps = 0;
	res.each([&](natural i) {
	
	    real old = res(i);
	    res(i) -=  (q(i) + M.col(i).dot(res)) / M(i, i);
	    
	    // projection
	    res(i) = std::max(0.0, res(i));
	    
	    real delta = res(i) - old;

	    eps += delta * delta;
	  });
	
	return std::sqrt( eps );
      });
    
    return res;
  }
Ejemplo n.º 6
0
int main()
{
    for (auto i : range(10)) {
        std::cout << i << std::endl;
    }
    for (auto i : range(20, 30)) {
        std::cout << i << std::endl;
    }
    for (auto i : range(50, 60, 2)) {
        std::cout << i << std::endl;
    }

    std::cout << "Negative Tests\n";
    for (auto i: range(-10, 0)) {
        std::cout << i << std::endl;
    }

    for (auto i : range(-10, 10, 2)) {
        std::cout << i << std::endl;
    }

    std::cout << "Tests where (stop - start)%step != 0" << std::endl;
    for (auto i : range(1, 10, 2)) {
        std::cout << i << std::endl;
    }

    for (auto i : range(-1, -10, -2)) {
        std::cout << i << std::endl;
    }
    
    // invalid ranges:
    std::cout << "Should not print anything after this line until exception\n";
    for (auto i : range(-10, 0, -1)) {
        std::cout << i << std::endl;
    }

    for (auto i : range(0, 1, -1)) {
        std::cout << i << std::endl;
    }

    std::cout << "Should see exception now\n";
    for (auto i : range(0, 10, 0) ) {
        std::cout << i << std::endl;
    }

    return 0;
}
Ejemplo n.º 7
0
int main() {
    //Ryan's test
    {
        std::vector<int> ivec{1, 4, 9, 16, 25, 36};
        std::vector<std::string> svec{"hello", "good day", "goodbye"};

        for (auto e : zip_longest(ivec, svec)) {
            std::cout << std::get<0>(e) << std::endl; 
            //has to deref iter and the optional object
            std::cout << std::get<1>(e) << std::endl; 
        }
    }
    //Aaron's test
    {
        std::array<int,4> i{{1,2,3,4}};
        std::vector<float> f{1.2,1.4,12.3,4.5,9.9};
        std::vector<std::string> s{"i","like","apples","alot","dude"};
        std::array<double,5> d{{1.2,1.2,1.2,1.2,1.2}};
        std::cout << std::endl << "Variadic template zip_longest" << std::endl;
        for (auto e : iter::zip_longest(i,f,s,d)) {
            std::cout << std::get<0>(e)
                << std::get<1>(e) 
                << std::get<2>(e) 
                << std::get<3>(e) << std::endl;
            **std::get<1>(e)=2.2f; //modify the float array
        }
        std::cout<<std::endl;
        for (auto e : iter::zip_longest(i,s,f,d)) {
            std::cout << std::get<0>(e) 
                << std::get<1>(e) 
                << std::get<2>(e) 
                << std::get<3>(e) << std::endl;
        }
        std::cout << std::endl << "Try some weird range differences" << std::endl;
        std::vector<int> empty{};
        for (auto e : iter::zip_longest(empty,f,s,d)) {
            std::cout << std::get<0>(e) 
                << std::get<1>(e) 
                << std::get<2>(e) 
                << std::get<3>(e) << std::endl;
        }
        std::cout<<std::endl;
        for (auto e : iter::zip_longest(f,empty,s,d)) {
            std::cout << std::get<0>(e) 
                << std::get<1>(e) 
                << std::get<2>(e) 
                << std::get<3>(e) << std::endl;
        }
        std::cout<<std::endl;
        for (auto e : iter::zip_longest(f,s,i,d)) { 
            std::cout << std::get<0>(e) 
                << std::get<1>(e) 
                << std::get<2>(e) 
                << std::get<3>(e) << std::endl;
        }
        std::cout<<std::endl;
    }
    return 0;
}
Ejemplo n.º 8
0
void testcase(std::vector<DataType> data_vec,
        std::vector<SelectorType> sel_vec)
{

    for (auto e : compress(data_vec, sel_vec)) {
        std::cout << e << '\n';
    }
}
Ejemplo n.º 9
0
int main() {
    {
        std::vector<int> ivec{1, 4, 7, 9};
        std::vector<int> lvec{100, 200, 300, 400, 500, 600};

        for (auto e : chain(ivec, lvec)) {
            std::cout << e << std::endl;
        }
    }
    {
         std::vector<int> empty{};
         std::vector<int> vec1{1,2,3,4,5,6};
         std::array<int,4> arr1{{7,8,9,10}};
         std::array<int,3> arr2{{11,12,13}};
         std::cout << std::endl << "Chain iter test" << std::endl;
         for (auto i : iter::chain(empty,vec1,arr1)) {
             std::cout << i << std::endl;
         }
         std::cout<<std::endl;
         for (auto i : iter::chain(vec1,empty,arr1)) {
             std::cout << i << std::endl;
         }
         std::cout<<std::endl;
         for (auto i : iter::chain(vec1,arr1,empty)) {
             std::cout << i << std::endl;
         }
         std::cout<<std::endl;//modifying the range
         for (auto & i : iter::chain(vec1,arr1,arr2)) {
             i = 0;//0 out the whole thing
         }
         std::cout<<std::endl;
         for (auto i : iter::chain(vec1,arr1,arr2)) {
             std::cout << i << std::endl;
         }
         //test only works with perfect forwarding
         std::cout<<std::endl;
         for (auto i : chain(il{1,2,3,4,5},il{6,7,8,9},il{10,11,12})) {
             std::cout << i << std::endl;
         }
    }
    return 0;
}
Ejemplo n.º 10
0
  vec solve(const vec& c, const vec& b, const iter& it) {
    natural m = c.size();
    natural n = b.size();
    
    vec at = vec::Zero( m + n );
    
    vec delta = vec::Zero( m + n );
    vec eps = vec::Constant(n, it.epsilon);
    vec log_eps = eps.array().log();

    vec exp;
    
    auto kkt = [&](const math::vec& dx) {
      
      assert(!nan(dx));
      vec res = vec::Zero(m + n);
      
      res.head(m) = Q * dx.head(m) - A.transpose() * exp.cwiseProduct( dx.tail(n) );
      res.tail(n) = -exp.cwiseProduct( A * dx.head(m) ) - eps.cwiseProduct(dx.tail(n));
      
      // core::log("res:", res.transpose());
      
      assert(!nan(res));
      
      return res;
    };
    
    vec rhs = vec::Zero( m + n );
    
    math::iter sub;
    sub.bound = 1 + std::log(m + n);
    sub.bound = it.bound;
    
    math::natural k = 0;
    it.go([&] {
	exp = at.tail(n).array().exp();
	assert(!nan(exp));
	
	rhs << -c - Q * at.head(m) + A.transpose() * exp,
	  -exp.cwiseProduct( b - A * at.head(m) ) - eps;
	
	assert(!nan(rhs));
	
	delta = math::minres(kkt).solve(rhs, sub);

	at += delta;

	++k;
	return rhs.norm();      
      });
    
    return at.head(m);
  }
Ejemplo n.º 11
0
int main() {
    std::vector<int> vec {1,2,3,4,5,6,7,8,9};
    for (auto v : powerset(vec)) {
        for (auto i : v) std::cout << i << " ";
        std::cout << std::endl;
    }
    std::cout << "with temporary\n";
    for (auto v : powerset(std::vector<int>{1,2,3})) {
        for (auto i : v) std::cout << i << " ";
        std::cout << std::endl;
    }
    std::cout << "with initializer_list\n";
    for (auto v : powerset({1,2,3})) {
        for (auto i : v) std::cout << i << " ";
        std::cout << std::endl;
    }
#if 0
#endif

    return 0;
}
Ejemplo n.º 12
0
int main() {
    std::vector<int> vec{1, 5, 6, 7, 2, 3, 8, 3, 2, 1};

    std::cout << "Greater than 4 (function pointer)\n";
    for (auto i : filterfalse(greater_than_four, vec)) {
        std::cout << i << '\n';
    }

    std::cout << "Less than 4 (lambda)\n";
    for (auto i : filterfalse([] (const int i) { return i < 4; }, vec)) {
        std::cout << i << '\n';
    }

    LessThanValue lv(4);
    std::cout << "Less than 4 (callable object)\n";
    for (auto i : filterfalse(lv, vec)) {
        std::cout << i << '\n';
    }

    return 0;
}
Ejemplo n.º 13
0
int main() {
    std::vector<int> vec1 = {1, 2, 3, 4, 5, 6};
    std::vector<int> vec2 = {10, 20, 30, 40, 50, 60};
    for (auto i : imap([] (int x, int y) {
    return x + y;
}, vec1, vec2)) {
        std::cout << i << '\n';
    }

    std::vector<int> vec3 = {100, 200, 300, 400, 500, 600};
    for (auto i : imap([] (int a, int b, int c) {
    return a + b + c;
},
vec1, vec2, vec3)) {
        std::cout << i << '\n';
    }

    for (auto i : imap([] (int i) {
    return i * i;
}, vec1)) {
        std::cout << i << '\n';
    }

    std::vector<int> vec{1, 2, 3, 4, 5};
    for (auto i : imap([] (int x) {
    return x * x;
}, vec)) {
        std::cout << i << '\n';
    }

    std::vector<int> vec4{1, 2, 3};
    for (auto i : imap([] (int a, int b) {
    return a + b;
}, vec, vec4)) {
        std::cout << i << '\n';
    }

    return 0;
}
Ejemplo n.º 14
0
int main() {
    std::vector<int> ivec{1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1};
    for (auto i : takewhile([] (int i) {return i < 5;}, ivec)) {
        std::cout << i << '\n';
    }

    for (auto i : takewhile([] (int i) {return i < 5;}, range(10))) {
        std::cout << i << '\n';
    }

    for (auto i : takewhile([] (int i) {return i < 5;}, 
                {1, 2, 3, 4, 5, 6, 7, 8, 9})) {
        std::cout << i << '\n';
    }

    std::cout << "with temporary\n";
    for (auto i : takewhile([] (int i) {return i < 5;}, 
                std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9})) {
        std::cout << i << '\n';
    }

    return 0;
}
int main() {
    std::vector<int> v = {1,2,3,4,5};
    for (auto i : combinations_with_replacement(v,40)) {
        //std::cout << i << std::endl;
        for (auto j : i ) std::cout << j << " ";
        std::cout<<std::endl;
    }
    for (auto i : combinations_with_replacement(v,4)) {
        //std::cout << i << std::endl;
        for (auto j : i ) std::cout << j << " ";
        std::cout<<std::endl;
    }
    for (auto i : combinations_with_replacement(v,1)) {
        //std::cout << i << std::endl;
        for (auto j : i ) std::cout << j << " ";
        std::cout<<std::endl;
    }
    for (auto i : combinations_with_replacement({1,2,3,4},4)) {
        //std::cout << i << std::endl;
        for (auto j : i ) std::cout << j << " ";
        std::cout<<std::endl;
    }
    return 0;
}
Ejemplo n.º 16
0
int main() {
    std::vector<int> v {1,2,3,4,5,6,7,8,9}; 
    for (auto sec : grouper(v,4)) {
        for (auto i : sec) {
            std::cout << i << " ";
            i.get() *= 2;
        }
        std::cout << '\n';
    }

    for (auto sec : grouper(std::vector<int>{1,2,3,4,5,6,7,8,9}, 4)) {
        for (auto i : sec) {
            std::cout << i << " ";
            i.get() *= 2;
        }
        std::cout << '\n';
    }

    for (auto sec : grouper(v,3)) {
        for (auto i : sec) {
            std::cout << i << " ";
        }
        std::cout << '\n';
    }
    std::vector<int> empty {};
    for (auto sec : grouper(empty,3)) {
        std::cout << "Shouldn't print\n";
        for (auto i : sec) {
            std::cout << i << " Shouldn't print\n";
        }
    }

    int arr[] = {1,2,3,4,5,6,7};
    for (auto sec : grouper(arr, 2)) {
        for (auto i : sec) {
            std::cout << i << ' ';
        }
        std::cout << '\n';
    }

    for (auto sec : grouper({1,2,3,4,5,6,7}, 2)) {
        for (auto i : sec) {
            std::cout << i << ' ';
        }
        std::cout << '\n';
    }
}
int main() {
    std::vector<itertest::MoveOnly> mv;
    for (auto i : iter::range(3)) {
        mv.emplace_back(i);
    }

    std::vector<int> v = {1,2,3,};
    for (auto i : combinations_with_replacement(v,4)) {
        for (auto j : i ) std::cout << j << " ";
        std::cout<<std::endl;
    }

    itertest::DerefByValue dbv;
    std::cout << "with deref by value iterator\n";
    for (auto i : combinations_with_replacement(dbv, 2)) {
        for (auto j : i ) std::cout << j << " ";
        std::cout<<std::endl;
    }

    std::cout << "with container of move-only\n";
    for (auto i : combinations_with_replacement(mv,2)) {
        for (auto j : i ) std::cout << j << " ";
        std::cout<<std::endl;
    }

    std::cout << "with temporary\n";
    for (auto i : combinations_with_replacement(std::vector<int>{1,2,3},4)) {
        for (auto j : i ) std::cout << j << " ";
        std::cout<<std::endl;
    }

    std::cout << "with init list\n";
    for (auto i : combinations_with_replacement({1,2,3},4)) {
        for (auto j : i ) std::cout << j << " ";
        std::cout<<std::endl;
    }

    std::cout << "with static array\n";
    int arr[] = {1, 2, 3};
    for (auto i : combinations_with_replacement(arr,4)) {
        for (auto j : i ) std::cout << j << " ";
        std::cout<<std::endl;
    }
}
Ejemplo n.º 18
0
int main() {
    std::vector<int> vec{1, 5, 6, 7, 2, 3, 8, 3, 2, 1};

    std::cout << "Greater than 4 (function pointer)\n";
    for (auto i : filterfalse(greater_than_four, vec)) {
        std::cout << i << '\n';
    }

    std::cout << "Less than 4 (lambda)\n";
    for (auto i : filterfalse([] (const int i) { return i < 4; }, vec)) {
        std::cout << i << '\n';
    }

    LessThanValue lv(4);
    std::cout << "Less than 4 (callable object)\n";
    for (auto i : filterfalse(lv, vec)) {
        std::cout << i << '\n';
    }

    std::cout << "zero ints filter(vec2)\n";
    std::vector<int> vec2 {0, 1, 2, 0, 3, 0, 0, 0, 4, 5, 0};
    for (auto i : filterfalse(vec2)) {
        std::cout << i << '\n';
    }

    std::cout << "Constness tests\n";
    const std::vector<int> cvec(vec);
    for (auto i : filterfalse(greater_than_four, cvec)) {
        std::cout << i << '\n';
    }

    for (auto i : filterfalse([] (const int & i) { return i < 4; }, cvec)) {
        std::cout << i << '\n';
    }

    return 0;
}
Ejemplo n.º 19
0
#include "helpers.hpp"

#include <vector>
#include <string>
#include <iterator>

#include "catch.hpp"

using iter::dropwhile;

using Vec = const std::vector<int>;

TEST_CASE("dropwhile: skips initial elements", "[dropwhile]") {
    Vec ns{1,2,3,4,5,6,7,8};
    auto d = dropwhile([](int i){return i < 5; }, ns);
    Vec v(std::begin(d), std::end(d));
    Vec vc = {5,6,7,8};
    REQUIRE( v == vc );
}

TEST_CASE("dropwhile: doesn't skip anything if it shouldn't", "[dropwhile]") {
    Vec ns {3,4,5,6};
    auto d = dropwhile([](int i){return i < 3; }, ns);
    Vec v(std::begin(d), std::end(d));
    Vec vc = {3,4,5,6};
    REQUIRE( v == vc );
}

TEST_CASE("dropwhile: skips all elements when all are true under predicate",
        "[dropwhile]") {
Ejemplo n.º 20
0
    int operator()(int a) {
      return a;
    }
  };
}

TEST_CASE("starmap: works with function pointer and lambda", "[starmap]") {
  using Vec = const std::vector<int>;
  const std::vector<std::pair<double, int>> v1 = {{1l, 2}, {3l, 11}, {6l, 7}};
  Vec vc = {2l, 33l, 42l};

  std::vector<int> v;
  SECTION("with function") {
    SECTION("Normal call") {
      auto sm = starmap(f, v1);
      v.assign(std::begin(sm), std::end(sm));
    }
    SECTION("pipe") {
      auto sm = v1 | starmap(f);
      v.assign(std::begin(sm), std::end(sm));
    }
  }

  SECTION("with lambda") {
    auto sm = starmap([](long a, int b) { return a * b; }, v1);
    v.assign(std::begin(sm), std::end(sm));
  }
  REQUIRE(v == vc);
}
Ejemplo n.º 21
0
  };
  auto dec_ten = [](MyUnMovable& el) -> MyUnMovable& {
    int va = el.get_val();
    el.set_val(va - 10);
    return el;
  };
}

TEST_CASE("filtering doesn't dereference multiple times", "[imap][filter]") {
  using iter::filter;
  using iter::imap;

  // source data
  std::array<MyUnMovable, 3> arr = {{{41}, {42}, {43}}};

  auto transformed1 = imap(inc_ten, arr);
  auto filtered = filter(
      [](const MyUnMovable& el) { return 52 != el.get_val(); }, transformed1);
  auto transformed2 = imap(dec_ten, filtered);

  std::vector<int> v;
  for (auto&& el : transformed2) {
    // I would use imap again instead of the loop if this wasn't an imap
    // test
    v.push_back(el.get_val());
  }

  std::vector<int> vc = {41, 43};

  REQUIRE(v == vc);
Ejemplo n.º 22
0
#include <vector>
#include <iterator>
#include <utility>

#include "catch.hpp"

using iter::chain;
using itertest::SolidInt;
using itertest::BasicIterable;
using Vec = const std::vector<char>;

TEST_CASE("chain: three strings", "[chain]") {
  std::string s1{"abc"};
  std::string s2{"mno"};
  std::string s3{"xyz"};
  auto ch = chain(s1, s2, s3);

  Vec v(std::begin(ch), std::end(ch));
  Vec vc{'a', 'b', 'c', 'm', 'n', 'o', 'x', 'y', 'z'};

  REQUIRE(v == vc);
}

TEST_CASE("chain: with different container types", "[chain]") {
  std::string s1{"abc"};
  std::list<char> li{'m', 'n', 'o'};
  std::vector<char> vec{'x', 'y', 'z'};
  auto ch = chain(s1, li, vec);

  Vec v(std::begin(ch), std::end(ch));
  Vec vc{'a', 'b', 'c', 'm', 'n', 'o', 'x', 'y', 'z'};
Ejemplo n.º 23
0
int main() {

    std::vector<itertest::MoveOnly> mv;
    for (auto i : iter::range(10)) {
        mv.emplace_back(i);
    }
    std::vector<int> empty{};
    std::vector<int> v1{1,2,3};
    std::vector<int> v2{7,8};
    std::vector<std::string> v3{"the","cat"};
    std::vector<std::string> v4{"hi","what","up","dude"};

    for (auto t : product(v1, mv)) {
        std::cout << std::get<0>(t) << ", "
            << std::get<1>(t) << std::endl;
    }
    for (auto t : product(empty,v1)) {
        std::cout << std::get<0>(t) << ", "
            << std::get<1>(t) << std::endl;
    }
    for (auto t : product(v1,empty)) {
        std::cout << std::get<0>(t) << ", "
            << std::get<1>(t) << std::endl;
    }
    std::cout<<std::endl;
    for (auto t : product(v1,v2)) {
        std::cout << std::get<0>(t) << ", "
            << std::get<1>(t) << std::endl;
    }
    std::cout<<std::endl;
    for (auto t : product(v1,v2,v3,v4)) {
        std::cout << std::get<0>(t) << ", "
            << std::get<1>(t) << ", "
            << std::get<2>(t) << ", " 
            << std::get<3>(t) << std::endl;
    }
    std::cout<<std::endl;
    for (auto t : product(v1)) {
        std::cout << std::get<0>(t)  << std::endl;
    }
    std::cout<<std::endl;
    for (auto t : product(v1,v4)) {
        std::cout << std::get<0>(t) << ", "
          << std::get<1>(t)  << std::endl;
    }
    std::cout << '\n';

    for (auto t : product()) { t=t; }

    for (auto t : product(std::string{"hi"}, v1)) {
        std::cout << std::get<0>(t) << ", "
            << std::get<1>(t) << std::endl;
    }
    std::cout << '\n';

    int arr[] = {1,2};
    for (auto t : product(std::string{"hi"}, arr)) {
        std::cout << std::get<0>(t) << ", "
            << std::get<1>(t) << std::endl;
    }
    std::cout << '\n';
    for (auto&& ij: iter::product(iter::range(10), iter::range(5))) {
        std::cout << std::get<0>(ij) << "," << std::get<1>(ij) << std::endl;
    }

    return 0;
}
Ejemplo n.º 24
0
#include <permutations.hpp>

#include "helpers.hpp"

#include <vector>
#include <string>
#include <iterator>

#include "catch.hpp"

using iter::permutations;
using IntPermSet = std::multiset<std::vector<int>>;

TEST_CASE("permutations: basic test, 3 element sequence", "[permutations]") {
  const std::vector<int> ns = {1, 7, 9};
  auto p = permutations(ns);

  IntPermSet v;
  for (auto&& st : p) {
    v.emplace(std::begin(st), std::end(st));
  }

  const IntPermSet vc = {
      {1, 7, 9}, {1, 9, 7}, {7, 1, 9}, {7, 9, 1}, {9, 1, 7}, {9, 7, 1}};
  REQUIRE(v == vc);
}

TEST_CASE(
    "permutations: empty sequence has one empy permutation", "[permutations]") {
  const std::vector<int> ns{};
  auto p = permutations(ns);
Ejemplo n.º 25
0
#include <sliding_window.hpp>

#include <vector>
#include <array>
#include <string>
#include <utility>

#include "helpers.hpp"
#include "catch.hpp"

using iter::sliding_window;
using Vec = const std::vector<int>;

TEST_CASE("sliding_window: window of size 3", "[sliding_window]") {
    Vec ns = { 10, 20, 30, 40, 50};
    auto sw = sliding_window(ns, 3);
    auto it = std::begin(sw);
    REQUIRE( it != std::end(sw) );
    {
        Vec v(std::begin(*it), std::end(*it));
        Vec vc = {10, 20, 30};
        REQUIRE( v == vc );

    }
    ++it;
    REQUIRE( it != std::end(sw) );
    {
        Vec v(std::begin(*it), std::end(*it));
        Vec vc = {20, 30, 40};
        REQUIRE( v == vc );
    }
Ejemplo n.º 26
0
  bool under_ten(int i) {
    return i < 10;
  }

  struct UnderTen {
    bool operator()(int i) {
      return i < 10;
    }
  };
}

TEST_CASE("takewhile: works with lambda, callable, and function pointer",
    "[takewhile]") {
  Vec ns = {1, 3, 5, 20, 2, 4, 6, 8};
  SECTION("function pointer") {
    auto tw = takewhile(under_ten, ns);
    Vec v(std::begin(tw), std::end(tw));
    Vec vc = {1, 3, 5};
    REQUIRE(v == vc);
  }

  SECTION("callable object") {
    std::vector<int> v;
    SECTION("Normal call") {
      auto tw = takewhile(UnderTen{}, ns);
      v.assign(std::begin(tw), std::end(tw));
    }

    SECTION("Pipe") {
      auto tw = ns | takewhile(UnderTen{});
      v.assign(std::begin(tw), std::end(tw));
Ejemplo n.º 27
0
            int compare_val;

        public:
            LessThanValue(int v) : compare_val(v) { }

            bool operator() (int i) {
                return i < this->compare_val;
            }
    };
}

TEST_CASE("filter: handles different functor types", "[filter]") {
    Vec ns = {1,2, 5,6, 3,1, 7, -1, 5};
    Vec vc = {1,2,3,1,-1};
    SECTION("with function pointer") {
        auto f = filter(less_than_five, ns);
        Vec v(std::begin(f), std::end(f));
        REQUIRE( v == vc );
    }

    SECTION("with callable object") {
        auto f = filter(LessThanValue{5}, ns);
        Vec v(std::begin(f), std::end(f));
        REQUIRE( v == vc );
    }

    SECTION("with lambda") {
        auto ltf = [](int i) {return i < 5;};
        auto f = filter(ltf, ns);
        Vec v(std::begin(f), std::end(f));
        REQUIRE( v == vc );
Ejemplo n.º 28
0
            }
    };

    int power(int b, int e) {
        int acc = 1;
        for (int i = 0; i < e; ++i) {
            acc *= b;
        }
        return acc;
    }
}

TEST_CASE("imap: works with lambda, callable, and function", "[imap]") {
    Vec ns = {10, 20, 30};
    SECTION("with lambda") {
        auto im = imap([](int i) { return i + 1; }, ns);
        Vec v(std::begin(im), std::end(im));
        Vec vc = {11, 21, 31};
        REQUIRE( v == vc );
    }

    SECTION("with function") {
        auto im = imap(plusone, ns);
        Vec v(std::begin(im), std::end(im));
        Vec vc = {11, 21, 31};
        REQUIRE( v == vc );
    }

    SECTION("with function") {
        auto im = imap(PlusOner{}, ns);
        Vec v(std::begin(im), std::end(im));
Ejemplo n.º 29
0
#include <iterator>

#include "catch.hpp"

using iter::zip;
using itertest::BasicIterable;
using itertest::SolidInt;

TEST_CASE("zip: Simple case, same length", "[zip]") {
  using Tu = std::tuple<int, char, double>;
  using ResVec = const std::vector<Tu>;
  std::vector<int> iv{10, 20, 30};
  std::string s{"hey"};
  double arr[] = {1.0, 2.0, 4.0};

  auto z = zip(iv, s, arr);
  ResVec v(std::begin(z), std::end(z));
  ResVec vc{Tu{10, 'h', 1.0}, Tu{20, 'e', 2.0}, Tu{30, 'y', 4.0}};
  REQUIRE(v == vc);
}

TEST_CASE("zip: One empty, all empty", "[zip]") {
  std::vector<int> iv = {1, 2, 3};
  std::string s{};
  auto z = zip(iv, s);
  REQUIRE_FALSE(std::begin(z) != std::end(z));
  auto z2 = zip(s, iv);
  REQUIRE_FALSE(std::begin(z2) != std::end(z2));
}

TEST_CASE("zip: terminates on shortest sequence", "[zip]") {
Ejemplo n.º 30
0
#include <repeat.hpp>

#include "helpers.hpp"

#include <vector>
#include <string>
#include <iterator>

#include "catch.hpp"

using iter::repeat;

TEST_CASE("repeat: one argument keeps giving value back", "[repeat]") {
  auto r = repeat('a');
  auto it = std::begin(r);
  REQUIRE(*it == 'a');
  ++it;
  REQUIRE(*it == 'a');
  ++it;
  REQUIRE(*it == 'a');
  ++it;
  REQUIRE(*it == 'a');
  ++it;
  REQUIRE(*it == 'a');
}

TEST_CASE("repeat: can be used as constexpr", "[repeat]") {
  static constexpr char c = 'a';
  {
    constexpr auto r = repeat(c);
    constexpr auto i = r.begin();