#include "clang/Rewrite/Core/RewriteRope.h"
#include "clang/Basic/LLVM.h"
#include "llvm/Support/Casting.h"
#include <algorithm>
#include <cassert>
#include <cstring>
using namespace clang;
namespace {
class RopePieceBTreeNode {
protected:
enum { WidthFactor = 8 };
unsigned Size = 0;
bool IsLeaf;
RopePieceBTreeNode(bool isLeaf) : IsLeaf(isLeaf) {}
~RopePieceBTreeNode() = default;
public:
bool isLeaf() const { return IsLeaf; }
unsigned size() const { return Size; }
void Destroy();
RopePieceBTreeNode *split(unsigned Offset);
RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R);
void erase(unsigned Offset, unsigned NumBytes);
};
class RopePieceBTreeLeaf : public RopePieceBTreeNode {
unsigned char NumPieces = 0;
RopePiece Pieces[2*WidthFactor];
RopePieceBTreeLeaf **PrevLeaf = nullptr;
RopePieceBTreeLeaf *NextLeaf = nullptr;
public:
RopePieceBTreeLeaf() : RopePieceBTreeNode(true) {}
~RopePieceBTreeLeaf() {
if (PrevLeaf || NextLeaf)
removeFromLeafInOrder();
clear();
}
bool isFull() const { return NumPieces == 2*WidthFactor; }
void clear() {
while (NumPieces)
Pieces[--NumPieces] = RopePiece();
Size = 0;
}
unsigned getNumPieces() const { return NumPieces; }
const RopePiece &getPiece(unsigned i) const {
assert(i < getNumPieces() && "Invalid piece ID");
return Pieces[i];
}
const RopePieceBTreeLeaf *getNextLeafInOrder() const { return NextLeaf; }
void insertAfterLeafInOrder(RopePieceBTreeLeaf *Node) {
assert(!PrevLeaf && !NextLeaf && "Already in ordering");
NextLeaf = Node->NextLeaf;
if (NextLeaf)
NextLeaf->PrevLeaf = &NextLeaf;
PrevLeaf = &Node->NextLeaf;
Node->NextLeaf = this;
}
void removeFromLeafInOrder() {
if (PrevLeaf) {
*PrevLeaf = NextLeaf;
if (NextLeaf)
NextLeaf->PrevLeaf = PrevLeaf;
} else if (NextLeaf) {
NextLeaf->PrevLeaf = nullptr;
}
}
void FullRecomputeSizeLocally() {
Size = 0;
for (unsigned i = 0, e = getNumPieces(); i != e; ++i)
Size += getPiece(i).size();
}
RopePieceBTreeNode *split(unsigned Offset);
RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R);
void erase(unsigned Offset, unsigned NumBytes);
static bool classof(const RopePieceBTreeNode *N) {
return N->isLeaf();
}
};
}
RopePieceBTreeNode *RopePieceBTreeLeaf::split(unsigned Offset) {
if (Offset == 0 || Offset == size()) {
return nullptr;
}
unsigned PieceOffs = 0;
unsigned i = 0;
while (Offset >= PieceOffs+Pieces[i].size()) {
PieceOffs += Pieces[i].size();
++i;
}
if (PieceOffs == Offset)
return nullptr;
unsigned IntraPieceOffset = Offset-PieceOffs;
RopePiece Tail(Pieces[i].StrData, Pieces[i].StartOffs+IntraPieceOffset,
Pieces[i].EndOffs);
Size -= Pieces[i].size();
Pieces[i].EndOffs = Pieces[i].StartOffs+IntraPieceOffset;
Size += Pieces[i].size();
return insert(Offset, Tail);
}
RopePieceBTreeNode *RopePieceBTreeLeaf::insert(unsigned Offset,
const RopePiece &R) {
if (!isFull()) {
unsigned i = 0, e = getNumPieces();
if (Offset == size()) {
i = e;
} else {
unsigned SlotOffs = 0;
for (; Offset > SlotOffs; ++i)
SlotOffs += getPiece(i).size();
assert(SlotOffs == Offset && "Split didn't occur before insertion!");
}
for (; i != e; --e)
Pieces[e] = Pieces[e-1];
Pieces[i] = R;
++NumPieces;
Size += R.size();
return nullptr;
}
RopePieceBTreeLeaf *NewNode = new RopePieceBTreeLeaf();
std::copy(&Pieces[WidthFactor], &Pieces[2*WidthFactor],
&NewNode->Pieces[0]);
std::fill(&Pieces[WidthFactor], &Pieces[2*WidthFactor], RopePiece());
NewNode->NumPieces = NumPieces = WidthFactor;
NewNode->FullRecomputeSizeLocally();
FullRecomputeSizeLocally();
NewNode->insertAfterLeafInOrder(this);
if (this->size() >= Offset)
this->insert(Offset, R);
else
NewNode->insert(Offset - this->size(), R);
return NewNode;
}
void RopePieceBTreeLeaf::erase(unsigned Offset, unsigned NumBytes) {
unsigned PieceOffs = 0;
unsigned i = 0;
for (; Offset > PieceOffs; ++i)
PieceOffs += getPiece(i).size();
assert(PieceOffs == Offset && "Split didn't occur before erase!");
unsigned StartPiece = i;
for (; Offset+NumBytes > PieceOffs+getPiece(i).size(); ++i)
PieceOffs += getPiece(i).size();
if (Offset+NumBytes == PieceOffs+getPiece(i).size()) {
PieceOffs += getPiece(i).size();
++i;
}
if (i != StartPiece) {
unsigned NumDeleted = i-StartPiece;
for (; i != getNumPieces(); ++i)
Pieces[i-NumDeleted] = Pieces[i];
std::fill(&Pieces[getNumPieces()-NumDeleted], &Pieces[getNumPieces()],
RopePiece());
NumPieces -= NumDeleted;
unsigned CoverBytes = PieceOffs-Offset;
NumBytes -= CoverBytes;
Size -= CoverBytes;
}
if (NumBytes == 0) return;
assert(getPiece(StartPiece).size() > NumBytes);
Pieces[StartPiece].StartOffs += NumBytes;
Size -= NumBytes;
}
namespace {
class RopePieceBTreeInterior : public RopePieceBTreeNode {
unsigned char NumChildren = 0;
RopePieceBTreeNode *Children[2*WidthFactor];
public:
RopePieceBTreeInterior() : RopePieceBTreeNode(false) {}
RopePieceBTreeInterior(RopePieceBTreeNode *LHS, RopePieceBTreeNode *RHS)
: RopePieceBTreeNode(false) {
Children[0] = LHS;
Children[1] = RHS;
NumChildren = 2;
Size = LHS->size() + RHS->size();
}
~RopePieceBTreeInterior() {
for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
Children[i]->Destroy();
}
bool isFull() const { return NumChildren == 2*WidthFactor; }
unsigned getNumChildren() const { return NumChildren; }
const RopePieceBTreeNode *getChild(unsigned i) const {
assert(i < NumChildren && "invalid child #");
return Children[i];
}
RopePieceBTreeNode *getChild(unsigned i) {
assert(i < NumChildren && "invalid child #");
return Children[i];
}
void FullRecomputeSizeLocally() {
Size = 0;
for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
Size += getChild(i)->size();
}
RopePieceBTreeNode *split(unsigned Offset);
RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R);
RopePieceBTreeNode *HandleChildPiece(unsigned i, RopePieceBTreeNode *RHS);
void erase(unsigned Offset, unsigned NumBytes);
static bool classof(const RopePieceBTreeNode *N) {
return !N->isLeaf();
}
};
}
RopePieceBTreeNode *RopePieceBTreeInterior::split(unsigned Offset) {
if (Offset == 0 || Offset == size())
return nullptr;
unsigned ChildOffset = 0;
unsigned i = 0;
for (; Offset >= ChildOffset+getChild(i)->size(); ++i)
ChildOffset += getChild(i)->size();
if (ChildOffset == Offset)
return nullptr;
if (RopePieceBTreeNode *RHS = getChild(i)->split(Offset-ChildOffset))
return HandleChildPiece(i, RHS);
return nullptr; }
RopePieceBTreeNode *RopePieceBTreeInterior::insert(unsigned Offset,
const RopePiece &R) {
unsigned i = 0, e = getNumChildren();
unsigned ChildOffs = 0;
if (Offset == size()) {
i = e-1;
ChildOffs = size()-getChild(i)->size();
} else {
for (; Offset > ChildOffs+getChild(i)->size(); ++i)
ChildOffs += getChild(i)->size();
}
Size += R.size();
if (RopePieceBTreeNode *RHS = getChild(i)->insert(Offset-ChildOffs, R))
return HandleChildPiece(i, RHS);
return nullptr;
}
RopePieceBTreeNode *
RopePieceBTreeInterior::HandleChildPiece(unsigned i, RopePieceBTreeNode *RHS) {
if (!isFull()) {
if (i + 1 != getNumChildren())
memmove(&Children[i+2], &Children[i+1],
(getNumChildren()-i-1)*sizeof(Children[0]));
Children[i+1] = RHS;
++NumChildren;
return nullptr;
}
RopePieceBTreeInterior *NewNode = new RopePieceBTreeInterior();
memcpy(&NewNode->Children[0], &Children[WidthFactor],
WidthFactor*sizeof(Children[0]));
NewNode->NumChildren = NumChildren = WidthFactor;
if (i < WidthFactor)
this->HandleChildPiece(i, RHS);
else
NewNode->HandleChildPiece(i-WidthFactor, RHS);
NewNode->FullRecomputeSizeLocally();
FullRecomputeSizeLocally();
return NewNode;
}
void RopePieceBTreeInterior::erase(unsigned Offset, unsigned NumBytes) {
Size -= NumBytes;
unsigned i = 0;
for (; Offset >= getChild(i)->size(); ++i)
Offset -= getChild(i)->size();
while (NumBytes) {
RopePieceBTreeNode *CurChild = getChild(i);
if (Offset+NumBytes < CurChild->size()) {
CurChild->erase(Offset, NumBytes);
return;
}
if (Offset) {
unsigned BytesFromChild = CurChild->size()-Offset;
CurChild->erase(Offset, BytesFromChild);
NumBytes -= BytesFromChild;
Offset = 0;
++i;
continue;
}
NumBytes -= CurChild->size();
CurChild->Destroy();
--NumChildren;
if (i != getNumChildren())
memmove(&Children[i], &Children[i+1],
(getNumChildren()-i)*sizeof(Children[0]));
}
}
void RopePieceBTreeNode::Destroy() {
if (auto *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
delete Leaf;
else
delete cast<RopePieceBTreeInterior>(this);
}
RopePieceBTreeNode *RopePieceBTreeNode::split(unsigned Offset) {
assert(Offset <= size() && "Invalid offset to split!");
if (auto *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
return Leaf->split(Offset);
return cast<RopePieceBTreeInterior>(this)->split(Offset);
}
RopePieceBTreeNode *RopePieceBTreeNode::insert(unsigned Offset,
const RopePiece &R) {
assert(Offset <= size() && "Invalid offset to insert!");
if (auto *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
return Leaf->insert(Offset, R);
return cast<RopePieceBTreeInterior>(this)->insert(Offset, R);
}
void RopePieceBTreeNode::erase(unsigned Offset, unsigned NumBytes) {
assert(Offset+NumBytes <= size() && "Invalid offset to erase!");
if (auto *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
return Leaf->erase(Offset, NumBytes);
return cast<RopePieceBTreeInterior>(this)->erase(Offset, NumBytes);
}
static const RopePieceBTreeLeaf *getCN(const void *P) {
return static_cast<const RopePieceBTreeLeaf*>(P);
}
RopePieceBTreeIterator::RopePieceBTreeIterator(const void *n) {
const auto *N = static_cast<const RopePieceBTreeNode *>(n);
while (const auto *IN = dyn_cast<RopePieceBTreeInterior>(N))
N = IN->getChild(0);
CurNode = cast<RopePieceBTreeLeaf>(N);
while (CurNode && getCN(CurNode)->getNumPieces() == 0)
CurNode = getCN(CurNode)->getNextLeafInOrder();
if (CurNode)
CurPiece = &getCN(CurNode)->getPiece(0);
else CurPiece = nullptr;
CurChar = 0;
}
void RopePieceBTreeIterator::MoveToNextPiece() {
if (CurPiece != &getCN(CurNode)->getPiece(getCN(CurNode)->getNumPieces()-1)) {
CurChar = 0;
++CurPiece;
return;
}
do
CurNode = getCN(CurNode)->getNextLeafInOrder();
while (CurNode && getCN(CurNode)->getNumPieces() == 0);
if (CurNode)
CurPiece = &getCN(CurNode)->getPiece(0);
else CurPiece = nullptr;
CurChar = 0;
}
static RopePieceBTreeNode *getRoot(void *P) {
return static_cast<RopePieceBTreeNode*>(P);
}
RopePieceBTree::RopePieceBTree() {
Root = new RopePieceBTreeLeaf();
}
RopePieceBTree::RopePieceBTree(const RopePieceBTree &RHS) {
assert(RHS.empty() && "Can't copy non-empty tree yet");
Root = new RopePieceBTreeLeaf();
}
RopePieceBTree::~RopePieceBTree() {
getRoot(Root)->Destroy();
}
unsigned RopePieceBTree::size() const {
return getRoot(Root)->size();
}
void RopePieceBTree::clear() {
if (auto *Leaf = dyn_cast<RopePieceBTreeLeaf>(getRoot(Root)))
Leaf->clear();
else {
getRoot(Root)->Destroy();
Root = new RopePieceBTreeLeaf();
}
}
void RopePieceBTree::insert(unsigned Offset, const RopePiece &R) {
if (RopePieceBTreeNode *RHS = getRoot(Root)->split(Offset))
Root = new RopePieceBTreeInterior(getRoot(Root), RHS);
if (RopePieceBTreeNode *RHS = getRoot(Root)->insert(Offset, R))
Root = new RopePieceBTreeInterior(getRoot(Root), RHS);
}
void RopePieceBTree::erase(unsigned Offset, unsigned NumBytes) {
if (RopePieceBTreeNode *RHS = getRoot(Root)->split(Offset))
Root = new RopePieceBTreeInterior(getRoot(Root), RHS);
getRoot(Root)->erase(Offset, NumBytes);
}
RopePiece RewriteRope::MakeRopeString(const char *Start, const char *End) {
unsigned Len = End-Start;
assert(Len && "Zero length RopePiece is invalid!");
if (AllocOffs+Len <= AllocChunkSize) {
memcpy(AllocBuffer->Data+AllocOffs, Start, Len);
AllocOffs += Len;
return RopePiece(AllocBuffer, AllocOffs-Len, AllocOffs);
}
if (Len > AllocChunkSize) {
unsigned Size = End-Start+sizeof(RopeRefCountString)-1;
auto *Res = reinterpret_cast<RopeRefCountString *>(new char[Size]);
Res->RefCount = 0;
memcpy(Res->Data, Start, End-Start);
return RopePiece(Res, 0, End-Start);
}
unsigned AllocSize = offsetof(RopeRefCountString, Data) + AllocChunkSize;
auto *Res = reinterpret_cast<RopeRefCountString *>(new char[AllocSize]);
Res->RefCount = 0;
memcpy(Res->Data, Start, Len);
AllocBuffer = Res;
AllocOffs = Len;
return RopePiece(AllocBuffer, 0, Len);
}