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
//! A persistent history tree for undo/redo.
//!
//! This data structure makes programming of editors easier when the editor environment
//! is open ended, such as editors that are hosting other editors.
//! It makes it possible to create game engines where scripted components
//! are interdependent and basis for new editor functionality.
//!
//! A persistent data structure is one that stores immutable data efficiently.
//! This allows a programming pattern that does not rely on undoing
//! and redoing by mutating a data structure.
//! Instead, you store data in blocks that is referenced by index in the history tree.
//!
//! The relations between the blocks is controlled by reading out child relations.
//! Data blocks can reference earlier data blocks safely.
//! The history tree does not need to know how these references are represented,
//! because the consistency is guaranteed by replicating the same state of earlier trees.
//!
//! This history tree stores records that points to previous version and parent.
//! The tree is a function of these records plus a cursor.
//! The cursor determine which records are active.
//!
//! When a record is pointed to by a new active record, it gets overriden.
//! A record is considered child of a parent when it points to the parent or any previous version.
//!
//! `.add`/`.change`/`.delete` are `O(1)` operations.
//!
//! `.children` is `O(N * M)` operation where `N` is number of parent versions and `M` is records.
//!
//! To make `.children` fast, records are stored with only indices.

#![deny(missing_docs)]

/// Stores information about a node relation.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Record {
    /// Previous version.
    pub prev: usize,
    /// Parent id.
    pub parent: usize,
    /// Removes previous nodes.
    pub remove: bool,
}

/// Stores information about history tree relations.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HistoryTree {
    /// Stores records.
    pub records: Vec<Record>,
    /// History cursor.
    /// Points to an index of records where all previous changes
    /// are active, and those after are inactive.
    /// When set to `None`, it is assumed to point to the latest version.
    pub cursor: Option<usize>,
}

impl HistoryTree {
    /// Creates a new history tree.
    pub fn new() -> HistoryTree {
        HistoryTree {
            records: vec![Record {
                prev: 0, // Points back to itself.
                parent: 0, // Points back to itself.
                remove: false,
            }],
            cursor: None,
        }
    }

    /// Gets the root.
    pub fn root(&self) -> usize {0}

    /// Gets the cursor.
    pub fn cursor(&self) -> usize {
        self.cursor.unwrap_or(self.records.len() - 1)
    }

    /// Add new node.
    pub fn add(&mut self, parent: usize) -> usize {
        let cursor = self.cursor();
        self.records.truncate(cursor + 1);
        self.cursor = None;

        let n = self.records.len();
        self.records.push(Record {
            prev: n, // Points back to itself.
            parent: parent,
            remove: false,
        });
        n
    }

    /// Change node.
    pub fn change(&mut self, node: &mut usize) {
        let cursor = self.cursor();
        self.records.truncate(cursor + 1);
        self.cursor = None;

        let n = self.records.len();
        let parent = self.records[*node].parent;
        self.records.push(Record {
            prev: *node,
            parent: parent,
            remove: false,
        });
        *node = n
    }

    /// Delete node.
    pub fn delete(&mut self, node: usize) {
        let cursor = self.cursor();
        self.records.truncate(cursor + 1);
        self.cursor = None;

        let parent = self.records[node].parent;
        self.records.push(Record {
            prev: node,
            parent: parent,
            remove: true,
        });
    }

    /// Gets the names of children.
    pub fn children(&self, parent: usize) -> Vec<usize> {
        let cursor = self.cursor.unwrap_or(self.records.len() - 1);
        if cursor < parent {return vec![];}

        let nodes: Vec<usize> = self.records[1..cursor + 1].iter()
            .enumerate()
            .filter(|&(_, r)| {
                    let mut node = parent;
                    loop {
                        if r.parent == node {return true;}
                        let new_node = self.records[node].prev;
                        if new_node == node {break;}
                        node = new_node
                    }
                    false
                })
            .map(|(i, _)| i + 1)
            .collect();

        // Remove the older versions.
        let mut new: Vec<bool> = vec![true; nodes.len()];
        for i in 0..nodes.len() {
            let a = nodes[i];
            if self.records[a].remove {new[i] = false;}
            let b = self.records[a].prev;
            if b == a  {continue;}
            if let Ok(j) = nodes.binary_search(&b) {
                new[j] = false;
            }
        }
        nodes.into_iter()
            .enumerate()
            .filter(|&(i, _)| new[i])
            .map(|(_, id)| id)
            .collect()
    }

    /// Goes back one step in history.
    pub fn undo(&mut self) {
        self.cursor = if let Some(index) = self.cursor {
            if index > 0 {Some(index - 1)}
            else if self.records.len() == 0 {None}
            else {Some(0)}
        } else {
            if self.records.len() == 0 {None}
            else {Some(self.records.len() - 2)}
        };
    }

    /// Goes forward one step in history.
    pub fn redo(&mut self) {
        self.cursor = if let Some(index) = self.cursor {
            if index + 1 >= self.records.len() {None}
            else {Some(index + 1)}
        } else {
            None
        }
    }

    /// Prints relations to standard output.
    /// This is used for debugging.
    pub fn print(&self, parent: usize, tabs: u32) {
        if tabs > 0 {
            for _ in 0..tabs - 1 {print!("  ")}
            print!("|-");
        }
        println!("{}", parent);
        for &ch in &self.children(parent) {
            self.print(ch, tabs + 1);
        }
    }
}