Skip to content

Commit

Permalink
moves void_t into iterbase
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhaining committed Apr 8, 2015
1 parent dd46ff3 commit a740829
Show file tree
Hide file tree
Showing 35 changed files with 1,982 additions and 9 deletions.
12 changes: 3 additions & 9 deletions catchtest/helpers.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef TEST_HELPER_H_
#define TEST_HELPER_H_

#include <iterbase.hpp>

#include <stdexcept>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -170,15 +172,7 @@ class BasicIterable {
}
};


// gcc CWG 1558
template <typename...>
struct void_t_help {
using type = void;
};
template <typename... Ts>

using void_t = typename void_t_help<Ts...>::type;
using iter::void_t;

template <typename, typename =void>
struct IsIterator : std::false_type { };
Expand Down
5 changes: 5 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.o
*.swp
test*
!test*.cpp
.sconsign.dblite
51 changes: 51 additions & 0 deletions examples/SConstruct
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os

env = Environment(
ENV = {'PATH' : os.environ['PATH']},
CXX='c++',
CXXFLAGS= ['-g', '-Wall', '-Wextra',
'-pedantic', '-std=c++11',
'-fdiagnostics-color=always',
'-I/usr/local/include'],
CPPPATH='..',
LINKFLAGS='-L/usr/local/lib')

# allows highighting to print to terminal from compiler output
env['ENV']['TERM'] = os.environ['TERM']

progs = Split('''
accumulate
cycle
enumerate
range
zip
slice
reversed
filter
repeat
takewhile
dropwhile
zip_longest
product
permutations
compress
combinations_with_replacement
combinations
powerset
sliding_window
imap
count
filterfalse
grouper
chain
chainfromiterable
groupby
sorted
unique_justseen
unique_everseen
command_chains
''')


for p in progs:
env.Program('test{0}.cpp'.format(p))
98 changes: 98 additions & 0 deletions examples/samples.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#ifndef ITERTOOLS_SAMPLE_CLASSES_HPP
#define ITERTOOLS_SAMPLE_CLASSES_HPP

#include <iostream>
#include <utility>
#include <cstddef>

namespace itertest {
class MoveOnly {
private:
int i; // not an aggregate
public:
MoveOnly(int v)
: i{v}
{ }

MoveOnly(const MoveOnly&) = delete;
MoveOnly& operator=(const MoveOnly&) = delete;

MoveOnly(MoveOnly&& other) noexcept
: i{other.i}
{ }

MoveOnly& operator=(MoveOnly&& other) noexcept {
this->i = other.i;
return *this;
}

// for std::next_permutation compatibility
friend bool operator<(const MoveOnly& lhs, const MoveOnly& rhs) {
return lhs.i < rhs.i;
}

friend std::ostream& operator<<(
std::ostream& out, const MoveOnly& self) {
return out << self.i;
}

};

class DerefByValue {
private:
static constexpr std::size_t N = 3;
int array[N] = {0, 1, 2};
public:
DerefByValue() = default;

class Iterator {
private:
int *current;
public:
Iterator() = default;
Iterator(int *p)
: current{p}
{ }

bool operator!=(const Iterator& other) const {
return this->current != other.current;
}

// for testing, iterator derefences to an int instead of
// an int&
int operator*() /*const*/ {
return *this->current;
}

Iterator& operator++() {
++this->current;
return *this;
}
};

Iterator begin() {
return {this->array};
}

Iterator end() {
return {this->array + N};
}
};

class DerefByValueFancy {
private:
static constexpr std::size_t N = 3;
int array[N] = {0, 1, 2};
public:
DerefByValueFancy() = default;

int *begin() {
return this->array;
}

int *end() {
return this->array + N;
}
};
}
#endif // #ifndef ITERTOOLS_SAMPLE_CLASSES_HPP
35 changes: 35 additions & 0 deletions examples/testaccumulate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <accumulate.hpp>
#include <range.hpp>

#include <iostream>
#include <vector>

int main() {
// accumulate with a lambda for subtraction
std::vector<int> vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (auto v : iter::accumulate(vec, [](int a, int b){return a - b;})) {
std::cout << v << '\n';
}

// using a range instead of a vector
for (auto v : iter::accumulate(iter::range(10),
[](int a, int b){return a - b;})) {
std::cout << v << '\n';
}

// using a range and the default summing behavior
for (auto v : iter::accumulate(iter::range(10))) {
std::cout << v << '\n';
}


for (auto v : iter::accumulate({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})) {
std::cout << v << '\n';
}

for (auto v : iter::accumulate(std::vector<int>{1,2,3,4,5,6,7,8,9})) {
std::cout << v << '\n';
}

return 0;
}
61 changes: 61 additions & 0 deletions examples/testchain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <chain.hpp>
#include <range.hpp>

#include <vector>
#include <string>
#include <iostream>
#include <array>
#include <initializer_list>

using iter::chain;
using il = std::initializer_list<int>;

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;
}

// test with temporaries
std::cout << '\n';
for (auto i : chain(std::vector<int>{1,2,3,4},
std::array<int,4>{{5,6,7,8}})) {
std::cout << i << '\n';
}
}
}
28 changes: 28 additions & 0 deletions examples/testchainfromiterable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <chain.hpp>

#include <iostream>
#include <vector>

using iter::chain;

int main() {
std::vector<std::vector<int>> matrix = {
{1, 2, 3},
{4, 5},
{6, 8, 9, 10, 11, 12}
};
for (auto i : chain.from_iterable(matrix)) {
std::cout << i << '\n';
}

std::cout << "with temporary\n";
for (auto i : chain.from_iterable(std::vector<std::vector<int>>{
{1, 2, 3},
{4, 5},
{6, 8, 9, 10, 11, 12}
})) {
std::cout << i << '\n';
}

return 0;
}
75 changes: 75 additions & 0 deletions examples/testcombinations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "samples.hpp"
#include <combinations.hpp>
#include <range.hpp>

#include <vector>
#include <string>
#include <iostream>
#include <array>

using iter::combinations;
int main() {
itertest::DerefByValue dbv;
std::vector<itertest::MoveOnly> mv;
for (auto i : iter::range(3)) {
mv.emplace_back(i);
}
std::vector<int> v = {1,2,3,4,5};

for (auto&& i : combinations(mv,2)) {
for (auto&& j : i ) std::cout << j << " ";
std::cout<<std::endl;
}

for (auto i : combinations(v,0)) {
for (auto j : i ) std::cout << j << " ";
std::cout<<std::endl;
}

for (auto i : combinations(v,3)) {
for (auto j : i ) std::cout << j << " ";
std::cout<<std::endl;
}

for (auto i : combinations(v,1)) {
for (auto j : i ) std::cout << j << " ";
std::cout<<std::endl;
}

for (auto i : combinations(v,5)) {
for (auto j : i ) std::cout << j << " ";
std::cout<<std::endl;
}

std::cout << "deref by value\n";
for (auto&& i : combinations(dbv, 2)) {
for (auto&& j : i) std::cout << j << " ";
std::cout << '\n';
}

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

}

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

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

std::cout << "static array\n";
int array[] = {1,2,3,4,5};
for (auto i : combinations(array, 3)) {
for (auto j : i) std::cout << j << " ";
std::cout << '\n';
}
}
Loading

0 comments on commit a740829

Please sign in to comment.