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> |
| 17 | using namespace clang; |
| 18 | using llvm::cast; |
| 19 | using llvm::dyn_cast; |
| 20 | |
| 21 | namespace { |
| 22 | struct SourceDelta; |
| 23 | class DeltaTreeNode; |
| 24 | class DeltaTreeInteriorNode; |
| 25 | } |
| 26 | |
| 27 | /// The DeltaTree class is a multiway search tree (BTree) structure with some |
| 28 | /// fancy features. B-Trees are are generally more memory and cache efficient |
| 29 | /// than binary trees, because they store multiple keys/values in each node. |
| 30 | /// |
| 31 | /// DeltaTree implements a key/value mapping from FileIndex to Delta, allowing |
| 32 | /// fast lookup by FileIndex. However, an added (important) bonus is that it |
| 33 | /// can also efficiently tell us the full accumulated delta for a specific |
| 34 | /// file offset as well, without traversing the whole tree. |
| 35 | /// |
| 36 | /// The nodes of the tree are made up of instances of two classes: |
| 37 | /// DeltaTreeNode and DeltaTreeInteriorNode. The later subclasses the |
| 38 | /// former and adds children pointers. Each node knows the full delta of all |
| 39 | /// entries (recursively) contained inside of it, which allows us to get the |
| 40 | /// full delta implied by a whole subtree in constant time. |
| 41 | |
| 42 | namespace { |
| 43 | /// SourceDelta - As code in the original input buffer is added and deleted, |
| 44 | /// SourceDelta records are used to keep track of how the input SourceLocation |
| 45 | /// object is mapped into the output buffer. |
| 46 | struct SourceDelta { |
| 47 | unsigned FileLoc; |
| 48 | int Delta; |
| 49 | |
| 50 | static SourceDelta get(unsigned Loc, int D) { |
| 51 | SourceDelta Delta; |
| 52 | Delta.FileLoc = Loc; |
| 53 | Delta.Delta = D; |
| 54 | return Delta; |
| 55 | } |
| 56 | }; |
| 57 | } // end anonymous namespace |
| 58 | |
| 59 | namespace { |
| 60 | /// DeltaTreeNode - The common part of all nodes. |
| 61 | /// |
| 62 | class DeltaTreeNode { |
| 63 | friend class DeltaTreeInteriorNode; |
| 64 | |
| 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 |
| 67 | /// WidthFactor-1 K/V pairs (unless the whole tree is less full than that) |
| 68 | /// and may have at most 2*WidthFactor-1 K/V pairs. |
| 69 | enum { WidthFactor = 8 }; |
| 70 | |
| 71 | /// Values - This tracks the SourceDelta's currently in this node. |
| 72 | /// |
| 73 | SourceDelta Values[2*WidthFactor-1]; |
| 74 | |
| 75 | /// NumValuesUsed - This tracks the number of values this node currently |
| 76 | /// holds. |
| 77 | unsigned char NumValuesUsed; |
| 78 | |
| 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; |
| 82 | |
| 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) |
| 88 | : NumValuesUsed(0), IsLeaf(isLeaf), FullDelta(0) {} |
| 89 | |
| 90 | bool isLeaf() const { return IsLeaf; } |
| 91 | int getFullDelta() const { return FullDelta; } |
| 92 | bool isFull() const { return NumValuesUsed == 2*WidthFactor-1; } |
| 93 | |
| 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 | } |
| 103 | |
| 104 | /// AddDeltaNonFull - Add a delta to this tree and/or it's children, knowing |
| 105 | /// that this node is not currently full. |
| 106 | void AddDeltaNonFull(unsigned FileIndex, int Delta); |
| 107 | |
| 108 | /// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a |
| 109 | /// local walk over our contained deltas. |
| 110 | void RecomputeFullDeltaLocally(); |
| 111 | |
| 112 | void Destroy(); |
| 113 | |
| 114 | static inline bool classof(const DeltaTreeNode *) { return true; } |
| 115 | }; |
| 116 | } // end anonymous namespace |
| 117 | |
| 118 | namespace { |
| 119 | /// DeltaTreeInteriorNode - When isLeaf = false, a node has child pointers. |
| 120 | /// This class tracks them. |
| 121 | class DeltaTreeInteriorNode : public DeltaTreeNode { |
| 122 | DeltaTreeNode *Children[2*WidthFactor]; |
| 123 | ~DeltaTreeInteriorNode() { |
| 124 | for (unsigned i = 0, e = NumValuesUsed+1; i != e; ++i) |
| 125 | Children[i]->Destroy(); |
| 126 | } |
| 127 | friend class DeltaTreeNode; |
| 128 | public: |
| 129 | DeltaTreeInteriorNode() : DeltaTreeNode(false /*nonleaf*/) {} |
| 130 | |
| 131 | DeltaTreeInteriorNode(DeltaTreeNode *FirstChild) |
| 132 | : DeltaTreeNode(false /*nonleaf*/) { |
| 133 | FullDelta = FirstChild->FullDelta; |
| 134 | Children[0] = FirstChild; |
| 135 | } |
| 136 | |
| 137 | const DeltaTreeNode *getChild(unsigned i) const { |
| 138 | assert(i < getNumValuesUsed()+1 && "Invalid child"); |
| 139 | return Children[i]; |
| 140 | } |
| 141 | DeltaTreeNode *getChild(unsigned i) { |
| 142 | assert(i < getNumValuesUsed()+1 && "Invalid child"); |
| 143 | return Children[i]; |
| 144 | } |
| 145 | |
| 146 | static inline bool classof(const DeltaTreeInteriorNode *) { return true; } |
| 147 | static inline bool classof(const DeltaTreeNode *N) { return !N->isLeaf(); } |
| 148 | private: |
| 149 | void SplitChild(unsigned ChildNo); |
| 150 | }; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /// Destroy - A 'virtual' destructor. |
| 155 | void DeltaTreeNode::Destroy() { |
| 156 | if (isLeaf()) |
| 157 | delete this; |
| 158 | else |
| 159 | delete cast<DeltaTreeInteriorNode>(this); |
| 160 | } |
| 161 | |
| 162 | /// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a |
| 163 | /// local walk over our contained deltas. |
| 164 | void DeltaTreeNode::RecomputeFullDeltaLocally() { |
| 165 | int NewFullDelta = 0; |
| 166 | for (unsigned i = 0, e = getNumValuesUsed(); i != e; ++i) |
| 167 | NewFullDelta += Values[i].Delta; |
| 168 | if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this)) |
| 169 | for (unsigned i = 0, e = getNumValuesUsed()+1; i != e; ++i) |
| 170 | NewFullDelta += IN->getChild(i)->getFullDelta(); |
| 171 | FullDelta = NewFullDelta; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /// AddDeltaNonFull - Add a delta to this tree and/or it's children, knowing |
| 176 | /// that this node is not currently full. |
| 177 | void DeltaTreeNode::AddDeltaNonFull(unsigned FileIndex, int Delta) { |
| 178 | assert(!isFull() && "AddDeltaNonFull on a full tree?"); |
| 179 | |
| 180 | // Maintain full delta for this node. |
| 181 | FullDelta += Delta; |
| 182 | |
| 183 | // Find the insertion point, the first delta whose index is >= FileIndex. |
| 184 | unsigned i = 0, e = getNumValuesUsed(); |
| 185 | while (i != e && FileIndex > getValue(i).FileLoc) |
| 186 | ++i; |
| 187 | |
| 188 | // If we found an a record for exactly this file index, just merge this |
| 189 | // value into the preexisting record and finish early. |
| 190 | if (i != e && getValue(i).FileLoc == FileIndex) { |
| 191 | // NOTE: Delta could drop to zero here. This means that the next delta |
| 192 | // entry is useless and could be removed. Supporting erases is |
| 193 | // significantly more complex though, so we just leave an entry with |
| 194 | // Delta=0 in the tree. |
| 195 | Values[i].Delta += Delta; |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this)) { |
| 200 | // Insertion into an interior node propagates the value down to a child. |
| 201 | DeltaTreeNode *Child = IN->getChild(i); |
| 202 | |
| 203 | // If the child tree is full, split it, pulling an element up into our |
| 204 | // node. |
| 205 | if (Child->isFull()) { |
| 206 | IN->SplitChild(i); |
| 207 | SourceDelta &MedianVal = getValue(i); |
| 208 | |
| 209 | // If the median value we pulled up is exactly our insert position, add |
| 210 | // the delta and return. |
| 211 | if (MedianVal.FileLoc == FileIndex) { |
| 212 | MedianVal.Delta += Delta; |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | // If the median value pulled up is less than our current search point, |
| 217 | // include those deltas and search down the RHS now. |
| 218 | if (MedianVal.FileLoc < FileIndex) |
| 219 | Child = IN->getChild(i+1); |
| 220 | } |
| 221 | |
| 222 | Child->AddDeltaNonFull(FileIndex, Delta); |
| 223 | } else { |
| 224 | // For an insertion into a non-full leaf node, just insert the value in |
| 225 | // its sorted position. This requires moving later values over. |
| 226 | if (i != e) |
| 227 | memmove(&Values[i+1], &Values[i], sizeof(Values[0])*(e-i)); |
| 228 | Values[i] = SourceDelta::get(FileIndex, Delta); |
| 229 | ++NumValuesUsed; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /// SplitChild - At this point, we know that the current node is not full and |
| 234 | /// that the specified child of this node is. Split the child in half at its |
| 235 | /// median, propagating one value up into us. Child may be either an interior |
| 236 | /// or leaf node. |
| 237 | void DeltaTreeInteriorNode::SplitChild(unsigned ChildNo) { |
| 238 | DeltaTreeNode *Child = getChild(ChildNo); |
| 239 | assert(!isFull() && Child->isFull() && "Inconsistent constraints"); |
| 240 | |
| 241 | // Since the child is full, it contains 2*WidthFactor-1 values. We move |
| 242 | // the first 'WidthFactor-1' values to the LHS child (which we leave in the |
| 243 | // original child), propagate one value up into us, and move the last |
| 244 | // 'WidthFactor-1' values into thew RHS child. |
| 245 | |
| 246 | // Create the new child node. |
| 247 | DeltaTreeNode *NewNode; |
| 248 | if (DeltaTreeInteriorNode *CIN = dyn_cast<DeltaTreeInteriorNode>(Child)) { |
| 249 | // If the child is an interior node, also move over 'WidthFactor' grand |
| 250 | // children into the new node. |
| 251 | NewNode = new DeltaTreeInteriorNode(); |
| 252 | memcpy(&((DeltaTreeInteriorNode*)NewNode)->Children[0], |
| 253 | &CIN->Children[WidthFactor], |
| 254 | WidthFactor*sizeof(CIN->Children[0])); |
| 255 | } else { |
| 256 | // Just create the child node. |
| 257 | NewNode = new DeltaTreeNode(); |
| 258 | } |
| 259 | |
| 260 | // Move over the last 'WidthFactor-1' values from Child to NewNode. |
| 261 | memcpy(&NewNode->Values[0], &Child->Values[WidthFactor], |
| 262 | (WidthFactor-1)*sizeof(Child->Values[0])); |
| 263 | |
| 264 | // Decrease the number of values in the two children. |
| 265 | NewNode->NumValuesUsed = Child->NumValuesUsed = WidthFactor-1; |
| 266 | |
| 267 | // Recompute the two children's full delta. Our delta hasn't changed, but |
| 268 | // their delta has. |
| 269 | NewNode->RecomputeFullDeltaLocally(); |
| 270 | Child->RecomputeFullDeltaLocally(); |
| 271 | |
| 272 | // Now that we have two nodes and a new element, insert the median value |
| 273 | // into ourself by moving all the later values/children down, then inserting |
| 274 | // the new one. |
| 275 | if (getNumValuesUsed() != ChildNo) |
| 276 | memmove(&Children[ChildNo+2], &Children[ChildNo+1], |
| 277 | (getNumValuesUsed()-ChildNo)*sizeof(Children[0])); |
| 278 | Children[ChildNo+1] = NewNode; |
| 279 | |
| 280 | if (getNumValuesUsed() != ChildNo) |
| 281 | memmove(&Values[ChildNo+1], &Values[ChildNo], |
| 282 | (getNumValuesUsed()-ChildNo)*sizeof(Values[0])); |
| 283 | Values[ChildNo] = Child->Values[WidthFactor-1]; |
| 284 | ++NumValuesUsed; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | //===----------------------------------------------------------------------===// |
| 289 | // DeltaTree Implementation |
| 290 | //===----------------------------------------------------------------------===// |
| 291 | |
| 292 | |
| 293 | /// VerifyTree - Walk the btree performing assertions on various properties to |
| 294 | /// verify consistency. This is useful for debugging new changes to the tree. |
| 295 | static void VerifyTree(const DeltaTreeNode *N) { |
| 296 | const DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(N); |
| 297 | if (IN == 0) { |
| 298 | // Verify leaves, just ensure that FullDelta matches up and the elements |
| 299 | // are in proper order. |
| 300 | int FullDelta = 0; |
| 301 | for (unsigned i = 0, e = N->getNumValuesUsed(); i != e; ++i) { |
| 302 | if (i) |
| 303 | assert(N->getValue(i-1).FileLoc < N->getValue(i).FileLoc); |
| 304 | FullDelta += N->getValue(i).Delta; |
| 305 | } |
| 306 | assert(FullDelta == N->getFullDelta()); |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | // Verify interior nodes: Ensure that FullDelta matches up and the |
| 311 | // elements are in proper order and the children are in proper order. |
| 312 | int FullDelta = 0; |
| 313 | for (unsigned i = 0, e = IN->getNumValuesUsed(); i != e; ++i) { |
| 314 | const SourceDelta &IVal = N->getValue(i); |
| 315 | const DeltaTreeNode *IChild = IN->getChild(i); |
| 316 | if (i) |
| 317 | assert(IN->getValue(i-1).FileLoc < IVal.FileLoc); |
| 318 | FullDelta += IVal.Delta; |
| 319 | FullDelta += IChild->getFullDelta(); |
| 320 | |
| 321 | // The largest value in child #i should be smaller than FileLoc. |
| 322 | assert(IChild->getValue(IChild->getNumValuesUsed()-1).FileLoc < |
| 323 | IVal.FileLoc); |
| 324 | |
| 325 | // The smallest value in child #i+1 should be larger than FileLoc. |
| 326 | assert(IN->getChild(i+1)->getValue(0).FileLoc > IVal.FileLoc); |
| 327 | VerifyTree(IChild); |
| 328 | } |
| 329 | |
| 330 | FullDelta += IN->getChild(IN->getNumValuesUsed())->getFullDelta(); |
| 331 | |
| 332 | assert(FullDelta == N->getFullDelta()); |
| 333 | } |
| 334 | |
| 335 | static DeltaTreeNode *getRoot(void *Root) { |
| 336 | return (DeltaTreeNode*)Root; |
| 337 | } |
| 338 | |
| 339 | DeltaTree::DeltaTree() { |
| 340 | Root = new DeltaTreeNode(); |
| 341 | } |
| 342 | DeltaTree::DeltaTree(const DeltaTree &RHS) { |
| 343 | // Currently we only support copying when the RHS is empty. |
| 344 | assert(getRoot(RHS.Root)->getNumValuesUsed() == 0 && |
| 345 | "Can only copy empty tree"); |
| 346 | Root = new DeltaTreeNode(); |
| 347 | } |
| 348 | |
| 349 | DeltaTree::~DeltaTree() { |
| 350 | getRoot(Root)->Destroy(); |
| 351 | } |
| 352 | |
| 353 | /// getDeltaAt - Return the accumulated delta at the specified file offset. |
| 354 | /// This includes all insertions or delections that occurred *before* the |
| 355 | /// specified file index. |
| 356 | int DeltaTree::getDeltaAt(unsigned FileIndex) const { |
| 357 | const DeltaTreeNode *Node = getRoot(Root); |
| 358 | |
| 359 | int Result = 0; |
| 360 | |
| 361 | // Walk down the tree. |
| 362 | while (1) { |
| 363 | // For all nodes, include any local deltas before the specified file |
| 364 | // index by summing them up directly. Keep track of how many were |
| 365 | // included. |
| 366 | unsigned NumValsGreater = 0; |
| 367 | for (unsigned e = Node->getNumValuesUsed(); NumValsGreater != e; |
| 368 | ++NumValsGreater) { |
| 369 | const SourceDelta &Val = Node->getValue(NumValsGreater); |
| 370 | |
| 371 | if (Val.FileLoc >= FileIndex) |
| 372 | break; |
| 373 | Result += Val.Delta; |
| 374 | } |
| 375 | |
| 376 | // If we have an interior node, include information about children and |
| 377 | // recurse. Otherwise, if we have a leaf, we're done. |
| 378 | const DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(Node); |
| 379 | if (!IN) return Result; |
| 380 | |
| 381 | // Include any children to the left of the values we skipped, all of |
| 382 | // their deltas should be included as well. |
| 383 | for (unsigned i = 0; i != NumValsGreater; ++i) |
| 384 | Result += IN->getChild(i)->getFullDelta(); |
| 385 | |
| 386 | // If we found exactly the value we were looking for, break off the |
| 387 | // search early. There is no need to search the RHS of the value for |
| 388 | // partial results. |
| 389 | if (NumValsGreater != Node->getNumValuesUsed() && |
| 390 | Node->getValue(NumValsGreater).FileLoc == FileIndex) |
| 391 | return Result; |
| 392 | |
| 393 | // Otherwise, traverse down the tree. The selected subtree may be |
| 394 | // partially included in the range. |
| 395 | Node = IN->getChild(NumValsGreater); |
| 396 | } |
| 397 | // NOT REACHED. |
| 398 | } |
| 399 | |
| 400 | |
| 401 | /// AddDelta - When a change is made that shifts around the text buffer, |
| 402 | /// this method is used to record that info. It inserts a delta of 'Delta' |
| 403 | /// into the current DeltaTree at offset FileIndex. |
| 404 | void DeltaTree::AddDelta(unsigned FileIndex, int Delta) { |
| 405 | assert(Delta && "Adding a noop?"); |
| 406 | |
| 407 | // If the root is full, create a new dummy (non-empty) interior node that |
| 408 | // points to it, allowing the old root to be split. |
| 409 | if (getRoot(Root)->isFull()) |
| 410 | Root = new DeltaTreeInteriorNode(getRoot(Root)); |
| 411 | |
| 412 | getRoot(Root)->AddDeltaNonFull(FileIndex, Delta); |
| 413 | |
| 414 | //VerifyTree(Root); |
| 415 | } |
| 416 | |