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
use super::*;
use std::fmt::{Display, Error, Formatter};

/// For display purposes, pointer is just a number.
impl Display for BddPointer {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        f.write_fmt(format_args!("{}", self.0))
    }
}

impl BddPointer {
    /// Make a new pointer to the `0` terminal node.
    pub fn zero() -> BddPointer {
        BddPointer(0)
    }

    /// Make a new pointer to the `1` terminal node.
    pub fn one() -> BddPointer {
        BddPointer(1)
    }

    /// Check if the pointer corresponds to the `0` terminal.
    pub fn is_zero(&self) -> bool {
        self.0 == 0
    }

    /// Check if the pointer corresponds to the `1` terminal.
    pub fn is_one(&self) -> bool {
        self.0 == 1
    }

    /// Check if the pointer corresponds to the `0` or `1` terminal.
    pub fn is_terminal(&self) -> bool {
        self.0 < 2
    }

    /// Cast this pointer to standard usize index.
    pub fn to_index(self) -> usize {
        self.0 as usize
    }

    /// Create a pointer from an usize index.
    pub fn from_index(index: usize) -> BddPointer {
        BddPointer(index as u32)
    }

    /// Convert a `bool` value to valid terminal BDD pointer.
    pub fn from_bool(value: bool) -> BddPointer {
        if value {
            BddPointer::one()
        } else {
            BddPointer::zero()
        }
    }

    /// If this pointer is a terminal, convert it to `bool`, otherwise return `None`.
    pub fn as_bool(&self) -> Option<bool> {
        match self.0 {
            0 => Some(false),
            1 => Some(true),
            _ => None,
        }
    }

    /// If this pointer corresponds to a terminal node, flip it (switching `1` to `0` and
    /// vice versa).
    pub fn flip_if_terminal(&mut self) {
        if self.0 < 2 {
            self.0 = (self.0 + 1) % 2;
        }
    }

    /// Convert to little endian bytes
    pub fn to_le_bytes(self) -> [u8; 4] {
        self.0.to_le_bytes()
    }

    /// Read from little endian byte representation
    pub fn from_le_bytes(bytes: [u8; 4]) -> BddPointer {
        BddPointer(u32::from_le_bytes(bytes))
    }
}