Skip to content

Commit

Permalink
feat(templates): i/o helper for valarray
Browse files Browse the repository at this point in the history
  • Loading branch information
STommydx committed Apr 5, 2024
1 parent a1b3a67 commit 729f33e
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
59 changes: 59 additions & 0 deletions io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <iostream>
#include <queue>
#include <ranges>
#include <valarray>
#include <vector>

/**
Expand All @@ -20,8 +21,13 @@ std::ostream &operator<<(std::ostream &os, const std::vector<T> &r);
template <class T>
std::ostream &operator<<(std::ostream &os, const std::deque<T> &r);
template <class T>
std::ostream &operator<<(std::ostream &os, const std::valarray<T> &r);
template <class T>
std::ostream &operator<<(std::ostream &os,
const std::vector<std::vector<T>> &r);
template <class T>
std::ostream &operator<<(std::ostream &os,
const std::valarray<std::valarray<T>> &r);
template <class T1, class T2>
std::ostream &operator<<(std::ostream &os,
const std::vector<std::pair<T1, T2>> &r);
Expand All @@ -47,10 +53,19 @@ std::ostream &operator<<(std::ostream &os, const std::deque<T> &r) {
return print_range(os, r);
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::valarray<T> &r) {
return print_range(os, r);
}
template <class T>
std::ostream &operator<<(std::ostream &os,
const std::vector<std::vector<T>> &r) {
return print_range(os, r, '\n');
}
template <class T>
std::ostream &operator<<(std::ostream &os,
const std::valarray<std::valarray<T>> &r) {
return print_range(os, r, '\n');
}
template <class T1, class T2>
std::ostream &operator<<(std::ostream &os,
const std::vector<std::pair<T1, T2>> &r) {
Expand Down Expand Up @@ -103,9 +118,16 @@ template <class T, int base>
std::ostream &operator<<(std::ostream &os,
_put_indices<std::deque<T>, base> pi);
template <class T, int base>
std::ostream &operator<<(std::ostream &os,
_put_indices<std::valarray<T>, base> pi);
template <class T, int base>
std::ostream &operator<<(std::ostream &os,
_put_indices<std::vector<std::vector<T>>, base> pi);
template <class T, int base>
std::ostream &
operator<<(std::ostream &os,
_put_indices<std::valarray<std::valarray<T>>, base> pi);
template <class T, int base>
std::ostream &operator<<(std::ostream &os,
_put_indices<std::vector<std::pair<T, T>>, base> pi);
template <class... Ts, int base>
Expand Down Expand Up @@ -133,6 +155,13 @@ std::ostream &operator<<(std::ostream &os,
}));
}
template <class T, int base>
std::ostream &operator<<(std::ostream &os,
_put_indices<std::valarray<T>, base> pi) {
return print_range(os, pi.v | std::views::transform([](const auto &x) {
return put_indices<T, base>(x);
}));
}
template <class T, int base>
std::ostream &operator<<(std::ostream &os,
_put_indices<std::deque<T>, base> pi) {
return print_range(os, pi.v | std::views::transform([](const auto &x) {
Expand All @@ -148,6 +177,15 @@ std::ostream &operator<<(std::ostream &os,
'\n');
}
template <class T, int base>
std::ostream &
operator<<(std::ostream &os,
_put_indices<std::valarray<std::valarray<T>>, base> pi) {
return print_range(os, pi.v | std::views::transform([](const auto &x) {
return put_indices<std::valarray<T>, base>(x);
}),
'\n');
}
template <class T, int base>
std::ostream &operator<<(std::ostream &os,
_put_indices<std::vector<std::pair<T, T>>, base> pi) {
return print_range(os, pi.v | std::views::transform([](const auto &x) {
Expand Down Expand Up @@ -184,6 +222,11 @@ std::ostream &operator<<(std::ostream &os,
return os;
}

template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &r);
template <class T> std::istream &operator>>(std::istream &is, std::deque<T> &r);
template <class T>
std::istream &operator>>(std::istream &is, std::valarray<T> &r);
template <class T1, class T2>
std::istream &operator>>(std::istream &is, std::pair<T1, T2> &x);
template <class... Ts>
Expand All @@ -203,6 +246,12 @@ std::istream &operator>>(std::istream &is, std::deque<T> &r) {
is >> x;
return is;
}
template <class T>
std::istream &operator>>(std::istream &is, std::valarray<T> &r) {
for (auto &x : r)
is >> x;
return is;
}
template <class T1, class T2>
std::istream &operator>>(std::istream &is, std::pair<T1, T2> &x) {
return is >> x.first >> x.second;
Expand Down Expand Up @@ -233,6 +282,9 @@ std::istream &operator>>(std::istream &is,
template <class T, int base>
std::istream &operator>>(std::istream &is,
_get_indices<std::deque<T>, base> gi);
template <class T, int base>
std::istream &operator>>(std::istream &is,
_get_indices<std::valarray<T>, base> gi);
template <class T1, class T2, int base>
std::istream &operator>>(std::istream &is,
_get_indices<std::pair<T1, T2>, base> gi);
Expand Down Expand Up @@ -260,6 +312,13 @@ std::istream &operator>>(std::istream &is,
is >> get_indices<T, base>(x);
return is;
}
template <class T, int base>
std::istream &operator>>(std::istream &is,
_get_indices<std::valarray<T>, base> gi) {
for (auto &x : gi.v)
is >> get_indices<T, base>(x);
return is;
}
template <class T1, class T2, int base>
std::istream &operator>>(std::istream &is,
_get_indices<std::pair<T1, T2>, base> gi) {
Expand Down
38 changes: 38 additions & 0 deletions io_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,35 @@ TEST_CASE("IO operator overload works for common cp types", "[io]") {
REQUIRE(v == std::vector<std::vector<std::string>>(
{{"1", "22", "333"}, {"4444", "55555", "666666"}}));
}
SECTION("valarray outputs elements separated by space") {
std::stringstream ss;
std::valarray<std::string> v{"1", "22", "333"};
ss << v;
REQUIRE(ss.str() == "1 22 333");
}
SECTION("valarray inputs elements separated by space") {
std::stringstream ss{"1 22 333"};
std::valarray<std::string> v(3);
ss >> v;
std::valarray<std::string> expected{"1", "22", "333"};
REQUIRE((v == expected).min());
}
SECTION("2d valarray outputs elements separated by space and newline") {
std::stringstream ss;
std::valarray<std::valarray<std::string>> v{
{"1", "22", "333"}, {"4444", "55555", "666666"}};
ss << v;
REQUIRE(ss.str() == "1 22 333\n4444 55555 666666");
}
SECTION("2d valarray inputs elements separated by space and newline") {
std::stringstream ss{"1 22 333\n4444 55555 666666"};
std::valarray<std::valarray<std::string>> v(
std::valarray<std::string>(3), 2);
ss >> v;
REQUIRE(v[0][0] == "1");
REQUIRE(v[1][1] == "55555");
REQUIRE(v[1][2] == "666666");
}
SECTION("pair outputs elements separated by space") {
std::stringstream ss;
std::pair<std::string, int> p{"1", 22};
Expand Down Expand Up @@ -115,4 +144,13 @@ TEST_CASE("I/O for 1-based indices", "[io]") {
ss >> get_indices(v2);
REQUIRE(v == v2);
}
SECTION("valarray of 1-based indices") {
std::stringstream ss;
std::valarray<int> p{3, 0, 2, 1};
ss << put_indices(p);
REQUIRE(ss.str() == "4 1 3 2");
std::valarray<int> p2(4);
ss >> get_indices(p2);
REQUIRE((p == p2).min());
}
}

0 comments on commit 729f33e

Please sign in to comment.