Skip to content

Commit e8943e8

Browse files
committed
containers: handle fixed format and separators the same way
This screams for factoring.
1 parent f395ce3 commit e8943e8

File tree

7 files changed

+55
-77
lines changed

7 files changed

+55
-77
lines changed

src/elle/flat-set.hh

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,15 @@ namespace boost
4646
std::ostream&
4747
operator <<(std::ostream& out, flat_set<Args...> const& s)
4848
{
49-
out << "{";
50-
bool first = true;
49+
auto const format = is_fixed(out) ? "%s%f" : "%s%s";
50+
out << '{';
51+
auto* sep = "";
5152
for (auto const& e: s)
52-
{
53-
if (first)
54-
first = false;
55-
else
56-
out << ", ";
57-
elle::fprintf(out, "%s", e);
58-
}
59-
out << "}";
53+
{
54+
elle::fprintf(out, format, sep, e);
55+
sep = ", ";
56+
}
57+
out << '}';
6058
return out;
6159
}
6260
}

src/map

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,31 @@
44
#include_next <map>
55
#include <vector>
66

7+
#include <elle/make-vector.hh>
78
#include <elle/printf-fwd.hh>
89

910
namespace std
1011
{
1112
template <typename... Args>
1213
std::ostream&
1314
operator <<(ostream& out,
14-
map<Args...> const& map)
15+
map<Args...> const& s)
1516
{
16-
out << "{";
17-
bool first = true;
18-
for (auto const& elt: map)
17+
auto const format = is_fixed(out) ? "%s%f: %f" : "%s%s: %s";
18+
out << '{';
19+
auto* sep = "";
20+
for (auto const& e: s)
1921
{
20-
if (first)
21-
first = false;
22-
else
23-
out << ", ";
24-
elle::fprintf(out, "%s: %s", elt.first, elt.second);
22+
elle::fprintf(out, format, sep, e.first, e.second);
23+
sep = ", ";
2524
}
26-
out << "}";
25+
out << '}';
2726
return out;
2827
}
2928

3029
template <typename... Args, typename E>
3130
bool
32-
contains(std::map<Args...> const& map, E const& e)
31+
contains(map<Args...> const& map, E const& e)
3332
{
3433
return map.find(e) != map.end();
3534
}
@@ -42,10 +41,8 @@ namespace elle
4241
keys(std::map<K, Args...> const& m)
4342
-> std::vector<K>
4443
{
45-
auto res = std::vector<K>{};
46-
for (const auto& p: m)
47-
res.emplace_back(p.first);
48-
return res;
44+
return make_vector(m,
45+
[](const auto& p) { return p.first; });
4946
}
5047
}
5148

src/set

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,17 @@ namespace std
1515

1616
template <typename... Args>
1717
ostream&
18-
operator <<(ostream& out, set<Args...> const& v)
18+
operator <<(ostream& out, set<Args...> const& s)
1919
{
20-
elle::fprintf(out, "{");
21-
bool first = true;
22-
for (auto const& elt: v)
20+
auto const format = is_fixed(out) ? "%s%f" : "%s%s";
21+
out << '{';
22+
auto* sep = "";
23+
for (auto const& e: s)
2324
{
24-
if (first)
25-
{
26-
first = false;
27-
elle::fprintf(out, "%s", elt);
28-
}
29-
else
30-
elle::fprintf(out, ", %s", elt);
25+
elle::fprintf(out, format, sep, e);
26+
sep = ", ";
3127
}
32-
elle::fprintf(out, "}");
28+
out << '}';
3329
return out;
3430
}
3531
}

src/unordered_map

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@ namespace std
1010
template <typename... Args>
1111
std::ostream&
1212
operator <<(ostream& out,
13-
unordered_map<Args...> const& map)
13+
unordered_map<Args...> const& s)
1414
{
15-
out << "{";
16-
bool first = true;
17-
for (auto const& elt: map)
15+
auto const format = is_fixed(out) ? "%s%f: %f" : "%s%s: %s";
16+
out << '{';
17+
auto* sep = "";
18+
for (auto const& e: s)
1819
{
19-
if (first)
20-
first = false;
21-
else
22-
out << ", ";
23-
elle::fprintf(out, "%s: %s", elt.first, elt.second);
20+
elle::fprintf(out, format, sep, e.first, e.second);
21+
sep = ", ";
2422
}
25-
out << "}";
23+
out << '}';
2624
return out;
2725
}
2826

src/unordered_set

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,17 @@ namespace std
88
{
99
template <typename... Args>
1010
ostream&
11-
operator <<(ostream& out, unordered_set<Args...> const& v)
11+
operator <<(ostream& out, unordered_set<Args...> const& s)
1212
{
13-
elle::fprintf(out, "{");
14-
bool first = true;
15-
bool fixed = out.flags() & std::ios::fixed;
16-
for (auto const& elt: v)
13+
auto const format = is_fixed(out) ? "%s%f" : "%s%s";
14+
out << '{';
15+
auto* sep = "";
16+
for (auto const& e: s)
1717
{
18-
if (first)
19-
{
20-
first = false;
21-
elle::fprintf(out, fixed ? "%f" : "%s", elt);
22-
}
23-
else
24-
elle::fprintf(out, fixed ? ", %f" : ", %s", elt);
18+
elle::fprintf(out, format, sep, e);
19+
sep = ", ";
2520
}
26-
elle::fprintf(out, "}");
21+
out << '}';
2722
return out;
2823
}
2924

src/utility

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ namespace std
1010
std::basic_ostream<charT, traits>&
1111
operator <<(std::basic_ostream<charT, traits>& o, std::pair<T1, T2> const& p)
1212
{
13-
if (is_fixed(o))
14-
elle::fprintf(o, "(%f, %f)", p.first, p.second);
15-
else
16-
elle::fprintf(o, "(%s, %s)", p.first, p.second);
17-
return o;
13+
return elle::fprintf(o,
14+
is_fixed(o) ? "(%f, %f)" : "(%s, %s)",
15+
p.first, p.second);
1816
}
1917
}
2018

src/vector

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,17 @@ namespace std
99
{
1010
template <typename... Args>
1111
ostream&
12-
operator <<(ostream& out, vector<Args...> const& v)
12+
operator <<(ostream& out, vector<Args...> const& s)
1313
{
14-
elle::fprintf(out, "[");
15-
bool first = true;
16-
for (auto const& elt: v)
14+
auto const format = is_fixed(out) ? "%s%f" : "%s%s";
15+
out << '[';
16+
auto* sep = "";
17+
for (auto const& e: s)
1718
{
18-
if (first)
19-
{
20-
first = false;
21-
elle::fprintf(out, is_fixed(out) ? "%f" : "%s", elt);
22-
}
23-
else
24-
elle::fprintf(out, is_fixed(out) ? ", %f" : ", %s", elt);
19+
elle::fprintf(out, format, sep, e);
20+
sep = ", ";
2521
}
26-
elle::fprintf(out, "]");
22+
out << ']';
2723
return out;
2824
}
2925
}

0 commit comments

Comments
 (0)