synthphonia/expr/
context.rs1
2use derive_more::{DebugCustom, Constructor};
3use itertools::Itertools;
4use crate::{parser::{ioexamples::IOExamples, problem::PBEProblem}, tree_learning::Bits, value::Value};
5
6use super::Expr;
7
8#[derive(DebugCustom, Constructor, Clone)]
9#[debug(fmt = "(n: {:?}, p: {:?})", n, p)]
10pub struct Context{
12 pub len: usize,
13 pub p: Vec<Value>,
15 pub n: Vec<Value>,
17 pub output: Value,
18}
19
20impl Context {
21 pub fn len(&self) -> usize { self.len }
23
24 pub fn get(&self, index: i64) -> Option<&Value> {
26 if index >= 0 { self.p.get(index as usize) }
27 else { self.n.get((!index) as usize) }
28 }
29 pub fn iter(&self) -> impl Iterator<Item=Value> + '_ {
31 [self.output].into_iter().chain(self.p.iter().cloned()).chain(self.n.iter().cloned())
32 }
33 pub fn inputs(&self) -> impl Iterator<Item=Value> + '_ {
35 self.p.iter().cloned().chain(self.n.iter().cloned())
36 }
37 pub fn outputs(&self) -> impl Iterator<Item=Value> + '_ {
39 [self.output].into_iter()
40 }
41 pub fn evaluate(&self, e: &'static Expr) -> Option<Bits> {
43 let v = e.eval(self);
44 self.output.eq_bits(&v)
45 }
46 pub fn with_examples(&self, exs: &[usize]) -> Context {
48 Context {
49 len: exs.len(),
50 p: self.p.iter().map(|x| x.with_examples(exs)).collect_vec(),
51 n: self.n.iter().map(|x| x.with_examples(exs)).collect_vec(),
52 output: self.output.with_examples(exs),
53 }
54 }
55}
56
57impl std::ops::Index<i64> for Context {
58 type Output = Value;
59
60 fn index(&self, index: i64) -> &Self::Output {
62 self.get(index).expect("out of range")
63 }
64}
65
66
67impl Context {
68 pub fn from_examples(examples: &IOExamples) -> Self {
70 Self {
71 len: examples.output.len(),
72 p: examples.inputs.clone(),
73 n: Vec::new(),
74 output: examples.output
75 }
76 }
77}
78