#include "clang/Rewrite/Core/DeltaTree.h"
#include "clang/Basic/LLVM.h"
#include "llvm/Support/Casting.h"
#include <cassert>
#include <cstring>
using namespace clang;
namespace {
struct SourceDelta {
unsigned FileLoc;
int Delta;
static SourceDelta get(unsigned Loc, int D) {
SourceDelta Delta;
Delta.FileLoc = Loc;
Delta.Delta = D;
return Delta;
}
};
class DeltaTreeNode {
public:
struct InsertResult {
DeltaTreeNode *LHS, *RHS;
SourceDelta Split;
};
private:
friend class DeltaTreeInteriorNode;
enum { WidthFactor = 8 };
SourceDelta Values[2*WidthFactor-1];
unsigned char NumValuesUsed = 0;
bool IsLeaf;
int FullDelta = 0;
public:
DeltaTreeNode(bool isLeaf = true) : IsLeaf(isLeaf) {}
bool isLeaf() const { return IsLeaf; }
int getFullDelta() const { return FullDelta; }
bool isFull() const { return NumValuesUsed == 2*WidthFactor-1; }
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];
}
bool DoInsertion(unsigned FileIndex, int Delta, InsertResult *InsertRes);
void DoSplit(InsertResult &InsertRes);
void RecomputeFullDeltaLocally();
void Destroy();
};
class DeltaTreeInteriorNode : public DeltaTreeNode {
friend class DeltaTreeNode;
DeltaTreeNode *Children[2*WidthFactor];
~DeltaTreeInteriorNode() {
for (unsigned i = 0, e = NumValuesUsed+1; i != e; ++i)
Children[i]->Destroy();
}
public:
DeltaTreeInteriorNode() : DeltaTreeNode(false ) {}
DeltaTreeInteriorNode(const InsertResult &IR)
: DeltaTreeNode(false ) {
Children[0] = IR.LHS;
Children[1] = IR.RHS;
Values[0] = IR.Split;
FullDelta = IR.LHS->getFullDelta()+IR.RHS->getFullDelta()+IR.Split.Delta;
NumValuesUsed = 1;
}
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];
}
static bool classof(const DeltaTreeNode *N) { return !N->isLeaf(); }
};
}
void DeltaTreeNode::Destroy() {
if (isLeaf())
delete this;
else
delete cast<DeltaTreeInteriorNode>(this);
}
void DeltaTreeNode::RecomputeFullDeltaLocally() {
int NewFullDelta = 0;
for (unsigned i = 0, e = getNumValuesUsed(); i != e; ++i)
NewFullDelta += Values[i].Delta;
if (auto *IN = dyn_cast<DeltaTreeInteriorNode>(this))
for (unsigned i = 0, e = getNumValuesUsed()+1; i != e; ++i)
NewFullDelta += IN->getChild(i)->getFullDelta();
FullDelta = NewFullDelta;
}
bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta,
InsertResult *InsertRes) {
FullDelta += Delta;
unsigned i = 0, e = getNumValuesUsed();
while (i != e && FileIndex > getValue(i).FileLoc)
++i;
if (i != e && getValue(i).FileLoc == FileIndex) {
Values[i].Delta += Delta;
return false;
}
if (isLeaf()) {
if (!isFull()) {
if (i != e)
memmove(&Values[i+1], &Values[i], sizeof(Values[0])*(e-i));
Values[i] = SourceDelta::get(FileIndex, Delta);
++NumValuesUsed;
return false;
}
assert(InsertRes && "No result location specified");
DoSplit(*InsertRes);
if (InsertRes->Split.FileLoc > FileIndex)
InsertRes->LHS->DoInsertion(FileIndex, Delta, nullptr );
else
InsertRes->RHS->DoInsertion(FileIndex, Delta, nullptr );
return true;
}
auto *IN = cast<DeltaTreeInteriorNode>(this);
if (!IN->Children[i]->DoInsertion(FileIndex, Delta, InsertRes))
return false;
if (!isFull()) {
if (i != e)
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;
if (e != i)
memmove(&Values[i+1], &Values[i], (e-i)*sizeof(Values[0]));
Values[i] = InsertRes->Split;
++NumValuesUsed;
return false;
}
IN->Children[i] = InsertRes->LHS;
DeltaTreeNode *SubRHS = InsertRes->RHS;
SourceDelta SubSplit = InsertRes->Split;
DoSplit(*InsertRes);
DeltaTreeInteriorNode *InsertSide;
if (SubSplit.FileLoc < InsertRes->Split.FileLoc)
InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->LHS);
else
InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->RHS);
i = 0; e = InsertSide->getNumValuesUsed();
while (i != e && SubSplit.FileLoc > InsertSide->getValue(i).FileLoc)
++i;
if (i != e)
memmove(&InsertSide->Children[i+2], &InsertSide->Children[i+1],
(e-i)*sizeof(IN->Children[0]));
InsertSide->Children[i+1] = SubRHS;
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;
}
void DeltaTreeNode::DoSplit(InsertResult &InsertRes) {
assert(isFull() && "Why split a non-full node?");
DeltaTreeNode *NewNode;
if (auto *IN = dyn_cast<DeltaTreeInteriorNode>(this)) {
DeltaTreeInteriorNode *New = new DeltaTreeInteriorNode();
memcpy(&New->Children[0], &IN->Children[WidthFactor],
WidthFactor*sizeof(IN->Children[0]));
NewNode = New;
} else {
NewNode = new DeltaTreeNode();
}
memcpy(&NewNode->Values[0], &Values[WidthFactor],
(WidthFactor-1)*sizeof(Values[0]));
NewNode->NumValuesUsed = NumValuesUsed = WidthFactor-1;
NewNode->RecomputeFullDeltaLocally();
RecomputeFullDeltaLocally();
InsertRes.LHS = this;
InsertRes.RHS = NewNode;
InsertRes.Split = Values[WidthFactor-1];
}
#ifdef VERIFY_TREE
static void VerifyTree(const DeltaTreeNode *N) {
const auto *IN = dyn_cast<DeltaTreeInteriorNode>(N);
if (IN == 0) {
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;
}
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();
assert(IChild->getValue(IChild->getNumValuesUsed()-1).FileLoc <
IVal.FileLoc);
assert(IN->getChild(i+1)->getValue(0).FileLoc > IVal.FileLoc);
VerifyTree(IChild);
}
FullDelta += IN->getChild(IN->getNumValuesUsed())->getFullDelta();
assert(FullDelta == N->getFullDelta());
}
#endif
static DeltaTreeNode *getRoot(void *Root) {
return (DeltaTreeNode*)Root;
}
DeltaTree::DeltaTree() {
Root = new DeltaTreeNode();
}
DeltaTree::DeltaTree(const DeltaTree &RHS) {
assert(getRoot(RHS.Root)->getNumValuesUsed() == 0 &&
"Can only copy empty tree");
Root = new DeltaTreeNode();
}
DeltaTree::~DeltaTree() {
getRoot(Root)->Destroy();
}
int DeltaTree::getDeltaAt(unsigned FileIndex) const {
const DeltaTreeNode *Node = getRoot(Root);
int Result = 0;
while (true) {
unsigned NumValsGreater = 0;
for (unsigned e = Node->getNumValuesUsed(); NumValsGreater != e;
++NumValsGreater) {
const SourceDelta &Val = Node->getValue(NumValsGreater);
if (Val.FileLoc >= FileIndex)
break;
Result += Val.Delta;
}
const auto *IN = dyn_cast<DeltaTreeInteriorNode>(Node);
if (!IN) return Result;
for (unsigned i = 0; i != NumValsGreater; ++i)
Result += IN->getChild(i)->getFullDelta();
if (NumValsGreater != Node->getNumValuesUsed() &&
Node->getValue(NumValsGreater).FileLoc == FileIndex)
return Result+IN->getChild(NumValsGreater)->getFullDelta();
Node = IN->getChild(NumValsGreater);
}
}
void DeltaTree::AddDelta(unsigned FileIndex, int Delta) {
assert(Delta && "Adding a noop?");
DeltaTreeNode *MyRoot = getRoot(Root);
DeltaTreeNode::InsertResult InsertRes;
if (MyRoot->DoInsertion(FileIndex, Delta, &InsertRes)) {
Root = new DeltaTreeInteriorNode(InsertRes);
#ifdef VERIFY_TREE
MyRoot = Root;
#endif
}
#ifdef VERIFY_TREE
VerifyTree(MyRoot);
#endif
}