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