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
use std::collections::BTreeMap;
use std::ops::{Index, IndexMut};
use std::cmp::min;

use crate::graph::{Graph, Node, NodeId};

#[derive(Debug)]
pub struct Meta {
    map: BTreeMap<NodeId, MetaItem>,
}

#[derive(Debug, Default)]
pub struct MetaItem {
    /// Number of references to this node
    pub refcount: usize,
    /// Minimum number of bytes that ought to be read for this
    /// node to find a match
    pub min_read: usize,
    /// Marks whether or not this node leads to a loop entry node.
    pub is_loop_init: bool,
    /// Ids of other nodes that point to this node while this
    /// node is on a stack (creating a loop)
    pub loop_entry_from: Vec<NodeId>,
}

impl Index<NodeId> for Meta {
    type Output = MetaItem;

    fn index(&self, id: NodeId) -> &MetaItem {
        &self.map[&id]
    }
}

impl IndexMut<NodeId> for Meta {
    fn index_mut(&mut self, id: NodeId) -> &mut MetaItem {
        self.map.entry(id).or_default()
    }
}

impl MetaItem {
    fn loop_entry(&mut self, id: NodeId) {
        if let Err(idx) = self.loop_entry_from.binary_search(&id) {
            self.loop_entry_from.insert(idx, id);
        }
    }
}

impl Meta {
    pub fn analyze<T>(root: NodeId, graph: &Graph<T>) -> Self {
        let mut meta = Meta {
            map: Default::default(),
        };

        meta.first_pass(root, root, graph, &mut Vec::new());

        meta
    }

    pub fn first_pass<T>(
        &mut self,
        this: NodeId,
        parent: NodeId,
        graph: &Graph<T>,
        stack: &mut Vec<NodeId>,
    ) -> &MetaItem {
        let meta = &mut self[this];
        let is_done = meta.refcount > 0;

        meta.refcount += 1;

        if stack.contains(&this) {
            meta.loop_entry(parent);
            self[parent].is_loop_init = true;
        }
        if is_done {
            return &self[this];
        }

        stack.push(this);

        let mut min_read;

        match &graph[this] {
            Node::Fork(fork) => {
                min_read = usize::max_value();
                for (_, id) in fork.branches() {
                    let meta = self.first_pass(id, this, graph, stack);

                    if meta.is_loop_init {
                        min_read = 1;
                    } else {
                        min_read = min(min_read, meta.min_read + 1);
                    }
                }
                if let Some(id) = fork.miss {
                    let meta = self.first_pass(id, this, graph, stack);

                    if meta.is_loop_init {
                        min_read = 0;
                    } else {
                        min_read = min(min_read, meta.min_read);
                    }
                }
                if min_read == usize::max_value() {
                    min_read = 0;
                }
            },
            Node::Rope(rope) => {
                min_read = rope.pattern.len();
                let meta = self.first_pass(rope.then, this, graph, stack);

                if !meta.is_loop_init {
                    min_read += meta.min_read;
                }

                if let Some(id) = rope.miss.first() {
                    let meta = self.first_pass(id, this, graph, stack);

                    if meta.is_loop_init {
                        min_read = 0;
                    } else {
                        min_read = min(min_read, meta.min_read);
                    }
                }
            },
            Node::Leaf(_) => min_read = 0,
        }

        stack.pop();

        let meta = &mut self[this];
        meta.min_read = min_read;
        let second_pass = meta.loop_entry_from.clone();

        for id in second_pass {
            self.meta_second_pass(id, graph);
        }

        &self[this]
    }

    fn meta_second_pass<T>(&mut self, id: NodeId, graph: &Graph<T>) {
        let mut min_read;

        match &graph[id] {
            Node::Fork(fork) => {
                min_read = usize::max_value();
                for (_, id) in fork.branches() {
                    let meta = &self[id];

                    if meta.is_loop_init {
                        min_read = 1;
                    } else {
                        min_read = min(min_read, meta.min_read + 1);
                    }
                }
                if min_read == usize::max_value() {
                    min_read = 0;
                }
            },
            Node::Rope(rope) => {
                min_read = rope.pattern.len();
                let meta = &self[rope.then];

                if !meta.is_loop_init {
                    min_read += meta.min_read;
                }
            },
            Node::Leaf(_) => unreachable!(),
        }

        self[id].min_read = min_read;
    }
}