Skip to content

Commit

Permalink
Add std::set generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam-Rowell committed Sep 14, 2022
1 parent f8287b0 commit d008ae2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
13 changes: 7 additions & 6 deletions include/autocheck/generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cassert>
#include <random>
#include <vector>
#include <set>
#include <iterator>
#include <limits>
#include <algorithm>
Expand Down Expand Up @@ -240,9 +241,7 @@ namespace autocheck {
class generator<std::basic_string<CharType>> :
public string_generator<generator<CharType>> {};

/* TODO: Generic sequence generator. */

template <typename Gen>
template <typename Gen, template<typename, typename...> class Container = std::vector>
class list_generator {
private:
Gen eltgen;
Expand All @@ -251,12 +250,11 @@ namespace autocheck {
list_generator(const Gen& eltgen = Gen()) :
eltgen(eltgen) {}

typedef std::vector<typename Gen::result_type> result_type;
typedef Container<typename Gen::result_type> result_type;

result_type operator() (size_t size = 0) {
result_type rv;
rv.reserve(size);
std::generate_n(std::back_insert_iterator<result_type>(rv), size,
std::generate_n(std::insert_iterator<result_type>(rv, rv.end()), size,
fix(size, eltgen));
return rv;
}
Expand All @@ -275,6 +273,9 @@ namespace autocheck {
template <typename T>
class generator<std::vector<T>> : public list_generator<generator<T>> {};

template<typename T>
class generator<std::set<T>> : public list_generator<generator<T>, std::set> {};

/* Ordered list combinator. */

namespace detail {
Expand Down
4 changes: 4 additions & 0 deletions include/autocheck/ostream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <ostream>
#include <tuple>
#include <vector>
#include <set>

/* Forward declarations of output stream operators. */

Expand All @@ -15,6 +16,9 @@ namespace autocheck {
template <typename T>
std::ostream& operator<< (std::ostream& out, const std::vector<T>& seq);

template <typename T>
std::ostream& operator<< (std::ostream& out, const std::set<T>& seq);

}

#endif
Expand Down
16 changes: 16 additions & 0 deletions include/autocheck/sequence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ namespace autocheck {
return out;
}

template <typename T>
std::ostream& operator<< (std::ostream& out, const std::set<T>& seq) {
out << "[";
bool first = true;
for (auto b = seq.begin(), e = seq.end(); b != e; ++b) {
if (first) {
first = false;
} else {
out << ", ";
}
out << *b;
}
out << "]";
return out;
}

}

#endif
Expand Down

0 comments on commit d008ae2

Please sign in to comment.