Skip to content

Commit

Permalink
Pass value to print helper formatter, not iterator
Browse files Browse the repository at this point in the history
Can't do anything with the iterator except dereference it.
  • Loading branch information
wthrowe committed Oct 1, 2023
1 parent 911ee07 commit 139c782
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/DataStructures/FixedHashMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,9 @@ std::ostream& operator<<(
unordered_print_helper(
os, std::begin(m), std::end(m),
[](std::ostream& out,
typename FixedHashMap<MaxSize, Key, ValueType, Hash,
KeyEqual>::const_iterator it) {
out << "[" << it->first << "," << it->second << "]";
const typename FixedHashMap<MaxSize, Key, ValueType, Hash,
KeyEqual>::value_type& entry) {
out << "[" << entry.first << "," << entry.second << "]";
});
return os;
}
15 changes: 11 additions & 4 deletions src/Utilities/PrintHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <utility>
Expand All @@ -20,7 +21,7 @@ void sequence_print_helper(std::ostream& out, ForwardIt begin,
out << "(";
if (begin != end) {
while (true) {
f(out, begin++);
f(out, *begin++);
if (begin == end) {
break;
}
Expand All @@ -39,7 +40,10 @@ void sequence_print_helper(std::ostream& out, ForwardIt begin,
const ForwardIt& end) {
sequence_print_helper(
out, std::move(begin), end,
[](std::ostream& os, const ForwardIt& it) { os << *it; });
[](std::ostream& os,
const typename std::iterator_traits<ForwardIt>::value_type& it) {
os << it;
});
}

/// @{
Expand All @@ -53,7 +57,7 @@ void unordered_print_helper(std::ostream& out, ForwardIt begin,
std::vector<std::string> entries;
while (begin != end) {
std::ostringstream ss;
f(ss, begin++);
f(ss, *begin++);
entries.push_back(ss.str());
}
std::sort(entries.begin(), entries.end());
Expand All @@ -65,6 +69,9 @@ void unordered_print_helper(std::ostream& out, ForwardIt begin,
const ForwardIt& end) {
unordered_print_helper(
out, std::move(begin), end,
[](std::ostream& os, const ForwardIt& it) { os << *it; });
[](std::ostream& os,
const typename std::iterator_traits<ForwardIt>::value_type& it) {
os << it;
});
}
/// @}
18 changes: 10 additions & 8 deletions src/Utilities/StdHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ inline std::ostream& operator<<(std::ostream& os,
unordered_print_helper(
os, begin(m), end(m),
[](std::ostream& out,
typename std::unordered_map<K, V, H>::const_iterator it) {
out << "[" << it->first << "," << it->second << "]";
const typename std::unordered_map<K, V, H>::value_type& value) {
out << "[" << value.first << "," << value.second << "]";
});
return os;
}
Expand All @@ -137,8 +137,9 @@ template <typename K, typename V, typename C>
inline std::ostream& operator<<(std::ostream& os, const std::map<K, V, C>& m) {
sequence_print_helper(
os, begin(m), end(m),
[](std::ostream& out, typename std::map<K, V, C>::const_iterator it) {
out << "[" << it->first << "," << it->second << "]";
[](std::ostream& out,
const typename std::map<K, V, C>::value_type& value) {
out << "[" << value.first << "," << value.second << "]";
});
return os;
}
Expand Down Expand Up @@ -226,8 +227,8 @@ inline std::string keys_of(const std::unordered_map<K, V, H>& m) {
unordered_print_helper(
os, begin(m), end(m),
[](std::ostream& out,
typename std::unordered_map<K, V, H>::const_iterator it) {
out << it->first;
const typename std::unordered_map<K, V, H>::value_type& value) {
out << value.first;
});
return os.str();
}
Expand All @@ -241,8 +242,9 @@ inline std::string keys_of(const std::map<K, V, C>& m) {
std::ostringstream os;
sequence_print_helper(
os, begin(m), end(m),
[](std::ostream& out, typename std::map<K, V, C>::const_iterator it) {
out << it->first;
[](std::ostream& out,
const typename std::map<K, V, C>::value_type& value) {
out << value.first;
});
return os.str();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Utilities/Test_PrintHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ void test_vector_and_list(const F& test) {
} // namespace

SPECTRE_TEST_CASE("Unit.Utilities.PrintHelpers", "[Utilities][Unit]") {
const auto stars = [](std::ostream& os, const auto& it) {
os << "*" << *it << "*";
const auto stars = [](std::ostream& os, const int value) {
os << "*" << value << "*";
};

test_vector_and_list([](auto&& begin, auto&& end) {
Expand Down

0 comments on commit 139c782

Please sign in to comment.