Skip to content

Commit

Permalink
made various std:: collections printable
Browse files Browse the repository at this point in the history
  • Loading branch information
guicho271828 committed Jan 13, 2025
1 parent 40960cf commit 1213205
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/search/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ create_fast_downward_library(
utils/markup
utils/math
utils/memory
utils/printer
utils/rng
utils/rng_options
utils/strings
Expand Down
174 changes: 174 additions & 0 deletions src/search/utils/printer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#pragma once

#include <iostream>

#include <tuple>
#include <utility> // for pair

#include <vector>
#include <deque>
#include <array>

#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

#include <memory>

namespace utils {

template<class T, class S>
std::ostream& operator<<(std::ostream &os, const std::pair<T,S> &thing) {
os << "[" << thing.first << ", " << thing.second << "]" ;
return os;
}

// Helper functions to iterate over tuples
template <typename Tuple>
struct TupleIterator {
template <std::size_t I, std::size_t... Is>
static void print(std::ostream& os, const Tuple& tuple, std::index_sequence<I, Is...>) {
os << std::get<0>(tuple);
((os << ", " << std::get<Is>(tuple)), ...);
}
};

template <typename... Args>
std::ostream& operator<<(std::ostream& os, const std::tuple<Args...>& tuple) {
constexpr std::size_t tuple_size = std::tuple_size<std::tuple<Args...>>::value;
os << "[";
if constexpr (tuple_size > 0) {
TupleIterator<std::tuple<Args...>>::print(os, tuple, std::index_sequence_for<Args...>());
}
os << "]";
return os;
}


template<class T>
std::ostream& operator<<(std::ostream &os, const std::deque<T> &thing) {
os << "[";
bool first = true;
for (auto elem : thing) {
if (! first) {
os << ", ";
}
os << elem;
first = false;
}
os << "]" ;
return os;
}


template<class T>
std::ostream& operator<<(std::ostream &os, const std::vector<T> &thing) {
os << "[";
bool first = true;
for (auto elem : thing) {
if (! first) {
os << ", ";
}
os << elem;
first = false;
}
os << "]" ;
return os;
}


template<class T, std::size_t N>
std::ostream& operator<<(std::ostream &os, const std::array<T, N> &thing) {
os << "[";
bool first = true;
for (auto elem : thing) {
if (! first) {
os << ", ";
}
os << elem;
first = false;
}
os << "]" ;
return os;
}


template<class T, typename... Args>
std::ostream& operator<<(std::ostream &os, const std::set<T,Args...> &thing) {
os << "{";
bool first = true;
for (auto elem : thing) {
if (! first) {
os << ", ";
}
os << elem;
first = false;
}
os << "}" ;
return os;
}

template<class T, typename... Args>
std::ostream& operator<<(std::ostream &os, const std::unordered_set<T,Args...> &thing) {
os << "{";
bool first = true;
for (auto elem : thing) {
if (! first) {
os << ", ";
}
os << elem;
first = false;
}
os << "}" ;
return os;
}

template<class K, class V, typename... Args>
std::ostream& operator<<(std::ostream &os, const std::map<K,V,Args...> &thing) {
os << "{";
bool first = true;
for (auto elem : thing) {
if (! first) {
os << ", ";
}
os << elem.first
<< " : "
<< elem.second ;
first = false;
}
os << "}" ;
return os;
}

template<class K, class V, typename... Args>
std::ostream& operator<<(std::ostream &os, const std::unordered_map<K,V,Args...> &thing) {
os << "{";
bool first = true;
for (auto elem : thing) {
if (! first) {
os << ", ";
}
os << elem.first
<< " : "
<< elem.second ;
first = false;
}
os << "}" ;
return os;
}


template<class T>
std::ostream& operator<<(std::ostream &os, const std::shared_ptr<T> &thing) {
os << "@(" << reinterpret_cast<std::size_t>(thing.get()) << ", c=" << thing.use_count() << ")";
return os;
}

// template<class T>
// std::ostream& operator<<(std::ostream &os, const T* thing) {
// os << "@"s << (thing);
// return os;
// }

}

0 comments on commit 1213205

Please sign in to comment.