Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 1 | //===--- DeltaTree.cpp - B-Tree for Rewrite Delta tracking ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the DeltaTree and related classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Rewrite/DeltaTree.h" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 15 | #include "clang/Basic/LLVM.h" |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 16 | #include <cstring> |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 17 | #include <cstdio> |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 18 | using namespace clang; |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 20 | /// The DeltaTree class is a multiway search tree (BTree) structure with some |
Chris Lattner | 3ea5cf8 | 2010-01-20 17:53:58 +0000 | [diff] [blame] | 21 | /// fancy features. B-Trees are generally more memory and cache efficient |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 22 | /// than binary trees, because they store multiple keys/values in each node. |
| 23 | /// |
| 24 | /// DeltaTree implements a key/value mapping from FileIndex to Delta, allowing |
| 25 | /// fast lookup by FileIndex. However, an added (important) bonus is that it |
| 26 | /// can also efficiently tell us the full accumulated delta for a specific |
| 27 | /// file offset as well, without traversing the whole tree. |
| 28 | /// |
| 29 | /// The nodes of the tree are made up of instances of two classes: |
| 30 | /// DeltaTreeNode and DeltaTreeInteriorNode. The later subclasses the |
| 31 | /// former and adds children pointers. Each node knows the full delta of all |
| 32 | /// entries (recursively) contained inside of it, which allows us to get the |
| 33 | /// full delta implied by a whole subtree in constant time. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | /// SourceDelta - As code in the original input buffer is added and deleted, |
| 37 | /// SourceDelta records are used to keep track of how the input SourceLocation |
| 38 | /// object is mapped into the output buffer. |
| 39 | struct SourceDelta { |
| 40 | unsigned FileLoc; |
| 41 | int Delta; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 43 | static SourceDelta get(unsigned Loc, int D) { |
| 44 | SourceDelta Delta; |
| 45 | Delta.FileLoc = Loc; |
| 46 | Delta.Delta = D; |
| 47 | return Delta; |
| 48 | } |
| 49 | }; |
Douglas Gregor | 6991218 | 2009-11-17 06:52:37 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 51 | /// DeltaTreeNode - The common part of all nodes. |
| 52 | /// |
| 53 | class DeltaTreeNode { |
Douglas Gregor | 6991218 | 2009-11-17 06:52:37 +0000 | [diff] [blame] | 54 | public: |
| 55 | struct InsertResult { |
| 56 | DeltaTreeNode *LHS, *RHS; |
| 57 | SourceDelta Split; |
| 58 | }; |
| 59 | |
| 60 | private: |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 61 | friend class DeltaTreeInteriorNode; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 63 | /// WidthFactor - This controls the number of K/V slots held in the BTree: |
| 64 | /// how wide it is. Each level of the BTree is guaranteed to have at least |
Chris Lattner | febe719 | 2008-04-14 07:17:29 +0000 | [diff] [blame] | 65 | /// WidthFactor-1 K/V pairs (except the root) and may have at most |
| 66 | /// 2*WidthFactor-1 K/V pairs. |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 67 | enum { WidthFactor = 8 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 69 | /// Values - This tracks the SourceDelta's currently in this node. |
| 70 | /// |
| 71 | SourceDelta Values[2*WidthFactor-1]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 73 | /// NumValuesUsed - This tracks the number of values this node currently |
| 74 | /// holds. |
| 75 | unsigned char NumValuesUsed; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 77 | /// IsLeaf - This is true if this is a leaf of the btree. If false, this is |
| 78 | /// an interior node, and is actually an instance of DeltaTreeInteriorNode. |
| 79 | bool IsLeaf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 81 | /// FullDelta - This is the full delta of all the values in this node and |
| 82 | /// all children nodes. |
| 83 | int FullDelta; |
| 84 | public: |
| 85 | DeltaTreeNode(bool isLeaf = true) |
Chris Lattner | b169e90 | 2008-04-13 08:22:30 +0000 | [diff] [blame] | 86 | : NumValuesUsed(0), IsLeaf(isLeaf), FullDelta(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 88 | bool isLeaf() const { return IsLeaf; } |
| 89 | int getFullDelta() const { return FullDelta; } |
| 90 | bool isFull() const { return NumValuesUsed == 2*WidthFactor-1; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 92 | unsigned getNumValuesUsed() const { return NumValuesUsed; } |
| 93 | const SourceDelta &getValue(unsigned i) const { |
| 94 | assert(i < NumValuesUsed && "Invalid value #"); |
| 95 | return Values[i]; |
| 96 | } |
| 97 | SourceDelta &getValue(unsigned i) { |
| 98 | assert(i < NumValuesUsed && "Invalid value #"); |
| 99 | return Values[i]; |
| 100 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 102 | /// DoInsertion - Do an insertion of the specified FileIndex/Delta pair into |
| 103 | /// this node. If insertion is easy, do it and return false. Otherwise, |
| 104 | /// split the node, populate InsertRes with info about the split, and return |
| 105 | /// true. |
| 106 | bool DoInsertion(unsigned FileIndex, int Delta, InsertResult *InsertRes); |
| 107 | |
Chris Lattner | b169e90 | 2008-04-13 08:22:30 +0000 | [diff] [blame] | 108 | void DoSplit(InsertResult &InsertRes); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
| 110 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 111 | /// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a |
| 112 | /// local walk over our contained deltas. |
| 113 | void RecomputeFullDeltaLocally(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 115 | void Destroy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Chris Lattner | fae9622 | 2010-09-03 04:34:38 +0000 | [diff] [blame] | 117 | //static inline bool classof(const DeltaTreeNode *) { return true; } |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 118 | }; |
| 119 | } // end anonymous namespace |
| 120 | |
| 121 | namespace { |
| 122 | /// DeltaTreeInteriorNode - When isLeaf = false, a node has child pointers. |
| 123 | /// This class tracks them. |
| 124 | class DeltaTreeInteriorNode : public DeltaTreeNode { |
| 125 | DeltaTreeNode *Children[2*WidthFactor]; |
| 126 | ~DeltaTreeInteriorNode() { |
| 127 | for (unsigned i = 0, e = NumValuesUsed+1; i != e; ++i) |
| 128 | Children[i]->Destroy(); |
| 129 | } |
| 130 | friend class DeltaTreeNode; |
| 131 | public: |
| 132 | DeltaTreeInteriorNode() : DeltaTreeNode(false /*nonleaf*/) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | DeltaTreeInteriorNode(const InsertResult &IR) |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 135 | : DeltaTreeNode(false /*nonleaf*/) { |
| 136 | Children[0] = IR.LHS; |
| 137 | Children[1] = IR.RHS; |
| 138 | Values[0] = IR.Split; |
| 139 | FullDelta = IR.LHS->getFullDelta()+IR.RHS->getFullDelta()+IR.Split.Delta; |
| 140 | NumValuesUsed = 1; |
| 141 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 143 | const DeltaTreeNode *getChild(unsigned i) const { |
| 144 | assert(i < getNumValuesUsed()+1 && "Invalid child"); |
| 145 | return Children[i]; |
| 146 | } |
| 147 | DeltaTreeNode *getChild(unsigned i) { |
| 148 | assert(i < getNumValuesUsed()+1 && "Invalid child"); |
| 149 | return Children[i]; |
| 150 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Chris Lattner | fae9622 | 2010-09-03 04:34:38 +0000 | [diff] [blame] | 152 | //static inline bool classof(const DeltaTreeInteriorNode *) { return true; } |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 153 | static inline bool classof(const DeltaTreeNode *N) { return !N->isLeaf(); } |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 154 | }; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /// Destroy - A 'virtual' destructor. |
| 159 | void DeltaTreeNode::Destroy() { |
| 160 | if (isLeaf()) |
| 161 | delete this; |
| 162 | else |
| 163 | delete cast<DeltaTreeInteriorNode>(this); |
| 164 | } |
| 165 | |
| 166 | /// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a |
| 167 | /// local walk over our contained deltas. |
| 168 | void DeltaTreeNode::RecomputeFullDeltaLocally() { |
| 169 | int NewFullDelta = 0; |
| 170 | for (unsigned i = 0, e = getNumValuesUsed(); i != e; ++i) |
| 171 | NewFullDelta += Values[i].Delta; |
| 172 | if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this)) |
| 173 | for (unsigned i = 0, e = getNumValuesUsed()+1; i != e; ++i) |
| 174 | NewFullDelta += IN->getChild(i)->getFullDelta(); |
| 175 | FullDelta = NewFullDelta; |
| 176 | } |
| 177 | |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 178 | /// DoInsertion - Do an insertion of the specified FileIndex/Delta pair into |
| 179 | /// this node. If insertion is easy, do it and return false. Otherwise, |
| 180 | /// split the node, populate InsertRes with info about the split, and return |
| 181 | /// true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta, |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 183 | InsertResult *InsertRes) { |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 184 | // Maintain full delta for this node. |
| 185 | FullDelta += Delta; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 187 | // Find the insertion point, the first delta whose index is >= FileIndex. |
| 188 | unsigned i = 0, e = getNumValuesUsed(); |
| 189 | while (i != e && FileIndex > getValue(i).FileLoc) |
| 190 | ++i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 192 | // If we found an a record for exactly this file index, just merge this |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 193 | // value into the pre-existing record and finish early. |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 194 | if (i != e && getValue(i).FileLoc == FileIndex) { |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 195 | // NOTE: Delta could drop to zero here. This means that the delta entry is |
| 196 | // useless and could be removed. Supporting erases is more complex than |
| 197 | // leaving an entry with Delta=0, so we just leave an entry with Delta=0 in |
| 198 | // the tree. |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 199 | Values[i].Delta += Delta; |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 200 | return false; |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 201 | } |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 202 | |
| 203 | // Otherwise, we found an insertion point, and we know that the value at the |
| 204 | // specified index is > FileIndex. Handle the leaf case first. |
| 205 | if (isLeaf()) { |
| 206 | if (!isFull()) { |
| 207 | // For an insertion into a non-full leaf node, just insert the value in |
| 208 | // its sorted position. This requires moving later values over. |
| 209 | if (i != e) |
| 210 | memmove(&Values[i+1], &Values[i], sizeof(Values[0])*(e-i)); |
| 211 | Values[i] = SourceDelta::get(FileIndex, Delta); |
| 212 | ++NumValuesUsed; |
| 213 | return false; |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 214 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 216 | // Otherwise, if this is leaf is full, split the node at its median, insert |
| 217 | // the value into one of the children, and return the result. |
| 218 | assert(InsertRes && "No result location specified"); |
| 219 | DoSplit(*InsertRes); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 221 | if (InsertRes->Split.FileLoc > FileIndex) |
| 222 | InsertRes->LHS->DoInsertion(FileIndex, Delta, 0 /*can't fail*/); |
| 223 | else |
| 224 | InsertRes->RHS->DoInsertion(FileIndex, Delta, 0 /*can't fail*/); |
| 225 | return true; |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 226 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 228 | // Otherwise, this is an interior node. Send the request down the tree. |
| 229 | DeltaTreeInteriorNode *IN = cast<DeltaTreeInteriorNode>(this); |
| 230 | if (!IN->Children[i]->DoInsertion(FileIndex, Delta, InsertRes)) |
| 231 | return false; // If there was space in the child, just return. |
| 232 | |
| 233 | // Okay, this split the subtree, producing a new value and two children to |
| 234 | // insert here. If this node is non-full, we can just insert it directly. |
| 235 | if (!isFull()) { |
| 236 | // Now that we have two nodes and a new element, insert the perclated value |
| 237 | // into ourself by moving all the later values/children down, then inserting |
| 238 | // the new one. |
| 239 | if (i != e) |
| 240 | memmove(&IN->Children[i+2], &IN->Children[i+1], |
| 241 | (e-i)*sizeof(IN->Children[0])); |
| 242 | IN->Children[i] = InsertRes->LHS; |
| 243 | IN->Children[i+1] = InsertRes->RHS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 245 | if (e != i) |
| 246 | memmove(&Values[i+1], &Values[i], (e-i)*sizeof(Values[0])); |
| 247 | Values[i] = InsertRes->Split; |
| 248 | ++NumValuesUsed; |
| 249 | return false; |
| 250 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 252 | // Finally, if this interior node was full and a node is percolated up, split |
| 253 | // ourself and return that up the chain. Start by saving all our info to |
| 254 | // avoid having the split clobber it. |
| 255 | IN->Children[i] = InsertRes->LHS; |
| 256 | DeltaTreeNode *SubRHS = InsertRes->RHS; |
| 257 | SourceDelta SubSplit = InsertRes->Split; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 259 | // Do the split. |
| 260 | DoSplit(*InsertRes); |
| 261 | |
| 262 | // Figure out where to insert SubRHS/NewSplit. |
| 263 | DeltaTreeInteriorNode *InsertSide; |
| 264 | if (SubSplit.FileLoc < InsertRes->Split.FileLoc) |
| 265 | InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->LHS); |
| 266 | else |
| 267 | InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | |
| 269 | // We now have a non-empty interior node 'InsertSide' to insert |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 270 | // SubRHS/SubSplit into. Find out where to insert SubSplit. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 272 | // Find the insertion point, the first delta whose index is >SubSplit.FileLoc. |
| 273 | i = 0; e = InsertSide->getNumValuesUsed(); |
| 274 | while (i != e && SubSplit.FileLoc > InsertSide->getValue(i).FileLoc) |
| 275 | ++i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 277 | // Now we know that i is the place to insert the split value into. Insert it |
| 278 | // and the child right after it. |
| 279 | if (i != e) |
| 280 | memmove(&InsertSide->Children[i+2], &InsertSide->Children[i+1], |
| 281 | (e-i)*sizeof(IN->Children[0])); |
| 282 | InsertSide->Children[i+1] = SubRHS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 284 | if (e != i) |
| 285 | memmove(&InsertSide->Values[i+1], &InsertSide->Values[i], |
| 286 | (e-i)*sizeof(Values[0])); |
| 287 | InsertSide->Values[i] = SubSplit; |
| 288 | ++InsertSide->NumValuesUsed; |
| 289 | InsertSide->FullDelta += SubSplit.Delta + SubRHS->getFullDelta(); |
| 290 | return true; |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 293 | /// DoSplit - Split the currently full node (which has 2*WidthFactor-1 values) |
| 294 | /// into two subtrees each with "WidthFactor-1" values and a pivot value. |
| 295 | /// Return the pieces in InsertRes. |
Chris Lattner | b169e90 | 2008-04-13 08:22:30 +0000 | [diff] [blame] | 296 | void DeltaTreeNode::DoSplit(InsertResult &InsertRes) { |
| 297 | assert(isFull() && "Why split a non-full node?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | |
Chris Lattner | b169e90 | 2008-04-13 08:22:30 +0000 | [diff] [blame] | 299 | // Since this node is full, it contains 2*WidthFactor-1 values. We move |
| 300 | // the first 'WidthFactor-1' values to the LHS child (which we leave in this |
| 301 | // node), propagate one value up, and move the last 'WidthFactor-1' values |
| 302 | // into the RHS child. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Chris Lattner | b169e90 | 2008-04-13 08:22:30 +0000 | [diff] [blame] | 304 | // Create the new child node. |
| 305 | DeltaTreeNode *NewNode; |
| 306 | if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this)) { |
| 307 | // If this is an interior node, also move over 'WidthFactor' children |
| 308 | // into the new node. |
| 309 | DeltaTreeInteriorNode *New = new DeltaTreeInteriorNode(); |
| 310 | memcpy(&New->Children[0], &IN->Children[WidthFactor], |
| 311 | WidthFactor*sizeof(IN->Children[0])); |
| 312 | NewNode = New; |
| 313 | } else { |
| 314 | // Just create the new leaf node. |
| 315 | NewNode = new DeltaTreeNode(); |
| 316 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | |
Chris Lattner | b169e90 | 2008-04-13 08:22:30 +0000 | [diff] [blame] | 318 | // Move over the last 'WidthFactor-1' values from here to NewNode. |
| 319 | memcpy(&NewNode->Values[0], &Values[WidthFactor], |
| 320 | (WidthFactor-1)*sizeof(Values[0])); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Chris Lattner | b169e90 | 2008-04-13 08:22:30 +0000 | [diff] [blame] | 322 | // Decrease the number of values in the two nodes. |
| 323 | NewNode->NumValuesUsed = NumValuesUsed = WidthFactor-1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Chris Lattner | b169e90 | 2008-04-13 08:22:30 +0000 | [diff] [blame] | 325 | // Recompute the two nodes' full delta. |
| 326 | NewNode->RecomputeFullDeltaLocally(); |
| 327 | RecomputeFullDeltaLocally(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Chris Lattner | b169e90 | 2008-04-13 08:22:30 +0000 | [diff] [blame] | 329 | InsertRes.LHS = this; |
| 330 | InsertRes.RHS = NewNode; |
| 331 | InsertRes.Split = Values[WidthFactor-1]; |
| 332 | } |
| 333 | |
| 334 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 335 | |
| 336 | //===----------------------------------------------------------------------===// |
| 337 | // DeltaTree Implementation |
| 338 | //===----------------------------------------------------------------------===// |
| 339 | |
Chris Lattner | 22cb282 | 2008-04-12 22:04:18 +0000 | [diff] [blame] | 340 | //#define VERIFY_TREE |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 22cb282 | 2008-04-12 22:04:18 +0000 | [diff] [blame] | 342 | #ifdef VERIFY_TREE |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 343 | /// VerifyTree - Walk the btree performing assertions on various properties to |
| 344 | /// verify consistency. This is useful for debugging new changes to the tree. |
| 345 | static void VerifyTree(const DeltaTreeNode *N) { |
| 346 | const DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(N); |
| 347 | if (IN == 0) { |
| 348 | // Verify leaves, just ensure that FullDelta matches up and the elements |
| 349 | // are in proper order. |
| 350 | int FullDelta = 0; |
| 351 | for (unsigned i = 0, e = N->getNumValuesUsed(); i != e; ++i) { |
| 352 | if (i) |
| 353 | assert(N->getValue(i-1).FileLoc < N->getValue(i).FileLoc); |
| 354 | FullDelta += N->getValue(i).Delta; |
| 355 | } |
| 356 | assert(FullDelta == N->getFullDelta()); |
| 357 | return; |
| 358 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 360 | // Verify interior nodes: Ensure that FullDelta matches up and the |
| 361 | // elements are in proper order and the children are in proper order. |
| 362 | int FullDelta = 0; |
| 363 | for (unsigned i = 0, e = IN->getNumValuesUsed(); i != e; ++i) { |
| 364 | const SourceDelta &IVal = N->getValue(i); |
| 365 | const DeltaTreeNode *IChild = IN->getChild(i); |
| 366 | if (i) |
| 367 | assert(IN->getValue(i-1).FileLoc < IVal.FileLoc); |
| 368 | FullDelta += IVal.Delta; |
| 369 | FullDelta += IChild->getFullDelta(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 371 | // The largest value in child #i should be smaller than FileLoc. |
| 372 | assert(IChild->getValue(IChild->getNumValuesUsed()-1).FileLoc < |
| 373 | IVal.FileLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 375 | // The smallest value in child #i+1 should be larger than FileLoc. |
| 376 | assert(IN->getChild(i+1)->getValue(0).FileLoc > IVal.FileLoc); |
| 377 | VerifyTree(IChild); |
| 378 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 380 | FullDelta += IN->getChild(IN->getNumValuesUsed())->getFullDelta(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 382 | assert(FullDelta == N->getFullDelta()); |
| 383 | } |
Chris Lattner | 22cb282 | 2008-04-12 22:04:18 +0000 | [diff] [blame] | 384 | #endif // VERIFY_TREE |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 385 | |
| 386 | static DeltaTreeNode *getRoot(void *Root) { |
| 387 | return (DeltaTreeNode*)Root; |
| 388 | } |
| 389 | |
| 390 | DeltaTree::DeltaTree() { |
| 391 | Root = new DeltaTreeNode(); |
| 392 | } |
| 393 | DeltaTree::DeltaTree(const DeltaTree &RHS) { |
| 394 | // Currently we only support copying when the RHS is empty. |
| 395 | assert(getRoot(RHS.Root)->getNumValuesUsed() == 0 && |
| 396 | "Can only copy empty tree"); |
| 397 | Root = new DeltaTreeNode(); |
| 398 | } |
| 399 | |
| 400 | DeltaTree::~DeltaTree() { |
| 401 | getRoot(Root)->Destroy(); |
| 402 | } |
| 403 | |
| 404 | /// getDeltaAt - Return the accumulated delta at the specified file offset. |
| 405 | /// This includes all insertions or delections that occurred *before* the |
| 406 | /// specified file index. |
| 407 | int DeltaTree::getDeltaAt(unsigned FileIndex) const { |
| 408 | const DeltaTreeNode *Node = getRoot(Root); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 410 | int Result = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 412 | // Walk down the tree. |
| 413 | while (1) { |
| 414 | // For all nodes, include any local deltas before the specified file |
| 415 | // index by summing them up directly. Keep track of how many were |
| 416 | // included. |
| 417 | unsigned NumValsGreater = 0; |
| 418 | for (unsigned e = Node->getNumValuesUsed(); NumValsGreater != e; |
| 419 | ++NumValsGreater) { |
| 420 | const SourceDelta &Val = Node->getValue(NumValsGreater); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 422 | if (Val.FileLoc >= FileIndex) |
| 423 | break; |
| 424 | Result += Val.Delta; |
| 425 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 427 | // If we have an interior node, include information about children and |
| 428 | // recurse. Otherwise, if we have a leaf, we're done. |
| 429 | const DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(Node); |
| 430 | if (!IN) return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 432 | // Include any children to the left of the values we skipped, all of |
| 433 | // their deltas should be included as well. |
| 434 | for (unsigned i = 0; i != NumValsGreater; ++i) |
| 435 | Result += IN->getChild(i)->getFullDelta(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 437 | // If we found exactly the value we were looking for, break off the |
| 438 | // search early. There is no need to search the RHS of the value for |
| 439 | // partial results. |
| 440 | if (NumValsGreater != Node->getNumValuesUsed() && |
| 441 | Node->getValue(NumValsGreater).FileLoc == FileIndex) |
Chris Lattner | 53f9e20 | 2008-04-15 02:26:21 +0000 | [diff] [blame] | 442 | return Result+IN->getChild(NumValsGreater)->getFullDelta(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 444 | // Otherwise, traverse down the tree. The selected subtree may be |
| 445 | // partially included in the range. |
| 446 | Node = IN->getChild(NumValsGreater); |
| 447 | } |
| 448 | // NOT REACHED. |
| 449 | } |
| 450 | |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 451 | /// AddDelta - When a change is made that shifts around the text buffer, |
| 452 | /// this method is used to record that info. It inserts a delta of 'Delta' |
| 453 | /// into the current DeltaTree at offset FileIndex. |
| 454 | void DeltaTree::AddDelta(unsigned FileIndex, int Delta) { |
| 455 | assert(Delta && "Adding a noop?"); |
Chris Lattner | b169e90 | 2008-04-13 08:22:30 +0000 | [diff] [blame] | 456 | DeltaTreeNode *MyRoot = getRoot(Root); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | |
Douglas Gregor | 6991218 | 2009-11-17 06:52:37 +0000 | [diff] [blame] | 458 | DeltaTreeNode::InsertResult InsertRes; |
Chris Lattner | 3b7ff0d | 2008-04-13 08:52:45 +0000 | [diff] [blame] | 459 | if (MyRoot->DoInsertion(FileIndex, Delta, &InsertRes)) { |
| 460 | Root = MyRoot = new DeltaTreeInteriorNode(InsertRes); |
| 461 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 22cb282 | 2008-04-12 22:04:18 +0000 | [diff] [blame] | 463 | #ifdef VERIFY_TREE |
Chris Lattner | b169e90 | 2008-04-13 08:22:30 +0000 | [diff] [blame] | 464 | VerifyTree(MyRoot); |
Chris Lattner | 22cb282 | 2008-04-12 22:04:18 +0000 | [diff] [blame] | 465 | #endif |
Chris Lattner | 8100d74 | 2008-04-12 22:00:40 +0000 | [diff] [blame] | 466 | } |
| 467 | |