SAT Solver Template
Loading...
Searching...
No Matches
printing.hpp
Go to the documentation of this file.
1/**
2* @author Tim Luchterhand
3* @date 06.01.26
4* @file printing.hpp
5* @brief Contains functions ostream operators for various structures: ranges, tuples and the structures
6* like variables and literals for easy printing.
7*/
8
9#ifndef PRINTING_HPP
10#define PRINTING_HPP
11
12#include <ostream>
13#include <tuple>
14#include <ranges>
15#include <concepts>
16#include <string>
17#include <string_view>
18
20
21template<typename T>
22concept printable = requires(const T &obj, std::ostream &os) {
23 os << obj;
24};
25
26
27namespace printing_detail {
28 template<typename T>
29 concept string = std::same_as<std::string, std::remove_cvref_t<T>> or
30 std::same_as<std::string_view, std::remove_cvref_t<T>> or
31 std::same_as<std::decay_t<T>, char *> or
32 std::same_as<std::decay_t<T>, const char *>;
33
34 template<std::size_t Idx, typename Tuple>
35 void printElem(std::ostream &os, const Tuple &tuple) {
36 if constexpr (Idx != 0) {
37 os << ", ";
38 }
39
40 os << std::get<Idx>(tuple);
41 }
42}
43
44namespace std {
45 template<printable ...Ts>
46 std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &tuple) {
47 os << "(";
48 [&]<std::size_t ... Idx>(std::index_sequence<Idx...>) {
49 (printing_detail::printElem<Idx>(os, tuple), ...);
50 }(std::make_index_sequence<sizeof...(Ts)>());
51 os << ")";
52 return os;
53 }
54
55 template<printable T, printable U>
56 std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &pair) {
57 os << "(" << pair.first << ", " << pair.second << ")";
58 return os;
59 }
60
61 template<std::ranges::range R>
62 requires(printable<std::ranges::range_value_t<R>> and
63 not printing_detail::string<R>)
64 std::ostream &operator<<(std::ostream &os, R &&range) {
65 os << "[";
66 bool first = true;
67 for (const auto &elem: std::forward<R>(range)) {
68 if (first) {
69 first = false;
70 } else {
71 os << ", ";
72 }
73 os << elem;
74 }
75
76 os << "]";
77 return os;
78 }
79}
80
81namespace sat {
82 /**
83 * Ostream operator allowing for easy printing of variables
84 */
85 std::ostream &operator<<(std::ostream &os, Variable x);
86
87 /**
88 * Ostream operator allowing for easy printing of literals
89 */
90 std::ostream &operator<<(std::ostream &os, Literal l);
91}
92
93#endif
Structure representing a literal in a CNF-SAT problem.
Definition basic_structures.hpp:56
unsigned get() const
Definition basic_structures.cpp:30
short sign() const
Definition basic_structures.cpp:38
Structure representing a binary variable in a CNF-SAT problem.
Definition basic_structures.hpp:29
unsigned get() const
Definition basic_structures.cpp:18
Definition basic_structures.cpp:10
Variable var(Literal l)
Definition basic_structures.cpp:54