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
use super::math_node::{MathNodeType, NodeIndex};
use std::fmt;

#[derive(Default, Debug, Clone)]
pub struct Piecewise {
    pub children: Vec<NodeIndex>,
    pub pieces: Vec<NodeIndex>,
    pub otherwise: Option<NodeIndex>,
    pub parent: Option<NodeIndex>,
}

impl Piecewise {
    pub fn index(&mut self, tag_type: MathNodeType, location: NodeIndex) {
        match tag_type {
            MathNodeType::Piece => {
                self.pieces.push(location);
            }
            MathNodeType::Otherwise => {
                if self.otherwise == None {
                    self.otherwise = Some(location);
                } else {
                    panic!("Can't have multiple otherwise expressions in an piecewise function!");
                }
            }
            MathNodeType::Op
            | MathNodeType::Ci
            | MathNodeType::Cn
            | MathNodeType::Root
            | MathNodeType::BVar
            | MathNodeType::Apply
            | MathNodeType::Lambda
            | MathNodeType::Piecewise
            | MathNodeType::Constant => {
                panic!("Can't have {} in a piecewise function!", tag_type);
            }
        }
    }
}

impl fmt::Display for Piecewise {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Pieces: {:?}, Otherwise: {:?}, Children: {:?}, Parent: {:?}",
            self.pieces, self.otherwise, self.children, self.parent
        )
    }
}

#[derive(Default, Debug, Clone)]
pub struct Piece {
    pub children: Vec<NodeIndex>,
    pub expr: Option<NodeIndex>,
    pub condition: Option<NodeIndex>,
    pub parent: Option<NodeIndex>,
}

impl Piece {
    pub fn index(&mut self, _tag_type: MathNodeType, location: NodeIndex) {
        if self.children.len() == 1 {
            self.expr = Some(location);
        } else if self.children.len() == 2 {
            self.condition = Some(location);
        } else {
            panic!("A piece in a piecewise function can have only two children.");
        }
    }
}

impl fmt::Display for Piece {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Expr: {:?}, Condition: {:?}, Children: {:?}, Parent: {:?}",
            self.expr, self.condition, self.children, self.parent
        )
    }
}

#[derive(Default, Debug, Clone)]
pub struct Otherwise {
    pub children: Vec<NodeIndex>,
    pub expr: Option<NodeIndex>,
    pub parent: Option<NodeIndex>,
}

impl Otherwise {
    pub fn index(&mut self, tag_type: MathNodeType, location: NodeIndex) {
        match tag_type {
            MathNodeType::Apply
            | MathNodeType::Lambda
            | MathNodeType::Ci
            | MathNodeType::Cn
            | MathNodeType::Piecewise
            | MathNodeType::Constant => {
                if self.expr == None {
                    self.expr = Some(location);
                } else {
                    panic!("Can't have two children in an \"otherwise\" branch!");
                }
            }
            MathNodeType::Root
            | MathNodeType::Op
            | MathNodeType::Otherwise
            | MathNodeType::BVar
            | MathNodeType::Piece => {
                panic!("Can't have {} in an \"otherwise\" branch!", tag_type);
            }
        }
    }
}

impl fmt::Display for Otherwise {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Expr: {:?}, Children: {:?}, Parent: {:?}",
            self.expr, self.children, self.parent
        )
    }
}