blob: 352fab077a2e8cf94dd2da214d5c31b8c0a5e837 [file] [log] [blame]
Chris Lattnercbb6bad2008-04-12 22:00:40 +00001//===--- 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
Ted Kremenekcdf81492012-09-01 05:09:24 +000014#include "clang/Rewrite/Core/DeltaTree.h"
Chris Lattner0e62c1c2011-07-23 10:55:15 +000015#include "clang/Basic/LLVM.h"
Chris Lattnercc0ef632008-04-13 08:52:45 +000016#include <cstdio>
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include <cstring>
Chris Lattnercbb6bad2008-04-12 22:00:40 +000018using namespace clang;
Chris Lattnercbb6bad2008-04-12 22:00:40 +000019
Chris Lattnercbb6bad2008-04-12 22:00:40 +000020/// The DeltaTree class is a multiway search tree (BTree) structure with some
Chris Lattner6a89c502010-01-20 17:53:58 +000021/// fancy features. B-Trees are generally more memory and cache efficient
Chris Lattnercbb6bad2008-04-12 22:00:40 +000022/// than binary trees, because they store multiple keys/values in each node.
23///
24/// DeltaTree implements a key/value mapping from FileIndex to Delta, allowing
25/// fast lookup by FileIndex. However, an added (important) bonus is that it
26/// can also efficiently tell us the full accumulated delta for a specific
27/// file offset as well, without traversing the whole tree.
28///
29/// The nodes of the tree are made up of instances of two classes:
30/// DeltaTreeNode and DeltaTreeInteriorNode. The later subclasses the
31/// former and adds children pointers. Each node knows the full delta of all
32/// entries (recursively) contained inside of it, which allows us to get the
33/// full delta implied by a whole subtree in constant time.
Mike Stump11289f42009-09-09 15:08:12 +000034
Chris Lattnercbb6bad2008-04-12 22:00:40 +000035namespace {
36 /// SourceDelta - As code in the original input buffer is added and deleted,
37 /// SourceDelta records are used to keep track of how the input SourceLocation
38 /// object is mapped into the output buffer.
39 struct SourceDelta {
40 unsigned FileLoc;
41 int Delta;
Mike Stump11289f42009-09-09 15:08:12 +000042
Chris Lattnercbb6bad2008-04-12 22:00:40 +000043 static SourceDelta get(unsigned Loc, int D) {
44 SourceDelta Delta;
45 Delta.FileLoc = Loc;
46 Delta.Delta = D;
47 return Delta;
48 }
49 };
Douglas Gregor5492edc2009-11-17 06:52:37 +000050
Chris Lattnercbb6bad2008-04-12 22:00:40 +000051 /// DeltaTreeNode - The common part of all nodes.
52 ///
53 class DeltaTreeNode {
Douglas Gregor5492edc2009-11-17 06:52:37 +000054 public:
55 struct InsertResult {
56 DeltaTreeNode *LHS, *RHS;
57 SourceDelta Split;
58 };
59
60 private:
Chris Lattnercbb6bad2008-04-12 22:00:40 +000061 friend class DeltaTreeInteriorNode;
Mike Stump11289f42009-09-09 15:08:12 +000062
Chris Lattnercbb6bad2008-04-12 22:00:40 +000063 /// WidthFactor - This controls the number of K/V slots held in the BTree:
64 /// how wide it is. Each level of the BTree is guaranteed to have at least
Chris Lattner3374b032008-04-14 07:17:29 +000065 /// WidthFactor-1 K/V pairs (except the root) and may have at most
66 /// 2*WidthFactor-1 K/V pairs.
Chris Lattnercbb6bad2008-04-12 22:00:40 +000067 enum { WidthFactor = 8 };
Mike Stump11289f42009-09-09 15:08:12 +000068
Chris Lattnercbb6bad2008-04-12 22:00:40 +000069 /// Values - This tracks the SourceDelta's currently in this node.
70 ///
71 SourceDelta Values[2*WidthFactor-1];
Mike Stump11289f42009-09-09 15:08:12 +000072
Chris Lattnercbb6bad2008-04-12 22:00:40 +000073 /// NumValuesUsed - This tracks the number of values this node currently
74 /// holds.
75 unsigned char NumValuesUsed;
Mike Stump11289f42009-09-09 15:08:12 +000076
Chris Lattnercbb6bad2008-04-12 22:00:40 +000077 /// IsLeaf - This is true if this is a leaf of the btree. If false, this is
78 /// an interior node, and is actually an instance of DeltaTreeInteriorNode.
79 bool IsLeaf;
Mike Stump11289f42009-09-09 15:08:12 +000080
Chris Lattnercbb6bad2008-04-12 22:00:40 +000081 /// FullDelta - This is the full delta of all the values in this node and
82 /// all children nodes.
83 int FullDelta;
84 public:
85 DeltaTreeNode(bool isLeaf = true)
Chris Lattnercdefa7a2008-04-13 08:22:30 +000086 : NumValuesUsed(0), IsLeaf(isLeaf), FullDelta(0) {}
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattnercbb6bad2008-04-12 22:00:40 +000088 bool isLeaf() const { return IsLeaf; }
89 int getFullDelta() const { return FullDelta; }
90 bool isFull() const { return NumValuesUsed == 2*WidthFactor-1; }
Mike Stump11289f42009-09-09 15:08:12 +000091
Chris Lattnercbb6bad2008-04-12 22:00:40 +000092 unsigned getNumValuesUsed() const { return NumValuesUsed; }
93 const SourceDelta &getValue(unsigned i) const {
94 assert(i < NumValuesUsed && "Invalid value #");
95 return Values[i];
96 }
97 SourceDelta &getValue(unsigned i) {
98 assert(i < NumValuesUsed && "Invalid value #");
99 return Values[i];
100 }
Mike Stump11289f42009-09-09 15:08:12 +0000101
Chris Lattnercc0ef632008-04-13 08:52:45 +0000102 /// DoInsertion - Do an insertion of the specified FileIndex/Delta pair into
103 /// this node. If insertion is easy, do it and return false. Otherwise,
104 /// split the node, populate InsertRes with info about the split, and return
105 /// true.
106 bool DoInsertion(unsigned FileIndex, int Delta, InsertResult *InsertRes);
107
Chris Lattnercdefa7a2008-04-13 08:22:30 +0000108 void DoSplit(InsertResult &InsertRes);
Mike Stump11289f42009-09-09 15:08:12 +0000109
110
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000111 /// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a
112 /// local walk over our contained deltas.
113 void RecomputeFullDeltaLocally();
Mike Stump11289f42009-09-09 15:08:12 +0000114
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000115 void Destroy();
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000116 };
117} // end anonymous namespace
118
119namespace {
120 /// DeltaTreeInteriorNode - When isLeaf = false, a node has child pointers.
121 /// This class tracks them.
122 class DeltaTreeInteriorNode : public DeltaTreeNode {
123 DeltaTreeNode *Children[2*WidthFactor];
124 ~DeltaTreeInteriorNode() {
125 for (unsigned i = 0, e = NumValuesUsed+1; i != e; ++i)
126 Children[i]->Destroy();
127 }
128 friend class DeltaTreeNode;
129 public:
130 DeltaTreeInteriorNode() : DeltaTreeNode(false /*nonleaf*/) {}
Mike Stump11289f42009-09-09 15:08:12 +0000131
Mike Stump11289f42009-09-09 15:08:12 +0000132 DeltaTreeInteriorNode(const InsertResult &IR)
Chris Lattnercc0ef632008-04-13 08:52:45 +0000133 : DeltaTreeNode(false /*nonleaf*/) {
134 Children[0] = IR.LHS;
135 Children[1] = IR.RHS;
136 Values[0] = IR.Split;
137 FullDelta = IR.LHS->getFullDelta()+IR.RHS->getFullDelta()+IR.Split.Delta;
138 NumValuesUsed = 1;
139 }
Mike Stump11289f42009-09-09 15:08:12 +0000140
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000141 const DeltaTreeNode *getChild(unsigned i) const {
142 assert(i < getNumValuesUsed()+1 && "Invalid child");
143 return Children[i];
144 }
145 DeltaTreeNode *getChild(unsigned i) {
146 assert(i < getNumValuesUsed()+1 && "Invalid child");
147 return Children[i];
148 }
Mike Stump11289f42009-09-09 15:08:12 +0000149
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000150 static inline bool classof(const DeltaTreeNode *N) { return !N->isLeaf(); }
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000151 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000152}
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000153
154
155/// Destroy - A 'virtual' destructor.
156void DeltaTreeNode::Destroy() {
157 if (isLeaf())
158 delete this;
159 else
160 delete cast<DeltaTreeInteriorNode>(this);
161}
162
163/// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a
164/// local walk over our contained deltas.
165void DeltaTreeNode::RecomputeFullDeltaLocally() {
166 int NewFullDelta = 0;
167 for (unsigned i = 0, e = getNumValuesUsed(); i != e; ++i)
168 NewFullDelta += Values[i].Delta;
169 if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this))
170 for (unsigned i = 0, e = getNumValuesUsed()+1; i != e; ++i)
171 NewFullDelta += IN->getChild(i)->getFullDelta();
172 FullDelta = NewFullDelta;
173}
174
Chris Lattnercc0ef632008-04-13 08:52:45 +0000175/// DoInsertion - Do an insertion of the specified FileIndex/Delta pair into
176/// this node. If insertion is easy, do it and return false. Otherwise,
177/// split the node, populate InsertRes with info about the split, and return
178/// true.
Mike Stump11289f42009-09-09 15:08:12 +0000179bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta,
Chris Lattnercc0ef632008-04-13 08:52:45 +0000180 InsertResult *InsertRes) {
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000181 // Maintain full delta for this node.
182 FullDelta += Delta;
Mike Stump11289f42009-09-09 15:08:12 +0000183
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000184 // Find the insertion point, the first delta whose index is >= FileIndex.
185 unsigned i = 0, e = getNumValuesUsed();
186 while (i != e && FileIndex > getValue(i).FileLoc)
187 ++i;
Mike Stump11289f42009-09-09 15:08:12 +0000188
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000189 // If we found an a record for exactly this file index, just merge this
Chris Lattnercc0ef632008-04-13 08:52:45 +0000190 // value into the pre-existing record and finish early.
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000191 if (i != e && getValue(i).FileLoc == FileIndex) {
Chris Lattnercc0ef632008-04-13 08:52:45 +0000192 // NOTE: Delta could drop to zero here. This means that the delta entry is
193 // useless and could be removed. Supporting erases is more complex than
194 // leaving an entry with Delta=0, so we just leave an entry with Delta=0 in
195 // the tree.
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000196 Values[i].Delta += Delta;
Chris Lattnercc0ef632008-04-13 08:52:45 +0000197 return false;
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000198 }
Chris Lattnercc0ef632008-04-13 08:52:45 +0000199
200 // Otherwise, we found an insertion point, and we know that the value at the
201 // specified index is > FileIndex. Handle the leaf case first.
202 if (isLeaf()) {
203 if (!isFull()) {
204 // For an insertion into a non-full leaf node, just insert the value in
205 // its sorted position. This requires moving later values over.
206 if (i != e)
207 memmove(&Values[i+1], &Values[i], sizeof(Values[0])*(e-i));
208 Values[i] = SourceDelta::get(FileIndex, Delta);
209 ++NumValuesUsed;
210 return false;
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000211 }
Mike Stump11289f42009-09-09 15:08:12 +0000212
Chris Lattnercc0ef632008-04-13 08:52:45 +0000213 // Otherwise, if this is leaf is full, split the node at its median, insert
214 // the value into one of the children, and return the result.
215 assert(InsertRes && "No result location specified");
216 DoSplit(*InsertRes);
Mike Stump11289f42009-09-09 15:08:12 +0000217
Chris Lattnercc0ef632008-04-13 08:52:45 +0000218 if (InsertRes->Split.FileLoc > FileIndex)
Craig Topper8ae12032014-05-07 06:21:57 +0000219 InsertRes->LHS->DoInsertion(FileIndex, Delta, nullptr /*can't fail*/);
Chris Lattnercc0ef632008-04-13 08:52:45 +0000220 else
Craig Topper8ae12032014-05-07 06:21:57 +0000221 InsertRes->RHS->DoInsertion(FileIndex, Delta, nullptr /*can't fail*/);
Chris Lattnercc0ef632008-04-13 08:52:45 +0000222 return true;
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000223 }
Mike Stump11289f42009-09-09 15:08:12 +0000224
Chris Lattnercc0ef632008-04-13 08:52:45 +0000225 // Otherwise, this is an interior node. Send the request down the tree.
226 DeltaTreeInteriorNode *IN = cast<DeltaTreeInteriorNode>(this);
227 if (!IN->Children[i]->DoInsertion(FileIndex, Delta, InsertRes))
228 return false; // If there was space in the child, just return.
229
230 // Okay, this split the subtree, producing a new value and two children to
231 // insert here. If this node is non-full, we can just insert it directly.
232 if (!isFull()) {
233 // Now that we have two nodes and a new element, insert the perclated value
234 // into ourself by moving all the later values/children down, then inserting
235 // the new one.
236 if (i != e)
237 memmove(&IN->Children[i+2], &IN->Children[i+1],
238 (e-i)*sizeof(IN->Children[0]));
239 IN->Children[i] = InsertRes->LHS;
240 IN->Children[i+1] = InsertRes->RHS;
Mike Stump11289f42009-09-09 15:08:12 +0000241
Chris Lattnercc0ef632008-04-13 08:52:45 +0000242 if (e != i)
243 memmove(&Values[i+1], &Values[i], (e-i)*sizeof(Values[0]));
244 Values[i] = InsertRes->Split;
245 ++NumValuesUsed;
246 return false;
247 }
Mike Stump11289f42009-09-09 15:08:12 +0000248
Chris Lattnercc0ef632008-04-13 08:52:45 +0000249 // Finally, if this interior node was full and a node is percolated up, split
250 // ourself and return that up the chain. Start by saving all our info to
251 // avoid having the split clobber it.
252 IN->Children[i] = InsertRes->LHS;
253 DeltaTreeNode *SubRHS = InsertRes->RHS;
254 SourceDelta SubSplit = InsertRes->Split;
Mike Stump11289f42009-09-09 15:08:12 +0000255
Chris Lattnercc0ef632008-04-13 08:52:45 +0000256 // Do the split.
257 DoSplit(*InsertRes);
258
259 // Figure out where to insert SubRHS/NewSplit.
260 DeltaTreeInteriorNode *InsertSide;
261 if (SubSplit.FileLoc < InsertRes->Split.FileLoc)
262 InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->LHS);
263 else
264 InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->RHS);
Mike Stump11289f42009-09-09 15:08:12 +0000265
266 // We now have a non-empty interior node 'InsertSide' to insert
Chris Lattnercc0ef632008-04-13 08:52:45 +0000267 // SubRHS/SubSplit into. Find out where to insert SubSplit.
Mike Stump11289f42009-09-09 15:08:12 +0000268
Chris Lattnercc0ef632008-04-13 08:52:45 +0000269 // Find the insertion point, the first delta whose index is >SubSplit.FileLoc.
270 i = 0; e = InsertSide->getNumValuesUsed();
271 while (i != e && SubSplit.FileLoc > InsertSide->getValue(i).FileLoc)
272 ++i;
Mike Stump11289f42009-09-09 15:08:12 +0000273
Chris Lattnercc0ef632008-04-13 08:52:45 +0000274 // Now we know that i is the place to insert the split value into. Insert it
275 // and the child right after it.
276 if (i != e)
277 memmove(&InsertSide->Children[i+2], &InsertSide->Children[i+1],
278 (e-i)*sizeof(IN->Children[0]));
279 InsertSide->Children[i+1] = SubRHS;
Mike Stump11289f42009-09-09 15:08:12 +0000280
Chris Lattnercc0ef632008-04-13 08:52:45 +0000281 if (e != i)
282 memmove(&InsertSide->Values[i+1], &InsertSide->Values[i],
283 (e-i)*sizeof(Values[0]));
284 InsertSide->Values[i] = SubSplit;
285 ++InsertSide->NumValuesUsed;
286 InsertSide->FullDelta += SubSplit.Delta + SubRHS->getFullDelta();
287 return true;
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000288}
289
Chris Lattnercc0ef632008-04-13 08:52:45 +0000290/// DoSplit - Split the currently full node (which has 2*WidthFactor-1 values)
291/// into two subtrees each with "WidthFactor-1" values and a pivot value.
292/// Return the pieces in InsertRes.
Chris Lattnercdefa7a2008-04-13 08:22:30 +0000293void DeltaTreeNode::DoSplit(InsertResult &InsertRes) {
294 assert(isFull() && "Why split a non-full node?");
Mike Stump11289f42009-09-09 15:08:12 +0000295
Chris Lattnercdefa7a2008-04-13 08:22:30 +0000296 // Since this node is full, it contains 2*WidthFactor-1 values. We move
297 // the first 'WidthFactor-1' values to the LHS child (which we leave in this
298 // node), propagate one value up, and move the last 'WidthFactor-1' values
299 // into the RHS child.
Mike Stump11289f42009-09-09 15:08:12 +0000300
Chris Lattnercdefa7a2008-04-13 08:22:30 +0000301 // Create the new child node.
302 DeltaTreeNode *NewNode;
303 if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this)) {
304 // If this is an interior node, also move over 'WidthFactor' children
305 // into the new node.
306 DeltaTreeInteriorNode *New = new DeltaTreeInteriorNode();
307 memcpy(&New->Children[0], &IN->Children[WidthFactor],
308 WidthFactor*sizeof(IN->Children[0]));
309 NewNode = New;
310 } else {
311 // Just create the new leaf node.
312 NewNode = new DeltaTreeNode();
313 }
Mike Stump11289f42009-09-09 15:08:12 +0000314
Chris Lattnercdefa7a2008-04-13 08:22:30 +0000315 // Move over the last 'WidthFactor-1' values from here to NewNode.
316 memcpy(&NewNode->Values[0], &Values[WidthFactor],
317 (WidthFactor-1)*sizeof(Values[0]));
Mike Stump11289f42009-09-09 15:08:12 +0000318
Chris Lattnercdefa7a2008-04-13 08:22:30 +0000319 // Decrease the number of values in the two nodes.
320 NewNode->NumValuesUsed = NumValuesUsed = WidthFactor-1;
Mike Stump11289f42009-09-09 15:08:12 +0000321
Chris Lattnercdefa7a2008-04-13 08:22:30 +0000322 // Recompute the two nodes' full delta.
323 NewNode->RecomputeFullDeltaLocally();
324 RecomputeFullDeltaLocally();
Mike Stump11289f42009-09-09 15:08:12 +0000325
Chris Lattnercdefa7a2008-04-13 08:22:30 +0000326 InsertRes.LHS = this;
327 InsertRes.RHS = NewNode;
328 InsertRes.Split = Values[WidthFactor-1];
329}
330
331
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000332
333//===----------------------------------------------------------------------===//
334// DeltaTree Implementation
335//===----------------------------------------------------------------------===//
336
Chris Lattner8fc77b72008-04-12 22:04:18 +0000337//#define VERIFY_TREE
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000338
Chris Lattner8fc77b72008-04-12 22:04:18 +0000339#ifdef VERIFY_TREE
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000340/// VerifyTree - Walk the btree performing assertions on various properties to
341/// verify consistency. This is useful for debugging new changes to the tree.
342static void VerifyTree(const DeltaTreeNode *N) {
343 const DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(N);
344 if (IN == 0) {
345 // Verify leaves, just ensure that FullDelta matches up and the elements
346 // are in proper order.
347 int FullDelta = 0;
348 for (unsigned i = 0, e = N->getNumValuesUsed(); i != e; ++i) {
349 if (i)
350 assert(N->getValue(i-1).FileLoc < N->getValue(i).FileLoc);
351 FullDelta += N->getValue(i).Delta;
352 }
353 assert(FullDelta == N->getFullDelta());
354 return;
355 }
Mike Stump11289f42009-09-09 15:08:12 +0000356
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000357 // Verify interior nodes: Ensure that FullDelta matches up and the
358 // elements are in proper order and the children are in proper order.
359 int FullDelta = 0;
360 for (unsigned i = 0, e = IN->getNumValuesUsed(); i != e; ++i) {
361 const SourceDelta &IVal = N->getValue(i);
362 const DeltaTreeNode *IChild = IN->getChild(i);
363 if (i)
364 assert(IN->getValue(i-1).FileLoc < IVal.FileLoc);
365 FullDelta += IVal.Delta;
366 FullDelta += IChild->getFullDelta();
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000368 // The largest value in child #i should be smaller than FileLoc.
369 assert(IChild->getValue(IChild->getNumValuesUsed()-1).FileLoc <
370 IVal.FileLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000371
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000372 // The smallest value in child #i+1 should be larger than FileLoc.
373 assert(IN->getChild(i+1)->getValue(0).FileLoc > IVal.FileLoc);
374 VerifyTree(IChild);
375 }
Mike Stump11289f42009-09-09 15:08:12 +0000376
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000377 FullDelta += IN->getChild(IN->getNumValuesUsed())->getFullDelta();
Mike Stump11289f42009-09-09 15:08:12 +0000378
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000379 assert(FullDelta == N->getFullDelta());
380}
Chris Lattner8fc77b72008-04-12 22:04:18 +0000381#endif // VERIFY_TREE
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000382
383static DeltaTreeNode *getRoot(void *Root) {
384 return (DeltaTreeNode*)Root;
385}
386
387DeltaTree::DeltaTree() {
388 Root = new DeltaTreeNode();
389}
390DeltaTree::DeltaTree(const DeltaTree &RHS) {
391 // Currently we only support copying when the RHS is empty.
392 assert(getRoot(RHS.Root)->getNumValuesUsed() == 0 &&
393 "Can only copy empty tree");
394 Root = new DeltaTreeNode();
395}
396
397DeltaTree::~DeltaTree() {
398 getRoot(Root)->Destroy();
399}
400
401/// getDeltaAt - Return the accumulated delta at the specified file offset.
402/// This includes all insertions or delections that occurred *before* the
403/// specified file index.
404int DeltaTree::getDeltaAt(unsigned FileIndex) const {
405 const DeltaTreeNode *Node = getRoot(Root);
Mike Stump11289f42009-09-09 15:08:12 +0000406
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000407 int Result = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000408
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000409 // Walk down the tree.
410 while (1) {
411 // For all nodes, include any local deltas before the specified file
412 // index by summing them up directly. Keep track of how many were
413 // included.
414 unsigned NumValsGreater = 0;
415 for (unsigned e = Node->getNumValuesUsed(); NumValsGreater != e;
416 ++NumValsGreater) {
417 const SourceDelta &Val = Node->getValue(NumValsGreater);
Mike Stump11289f42009-09-09 15:08:12 +0000418
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000419 if (Val.FileLoc >= FileIndex)
420 break;
421 Result += Val.Delta;
422 }
Mike Stump11289f42009-09-09 15:08:12 +0000423
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000424 // If we have an interior node, include information about children and
425 // recurse. Otherwise, if we have a leaf, we're done.
426 const DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(Node);
427 if (!IN) return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000428
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000429 // Include any children to the left of the values we skipped, all of
430 // their deltas should be included as well.
431 for (unsigned i = 0; i != NumValsGreater; ++i)
432 Result += IN->getChild(i)->getFullDelta();
Mike Stump11289f42009-09-09 15:08:12 +0000433
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000434 // If we found exactly the value we were looking for, break off the
435 // search early. There is no need to search the RHS of the value for
436 // partial results.
437 if (NumValsGreater != Node->getNumValuesUsed() &&
438 Node->getValue(NumValsGreater).FileLoc == FileIndex)
Chris Lattner52bc4ac2008-04-15 02:26:21 +0000439 return Result+IN->getChild(NumValsGreater)->getFullDelta();
Mike Stump11289f42009-09-09 15:08:12 +0000440
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000441 // Otherwise, traverse down the tree. The selected subtree may be
442 // partially included in the range.
443 Node = IN->getChild(NumValsGreater);
444 }
445 // NOT REACHED.
446}
447
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000448/// AddDelta - When a change is made that shifts around the text buffer,
449/// this method is used to record that info. It inserts a delta of 'Delta'
450/// into the current DeltaTree at offset FileIndex.
451void DeltaTree::AddDelta(unsigned FileIndex, int Delta) {
452 assert(Delta && "Adding a noop?");
Chris Lattnercdefa7a2008-04-13 08:22:30 +0000453 DeltaTreeNode *MyRoot = getRoot(Root);
Mike Stump11289f42009-09-09 15:08:12 +0000454
Douglas Gregor5492edc2009-11-17 06:52:37 +0000455 DeltaTreeNode::InsertResult InsertRes;
Chris Lattnercc0ef632008-04-13 08:52:45 +0000456 if (MyRoot->DoInsertion(FileIndex, Delta, &InsertRes)) {
457 Root = MyRoot = new DeltaTreeInteriorNode(InsertRes);
458 }
Mike Stump11289f42009-09-09 15:08:12 +0000459
Chris Lattner8fc77b72008-04-12 22:04:18 +0000460#ifdef VERIFY_TREE
Chris Lattnercdefa7a2008-04-13 08:22:30 +0000461 VerifyTree(MyRoot);
Chris Lattner8fc77b72008-04-12 22:04:18 +0000462#endif
Chris Lattnercbb6bad2008-04-12 22:00:40 +0000463}
464