triblespace_core/
debug.rs1pub mod query {
2 use crate::query::Binding;
3 use crate::query::Constraint;
4 use crate::query::VariableId;
5 use crate::query::VariableSet;
6 use crate::value::RawValue;
7 use std::cell::RefCell;
8 use std::rc::Rc;
9
10 pub struct DebugConstraint<C> {
11 pub constraint: C,
12 pub record: Rc<RefCell<Vec<VariableId>>>,
13 }
14
15 impl<C> DebugConstraint<C> {
16 pub fn new(constraint: C, record: Rc<RefCell<Vec<VariableId>>>) -> Self {
17 DebugConstraint { constraint, record }
18 }
19 }
20
21 impl<'a, C: Constraint<'a>> Constraint<'a> for DebugConstraint<C> {
22 fn variables(&self) -> VariableSet {
23 self.constraint.variables()
24 }
25
26 fn estimate(&self, variable: VariableId, binding: &Binding) -> Option<usize> {
27 self.constraint.estimate(variable, binding)
28 }
29
30 fn propose(&self, variable: VariableId, binding: &Binding, proposals: &mut Vec<RawValue>) {
31 self.record.borrow_mut().push(variable);
32 self.constraint.propose(variable, binding, proposals);
33 }
34
35 fn confirm(&self, variable: VariableId, binding: &Binding, proposals: &mut Vec<RawValue>) {
36 self.constraint.confirm(variable, binding, proposals);
37 }
38
39 fn influence(&self, variable: VariableId) -> VariableSet {
40 self.constraint.influence(variable)
41 }
42 }
43
44 pub struct EstimateOverrideConstraint<C> {
45 pub constraint: C,
46 pub estimates: [Option<usize>; 128],
47 }
48
49 impl<C> EstimateOverrideConstraint<C> {
50 pub fn new(constraint: C) -> Self {
51 EstimateOverrideConstraint {
52 constraint,
53 estimates: [None; 128],
54 }
55 }
56
57 pub fn with_estimates(constraint: C, estimates: [Option<usize>; 128]) -> Self {
58 EstimateOverrideConstraint {
59 constraint,
60 estimates,
61 }
62 }
63
64 pub fn set_estimate(&mut self, variable: VariableId, estimate: usize) {
65 self.estimates[variable] = Some(estimate);
66 }
67 }
68
69 impl<'a, C: Constraint<'a>> Constraint<'a> for EstimateOverrideConstraint<C> {
70 fn variables(&self) -> VariableSet {
71 self.constraint.variables()
72 }
73
74 fn estimate(&self, variable: VariableId, binding: &Binding) -> Option<usize> {
75 self.estimates[variable].or_else(|| self.constraint.estimate(variable, binding))
76 }
77
78 fn propose(&self, variable: VariableId, binding: &Binding, proposals: &mut Vec<RawValue>) {
79 self.constraint.propose(variable, binding, proposals);
80 }
81
82 fn confirm(&self, variable: VariableId, binding: &Binding, proposals: &mut Vec<RawValue>) {
83 self.constraint.confirm(variable, binding, proposals);
84 }
85
86 fn influence(&self, variable: VariableId) -> VariableSet {
87 self.constraint.influence(variable)
88 }
89 }
90}