Serialize a universe to bytes (iterative to avoid stack overflow).
(u: &Univ, buf: &mut Vec<u8>)
| 51 | |
| 52 | /// Serialize a universe to bytes (iterative to avoid stack overflow). |
| 53 | pub fn put_univ(u: &Univ, buf: &mut Vec<u8>) { |
| 54 | let mut stack: Vec<&Univ> = vec![u]; |
| 55 | |
| 56 | while let Some(curr) = stack.pop() { |
| 57 | match curr { |
| 58 | Univ::Zero => { |
| 59 | Tag2::new(Univ::FLAG_ZERO_SUCC, 0).put(buf); |
| 60 | }, |
| 61 | Univ::Succ(inner) => { |
| 62 | // Count the number of successors for telescope compression |
| 63 | let mut count = 1u64; |
| 64 | let mut base = inner.as_ref(); |
| 65 | while let Univ::Succ(next) = base { |
| 66 | count += 1; |
| 67 | base = next.as_ref(); |
| 68 | } |
| 69 | Tag2::new(Univ::FLAG_ZERO_SUCC, count).put(buf); |
| 70 | stack.push(base); |
| 71 | }, |
| 72 | Univ::Max(a, b) => { |
| 73 | Tag2::new(Univ::FLAG_MAX, 0).put(buf); |
| 74 | stack.push(b); // Process b after a |
| 75 | stack.push(a); |
| 76 | }, |
| 77 | Univ::IMax(a, b) => { |
| 78 | Tag2::new(Univ::FLAG_IMAX, 0).put(buf); |
| 79 | stack.push(b); // Process b after a |
| 80 | stack.push(a); |
| 81 | }, |
| 82 | Univ::Var(idx) => { |
| 83 | Tag2::new(Univ::FLAG_VAR, *idx).put(buf); |
| 84 | }, |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// Frame for iterative universe deserialization. |
| 90 | enum GetUnivFrame { |