1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use boow::Bow;

use std::collections::HashMap;
use std::fmt;
use std::sync::RwLock;

use transform::Transformation;

mod build;
mod compute;
mod iterators;
mod node;
pub use self::iterators::{Dependency, LinkIter, NodeIter};
pub use self::node::{Node, NodeId};

type Cache<T> = RwLock<Option<T>>;

/// Dynamic Syntax Tree
///
/// Represent the node graph for the computing tasks to be done.
/// Each node is identified by a [`NodeId`].
/// A DST has two types of nodes, transformation and output nodes.
/// An output node is a leaf, it is the end of the journey of the data.
/// A transformation node wraps a [`Transformation`] to takes input data and
/// compute output data out of it.
///
/// Each output node is identified by an [`OutputId`], while each transformation
/// node is identified by a [`TransformIdx`].
#[derive(Debug)]
pub struct DST<'t, T: Clone + 't, E: 't> {
    transforms: HashMap<TransformIdx, MetaTransform<'t, T, E>>,
    edges: HashMap<Output, InputList>,
    outputs: HashMap<OutputId, Option<Output>>,
    cache: HashMap<Output, Cache<T>>,
}

#[derive(Debug)]
pub struct MetaTransform<'t, T: Clone + 't, E: 't> {
    t: Bow<'t, Transformation<T, E>>,
    input_defaults: Vec<Option<T>>,
}

impl<'t, T, E> MetaTransform<'t, T, E>
where
    T: Clone,
{
    pub fn new(t: Bow<'t, Transformation<T, E>>) -> Self {
        let input_defaults: Vec<_> = t.input.iter().map(|(_, default)| default.clone()).collect();
        Self { t, input_defaults }
    }

    pub fn new_with_defaults(
        t: Bow<'t, Transformation<T, E>>,
        input_defaults: Vec<Option<T>>,
    ) -> Self {
        Self { t, input_defaults }
    }

    pub fn transform(&self) -> &Transformation<T, E> {
        self.t.as_ref()
    }

    pub fn defaults(&self) -> &[Option<T>] {
        &self.input_defaults
    }

    pub fn transform_mut(&mut self) -> Option<&mut Transformation<T, E>> {
        self.t.borrow_mut()
    }

    pub fn defaults_mut(&mut self) -> &mut [Option<T>] {
        &mut self.input_defaults
    }

    pub fn tokenize(self) -> (Bow<'t, Transformation<T, E>>, Vec<Option<T>>) {
        (self.t, self.input_defaults)
    }
}

/// Uniquely identify an ouput of a transformation node
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Output {
    pub t_idx: TransformIdx,
    output_i: OutputIdx,
}

/// Uniquely identify an input of a node
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Input {
    pub t_idx: TransformIdx,
    input_i: InputIdx,
}

impl Output {
    /// Create a new Output pointing to the *out_i*-th output of TransformIdx transform.
    /// Counting start from 0.
    pub fn new(t_idx: TransformIdx, out_i: usize) -> Self {
        Self {
            t_idx,
            output_i: OutputIdx(out_i),
        }
    }
    /// Get index of output (starting from 0 for the first output).
    pub fn index(&self) -> usize {
        self.output_i.into()
    }
}

impl Input {
    /// Create a new Input pointing to the *in_i*-th input of TransformIdx transform.
    /// Counting start from 0.
    pub fn new(t_idx: TransformIdx, in_i: usize) -> Self {
        Self {
            t_idx,
            input_i: InputIdx(in_i),
        }
    }
    /// Get index of input (starting from 0 for the first input).
    pub fn index(&self) -> usize {
        self.input_i.into()
    }
}

#[derive(Debug)]
struct InputList {
    /// List of all inputs to which the data is fed
    inputs: Vec<Input>,
}

impl InputList {
    pub fn new(inputs: Vec<Input>) -> Self {
        Self { inputs }
    }

    pub fn push(&mut self, input: Input) {
        self.inputs.push(input);
    }

    pub fn contains(&self, input: &Input) -> bool {
        self.inputs.contains(input)
    }
}

/// Identify a transformation node
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct TransformIdx(usize);
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
struct OutputIdx(usize);
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
struct InputIdx(usize);
/// Identify an output node
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct OutputId(usize);

/// Errors when computing or building a [`DST`].
#[derive(Debug)]
pub enum DSTError<E> {
    InvalidInput(String),
    InvalidOutput(String),
    DuplicateEdge(String),
    Cycle(String),
    IncompatibleTypes(String),
    MissingOutputID(String),
    ComputeError(String),
    InnerComputeError(E),
    NothingDoneYet,
}

impl From<OutputIdx> for usize {
    fn from(output: OutputIdx) -> usize {
        output.0
    }
}
impl From<InputIdx> for usize {
    fn from(input: InputIdx) -> usize {
        input.0
    }
}

impl TransformIdx {
    fn incr(self) -> Self {
        TransformIdx(self.0 + 1)
    }
    pub fn id(&self) -> usize {
        self.0
    }
}

impl OutputId {
    fn incr(self) -> Self {
        OutputId(self.0 + 1)
    }
    pub fn id(&self) -> usize {
        self.0
    }
}

/// Identify an input slot, i.e., the input of a transform or the input of a
/// final output node.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum InputSlot<'a> {
    Transform(&'a Input),
    Output(&'a OutputId),
}

/// Convenient implementation for debugging
impl<'t, T, E> fmt::Display for DST<'t, T, E>
where
    T: 't + Clone,
    E: 't,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for (output_id, output) in self.outputs_iter() {
            write!(f, "{:?}\n", output_id)?;
            if let Some(output) = output {
                self.write_output(f, 1, output)?;
            } else {
                write!(f, "{}*\n", pad(1))?;
            }
        }
        Ok(())
    }
}

impl<'t, T, E> DST<'t, T, E>
where
    T: 't + Clone,
    E: 't,
{
    fn write_output(&self, f: &mut fmt::Formatter, depth: usize, output: &Output) -> fmt::Result {
        if let Some(meta) = self.transforms.get(&output.t_idx) {
            let t = meta.transform();
            write!(f, "{}{}\n", pad(depth), t.name)?;
            let deps = self.outputs_attached_to_transform(&output.t_idx).unwrap();
            for (i, dep) in deps.into_iter().enumerate() {
                write!(f, "{}{}", pad(depth + 1), i)?;
                if let Some(dep) = dep {
                    write!(f, "\n")?;
                    self.write_output(f, depth + 2, &dep)?;
                } else {
                    write!(f, " (no node)\n")?;
                }
            }
            Ok(())
        } else {
            write!(f, "{}(missing node)\n", pad(depth))
        }
    }
}

fn pad(depth: usize) -> String {
    const SEPARATOR: &'static str = "\\_ ";
    const PADDER: &'static str = "    ";
    let mut out = String::with_capacity(depth * PADDER.len() + SEPARATOR.len());
    for _ in 0..depth {
        out.push_str(PADDER);
    }
    out.push_str(SEPARATOR);
    out
}