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