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