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