Debian Codegraph @

kiwi/row.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 "maptype.h"
10 #include "symbol.h"
11 #include "util.h"
12
13 namespace kiwi
14 {
15
16 namespace impl
17 {
18
19 class Row
20 {
21
22 public:
23 using CellMap = MapType<Symbol, double>;
24
25 Row() : Row(0.0) {}
26
27 Row(double constant) : m_constant(constant) {}
28
29 Row(const Row &other) {
30 m_constant = other.m_constant;
31 m_cells = other.m_cells;
32 }
33
34 Row(Row &&other) noexcept {
35 m_constant = other.m_constant;
36 m_cells = std::move(other.m_cells);
37 }
38
39 Row& operator=(const Row &other) {
40 if (this != &other) {
41 m_constant = other.m_constant;
42 m_cells = other.m_cells;
43 }
44 return *this;
45 }
46
47 Row& operator=(Row &&other) noexcept {
48 if (this != &other) {
49 m_constant = other.m_constant;
50 m_cells = std::move(other.m_cells);
51 }
52 return *this;
53 }
54
55 ~Row() = default;
56
57 const CellMap &cells() const
58 {
59 return m_cells;
60 }
61
62 double constant() const
63 {
64 return m_constant;
65 }
66
67 /* Add a constant value to the row constant.
68
69 The new value of the constant is returned.
70
71 */
72 double add(double value)
73 {
74 return m_constant += value;
75 }
76
77 /* Insert a symbol into the row with a given coefficient.
78
79 If the symbol already exists in the row, the coefficient will be
80 added to the existing coefficient. If the resulting coefficient
81 is zero, the symbol will be removed from the row.
82
83 */
84 void insert(const Symbol &symbol, double coefficient = 1.0)
85 {
86 if (nearZero(m_cells[symbol] += coefficient))
87 m_cells.erase(symbol);
88 }
89
90 /* Insert a row into this row with a given coefficient.
91
92 The constant and the cells of the other row will be multiplied by
93 the coefficient and added to this row. Any cell with a resulting
94 coefficient of zero will be removed from the row.
95
96 */
97 void insert(const Row &other, double coefficient = 1.0)
98 {
99 m_constant += other.m_constant * coefficient;
100
101 for (const auto & cellPair : other.m_cells)
102 {
103 double coeff = cellPair.second * coefficient;
104 if (nearZero(m_cells[cellPair.first] += coeff))
105 m_cells.erase(cellPair.first);
106 }
107 }
108
109 /* Remove the given symbol from the row.
110
111 */
112 void remove(const Symbol &symbol)
113 {
114 auto it = m_cells.find(symbol);
115 if (it != m_cells.end())
116 m_cells.erase(it);
117 }
118
119 /* Reverse the sign of the constant and all cells in the row.
120
121 */
122 void reverseSign()
123 {
124 m_constant = -m_constant;
125 for (auto &cellPair : m_cells)
126 cellPair.second = -cellPair.second;
127 }
128
129 /* Solve the row for the given symbol.
130
131 This method assumes the row is of the form a * x + b * y + c = 0
132 and (assuming solve for x) will modify the row to represent the
133 right hand side of x = -b/a * y - c / a. The target symbol will
134 be removed from the row, and the constant and other cells will
135 be multiplied by the negative inverse of the target coefficient.
136
137 The given symbol *must* exist in the row.
138
139 */
140 void solveFor(const Symbol &symbol)
141 {
142 double coeff = -1.0 / m_cells[symbol];
143 m_cells.erase(symbol);
144 m_constant *= coeff;
145 for (auto &cellPair : m_cells)
146 cellPair.second *= coeff;
147 }
148
149 /* Solve the row for the given symbols.
150
151 This method assumes the row is of the form x = b * y + c and will
152 solve the row such that y = x / b - c / b. The rhs symbol will be
153 removed from the row, the lhs added, and the result divided by the
154 negative inverse of the rhs coefficient.
155
156 The lhs symbol *must not* exist in the row, and the rhs symbol
157 *must* exist in the row.
158
159 */
160 void solveFor(const Symbol &lhs, const Symbol &rhs)
161 {
162 insert(lhs, -1.0);
163 solveFor(rhs);
164 }
165
166 /* Get the coefficient for the given symbol.
167
168 If the symbol does not exist in the row, zero will be returned.
169
170 */
171 double coefficientFor(const Symbol &symbol) const
172 {
173 CellMap::const_iterator it = m_cells.find(symbol);
174 if (it == m_cells.end())
175 return 0.0;
176 return it->second;
177 }
178
179 /* Substitute a symbol with the data from another row.
180
181 Given a row of the form a * x + b and a substitution of the
182 form x = 3 * y + c the row will be updated to reflect the
183 expression 3 * a * y + a * c + b.
184
185 If the symbol does not exist in the row, this is a no-op.
186
187 */
188 void substitute(const Symbol &symbol, const Row &row)
189 {
190 auto it = m_cells.find(symbol);
191 if (it != m_cells.end())
192 {
193 double coefficient = it->second;
194 m_cells.erase(it);
195 insert(row, coefficient);
196 }
197 }
198
199 private:
200 CellMap m_cells;
201 double m_constant;
202 };
203
204 } // namespace impl
205
206 } // namespace kiwi