|
| 1 | +#include "undoTree.h" |
| 2 | + |
| 3 | +// ================================================================================================ |
| 4 | +// Class UndoTree implementation |
| 5 | + |
| 6 | +UndoTree::UndoTree(): |
| 7 | + mainBranch(new Branch(this)), numNodes(0) { |
| 8 | + branches.insert(mainBranch); |
| 9 | + mainBranch->nodes.emplace_back(DifferenceSharedPtr(new Difference)); |
| 10 | +} |
| 11 | +UndoTree::~UndoTree() { |
| 12 | + for (Branch* branch : branches) { |
| 13 | + delete branch; |
| 14 | + } |
| 15 | + branches.clear(); |
| 16 | + mainBranch = nullptr; |
| 17 | +} |
| 18 | + |
| 19 | +size_t UndoTree::size() const { |
| 20 | + return numNodes; |
| 21 | +} |
| 22 | +size_t UndoTree::numBranches() const { |
| 23 | + return branches.size(); |
| 24 | +} |
| 25 | + |
| 26 | +UndoTree::iterator UndoTree::insert(const iterator& it, DifferenceSharedPtr diff) { |
| 27 | + // Make sure the iterator is on this tree |
| 28 | + assert(branches.contains(it.branch)); |
| 29 | + |
| 30 | + // If the iterator is at the end of its branch append the new diff |
| 31 | + if (it.pos == it.branch->nodes.size() - 1) { |
| 32 | + it.branch->nodes.emplace_back(Branch::Node(diff)); |
| 33 | + return it.next(); |
| 34 | + // If the iterator is not at the end of its branch make a new branch |
| 35 | + } else { |
| 36 | + Branch* newBranch = new Branch(this, it.branch, it.pos, diff); |
| 37 | + branches.insert(newBranch); |
| 38 | + if (it.branch->nodes[it.pos].branches == nullptr) { |
| 39 | + it.branch->nodes[it.pos].branches = new std::vector<Branch*>; |
| 40 | + } |
| 41 | + it.branch->nodes[it.pos].branches->push_back(newBranch); |
| 42 | + return iterator(newBranch, 0); |
| 43 | + } |
| 44 | + numNodes++; |
| 45 | +} |
| 46 | + |
| 47 | +void UndoTree::clear() { |
| 48 | + for (Branch* branch : branches) { |
| 49 | + delete branch; |
| 50 | + } |
| 51 | + branches.clear(); |
| 52 | + mainBranch = new Branch(this); |
| 53 | + mainBranch->nodes.emplace_back(DifferenceSharedPtr(new Difference)); |
| 54 | + branches.insert(mainBranch); |
| 55 | + numNodes = 0; |
| 56 | +} |
| 57 | + |
| 58 | +void UndoTree::prune(const iterator& begin) { |
| 59 | + // Make sure the iterator is on this tree |
| 60 | + assert(begin.branch->tree == this); |
| 61 | + // Delete all branches that originate after begin |
| 62 | + for (int i = begin.pos; i < begin.branch->nodes.size(); i++) { |
| 63 | + if (begin.branch->nodes[begin.pos].branches != nullptr) { |
| 64 | + for (Branch* b : *begin.branch->nodes[begin.pos].branches) { |
| 65 | + branches.erase(b); |
| 66 | + numNodes -= b->nodes.size(); |
| 67 | + delete b; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + // Remove all diffs after begin |
| 72 | + numNodes -= begin.branch->nodes.size() - begin.pos; |
| 73 | + begin.branch->nodes.erase(begin.branch->nodes.begin() + begin.pos); |
| 74 | +} |
| 75 | + |
| 76 | +UndoTree::iterator UndoTree::begin() { |
| 77 | + return iterator(mainBranch, 0); |
| 78 | +} |
| 79 | + |
| 80 | +UndoTree::iterator UndoTree::end() { |
| 81 | + return iterator(mainBranch, -1); |
| 82 | +} |
| 83 | + |
| 84 | +// ================================================================================================ |
| 85 | +// Class UndoTree::iterator implementation |
| 86 | + |
| 87 | +UndoTree::iterator::iterator(Branch* branch, int pos): |
| 88 | + branch(branch), pos(pos) { } |
| 89 | + |
| 90 | +UndoTree::iterator UndoTree::iterator::next(int whichBranch) const { |
| 91 | + if (whichBranch == -1) { |
| 92 | + if (pos + 1 == this->branch->nodes.size()) { |
| 93 | + return iterator(this->branch, -1); |
| 94 | + } else return iterator(this->branch, pos + 1); |
| 95 | + } else return iterator((*this->branch->nodes[pos].branches)[whichBranch], 0); |
| 96 | +} |
| 97 | + |
| 98 | +UndoTree::iterator UndoTree::iterator::prev() const { |
| 99 | + if (pos == -1) { |
| 100 | + return iterator(this->branch, this->branch->nodes.size() - 1); |
| 101 | + } else if (pos == 0) { |
| 102 | + |
| 103 | + return iterator(this->branch->parentBranch, this->branch->parentNode); |
| 104 | + } else return iterator(this->branch, pos - 1); |
| 105 | +} |
| 106 | + |
| 107 | +int UndoTree::iterator::numBranches() const { |
| 108 | + if (branch->nodes[pos].branches == nullptr) { |
| 109 | + return 0; |
| 110 | + } else return branch->nodes[pos].branches->size(); |
| 111 | +} |
| 112 | + |
| 113 | +int UndoTree::iterator::whichBranch() const { |
| 114 | + if (branch->parentBranch == nullptr) { |
| 115 | + return -1; |
| 116 | + } else { |
| 117 | + UndoTree::Branch::Node& parentNode = branch->parentBranch->nodes[branch->parentNode]; |
| 118 | + for (size_t i = 0; i < parentNode.branches->size(); i++) { |
| 119 | + if (branch == (*parentNode.branches)[i]) { |
| 120 | + return i; |
| 121 | + } |
| 122 | + } |
| 123 | + // Control should never reach here |
| 124 | + assert(false); |
| 125 | + return INT_MAX; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +DifferenceSharedPtr& UndoTree::iterator::operator*() { |
| 130 | + return branch->nodes[pos].diff; |
| 131 | +} |
| 132 | + |
| 133 | +bool UndoTree::iterator::operator==(const iterator& other) const { |
| 134 | + // -1 denotes end iterator |
| 135 | + if (this->pos == other.pos && other.pos == -1) { |
| 136 | + return true; |
| 137 | + // Otherwise check if they are at same place on same branch |
| 138 | + } else return this->pos == other.pos && this->branch == other.branch; |
| 139 | +} |
| 140 | + |
| 141 | +bool UndoTree::iterator::operator!=(const iterator& other) const { |
| 142 | + return !(*this == other); |
| 143 | +} |
| 144 | + |
| 145 | +// ================================================================================================ |
| 146 | +// Class UndoTree::Branch implementation |
| 147 | + |
| 148 | +UndoTree::Branch::Branch(UndoTree* tree): |
| 149 | + tree(tree), parentBranch(nullptr), parentNode(-1) { } |
| 150 | +UndoTree::Branch::Branch(UndoTree* tree, Branch* parentBranch, int parentNode, DifferenceSharedPtr diff): |
| 151 | + tree(tree), parentBranch(parentBranch), parentNode(parentNode) { |
| 152 | + nodes.emplace_back(Node(diff)); |
| 153 | +} |
| 154 | + |
| 155 | +// ================================================================================================ |
| 156 | +// Class UndoTree::Branch::Node implementation |
| 157 | + |
| 158 | +UndoTree::Branch::Node::Node(DifferenceSharedPtr diff): |
| 159 | + diff(diff), branches(nullptr) { } |
| 160 | + |
| 161 | +UndoTree::Branch::Node::~Node() { |
| 162 | + delete branches; |
| 163 | +} |
| 164 | + |
| 165 | +bool onSameBranch(const UndoTree::iterator& a, const UndoTree::iterator& b) { |
| 166 | + return a.branch == b.branch; |
| 167 | +} |
0 commit comments