11#pragma once
22
33#include " var.hpp"
4- #include < set>
54#include < unordered_map>
65
76namespace linspire
@@ -124,10 +123,10 @@ namespace linspire
124123 * @param lhs The left-hand side linear expression of the constraint.
125124 * @param rhs The right-hand side linear expression of the constraint.
126125 * @param strict A boolean indicating whether the constraint is strict (default: false).
127- * @param reason An optional shared pointer to a constraint object that serves as the reason for this constraint.
126+ * @param reason An optional constraint that serves as the reason for adding this new constraint.
128127 * @return true if the constraint was successfully added; false if it leads to inconsistency.
129128 */
130- [[nodiscard]] bool new_lt (const utils::lin &lhs, const utils::lin &rhs, bool strict = false , std::shared_ptr< constraint> reason = nullptr ) noexcept ;
129+ [[nodiscard]] bool new_lt (const utils::lin &lhs, const utils::lin &rhs, bool strict = false , const std::optional<std::reference_wrapper< constraint>> & reason = std:: nullopt ) noexcept ;
131130 /* *
132131 * @brief Adds a new equality constraint to the solver.
133132 *
@@ -136,10 +135,10 @@ namespace linspire
136135 *
137136 * @param lhs The left-hand side linear expression of the equality constraint.
138137 * @param rhs The right-hand side linear expression of the equality constraint.
139- * @param reason An optional shared pointer to a constraint object that serves as the reason for this equality constraint.
138+ * @param reason An optional constraint that serves as the reason for adding this new equality constraint.
140139 * @return true if the equality constraint was successfully added; false if it leads to inconsistency.
141140 */
142- [[nodiscard]] bool new_eq (const utils::lin &lhs, const utils::lin &rhs, std::shared_ptr< constraint> reason = nullptr ) noexcept ;
141+ [[nodiscard]] bool new_eq (const utils::lin &lhs, const utils::lin &rhs, const std::optional<std::reference_wrapper< constraint>> & reason = std:: nullopt ) noexcept ;
143142 /* *
144143 * @brief Adds a new greater-than or greater-than-or-equal-to constraint to the solver.
145144 *
@@ -150,20 +149,20 @@ namespace linspire
150149 * @param lhs The left-hand side linear expression of the constraint.
151150 * @param rhs The right-hand side linear expression of the constraint.
152151 * @param strict A boolean indicating whether the constraint is strict (default: false).
153- * @param reason An optional shared pointer to a constraint object that serves as the reason for this constraint.
152+ * @param reason An optional constraint that serves as the reason for adding this new constraint.
154153 * @return true if the constraint was successfully added; false if it leads to inconsistency.
155154 */
156- [[nodiscard]] bool new_gt (const utils::lin &lhs, const utils::lin &rhs, bool strict = false , std::shared_ptr< constraint> reason = nullptr ) noexcept { return new_lt (rhs, lhs, strict, reason); }
155+ [[nodiscard]] bool new_gt (const utils::lin &lhs, const utils::lin &rhs, bool strict = false , const std::optional<std::reference_wrapper< constraint>> & reason = std:: nullopt ) noexcept { return new_lt (rhs, lhs, strict, reason); }
157156
158157 /* *
159158 * @brief Retracts a previously added constraint from the solver.
160159 *
161160 * This function removes a previously added constraint from the solver. It updates the
162161 * internal state of the solver to reflect the removal of the constraint.
163162 *
164- * @param c A shared pointer to the constraint object to be retracted.
163+ * @param c The constraint to be retracted.
165164 */
166- void retract (const std::shared_ptr< constraint> c) noexcept ;
165+ void retract (constraint & c) noexcept ;
167166
168167 /* *
169168 * @brief Checks the consistency of the current set of constraints.
@@ -184,9 +183,9 @@ namespace linspire
184183 * The conflict explanation consists of a set of constraints that led to an inconsistency
185184 * in the solver.
186185 *
187- * @return A constant reference to a vector of shared pointers to constraint objects representing the last conflict explanation.
186+ * @return A constant reference to the vector of constraints representing the last conflict explanation.
188187 */
189- [[nodiscard]] const std::vector<std::shared_ptr< const constraint>> &get_conflict () const noexcept { return cnfl; }
188+ [[nodiscard]] const std::vector<std::reference_wrapper< constraint>> &get_conflict () const noexcept { return cnfl; }
190189
191190 /* *
192191 * @brief Checks if two linear expressions can be made equal.
@@ -204,21 +203,21 @@ namespace linspire
204203 /* *
205204 * @brief Adds a listener to the solver.
206205 *
207- * This function registers a listener that will be notified of variable value changes
206+ * This function registers a listener that will be notified of changes to variable values
208207 * within the solver. The listener must implement the `listener` interface.
209208 *
210- * @param l A shared pointer to the listener to be added.
209+ * @param l A reference to the listener to be added.
211210 */
212- void add_listener (std::shared_ptr< listener> l) noexcept ;
211+ void add_listener (listener & l) noexcept ;
213212 /* *
214213 * @brief Removes a listener from the solver.
215214 *
216- * This function unregisters a previously added listener from the solver. The listener
217- * will no longer receive notifications of variable value changes.
215+ * This function unregisters a previously added listener from the solver. The listener will
216+ * no longer receive notifications of variable value changes.
218217 *
219- * @param l A shared pointer to the listener to be removed.
218+ * @param l A reference to the listener to be removed.
220219 */
221- void remove_listener (std::shared_ptr< listener> l) noexcept ;
220+ void remove_listener (listener & l) noexcept ;
222221#endif
223222
224223 friend std::string to_string (const solver &s) noexcept ;
@@ -227,8 +226,8 @@ namespace linspire
227226 private:
228227 [[nodiscard]] bool is_basic (const utils::var v) const noexcept { return tableau.count (v); }
229228
230- [[nodiscard]] bool set_lb (const utils::var x_i, const utils::inf_rational &v, std::shared_ptr< constraint> reason = nullptr ) noexcept ;
231- [[nodiscard]] bool set_ub (const utils::var x_i, const utils::inf_rational &v, std::shared_ptr< constraint> reason = nullptr ) noexcept ;
229+ [[nodiscard]] bool set_lb (const utils::var x_i, const utils::inf_rational &v, const std::optional<std::reference_wrapper< constraint>> & reason = std:: nullopt ) noexcept ;
230+ [[nodiscard]] bool set_ub (const utils::var x_i, const utils::inf_rational &v, const std::optional<std::reference_wrapper< constraint>> & reason = std:: nullopt ) noexcept ;
232231
233232 void update (const utils::var x_i, const utils::inf_rational &v) noexcept ;
234233
@@ -238,14 +237,14 @@ namespace linspire
238237
239238 void new_row (const utils::var x, utils::lin &&l) noexcept ;
240239
241- std::vector<var> vars; // index is the variable id
242- std::unordered_map<std::string, utils::var> exprs; // the expressions (string to numeric variable) for which already exist slack variables..
243- std::map<utils::var, utils::lin> tableau; // basic variable -> expression
244- std::vector<std::set<utils::var>> t_watches; // for each variable `v`, a set of tableau rows watching `v`..
245- std::vector<std::shared_ptr< const constraint>> cnfl; // the last conflict explanation..
240+ std::vector<var> vars; // index is the variable id
241+ std::unordered_map<std::string, utils::var> exprs; // the expressions (string to numeric variable) for which already exist slack variables..
242+ std::map<utils::var, utils::lin> tableau; // basic variable -> expression
243+ std::vector<std::set<utils::var>> t_watches; // for each variable `v`, a set of tableau rows watching `v`..
244+ std::vector<std::reference_wrapper< constraint>> cnfl; // the last conflict explanation..
246245#ifdef LINSPIRE_ENABLE_LISTENERS
247246 std::unordered_map<utils::var, std::set<listener *>> listening; // for each variable, the listeners listening to it..
248- std::set<std::shared_ptr< listener>> listeners; // the collection of listeners..
247+ std::set<listener *> listeners; // the collection of listeners..
249248#endif
250249 };
251250
0 commit comments