1db914a46SEugene Zelenko //===- DeltaTree.cpp - B-Tree for Rewrite Delta tracking ------------------===//
20621cb2eSAlp Toker //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60621cb2eSAlp Toker //
70621cb2eSAlp Toker //===----------------------------------------------------------------------===//
80621cb2eSAlp Toker //
90621cb2eSAlp Toker // This file implements the DeltaTree and related classes.
100621cb2eSAlp Toker //
110621cb2eSAlp Toker //===----------------------------------------------------------------------===//
120621cb2eSAlp Toker
130621cb2eSAlp Toker #include "clang/Rewrite/Core/DeltaTree.h"
140621cb2eSAlp Toker #include "clang/Basic/LLVM.h"
15db914a46SEugene Zelenko #include "llvm/Support/Casting.h"
16db914a46SEugene Zelenko #include <cassert>
170621cb2eSAlp Toker #include <cstring>
18db914a46SEugene Zelenko
190621cb2eSAlp Toker using namespace clang;
200621cb2eSAlp Toker
210621cb2eSAlp Toker /// The DeltaTree class is a multiway search tree (BTree) structure with some
220621cb2eSAlp Toker /// fancy features. B-Trees are generally more memory and cache efficient
230621cb2eSAlp Toker /// than binary trees, because they store multiple keys/values in each node.
240621cb2eSAlp Toker ///
250621cb2eSAlp Toker /// DeltaTree implements a key/value mapping from FileIndex to Delta, allowing
260621cb2eSAlp Toker /// fast lookup by FileIndex. However, an added (important) bonus is that it
270621cb2eSAlp Toker /// can also efficiently tell us the full accumulated delta for a specific
280621cb2eSAlp Toker /// file offset as well, without traversing the whole tree.
290621cb2eSAlp Toker ///
300621cb2eSAlp Toker /// The nodes of the tree are made up of instances of two classes:
310621cb2eSAlp Toker /// DeltaTreeNode and DeltaTreeInteriorNode. The later subclasses the
320621cb2eSAlp Toker /// former and adds children pointers. Each node knows the full delta of all
330621cb2eSAlp Toker /// entries (recursively) contained inside of it, which allows us to get the
340621cb2eSAlp Toker /// full delta implied by a whole subtree in constant time.
350621cb2eSAlp Toker
360621cb2eSAlp Toker namespace {
37db914a46SEugene Zelenko
380621cb2eSAlp Toker /// SourceDelta - As code in the original input buffer is added and deleted,
390621cb2eSAlp Toker /// SourceDelta records are used to keep track of how the input SourceLocation
400621cb2eSAlp Toker /// object is mapped into the output buffer.
410621cb2eSAlp Toker struct SourceDelta {
420621cb2eSAlp Toker unsigned FileLoc;
430621cb2eSAlp Toker int Delta;
440621cb2eSAlp Toker
get__anonb79a93750111::SourceDelta450621cb2eSAlp Toker static SourceDelta get(unsigned Loc, int D) {
460621cb2eSAlp Toker SourceDelta Delta;
470621cb2eSAlp Toker Delta.FileLoc = Loc;
480621cb2eSAlp Toker Delta.Delta = D;
490621cb2eSAlp Toker return Delta;
500621cb2eSAlp Toker }
510621cb2eSAlp Toker };
520621cb2eSAlp Toker
530621cb2eSAlp Toker /// DeltaTreeNode - The common part of all nodes.
540621cb2eSAlp Toker ///
550621cb2eSAlp Toker class DeltaTreeNode {
560621cb2eSAlp Toker public:
570621cb2eSAlp Toker struct InsertResult {
580621cb2eSAlp Toker DeltaTreeNode *LHS, *RHS;
590621cb2eSAlp Toker SourceDelta Split;
600621cb2eSAlp Toker };
610621cb2eSAlp Toker
620621cb2eSAlp Toker private:
630621cb2eSAlp Toker friend class DeltaTreeInteriorNode;
640621cb2eSAlp Toker
650621cb2eSAlp Toker /// WidthFactor - This controls the number of K/V slots held in the BTree:
660621cb2eSAlp Toker /// how wide it is. Each level of the BTree is guaranteed to have at least
670621cb2eSAlp Toker /// WidthFactor-1 K/V pairs (except the root) and may have at most
680621cb2eSAlp Toker /// 2*WidthFactor-1 K/V pairs.
690621cb2eSAlp Toker enum { WidthFactor = 8 };
700621cb2eSAlp Toker
710621cb2eSAlp Toker /// Values - This tracks the SourceDelta's currently in this node.
720621cb2eSAlp Toker SourceDelta Values[2*WidthFactor-1];
730621cb2eSAlp Toker
740621cb2eSAlp Toker /// NumValuesUsed - This tracks the number of values this node currently
750621cb2eSAlp Toker /// holds.
76db914a46SEugene Zelenko unsigned char NumValuesUsed = 0;
770621cb2eSAlp Toker
780621cb2eSAlp Toker /// IsLeaf - This is true if this is a leaf of the btree. If false, this is
790621cb2eSAlp Toker /// an interior node, and is actually an instance of DeltaTreeInteriorNode.
800621cb2eSAlp Toker bool IsLeaf;
810621cb2eSAlp Toker
820621cb2eSAlp Toker /// FullDelta - This is the full delta of all the values in this node and
830621cb2eSAlp Toker /// all children nodes.
84db914a46SEugene Zelenko int FullDelta = 0;
85db914a46SEugene Zelenko
860621cb2eSAlp Toker public:
DeltaTreeNode(bool isLeaf=true)87db914a46SEugene Zelenko DeltaTreeNode(bool isLeaf = true) : IsLeaf(isLeaf) {}
880621cb2eSAlp Toker
isLeaf() const890621cb2eSAlp Toker bool isLeaf() const { return IsLeaf; }
getFullDelta() const900621cb2eSAlp Toker int getFullDelta() const { return FullDelta; }
isFull() const910621cb2eSAlp Toker bool isFull() const { return NumValuesUsed == 2*WidthFactor-1; }
920621cb2eSAlp Toker
getNumValuesUsed() const930621cb2eSAlp Toker unsigned getNumValuesUsed() const { return NumValuesUsed; }
94db914a46SEugene Zelenko
getValue(unsigned i) const950621cb2eSAlp Toker const SourceDelta &getValue(unsigned i) const {
960621cb2eSAlp Toker assert(i < NumValuesUsed && "Invalid value #");
970621cb2eSAlp Toker return Values[i];
980621cb2eSAlp Toker }
99db914a46SEugene Zelenko
getValue(unsigned i)1000621cb2eSAlp Toker SourceDelta &getValue(unsigned i) {
1010621cb2eSAlp Toker assert(i < NumValuesUsed && "Invalid value #");
1020621cb2eSAlp Toker return Values[i];
1030621cb2eSAlp Toker }
1040621cb2eSAlp Toker
1050621cb2eSAlp Toker /// DoInsertion - Do an insertion of the specified FileIndex/Delta pair into
1060621cb2eSAlp Toker /// this node. If insertion is easy, do it and return false. Otherwise,
1070621cb2eSAlp Toker /// split the node, populate InsertRes with info about the split, and return
1080621cb2eSAlp Toker /// true.
1090621cb2eSAlp Toker bool DoInsertion(unsigned FileIndex, int Delta, InsertResult *InsertRes);
1100621cb2eSAlp Toker
1110621cb2eSAlp Toker void DoSplit(InsertResult &InsertRes);
1120621cb2eSAlp Toker
1130621cb2eSAlp Toker
1140621cb2eSAlp Toker /// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a
1150621cb2eSAlp Toker /// local walk over our contained deltas.
1160621cb2eSAlp Toker void RecomputeFullDeltaLocally();
1170621cb2eSAlp Toker
1180621cb2eSAlp Toker void Destroy();
1190621cb2eSAlp Toker };
1200621cb2eSAlp Toker
1210621cb2eSAlp Toker /// DeltaTreeInteriorNode - When isLeaf = false, a node has child pointers.
1220621cb2eSAlp Toker /// This class tracks them.
1230621cb2eSAlp Toker class DeltaTreeInteriorNode : public DeltaTreeNode {
124db914a46SEugene Zelenko friend class DeltaTreeNode;
125db914a46SEugene Zelenko
1260621cb2eSAlp Toker DeltaTreeNode *Children[2*WidthFactor];
127db914a46SEugene Zelenko
~DeltaTreeInteriorNode()1280621cb2eSAlp Toker ~DeltaTreeInteriorNode() {
1290621cb2eSAlp Toker for (unsigned i = 0, e = NumValuesUsed+1; i != e; ++i)
1300621cb2eSAlp Toker Children[i]->Destroy();
1310621cb2eSAlp Toker }
132db914a46SEugene Zelenko
1330621cb2eSAlp Toker public:
DeltaTreeInteriorNode()1340621cb2eSAlp Toker DeltaTreeInteriorNode() : DeltaTreeNode(false /*nonleaf*/) {}
1350621cb2eSAlp Toker
DeltaTreeInteriorNode(const InsertResult & IR)1360621cb2eSAlp Toker DeltaTreeInteriorNode(const InsertResult &IR)
1370621cb2eSAlp Toker : DeltaTreeNode(false /*nonleaf*/) {
1380621cb2eSAlp Toker Children[0] = IR.LHS;
1390621cb2eSAlp Toker Children[1] = IR.RHS;
1400621cb2eSAlp Toker Values[0] = IR.Split;
1410621cb2eSAlp Toker FullDelta = IR.LHS->getFullDelta()+IR.RHS->getFullDelta()+IR.Split.Delta;
1420621cb2eSAlp Toker NumValuesUsed = 1;
1430621cb2eSAlp Toker }
1440621cb2eSAlp Toker
getChild(unsigned i) const1450621cb2eSAlp Toker const DeltaTreeNode *getChild(unsigned i) const {
1460621cb2eSAlp Toker assert(i < getNumValuesUsed()+1 && "Invalid child");
1470621cb2eSAlp Toker return Children[i];
1480621cb2eSAlp Toker }
149db914a46SEugene Zelenko
getChild(unsigned i)1500621cb2eSAlp Toker DeltaTreeNode *getChild(unsigned i) {
1510621cb2eSAlp Toker assert(i < getNumValuesUsed()+1 && "Invalid child");
1520621cb2eSAlp Toker return Children[i];
1530621cb2eSAlp Toker }
1540621cb2eSAlp Toker
classof(const DeltaTreeNode * N)155db914a46SEugene Zelenko static bool classof(const DeltaTreeNode *N) { return !N->isLeaf(); }
1560621cb2eSAlp Toker };
1570621cb2eSAlp Toker
158db914a46SEugene Zelenko } // namespace
1590621cb2eSAlp Toker
1600621cb2eSAlp Toker /// Destroy - A 'virtual' destructor.
Destroy()1610621cb2eSAlp Toker void DeltaTreeNode::Destroy() {
1620621cb2eSAlp Toker if (isLeaf())
1630621cb2eSAlp Toker delete this;
1640621cb2eSAlp Toker else
1650621cb2eSAlp Toker delete cast<DeltaTreeInteriorNode>(this);
1660621cb2eSAlp Toker }
1670621cb2eSAlp Toker
1680621cb2eSAlp Toker /// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a
1690621cb2eSAlp Toker /// local walk over our contained deltas.
RecomputeFullDeltaLocally()1700621cb2eSAlp Toker void DeltaTreeNode::RecomputeFullDeltaLocally() {
1710621cb2eSAlp Toker int NewFullDelta = 0;
1720621cb2eSAlp Toker for (unsigned i = 0, e = getNumValuesUsed(); i != e; ++i)
1730621cb2eSAlp Toker NewFullDelta += Values[i].Delta;
174db914a46SEugene Zelenko if (auto *IN = dyn_cast<DeltaTreeInteriorNode>(this))
1750621cb2eSAlp Toker for (unsigned i = 0, e = getNumValuesUsed()+1; i != e; ++i)
1760621cb2eSAlp Toker NewFullDelta += IN->getChild(i)->getFullDelta();
1770621cb2eSAlp Toker FullDelta = NewFullDelta;
1780621cb2eSAlp Toker }
1790621cb2eSAlp Toker
1800621cb2eSAlp Toker /// DoInsertion - Do an insertion of the specified FileIndex/Delta pair into
1810621cb2eSAlp Toker /// this node. If insertion is easy, do it and return false. Otherwise,
1820621cb2eSAlp Toker /// split the node, populate InsertRes with info about the split, and return
1830621cb2eSAlp Toker /// true.
DoInsertion(unsigned FileIndex,int Delta,InsertResult * InsertRes)1840621cb2eSAlp Toker bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta,
1850621cb2eSAlp Toker InsertResult *InsertRes) {
1860621cb2eSAlp Toker // Maintain full delta for this node.
1870621cb2eSAlp Toker FullDelta += Delta;
1880621cb2eSAlp Toker
1890621cb2eSAlp Toker // Find the insertion point, the first delta whose index is >= FileIndex.
1900621cb2eSAlp Toker unsigned i = 0, e = getNumValuesUsed();
1910621cb2eSAlp Toker while (i != e && FileIndex > getValue(i).FileLoc)
1920621cb2eSAlp Toker ++i;
1930621cb2eSAlp Toker
1940621cb2eSAlp Toker // If we found an a record for exactly this file index, just merge this
1950621cb2eSAlp Toker // value into the pre-existing record and finish early.
1960621cb2eSAlp Toker if (i != e && getValue(i).FileLoc == FileIndex) {
1970621cb2eSAlp Toker // NOTE: Delta could drop to zero here. This means that the delta entry is
1980621cb2eSAlp Toker // useless and could be removed. Supporting erases is more complex than
1990621cb2eSAlp Toker // leaving an entry with Delta=0, so we just leave an entry with Delta=0 in
2000621cb2eSAlp Toker // the tree.
2010621cb2eSAlp Toker Values[i].Delta += Delta;
2020621cb2eSAlp Toker return false;
2030621cb2eSAlp Toker }
2040621cb2eSAlp Toker
2050621cb2eSAlp Toker // Otherwise, we found an insertion point, and we know that the value at the
2060621cb2eSAlp Toker // specified index is > FileIndex. Handle the leaf case first.
2070621cb2eSAlp Toker if (isLeaf()) {
2080621cb2eSAlp Toker if (!isFull()) {
2090621cb2eSAlp Toker // For an insertion into a non-full leaf node, just insert the value in
2100621cb2eSAlp Toker // its sorted position. This requires moving later values over.
2110621cb2eSAlp Toker if (i != e)
2120621cb2eSAlp Toker memmove(&Values[i+1], &Values[i], sizeof(Values[0])*(e-i));
2130621cb2eSAlp Toker Values[i] = SourceDelta::get(FileIndex, Delta);
2140621cb2eSAlp Toker ++NumValuesUsed;
2150621cb2eSAlp Toker return false;
2160621cb2eSAlp Toker }
2170621cb2eSAlp Toker
2180621cb2eSAlp Toker // Otherwise, if this is leaf is full, split the node at its median, insert
2190621cb2eSAlp Toker // the value into one of the children, and return the result.
2200621cb2eSAlp Toker assert(InsertRes && "No result location specified");
2210621cb2eSAlp Toker DoSplit(*InsertRes);
2220621cb2eSAlp Toker
2230621cb2eSAlp Toker if (InsertRes->Split.FileLoc > FileIndex)
2240621cb2eSAlp Toker InsertRes->LHS->DoInsertion(FileIndex, Delta, nullptr /*can't fail*/);
2250621cb2eSAlp Toker else
2260621cb2eSAlp Toker InsertRes->RHS->DoInsertion(FileIndex, Delta, nullptr /*can't fail*/);
2270621cb2eSAlp Toker return true;
2280621cb2eSAlp Toker }
2290621cb2eSAlp Toker
2300621cb2eSAlp Toker // Otherwise, this is an interior node. Send the request down the tree.
231db914a46SEugene Zelenko auto *IN = cast<DeltaTreeInteriorNode>(this);
2320621cb2eSAlp Toker if (!IN->Children[i]->DoInsertion(FileIndex, Delta, InsertRes))
2330621cb2eSAlp Toker return false; // If there was space in the child, just return.
2340621cb2eSAlp Toker
2350621cb2eSAlp Toker // Okay, this split the subtree, producing a new value and two children to
2360621cb2eSAlp Toker // insert here. If this node is non-full, we can just insert it directly.
2370621cb2eSAlp Toker if (!isFull()) {
2380621cb2eSAlp Toker // Now that we have two nodes and a new element, insert the perclated value
2390621cb2eSAlp Toker // into ourself by moving all the later values/children down, then inserting
2400621cb2eSAlp Toker // the new one.
2410621cb2eSAlp Toker if (i != e)
2420621cb2eSAlp Toker memmove(&IN->Children[i+2], &IN->Children[i+1],
2430621cb2eSAlp Toker (e-i)*sizeof(IN->Children[0]));
2440621cb2eSAlp Toker IN->Children[i] = InsertRes->LHS;
2450621cb2eSAlp Toker IN->Children[i+1] = InsertRes->RHS;
2460621cb2eSAlp Toker
2470621cb2eSAlp Toker if (e != i)
2480621cb2eSAlp Toker memmove(&Values[i+1], &Values[i], (e-i)*sizeof(Values[0]));
2490621cb2eSAlp Toker Values[i] = InsertRes->Split;
2500621cb2eSAlp Toker ++NumValuesUsed;
2510621cb2eSAlp Toker return false;
2520621cb2eSAlp Toker }
2530621cb2eSAlp Toker
2540621cb2eSAlp Toker // Finally, if this interior node was full and a node is percolated up, split
2550621cb2eSAlp Toker // ourself and return that up the chain. Start by saving all our info to
2560621cb2eSAlp Toker // avoid having the split clobber it.
2570621cb2eSAlp Toker IN->Children[i] = InsertRes->LHS;
2580621cb2eSAlp Toker DeltaTreeNode *SubRHS = InsertRes->RHS;
2590621cb2eSAlp Toker SourceDelta SubSplit = InsertRes->Split;
2600621cb2eSAlp Toker
2610621cb2eSAlp Toker // Do the split.
2620621cb2eSAlp Toker DoSplit(*InsertRes);
2630621cb2eSAlp Toker
2640621cb2eSAlp Toker // Figure out where to insert SubRHS/NewSplit.
2650621cb2eSAlp Toker DeltaTreeInteriorNode *InsertSide;
2660621cb2eSAlp Toker if (SubSplit.FileLoc < InsertRes->Split.FileLoc)
2670621cb2eSAlp Toker InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->LHS);
2680621cb2eSAlp Toker else
2690621cb2eSAlp Toker InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->RHS);
2700621cb2eSAlp Toker
2710621cb2eSAlp Toker // We now have a non-empty interior node 'InsertSide' to insert
2720621cb2eSAlp Toker // SubRHS/SubSplit into. Find out where to insert SubSplit.
2730621cb2eSAlp Toker
2740621cb2eSAlp Toker // Find the insertion point, the first delta whose index is >SubSplit.FileLoc.
2750621cb2eSAlp Toker i = 0; e = InsertSide->getNumValuesUsed();
2760621cb2eSAlp Toker while (i != e && SubSplit.FileLoc > InsertSide->getValue(i).FileLoc)
2770621cb2eSAlp Toker ++i;
2780621cb2eSAlp Toker
2790621cb2eSAlp Toker // Now we know that i is the place to insert the split value into. Insert it
2800621cb2eSAlp Toker // and the child right after it.
2810621cb2eSAlp Toker if (i != e)
2820621cb2eSAlp Toker memmove(&InsertSide->Children[i+2], &InsertSide->Children[i+1],
2830621cb2eSAlp Toker (e-i)*sizeof(IN->Children[0]));
2840621cb2eSAlp Toker InsertSide->Children[i+1] = SubRHS;
2850621cb2eSAlp Toker
2860621cb2eSAlp Toker if (e != i)
2870621cb2eSAlp Toker memmove(&InsertSide->Values[i+1], &InsertSide->Values[i],
2880621cb2eSAlp Toker (e-i)*sizeof(Values[0]));
2890621cb2eSAlp Toker InsertSide->Values[i] = SubSplit;
2900621cb2eSAlp Toker ++InsertSide->NumValuesUsed;
2910621cb2eSAlp Toker InsertSide->FullDelta += SubSplit.Delta + SubRHS->getFullDelta();
2920621cb2eSAlp Toker return true;
2930621cb2eSAlp Toker }
2940621cb2eSAlp Toker
2950621cb2eSAlp Toker /// DoSplit - Split the currently full node (which has 2*WidthFactor-1 values)
2960621cb2eSAlp Toker /// into two subtrees each with "WidthFactor-1" values and a pivot value.
2970621cb2eSAlp Toker /// Return the pieces in InsertRes.
DoSplit(InsertResult & InsertRes)2980621cb2eSAlp Toker void DeltaTreeNode::DoSplit(InsertResult &InsertRes) {
2990621cb2eSAlp Toker assert(isFull() && "Why split a non-full node?");
3000621cb2eSAlp Toker
3010621cb2eSAlp Toker // Since this node is full, it contains 2*WidthFactor-1 values. We move
3020621cb2eSAlp Toker // the first 'WidthFactor-1' values to the LHS child (which we leave in this
3030621cb2eSAlp Toker // node), propagate one value up, and move the last 'WidthFactor-1' values
3040621cb2eSAlp Toker // into the RHS child.
3050621cb2eSAlp Toker
3060621cb2eSAlp Toker // Create the new child node.
3070621cb2eSAlp Toker DeltaTreeNode *NewNode;
308db914a46SEugene Zelenko if (auto *IN = dyn_cast<DeltaTreeInteriorNode>(this)) {
3090621cb2eSAlp Toker // If this is an interior node, also move over 'WidthFactor' children
3100621cb2eSAlp Toker // into the new node.
3110621cb2eSAlp Toker DeltaTreeInteriorNode *New = new DeltaTreeInteriorNode();
3120621cb2eSAlp Toker memcpy(&New->Children[0], &IN->Children[WidthFactor],
3130621cb2eSAlp Toker WidthFactor*sizeof(IN->Children[0]));
3140621cb2eSAlp Toker NewNode = New;
3150621cb2eSAlp Toker } else {
3160621cb2eSAlp Toker // Just create the new leaf node.
3170621cb2eSAlp Toker NewNode = new DeltaTreeNode();
3180621cb2eSAlp Toker }
3190621cb2eSAlp Toker
3200621cb2eSAlp Toker // Move over the last 'WidthFactor-1' values from here to NewNode.
3210621cb2eSAlp Toker memcpy(&NewNode->Values[0], &Values[WidthFactor],
3220621cb2eSAlp Toker (WidthFactor-1)*sizeof(Values[0]));
3230621cb2eSAlp Toker
3240621cb2eSAlp Toker // Decrease the number of values in the two nodes.
3250621cb2eSAlp Toker NewNode->NumValuesUsed = NumValuesUsed = WidthFactor-1;
3260621cb2eSAlp Toker
3270621cb2eSAlp Toker // Recompute the two nodes' full delta.
3280621cb2eSAlp Toker NewNode->RecomputeFullDeltaLocally();
3290621cb2eSAlp Toker RecomputeFullDeltaLocally();
3300621cb2eSAlp Toker
3310621cb2eSAlp Toker InsertRes.LHS = this;
3320621cb2eSAlp Toker InsertRes.RHS = NewNode;
3330621cb2eSAlp Toker InsertRes.Split = Values[WidthFactor-1];
3340621cb2eSAlp Toker }
3350621cb2eSAlp Toker
3360621cb2eSAlp Toker //===----------------------------------------------------------------------===//
3370621cb2eSAlp Toker // DeltaTree Implementation
3380621cb2eSAlp Toker //===----------------------------------------------------------------------===//
3390621cb2eSAlp Toker
3400621cb2eSAlp Toker //#define VERIFY_TREE
3410621cb2eSAlp Toker
3420621cb2eSAlp Toker #ifdef VERIFY_TREE
3430621cb2eSAlp Toker /// VerifyTree - Walk the btree performing assertions on various properties to
3440621cb2eSAlp Toker /// verify consistency. This is useful for debugging new changes to the tree.
VerifyTree(const DeltaTreeNode * N)3450621cb2eSAlp Toker static void VerifyTree(const DeltaTreeNode *N) {
346db914a46SEugene Zelenko const auto *IN = dyn_cast<DeltaTreeInteriorNode>(N);
3470621cb2eSAlp Toker if (IN == 0) {
3480621cb2eSAlp Toker // Verify leaves, just ensure that FullDelta matches up and the elements
3490621cb2eSAlp Toker // are in proper order.
3500621cb2eSAlp Toker int FullDelta = 0;
3510621cb2eSAlp Toker for (unsigned i = 0, e = N->getNumValuesUsed(); i != e; ++i) {
3520621cb2eSAlp Toker if (i)
3530621cb2eSAlp Toker assert(N->getValue(i-1).FileLoc < N->getValue(i).FileLoc);
3540621cb2eSAlp Toker FullDelta += N->getValue(i).Delta;
3550621cb2eSAlp Toker }
3560621cb2eSAlp Toker assert(FullDelta == N->getFullDelta());
3570621cb2eSAlp Toker return;
3580621cb2eSAlp Toker }
3590621cb2eSAlp Toker
3600621cb2eSAlp Toker // Verify interior nodes: Ensure that FullDelta matches up and the
3610621cb2eSAlp Toker // elements are in proper order and the children are in proper order.
3620621cb2eSAlp Toker int FullDelta = 0;
3630621cb2eSAlp Toker for (unsigned i = 0, e = IN->getNumValuesUsed(); i != e; ++i) {
3640621cb2eSAlp Toker const SourceDelta &IVal = N->getValue(i);
3650621cb2eSAlp Toker const DeltaTreeNode *IChild = IN->getChild(i);
3660621cb2eSAlp Toker if (i)
3670621cb2eSAlp Toker assert(IN->getValue(i-1).FileLoc < IVal.FileLoc);
3680621cb2eSAlp Toker FullDelta += IVal.Delta;
3690621cb2eSAlp Toker FullDelta += IChild->getFullDelta();
3700621cb2eSAlp Toker
3710621cb2eSAlp Toker // The largest value in child #i should be smaller than FileLoc.
3720621cb2eSAlp Toker assert(IChild->getValue(IChild->getNumValuesUsed()-1).FileLoc <
3730621cb2eSAlp Toker IVal.FileLoc);
3740621cb2eSAlp Toker
3750621cb2eSAlp Toker // The smallest value in child #i+1 should be larger than FileLoc.
3760621cb2eSAlp Toker assert(IN->getChild(i+1)->getValue(0).FileLoc > IVal.FileLoc);
3770621cb2eSAlp Toker VerifyTree(IChild);
3780621cb2eSAlp Toker }
3790621cb2eSAlp Toker
3800621cb2eSAlp Toker FullDelta += IN->getChild(IN->getNumValuesUsed())->getFullDelta();
3810621cb2eSAlp Toker
3820621cb2eSAlp Toker assert(FullDelta == N->getFullDelta());
3830621cb2eSAlp Toker }
3840621cb2eSAlp Toker #endif // VERIFY_TREE
3850621cb2eSAlp Toker
getRoot(void * Root)3860621cb2eSAlp Toker static DeltaTreeNode *getRoot(void *Root) {
3870621cb2eSAlp Toker return (DeltaTreeNode*)Root;
3880621cb2eSAlp Toker }
3890621cb2eSAlp Toker
DeltaTree()3900621cb2eSAlp Toker DeltaTree::DeltaTree() {
3910621cb2eSAlp Toker Root = new DeltaTreeNode();
3920621cb2eSAlp Toker }
393db914a46SEugene Zelenko
DeltaTree(const DeltaTree & RHS)3940621cb2eSAlp Toker DeltaTree::DeltaTree(const DeltaTree &RHS) {
3950621cb2eSAlp Toker // Currently we only support copying when the RHS is empty.
3960621cb2eSAlp Toker assert(getRoot(RHS.Root)->getNumValuesUsed() == 0 &&
3970621cb2eSAlp Toker "Can only copy empty tree");
3980621cb2eSAlp Toker Root = new DeltaTreeNode();
3990621cb2eSAlp Toker }
4000621cb2eSAlp Toker
~DeltaTree()4010621cb2eSAlp Toker DeltaTree::~DeltaTree() {
4020621cb2eSAlp Toker getRoot(Root)->Destroy();
4030621cb2eSAlp Toker }
4040621cb2eSAlp Toker
4050621cb2eSAlp Toker /// getDeltaAt - Return the accumulated delta at the specified file offset.
4060621cb2eSAlp Toker /// This includes all insertions or delections that occurred *before* the
4070621cb2eSAlp Toker /// specified file index.
getDeltaAt(unsigned FileIndex) const4080621cb2eSAlp Toker int DeltaTree::getDeltaAt(unsigned FileIndex) const {
4090621cb2eSAlp Toker const DeltaTreeNode *Node = getRoot(Root);
4100621cb2eSAlp Toker
4110621cb2eSAlp Toker int Result = 0;
4120621cb2eSAlp Toker
4130621cb2eSAlp Toker // Walk down the tree.
414db914a46SEugene Zelenko while (true) {
4150621cb2eSAlp Toker // For all nodes, include any local deltas before the specified file
4160621cb2eSAlp Toker // index by summing them up directly. Keep track of how many were
4170621cb2eSAlp Toker // included.
4180621cb2eSAlp Toker unsigned NumValsGreater = 0;
4190621cb2eSAlp Toker for (unsigned e = Node->getNumValuesUsed(); NumValsGreater != e;
4200621cb2eSAlp Toker ++NumValsGreater) {
4210621cb2eSAlp Toker const SourceDelta &Val = Node->getValue(NumValsGreater);
4220621cb2eSAlp Toker
4230621cb2eSAlp Toker if (Val.FileLoc >= FileIndex)
4240621cb2eSAlp Toker break;
4250621cb2eSAlp Toker Result += Val.Delta;
4260621cb2eSAlp Toker }
4270621cb2eSAlp Toker
4280621cb2eSAlp Toker // If we have an interior node, include information about children and
4290621cb2eSAlp Toker // recurse. Otherwise, if we have a leaf, we're done.
430db914a46SEugene Zelenko const auto *IN = dyn_cast<DeltaTreeInteriorNode>(Node);
4310621cb2eSAlp Toker if (!IN) return Result;
4320621cb2eSAlp Toker
4330621cb2eSAlp Toker // Include any children to the left of the values we skipped, all of
4340621cb2eSAlp Toker // their deltas should be included as well.
4350621cb2eSAlp Toker for (unsigned i = 0; i != NumValsGreater; ++i)
4360621cb2eSAlp Toker Result += IN->getChild(i)->getFullDelta();
4370621cb2eSAlp Toker
4380621cb2eSAlp Toker // If we found exactly the value we were looking for, break off the
4390621cb2eSAlp Toker // search early. There is no need to search the RHS of the value for
4400621cb2eSAlp Toker // partial results.
4410621cb2eSAlp Toker if (NumValsGreater != Node->getNumValuesUsed() &&
4420621cb2eSAlp Toker Node->getValue(NumValsGreater).FileLoc == FileIndex)
4430621cb2eSAlp Toker return Result+IN->getChild(NumValsGreater)->getFullDelta();
4440621cb2eSAlp Toker
4450621cb2eSAlp Toker // Otherwise, traverse down the tree. The selected subtree may be
4460621cb2eSAlp Toker // partially included in the range.
4470621cb2eSAlp Toker Node = IN->getChild(NumValsGreater);
4480621cb2eSAlp Toker }
4490621cb2eSAlp Toker // NOT REACHED.
4500621cb2eSAlp Toker }
4510621cb2eSAlp Toker
4520621cb2eSAlp Toker /// AddDelta - When a change is made that shifts around the text buffer,
4530621cb2eSAlp Toker /// this method is used to record that info. It inserts a delta of 'Delta'
4540621cb2eSAlp Toker /// into the current DeltaTree at offset FileIndex.
AddDelta(unsigned FileIndex,int Delta)4550621cb2eSAlp Toker void DeltaTree::AddDelta(unsigned FileIndex, int Delta) {
4560621cb2eSAlp Toker assert(Delta && "Adding a noop?");
4570621cb2eSAlp Toker DeltaTreeNode *MyRoot = getRoot(Root);
4580621cb2eSAlp Toker
4590621cb2eSAlp Toker DeltaTreeNode::InsertResult InsertRes;
4600621cb2eSAlp Toker if (MyRoot->DoInsertion(FileIndex, Delta, &InsertRes)) {
461*fdae5573SSimon Pilgrim Root = new DeltaTreeInteriorNode(InsertRes);
462*fdae5573SSimon Pilgrim #ifdef VERIFY_TREE
463*fdae5573SSimon Pilgrim MyRoot = Root;
464*fdae5573SSimon Pilgrim #endif
4650621cb2eSAlp Toker }
4660621cb2eSAlp Toker
4670621cb2eSAlp Toker #ifdef VERIFY_TREE
4680621cb2eSAlp Toker VerifyTree(MyRoot);
4690621cb2eSAlp Toker #endif
4700621cb2eSAlp Toker }
471