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