2008-04-13 06:00:40 +08:00
|
|
|
//===--- DeltaTree.cpp - B-Tree for Rewrite Delta tracking ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the DeltaTree and related classes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-09-01 13:09:24 +08:00
|
|
|
#include "clang/Rewrite/Core/DeltaTree.h"
|
2011-07-23 18:55:15 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
2008-04-13 16:52:45 +08:00
|
|
|
#include <cstdio>
|
2012-12-04 17:13:33 +08:00
|
|
|
#include <cstring>
|
2008-04-13 06:00:40 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
/// The DeltaTree class is a multiway search tree (BTree) structure with some
|
2010-01-21 01:53:58 +08:00
|
|
|
/// fancy features. B-Trees are generally more memory and cache efficient
|
2008-04-13 06:00:40 +08:00
|
|
|
/// than binary trees, because they store multiple keys/values in each node.
|
|
|
|
///
|
|
|
|
/// DeltaTree implements a key/value mapping from FileIndex to Delta, allowing
|
|
|
|
/// fast lookup by FileIndex. However, an added (important) bonus is that it
|
|
|
|
/// can also efficiently tell us the full accumulated delta for a specific
|
|
|
|
/// file offset as well, without traversing the whole tree.
|
|
|
|
///
|
|
|
|
/// The nodes of the tree are made up of instances of two classes:
|
|
|
|
/// DeltaTreeNode and DeltaTreeInteriorNode. The later subclasses the
|
|
|
|
/// former and adds children pointers. Each node knows the full delta of all
|
|
|
|
/// entries (recursively) contained inside of it, which allows us to get the
|
|
|
|
/// full delta implied by a whole subtree in constant time.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
namespace {
|
|
|
|
/// SourceDelta - As code in the original input buffer is added and deleted,
|
|
|
|
/// SourceDelta records are used to keep track of how the input SourceLocation
|
|
|
|
/// object is mapped into the output buffer.
|
|
|
|
struct SourceDelta {
|
|
|
|
unsigned FileLoc;
|
|
|
|
int Delta;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
static SourceDelta get(unsigned Loc, int D) {
|
|
|
|
SourceDelta Delta;
|
|
|
|
Delta.FileLoc = Loc;
|
|
|
|
Delta.Delta = D;
|
|
|
|
return Delta;
|
|
|
|
}
|
|
|
|
};
|
2009-11-17 14:52:37 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
/// DeltaTreeNode - The common part of all nodes.
|
|
|
|
///
|
|
|
|
class DeltaTreeNode {
|
2009-11-17 14:52:37 +08:00
|
|
|
public:
|
|
|
|
struct InsertResult {
|
|
|
|
DeltaTreeNode *LHS, *RHS;
|
|
|
|
SourceDelta Split;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2008-04-13 06:00:40 +08:00
|
|
|
friend class DeltaTreeInteriorNode;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
/// WidthFactor - This controls the number of K/V slots held in the BTree:
|
|
|
|
/// how wide it is. Each level of the BTree is guaranteed to have at least
|
2008-04-14 15:17:29 +08:00
|
|
|
/// WidthFactor-1 K/V pairs (except the root) and may have at most
|
|
|
|
/// 2*WidthFactor-1 K/V pairs.
|
2008-04-13 06:00:40 +08:00
|
|
|
enum { WidthFactor = 8 };
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
/// Values - This tracks the SourceDelta's currently in this node.
|
|
|
|
///
|
|
|
|
SourceDelta Values[2*WidthFactor-1];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
/// NumValuesUsed - This tracks the number of values this node currently
|
|
|
|
/// holds.
|
|
|
|
unsigned char NumValuesUsed;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
/// IsLeaf - This is true if this is a leaf of the btree. If false, this is
|
|
|
|
/// an interior node, and is actually an instance of DeltaTreeInteriorNode.
|
|
|
|
bool IsLeaf;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
/// FullDelta - This is the full delta of all the values in this node and
|
|
|
|
/// all children nodes.
|
|
|
|
int FullDelta;
|
|
|
|
public:
|
|
|
|
DeltaTreeNode(bool isLeaf = true)
|
2008-04-13 16:22:30 +08:00
|
|
|
: NumValuesUsed(0), IsLeaf(isLeaf), FullDelta(0) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
bool isLeaf() const { return IsLeaf; }
|
|
|
|
int getFullDelta() const { return FullDelta; }
|
|
|
|
bool isFull() const { return NumValuesUsed == 2*WidthFactor-1; }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
unsigned getNumValuesUsed() const { return NumValuesUsed; }
|
|
|
|
const SourceDelta &getValue(unsigned i) const {
|
|
|
|
assert(i < NumValuesUsed && "Invalid value #");
|
|
|
|
return Values[i];
|
|
|
|
}
|
|
|
|
SourceDelta &getValue(unsigned i) {
|
|
|
|
assert(i < NumValuesUsed && "Invalid value #");
|
|
|
|
return Values[i];
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:52:45 +08:00
|
|
|
/// DoInsertion - Do an insertion of the specified FileIndex/Delta pair into
|
|
|
|
/// this node. If insertion is easy, do it and return false. Otherwise,
|
|
|
|
/// split the node, populate InsertRes with info about the split, and return
|
|
|
|
/// true.
|
|
|
|
bool DoInsertion(unsigned FileIndex, int Delta, InsertResult *InsertRes);
|
|
|
|
|
2008-04-13 16:22:30 +08:00
|
|
|
void DoSplit(InsertResult &InsertRes);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
/// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a
|
|
|
|
/// local walk over our contained deltas.
|
|
|
|
void RecomputeFullDeltaLocally();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
void Destroy();
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// DeltaTreeInteriorNode - When isLeaf = false, a node has child pointers.
|
|
|
|
/// This class tracks them.
|
|
|
|
class DeltaTreeInteriorNode : public DeltaTreeNode {
|
|
|
|
DeltaTreeNode *Children[2*WidthFactor];
|
|
|
|
~DeltaTreeInteriorNode() {
|
|
|
|
for (unsigned i = 0, e = NumValuesUsed+1; i != e; ++i)
|
|
|
|
Children[i]->Destroy();
|
|
|
|
}
|
|
|
|
friend class DeltaTreeNode;
|
|
|
|
public:
|
|
|
|
DeltaTreeInteriorNode() : DeltaTreeNode(false /*nonleaf*/) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
DeltaTreeInteriorNode(const InsertResult &IR)
|
2008-04-13 16:52:45 +08:00
|
|
|
: DeltaTreeNode(false /*nonleaf*/) {
|
|
|
|
Children[0] = IR.LHS;
|
|
|
|
Children[1] = IR.RHS;
|
|
|
|
Values[0] = IR.Split;
|
|
|
|
FullDelta = IR.LHS->getFullDelta()+IR.RHS->getFullDelta()+IR.Split.Delta;
|
|
|
|
NumValuesUsed = 1;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
const DeltaTreeNode *getChild(unsigned i) const {
|
|
|
|
assert(i < getNumValuesUsed()+1 && "Invalid child");
|
|
|
|
return Children[i];
|
|
|
|
}
|
|
|
|
DeltaTreeNode *getChild(unsigned i) {
|
|
|
|
assert(i < getNumValuesUsed()+1 && "Invalid child");
|
|
|
|
return Children[i];
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
static inline bool classof(const DeltaTreeNode *N) { return !N->isLeaf(); }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Destroy - A 'virtual' destructor.
|
|
|
|
void DeltaTreeNode::Destroy() {
|
|
|
|
if (isLeaf())
|
|
|
|
delete this;
|
|
|
|
else
|
|
|
|
delete cast<DeltaTreeInteriorNode>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a
|
|
|
|
/// local walk over our contained deltas.
|
|
|
|
void DeltaTreeNode::RecomputeFullDeltaLocally() {
|
|
|
|
int NewFullDelta = 0;
|
|
|
|
for (unsigned i = 0, e = getNumValuesUsed(); i != e; ++i)
|
|
|
|
NewFullDelta += Values[i].Delta;
|
|
|
|
if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this))
|
|
|
|
for (unsigned i = 0, e = getNumValuesUsed()+1; i != e; ++i)
|
|
|
|
NewFullDelta += IN->getChild(i)->getFullDelta();
|
|
|
|
FullDelta = NewFullDelta;
|
|
|
|
}
|
|
|
|
|
2008-04-13 16:52:45 +08:00
|
|
|
/// DoInsertion - Do an insertion of the specified FileIndex/Delta pair into
|
|
|
|
/// this node. If insertion is easy, do it and return false. Otherwise,
|
|
|
|
/// split the node, populate InsertRes with info about the split, and return
|
|
|
|
/// true.
|
2009-09-09 23:08:12 +08:00
|
|
|
bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta,
|
2008-04-13 16:52:45 +08:00
|
|
|
InsertResult *InsertRes) {
|
2008-04-13 06:00:40 +08:00
|
|
|
// Maintain full delta for this node.
|
|
|
|
FullDelta += Delta;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
// Find the insertion point, the first delta whose index is >= FileIndex.
|
|
|
|
unsigned i = 0, e = getNumValuesUsed();
|
|
|
|
while (i != e && FileIndex > getValue(i).FileLoc)
|
|
|
|
++i;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
// If we found an a record for exactly this file index, just merge this
|
2008-04-13 16:52:45 +08:00
|
|
|
// value into the pre-existing record and finish early.
|
2008-04-13 06:00:40 +08:00
|
|
|
if (i != e && getValue(i).FileLoc == FileIndex) {
|
2008-04-13 16:52:45 +08:00
|
|
|
// NOTE: Delta could drop to zero here. This means that the delta entry is
|
|
|
|
// useless and could be removed. Supporting erases is more complex than
|
|
|
|
// leaving an entry with Delta=0, so we just leave an entry with Delta=0 in
|
|
|
|
// the tree.
|
2008-04-13 06:00:40 +08:00
|
|
|
Values[i].Delta += Delta;
|
2008-04-13 16:52:45 +08:00
|
|
|
return false;
|
2008-04-13 06:00:40 +08:00
|
|
|
}
|
2008-04-13 16:52:45 +08:00
|
|
|
|
|
|
|
// Otherwise, we found an insertion point, and we know that the value at the
|
|
|
|
// specified index is > FileIndex. Handle the leaf case first.
|
|
|
|
if (isLeaf()) {
|
|
|
|
if (!isFull()) {
|
|
|
|
// For an insertion into a non-full leaf node, just insert the value in
|
|
|
|
// its sorted position. This requires moving later values over.
|
|
|
|
if (i != e)
|
|
|
|
memmove(&Values[i+1], &Values[i], sizeof(Values[0])*(e-i));
|
|
|
|
Values[i] = SourceDelta::get(FileIndex, Delta);
|
|
|
|
++NumValuesUsed;
|
|
|
|
return false;
|
2008-04-13 06:00:40 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:52:45 +08:00
|
|
|
// Otherwise, if this is leaf is full, split the node at its median, insert
|
|
|
|
// the value into one of the children, and return the result.
|
|
|
|
assert(InsertRes && "No result location specified");
|
|
|
|
DoSplit(*InsertRes);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:52:45 +08:00
|
|
|
if (InsertRes->Split.FileLoc > FileIndex)
|
|
|
|
InsertRes->LHS->DoInsertion(FileIndex, Delta, 0 /*can't fail*/);
|
|
|
|
else
|
|
|
|
InsertRes->RHS->DoInsertion(FileIndex, Delta, 0 /*can't fail*/);
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:52:45 +08:00
|
|
|
// Otherwise, this is an interior node. Send the request down the tree.
|
|
|
|
DeltaTreeInteriorNode *IN = cast<DeltaTreeInteriorNode>(this);
|
|
|
|
if (!IN->Children[i]->DoInsertion(FileIndex, Delta, InsertRes))
|
|
|
|
return false; // If there was space in the child, just return.
|
|
|
|
|
|
|
|
// Okay, this split the subtree, producing a new value and two children to
|
|
|
|
// insert here. If this node is non-full, we can just insert it directly.
|
|
|
|
if (!isFull()) {
|
|
|
|
// Now that we have two nodes and a new element, insert the perclated value
|
|
|
|
// into ourself by moving all the later values/children down, then inserting
|
|
|
|
// the new one.
|
2008-04-13 06:00:40 +08:00
|
|
|
if (i != e)
|
2008-04-13 16:52:45 +08:00
|
|
|
memmove(&IN->Children[i+2], &IN->Children[i+1],
|
|
|
|
(e-i)*sizeof(IN->Children[0]));
|
|
|
|
IN->Children[i] = InsertRes->LHS;
|
|
|
|
IN->Children[i+1] = InsertRes->RHS;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:52:45 +08:00
|
|
|
if (e != i)
|
|
|
|
memmove(&Values[i+1], &Values[i], (e-i)*sizeof(Values[0]));
|
|
|
|
Values[i] = InsertRes->Split;
|
2008-04-13 06:00:40 +08:00
|
|
|
++NumValuesUsed;
|
2008-04-13 16:52:45 +08:00
|
|
|
return false;
|
2008-04-13 06:00:40 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:52:45 +08:00
|
|
|
// Finally, if this interior node was full and a node is percolated up, split
|
|
|
|
// ourself and return that up the chain. Start by saving all our info to
|
|
|
|
// avoid having the split clobber it.
|
|
|
|
IN->Children[i] = InsertRes->LHS;
|
|
|
|
DeltaTreeNode *SubRHS = InsertRes->RHS;
|
|
|
|
SourceDelta SubSplit = InsertRes->Split;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:52:45 +08:00
|
|
|
// Do the split.
|
|
|
|
DoSplit(*InsertRes);
|
2008-04-13 06:00:40 +08:00
|
|
|
|
2008-04-13 16:52:45 +08:00
|
|
|
// Figure out where to insert SubRHS/NewSplit.
|
|
|
|
DeltaTreeInteriorNode *InsertSide;
|
|
|
|
if (SubSplit.FileLoc < InsertRes->Split.FileLoc)
|
|
|
|
InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->LHS);
|
|
|
|
else
|
|
|
|
InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->RHS);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// We now have a non-empty interior node 'InsertSide' to insert
|
2008-04-13 16:52:45 +08:00
|
|
|
// SubRHS/SubSplit into. Find out where to insert SubSplit.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:52:45 +08:00
|
|
|
// Find the insertion point, the first delta whose index is >SubSplit.FileLoc.
|
|
|
|
i = 0; e = InsertSide->getNumValuesUsed();
|
|
|
|
while (i != e && SubSplit.FileLoc > InsertSide->getValue(i).FileLoc)
|
|
|
|
++i;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:52:45 +08:00
|
|
|
// Now we know that i is the place to insert the split value into. Insert it
|
|
|
|
// and the child right after it.
|
|
|
|
if (i != e)
|
|
|
|
memmove(&InsertSide->Children[i+2], &InsertSide->Children[i+1],
|
|
|
|
(e-i)*sizeof(IN->Children[0]));
|
|
|
|
InsertSide->Children[i+1] = SubRHS;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:52:45 +08:00
|
|
|
if (e != i)
|
|
|
|
memmove(&InsertSide->Values[i+1], &InsertSide->Values[i],
|
|
|
|
(e-i)*sizeof(Values[0]));
|
|
|
|
InsertSide->Values[i] = SubSplit;
|
|
|
|
++InsertSide->NumValuesUsed;
|
|
|
|
InsertSide->FullDelta += SubSplit.Delta + SubRHS->getFullDelta();
|
|
|
|
return true;
|
2008-04-13 06:00:40 +08:00
|
|
|
}
|
|
|
|
|
2008-04-13 16:52:45 +08:00
|
|
|
/// DoSplit - Split the currently full node (which has 2*WidthFactor-1 values)
|
|
|
|
/// into two subtrees each with "WidthFactor-1" values and a pivot value.
|
|
|
|
/// Return the pieces in InsertRes.
|
2008-04-13 16:22:30 +08:00
|
|
|
void DeltaTreeNode::DoSplit(InsertResult &InsertRes) {
|
|
|
|
assert(isFull() && "Why split a non-full node?");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:22:30 +08:00
|
|
|
// Since this node is full, it contains 2*WidthFactor-1 values. We move
|
|
|
|
// the first 'WidthFactor-1' values to the LHS child (which we leave in this
|
|
|
|
// node), propagate one value up, and move the last 'WidthFactor-1' values
|
|
|
|
// into the RHS child.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:22:30 +08:00
|
|
|
// Create the new child node.
|
|
|
|
DeltaTreeNode *NewNode;
|
|
|
|
if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this)) {
|
|
|
|
// If this is an interior node, also move over 'WidthFactor' children
|
|
|
|
// into the new node.
|
|
|
|
DeltaTreeInteriorNode *New = new DeltaTreeInteriorNode();
|
|
|
|
memcpy(&New->Children[0], &IN->Children[WidthFactor],
|
|
|
|
WidthFactor*sizeof(IN->Children[0]));
|
|
|
|
NewNode = New;
|
|
|
|
} else {
|
|
|
|
// Just create the new leaf node.
|
|
|
|
NewNode = new DeltaTreeNode();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:22:30 +08:00
|
|
|
// Move over the last 'WidthFactor-1' values from here to NewNode.
|
|
|
|
memcpy(&NewNode->Values[0], &Values[WidthFactor],
|
|
|
|
(WidthFactor-1)*sizeof(Values[0]));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:22:30 +08:00
|
|
|
// Decrease the number of values in the two nodes.
|
|
|
|
NewNode->NumValuesUsed = NumValuesUsed = WidthFactor-1;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:22:30 +08:00
|
|
|
// Recompute the two nodes' full delta.
|
|
|
|
NewNode->RecomputeFullDeltaLocally();
|
|
|
|
RecomputeFullDeltaLocally();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 16:22:30 +08:00
|
|
|
InsertRes.LHS = this;
|
|
|
|
InsertRes.RHS = NewNode;
|
|
|
|
InsertRes.Split = Values[WidthFactor-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DeltaTree Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-13 06:04:18 +08:00
|
|
|
//#define VERIFY_TREE
|
2008-04-13 06:00:40 +08:00
|
|
|
|
2008-04-13 06:04:18 +08:00
|
|
|
#ifdef VERIFY_TREE
|
2008-04-13 06:00:40 +08:00
|
|
|
/// VerifyTree - Walk the btree performing assertions on various properties to
|
|
|
|
/// verify consistency. This is useful for debugging new changes to the tree.
|
|
|
|
static void VerifyTree(const DeltaTreeNode *N) {
|
|
|
|
const DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(N);
|
|
|
|
if (IN == 0) {
|
|
|
|
// Verify leaves, just ensure that FullDelta matches up and the elements
|
|
|
|
// are in proper order.
|
|
|
|
int FullDelta = 0;
|
|
|
|
for (unsigned i = 0, e = N->getNumValuesUsed(); i != e; ++i) {
|
|
|
|
if (i)
|
|
|
|
assert(N->getValue(i-1).FileLoc < N->getValue(i).FileLoc);
|
|
|
|
FullDelta += N->getValue(i).Delta;
|
|
|
|
}
|
|
|
|
assert(FullDelta == N->getFullDelta());
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
// Verify interior nodes: Ensure that FullDelta matches up and the
|
|
|
|
// elements are in proper order and the children are in proper order.
|
|
|
|
int FullDelta = 0;
|
|
|
|
for (unsigned i = 0, e = IN->getNumValuesUsed(); i != e; ++i) {
|
|
|
|
const SourceDelta &IVal = N->getValue(i);
|
|
|
|
const DeltaTreeNode *IChild = IN->getChild(i);
|
|
|
|
if (i)
|
|
|
|
assert(IN->getValue(i-1).FileLoc < IVal.FileLoc);
|
|
|
|
FullDelta += IVal.Delta;
|
|
|
|
FullDelta += IChild->getFullDelta();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
// The largest value in child #i should be smaller than FileLoc.
|
|
|
|
assert(IChild->getValue(IChild->getNumValuesUsed()-1).FileLoc <
|
|
|
|
IVal.FileLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
// The smallest value in child #i+1 should be larger than FileLoc.
|
|
|
|
assert(IN->getChild(i+1)->getValue(0).FileLoc > IVal.FileLoc);
|
|
|
|
VerifyTree(IChild);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
FullDelta += IN->getChild(IN->getNumValuesUsed())->getFullDelta();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
assert(FullDelta == N->getFullDelta());
|
|
|
|
}
|
2008-04-13 06:04:18 +08:00
|
|
|
#endif // VERIFY_TREE
|
2008-04-13 06:00:40 +08:00
|
|
|
|
|
|
|
static DeltaTreeNode *getRoot(void *Root) {
|
|
|
|
return (DeltaTreeNode*)Root;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeltaTree::DeltaTree() {
|
|
|
|
Root = new DeltaTreeNode();
|
|
|
|
}
|
|
|
|
DeltaTree::DeltaTree(const DeltaTree &RHS) {
|
|
|
|
// Currently we only support copying when the RHS is empty.
|
|
|
|
assert(getRoot(RHS.Root)->getNumValuesUsed() == 0 &&
|
|
|
|
"Can only copy empty tree");
|
|
|
|
Root = new DeltaTreeNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
DeltaTree::~DeltaTree() {
|
|
|
|
getRoot(Root)->Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getDeltaAt - Return the accumulated delta at the specified file offset.
|
|
|
|
/// This includes all insertions or delections that occurred *before* the
|
|
|
|
/// specified file index.
|
|
|
|
int DeltaTree::getDeltaAt(unsigned FileIndex) const {
|
|
|
|
const DeltaTreeNode *Node = getRoot(Root);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
int Result = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
// Walk down the tree.
|
|
|
|
while (1) {
|
|
|
|
// For all nodes, include any local deltas before the specified file
|
|
|
|
// index by summing them up directly. Keep track of how many were
|
|
|
|
// included.
|
|
|
|
unsigned NumValsGreater = 0;
|
|
|
|
for (unsigned e = Node->getNumValuesUsed(); NumValsGreater != e;
|
|
|
|
++NumValsGreater) {
|
|
|
|
const SourceDelta &Val = Node->getValue(NumValsGreater);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
if (Val.FileLoc >= FileIndex)
|
|
|
|
break;
|
|
|
|
Result += Val.Delta;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
// If we have an interior node, include information about children and
|
|
|
|
// recurse. Otherwise, if we have a leaf, we're done.
|
|
|
|
const DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(Node);
|
|
|
|
if (!IN) return Result;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
// Include any children to the left of the values we skipped, all of
|
|
|
|
// their deltas should be included as well.
|
|
|
|
for (unsigned i = 0; i != NumValsGreater; ++i)
|
|
|
|
Result += IN->getChild(i)->getFullDelta();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
// If we found exactly the value we were looking for, break off the
|
|
|
|
// search early. There is no need to search the RHS of the value for
|
|
|
|
// partial results.
|
|
|
|
if (NumValsGreater != Node->getNumValuesUsed() &&
|
|
|
|
Node->getValue(NumValsGreater).FileLoc == FileIndex)
|
2008-04-15 10:26:21 +08:00
|
|
|
return Result+IN->getChild(NumValsGreater)->getFullDelta();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:00:40 +08:00
|
|
|
// Otherwise, traverse down the tree. The selected subtree may be
|
|
|
|
// partially included in the range.
|
|
|
|
Node = IN->getChild(NumValsGreater);
|
|
|
|
}
|
|
|
|
// NOT REACHED.
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AddDelta - When a change is made that shifts around the text buffer,
|
|
|
|
/// this method is used to record that info. It inserts a delta of 'Delta'
|
|
|
|
/// into the current DeltaTree at offset FileIndex.
|
|
|
|
void DeltaTree::AddDelta(unsigned FileIndex, int Delta) {
|
|
|
|
assert(Delta && "Adding a noop?");
|
2008-04-13 16:22:30 +08:00
|
|
|
DeltaTreeNode *MyRoot = getRoot(Root);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-17 14:52:37 +08:00
|
|
|
DeltaTreeNode::InsertResult InsertRes;
|
2008-04-13 16:52:45 +08:00
|
|
|
if (MyRoot->DoInsertion(FileIndex, Delta, &InsertRes)) {
|
|
|
|
Root = MyRoot = new DeltaTreeInteriorNode(InsertRes);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-13 06:04:18 +08:00
|
|
|
#ifdef VERIFY_TREE
|
2008-04-13 16:22:30 +08:00
|
|
|
VerifyTree(MyRoot);
|
2008-04-13 06:04:18 +08:00
|
|
|
#endif
|
2008-04-13 06:00:40 +08:00
|
|
|
}
|
|
|
|
|