Debian Codegraph @

kiwi/debug.h

raw
1 /*-----------------------------------------------------------------------------
2 | Copyright (c) 2013-2026, Nucleic Development Team.
3 |
4 | Distributed under the terms of the Modified BSD License.
5 |
6 | The full license is in the file LICENSE, distributed with this software.
7 |----------------------------------------------------------------------------*/
8 #pragma once
9 #include <iostream>
10 #include <sstream>
11 #include <vector>
12 #include "constraint.h"
13 #include "solverimpl.h"
14 #include "term.h"
15
16 namespace kiwi
17 {
18
19 namespace impl
20 {
21
22 class DebugHelper
23 {
24
25 public:
26 static void dump(const SolverImpl &solver, std::ostream &out)
27 {
28 out << "Objective" << std::endl;
29 out << "---------" << std::endl;
30 dump(*solver.m_objective, out);
31 out << std::endl;
32 out << "Tableau" << std::endl;
33 out << "-------" << std::endl;
34 dump(solver.m_rows, out);
35 out << std::endl;
36 out << "Infeasible" << std::endl;
37 out << "----------" << std::endl;
38 dump(solver.m_infeasible_rows, out);
39 out << std::endl;
40 out << "Variables" << std::endl;
41 out << "---------" << std::endl;
42 dump(solver.m_vars, out);
43 out << std::endl;
44 out << "Edit Variables" << std::endl;
45 out << "--------------" << std::endl;
46 dump(solver.m_edits, out);
47 out << std::endl;
48 out << "Constraints" << std::endl;
49 out << "-----------" << std::endl;
50 dump(solver.m_cns, out);
51 out << std::endl;
52 out << std::endl;
53 }
54
55 static void dump(const SolverImpl::RowMap &rows, std::ostream &out)
56 {
57 for (const auto &rowPair : rows)
58 {
59 dump(rowPair.first, out);
60 out << " | ";
61 dump(*rowPair.second, out);
62 }
63 }
64
65 static void dump(const std::vector<Symbol> &symbols, std::ostream &out)
66 {
67 for (const auto &symbol : symbols)
68 {
69 dump(symbol, out);
70 out << std::endl;
71 }
72 }
73
74 static void dump(const SolverImpl::VarMap &vars, std::ostream &out)
75 {
76 for (const auto &varPair : vars)
77 {
78 out << varPair.first.name() << " = ";
79 dump(varPair.second, out);
80 out << std::endl;
81 }
82 }
83
84 static void dump(const SolverImpl::CnMap &cns, std::ostream &out)
85 {
86 for (const auto &cnPair : cns)
87 dump(cnPair.first, out);
88 }
89
90 static void dump(const SolverImpl::EditMap &edits, std::ostream &out)
91 {
92 for (const auto &editPair : edits)
93 out << editPair.first.name() << std::endl;
94 }
95
96 static void dump(const Row &row, std::ostream &out)
97 {
98 for (const auto &rowPair : row.cells())
99 {
100 out << " + " << rowPair.second << " * ";
101 dump(rowPair.first, out);
102 }
103 out << std::endl;
104 }
105
106 static void dump(const Symbol &symbol, std::ostream &out)
107 {
108 switch (symbol.type())
109 {
110 case Symbol::Invalid:
111 out << "i";
112 break;
113 case Symbol::External:
114 out << "v";
115 break;
116 case Symbol::Slack:
117 out << "s";
118 break;
119 case Symbol::Error:
120 out << "e";
121 break;
122 case Symbol::Dummy:
123 out << "d";
124 break;
125 default:
126 break;
127 }
128 out << symbol.id();
129 }
130
131 static void dump(const Constraint &cn, std::ostream &out)
132 {
133 for (const auto &term : cn.expression().terms())
134 {
135 out << term.coefficient() << " * ";
136 out << term.variable().name() << " + ";
137 }
138 out << cn.expression().constant();
139 switch (cn.op())
140 {
141 case OP_LE:
142 out << " <= 0 ";
143 break;
144 case OP_GE:
145 out << " >= 0 ";
146 break;
147 case OP_EQ:
148 out << " == 0 ";
149 break;
150 default:
151 break;
152 }
153 out << " | strength = " << cn.strength() << std::endl;
154 }
155 };
156
157 } // namespace impl
158
159 namespace debug
160 {
161
162 template <typename T>
163 void dump(const T &value)
164 {
165 impl::DebugHelper::dump(value, std::cout);
166 }
167
168 template <typename T>
169 void dump(const T &value, std::ostream &out)
170 {
171 impl::DebugHelper::dump(value, out);
172 }
173
174 template <typename T>
175 std::string dumps(const T &value)
176 {
177 std::stringstream stream;
178 impl::DebugHelper::dump(value, stream);
179 return stream.str();
180 }
181
182 } // namespace debug
183
184 } // namespace kiwi