forked from alabuzhev/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd46ff3
commit a740829
Showing
35 changed files
with
1,982 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
*.o | ||
*.swp | ||
test* | ||
!test*.cpp | ||
.sconsign.dblite |
This file contains 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 characters
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)) |
This file contains 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 characters
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 |
This file contains 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 characters
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; | ||
} |
This file contains 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 characters
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'; | ||
} | ||
} | ||
} |
This file contains 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 characters
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; | ||
} |
This file contains 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 characters
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'; | ||
} | ||
} |
Oops, something went wrong.