1*0b57cec5SDimitry Andric //===- RewriteRope.cpp - Rope specialized for rewriter --------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements the RewriteRope class, which is a powerful string.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric
13*0b57cec5SDimitry Andric #include "clang/Rewrite/Core/RewriteRope.h"
14*0b57cec5SDimitry Andric #include "clang/Basic/LLVM.h"
15*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
16*0b57cec5SDimitry Andric #include <algorithm>
17*0b57cec5SDimitry Andric #include <cassert>
18*0b57cec5SDimitry Andric #include <cstring>
19*0b57cec5SDimitry Andric
20*0b57cec5SDimitry Andric using namespace clang;
21*0b57cec5SDimitry Andric
22*0b57cec5SDimitry Andric /// RewriteRope is a "strong" string class, designed to make insertions and
23*0b57cec5SDimitry Andric /// deletions in the middle of the string nearly constant time (really, they are
24*0b57cec5SDimitry Andric /// O(log N), but with a very low constant factor).
25*0b57cec5SDimitry Andric ///
26*0b57cec5SDimitry Andric /// The implementation of this datastructure is a conceptual linear sequence of
27*0b57cec5SDimitry Andric /// RopePiece elements. Each RopePiece represents a view on a separately
28*0b57cec5SDimitry Andric /// allocated and reference counted string. This means that splitting a very
29*0b57cec5SDimitry Andric /// long string can be done in constant time by splitting a RopePiece that
30*0b57cec5SDimitry Andric /// references the whole string into two rope pieces that reference each half.
31*0b57cec5SDimitry Andric /// Once split, another string can be inserted in between the two halves by
32*0b57cec5SDimitry Andric /// inserting a RopePiece in between the two others. All of this is very
33*0b57cec5SDimitry Andric /// inexpensive: it takes time proportional to the number of RopePieces, not the
34*0b57cec5SDimitry Andric /// length of the strings they represent.
35*0b57cec5SDimitry Andric ///
36*0b57cec5SDimitry Andric /// While a linear sequences of RopePieces is the conceptual model, the actual
37*0b57cec5SDimitry Andric /// implementation captures them in an adapted B+ Tree. Using a B+ tree (which
38*0b57cec5SDimitry Andric /// is a tree that keeps the values in the leaves and has where each node
39*0b57cec5SDimitry Andric /// contains a reasonable number of pointers to children/values) allows us to
40*0b57cec5SDimitry Andric /// maintain efficient operation when the RewriteRope contains a *huge* number
41*0b57cec5SDimitry Andric /// of RopePieces. The basic idea of the B+ Tree is that it allows us to find
42*0b57cec5SDimitry Andric /// the RopePiece corresponding to some offset very efficiently, and it
43*0b57cec5SDimitry Andric /// automatically balances itself on insertions of RopePieces (which can happen
44*0b57cec5SDimitry Andric /// for both insertions and erases of string ranges).
45*0b57cec5SDimitry Andric ///
46*0b57cec5SDimitry Andric /// The one wrinkle on the theory is that we don't attempt to keep the tree
47*0b57cec5SDimitry Andric /// properly balanced when erases happen. Erases of string data can both insert
48*0b57cec5SDimitry Andric /// new RopePieces (e.g. when the middle of some other rope piece is deleted,
49*0b57cec5SDimitry Andric /// which results in two rope pieces, which is just like an insert) or it can
50*0b57cec5SDimitry Andric /// reduce the number of RopePieces maintained by the B+Tree. In the case when
51*0b57cec5SDimitry Andric /// the number of RopePieces is reduced, we don't attempt to maintain the
52*0b57cec5SDimitry Andric /// standard 'invariant' that each node in the tree contains at least
53*0b57cec5SDimitry Andric /// 'WidthFactor' children/values. For our use cases, this doesn't seem to
54*0b57cec5SDimitry Andric /// matter.
55*0b57cec5SDimitry Andric ///
56*0b57cec5SDimitry Andric /// The implementation below is primarily implemented in terms of three classes:
57*0b57cec5SDimitry Andric /// RopePieceBTreeNode - Common base class for:
58*0b57cec5SDimitry Andric ///
59*0b57cec5SDimitry Andric /// RopePieceBTreeLeaf - Directly manages up to '2*WidthFactor' RopePiece
60*0b57cec5SDimitry Andric /// nodes. This directly represents a chunk of the string with those
61*0b57cec5SDimitry Andric /// RopePieces concatenated.
62*0b57cec5SDimitry Andric /// RopePieceBTreeInterior - An interior node in the B+ Tree, which manages
63*0b57cec5SDimitry Andric /// up to '2*WidthFactor' other nodes in the tree.
64*0b57cec5SDimitry Andric
65*0b57cec5SDimitry Andric namespace {
66*0b57cec5SDimitry Andric
67*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
68*0b57cec5SDimitry Andric // RopePieceBTreeNode Class
69*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
70*0b57cec5SDimitry Andric
71*0b57cec5SDimitry Andric /// RopePieceBTreeNode - Common base class of RopePieceBTreeLeaf and
72*0b57cec5SDimitry Andric /// RopePieceBTreeInterior. This provides some 'virtual' dispatching methods
73*0b57cec5SDimitry Andric /// and a flag that determines which subclass the instance is. Also
74*0b57cec5SDimitry Andric /// important, this node knows the full extend of the node, including any
75*0b57cec5SDimitry Andric /// children that it has. This allows efficient skipping over entire subtrees
76*0b57cec5SDimitry Andric /// when looking for an offset in the BTree.
77*0b57cec5SDimitry Andric class RopePieceBTreeNode {
78*0b57cec5SDimitry Andric protected:
79*0b57cec5SDimitry Andric /// WidthFactor - This controls the number of K/V slots held in the BTree:
80*0b57cec5SDimitry Andric /// how wide it is. Each level of the BTree is guaranteed to have at least
81*0b57cec5SDimitry Andric /// 'WidthFactor' elements in it (either ropepieces or children), (except
82*0b57cec5SDimitry Andric /// the root, which may have less) and may have at most 2*WidthFactor
83*0b57cec5SDimitry Andric /// elements.
84*0b57cec5SDimitry Andric enum { WidthFactor = 8 };
85*0b57cec5SDimitry Andric
86*0b57cec5SDimitry Andric /// Size - This is the number of bytes of file this node (including any
87*0b57cec5SDimitry Andric /// potential children) covers.
88*0b57cec5SDimitry Andric unsigned Size = 0;
89*0b57cec5SDimitry Andric
90*0b57cec5SDimitry Andric /// IsLeaf - True if this is an instance of RopePieceBTreeLeaf, false if it
91*0b57cec5SDimitry Andric /// is an instance of RopePieceBTreeInterior.
92*0b57cec5SDimitry Andric bool IsLeaf;
93*0b57cec5SDimitry Andric
RopePieceBTreeNode(bool isLeaf)94*0b57cec5SDimitry Andric RopePieceBTreeNode(bool isLeaf) : IsLeaf(isLeaf) {}
95*0b57cec5SDimitry Andric ~RopePieceBTreeNode() = default;
96*0b57cec5SDimitry Andric
97*0b57cec5SDimitry Andric public:
isLeaf() const98*0b57cec5SDimitry Andric bool isLeaf() const { return IsLeaf; }
size() const99*0b57cec5SDimitry Andric unsigned size() const { return Size; }
100*0b57cec5SDimitry Andric
101*0b57cec5SDimitry Andric void Destroy();
102*0b57cec5SDimitry Andric
103*0b57cec5SDimitry Andric /// split - Split the range containing the specified offset so that we are
104*0b57cec5SDimitry Andric /// guaranteed that there is a place to do an insertion at the specified
105*0b57cec5SDimitry Andric /// offset. The offset is relative, so "0" is the start of the node.
106*0b57cec5SDimitry Andric ///
107*0b57cec5SDimitry Andric /// If there is no space in this subtree for the extra piece, the extra tree
108*0b57cec5SDimitry Andric /// node is returned and must be inserted into a parent.
109*0b57cec5SDimitry Andric RopePieceBTreeNode *split(unsigned Offset);
110*0b57cec5SDimitry Andric
111*0b57cec5SDimitry Andric /// insert - Insert the specified ropepiece into this tree node at the
112*0b57cec5SDimitry Andric /// specified offset. The offset is relative, so "0" is the start of the
113*0b57cec5SDimitry Andric /// node.
114*0b57cec5SDimitry Andric ///
115*0b57cec5SDimitry Andric /// If there is no space in this subtree for the extra piece, the extra tree
116*0b57cec5SDimitry Andric /// node is returned and must be inserted into a parent.
117*0b57cec5SDimitry Andric RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R);
118*0b57cec5SDimitry Andric
119*0b57cec5SDimitry Andric /// erase - Remove NumBytes from this node at the specified offset. We are
120*0b57cec5SDimitry Andric /// guaranteed that there is a split at Offset.
121*0b57cec5SDimitry Andric void erase(unsigned Offset, unsigned NumBytes);
122*0b57cec5SDimitry Andric };
123*0b57cec5SDimitry Andric
124*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
125*0b57cec5SDimitry Andric // RopePieceBTreeLeaf Class
126*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
127*0b57cec5SDimitry Andric
128*0b57cec5SDimitry Andric /// RopePieceBTreeLeaf - Directly manages up to '2*WidthFactor' RopePiece
129*0b57cec5SDimitry Andric /// nodes. This directly represents a chunk of the string with those
130*0b57cec5SDimitry Andric /// RopePieces concatenated. Since this is a B+Tree, all values (in this case
131*0b57cec5SDimitry Andric /// instances of RopePiece) are stored in leaves like this. To make iteration
132*0b57cec5SDimitry Andric /// over the leaves efficient, they maintain a singly linked list through the
133*0b57cec5SDimitry Andric /// NextLeaf field. This allows the B+Tree forward iterator to be constant
134*0b57cec5SDimitry Andric /// time for all increments.
135*0b57cec5SDimitry Andric class RopePieceBTreeLeaf : public RopePieceBTreeNode {
136*0b57cec5SDimitry Andric /// NumPieces - This holds the number of rope pieces currently active in the
137*0b57cec5SDimitry Andric /// Pieces array.
138*0b57cec5SDimitry Andric unsigned char NumPieces = 0;
139*0b57cec5SDimitry Andric
140*0b57cec5SDimitry Andric /// Pieces - This tracks the file chunks currently in this leaf.
141*0b57cec5SDimitry Andric RopePiece Pieces[2*WidthFactor];
142*0b57cec5SDimitry Andric
143*0b57cec5SDimitry Andric /// NextLeaf - This is a pointer to the next leaf in the tree, allowing
144*0b57cec5SDimitry Andric /// efficient in-order forward iteration of the tree without traversal.
145*0b57cec5SDimitry Andric RopePieceBTreeLeaf **PrevLeaf = nullptr;
146*0b57cec5SDimitry Andric RopePieceBTreeLeaf *NextLeaf = nullptr;
147*0b57cec5SDimitry Andric
148*0b57cec5SDimitry Andric public:
RopePieceBTreeLeaf()149*0b57cec5SDimitry Andric RopePieceBTreeLeaf() : RopePieceBTreeNode(true) {}
150*0b57cec5SDimitry Andric
~RopePieceBTreeLeaf()151*0b57cec5SDimitry Andric ~RopePieceBTreeLeaf() {
152*0b57cec5SDimitry Andric if (PrevLeaf || NextLeaf)
153*0b57cec5SDimitry Andric removeFromLeafInOrder();
154*0b57cec5SDimitry Andric clear();
155*0b57cec5SDimitry Andric }
156*0b57cec5SDimitry Andric
isFull() const157*0b57cec5SDimitry Andric bool isFull() const { return NumPieces == 2*WidthFactor; }
158*0b57cec5SDimitry Andric
159*0b57cec5SDimitry Andric /// clear - Remove all rope pieces from this leaf.
clear()160*0b57cec5SDimitry Andric void clear() {
161*0b57cec5SDimitry Andric while (NumPieces)
162*0b57cec5SDimitry Andric Pieces[--NumPieces] = RopePiece();
163*0b57cec5SDimitry Andric Size = 0;
164*0b57cec5SDimitry Andric }
165*0b57cec5SDimitry Andric
getNumPieces() const166*0b57cec5SDimitry Andric unsigned getNumPieces() const { return NumPieces; }
167*0b57cec5SDimitry Andric
getPiece(unsigned i) const168*0b57cec5SDimitry Andric const RopePiece &getPiece(unsigned i) const {
169*0b57cec5SDimitry Andric assert(i < getNumPieces() && "Invalid piece ID");
170*0b57cec5SDimitry Andric return Pieces[i];
171*0b57cec5SDimitry Andric }
172*0b57cec5SDimitry Andric
getNextLeafInOrder() const173*0b57cec5SDimitry Andric const RopePieceBTreeLeaf *getNextLeafInOrder() const { return NextLeaf; }
174*0b57cec5SDimitry Andric
insertAfterLeafInOrder(RopePieceBTreeLeaf * Node)175*0b57cec5SDimitry Andric void insertAfterLeafInOrder(RopePieceBTreeLeaf *Node) {
176*0b57cec5SDimitry Andric assert(!PrevLeaf && !NextLeaf && "Already in ordering");
177*0b57cec5SDimitry Andric
178*0b57cec5SDimitry Andric NextLeaf = Node->NextLeaf;
179*0b57cec5SDimitry Andric if (NextLeaf)
180*0b57cec5SDimitry Andric NextLeaf->PrevLeaf = &NextLeaf;
181*0b57cec5SDimitry Andric PrevLeaf = &Node->NextLeaf;
182*0b57cec5SDimitry Andric Node->NextLeaf = this;
183*0b57cec5SDimitry Andric }
184*0b57cec5SDimitry Andric
removeFromLeafInOrder()185*0b57cec5SDimitry Andric void removeFromLeafInOrder() {
186*0b57cec5SDimitry Andric if (PrevLeaf) {
187*0b57cec5SDimitry Andric *PrevLeaf = NextLeaf;
188*0b57cec5SDimitry Andric if (NextLeaf)
189*0b57cec5SDimitry Andric NextLeaf->PrevLeaf = PrevLeaf;
190*0b57cec5SDimitry Andric } else if (NextLeaf) {
191*0b57cec5SDimitry Andric NextLeaf->PrevLeaf = nullptr;
192*0b57cec5SDimitry Andric }
193*0b57cec5SDimitry Andric }
194*0b57cec5SDimitry Andric
195*0b57cec5SDimitry Andric /// FullRecomputeSizeLocally - This method recomputes the 'Size' field by
196*0b57cec5SDimitry Andric /// summing the size of all RopePieces.
FullRecomputeSizeLocally()197*0b57cec5SDimitry Andric void FullRecomputeSizeLocally() {
198*0b57cec5SDimitry Andric Size = 0;
199*0b57cec5SDimitry Andric for (unsigned i = 0, e = getNumPieces(); i != e; ++i)
200*0b57cec5SDimitry Andric Size += getPiece(i).size();
201*0b57cec5SDimitry Andric }
202*0b57cec5SDimitry Andric
203*0b57cec5SDimitry Andric /// split - Split the range containing the specified offset so that we are
204*0b57cec5SDimitry Andric /// guaranteed that there is a place to do an insertion at the specified
205*0b57cec5SDimitry Andric /// offset. The offset is relative, so "0" is the start of the node.
206*0b57cec5SDimitry Andric ///
207*0b57cec5SDimitry Andric /// If there is no space in this subtree for the extra piece, the extra tree
208*0b57cec5SDimitry Andric /// node is returned and must be inserted into a parent.
209*0b57cec5SDimitry Andric RopePieceBTreeNode *split(unsigned Offset);
210*0b57cec5SDimitry Andric
211*0b57cec5SDimitry Andric /// insert - Insert the specified ropepiece into this tree node at the
212*0b57cec5SDimitry Andric /// specified offset. The offset is relative, so "0" is the start of the
213*0b57cec5SDimitry Andric /// node.
214*0b57cec5SDimitry Andric ///
215*0b57cec5SDimitry Andric /// If there is no space in this subtree for the extra piece, the extra tree
216*0b57cec5SDimitry Andric /// node is returned and must be inserted into a parent.
217*0b57cec5SDimitry Andric RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R);
218*0b57cec5SDimitry Andric
219*0b57cec5SDimitry Andric /// erase - Remove NumBytes from this node at the specified offset. We are
220*0b57cec5SDimitry Andric /// guaranteed that there is a split at Offset.
221*0b57cec5SDimitry Andric void erase(unsigned Offset, unsigned NumBytes);
222*0b57cec5SDimitry Andric
classof(const RopePieceBTreeNode * N)223*0b57cec5SDimitry Andric static bool classof(const RopePieceBTreeNode *N) {
224*0b57cec5SDimitry Andric return N->isLeaf();
225*0b57cec5SDimitry Andric }
226*0b57cec5SDimitry Andric };
227*0b57cec5SDimitry Andric
228*0b57cec5SDimitry Andric } // namespace
229*0b57cec5SDimitry Andric
230*0b57cec5SDimitry Andric /// split - Split the range containing the specified offset so that we are
231*0b57cec5SDimitry Andric /// guaranteed that there is a place to do an insertion at the specified
232*0b57cec5SDimitry Andric /// offset. The offset is relative, so "0" is the start of the node.
233*0b57cec5SDimitry Andric ///
234*0b57cec5SDimitry Andric /// If there is no space in this subtree for the extra piece, the extra tree
235*0b57cec5SDimitry Andric /// node is returned and must be inserted into a parent.
split(unsigned Offset)236*0b57cec5SDimitry Andric RopePieceBTreeNode *RopePieceBTreeLeaf::split(unsigned Offset) {
237*0b57cec5SDimitry Andric // Find the insertion point. We are guaranteed that there is a split at the
238*0b57cec5SDimitry Andric // specified offset so find it.
239*0b57cec5SDimitry Andric if (Offset == 0 || Offset == size()) {
240*0b57cec5SDimitry Andric // Fastpath for a common case. There is already a splitpoint at the end.
241*0b57cec5SDimitry Andric return nullptr;
242*0b57cec5SDimitry Andric }
243*0b57cec5SDimitry Andric
244*0b57cec5SDimitry Andric // Find the piece that this offset lands in.
245*0b57cec5SDimitry Andric unsigned PieceOffs = 0;
246*0b57cec5SDimitry Andric unsigned i = 0;
247*0b57cec5SDimitry Andric while (Offset >= PieceOffs+Pieces[i].size()) {
248*0b57cec5SDimitry Andric PieceOffs += Pieces[i].size();
249*0b57cec5SDimitry Andric ++i;
250*0b57cec5SDimitry Andric }
251*0b57cec5SDimitry Andric
252*0b57cec5SDimitry Andric // If there is already a split point at the specified offset, just return
253*0b57cec5SDimitry Andric // success.
254*0b57cec5SDimitry Andric if (PieceOffs == Offset)
255*0b57cec5SDimitry Andric return nullptr;
256*0b57cec5SDimitry Andric
257*0b57cec5SDimitry Andric // Otherwise, we need to split piece 'i' at Offset-PieceOffs. Convert Offset
258*0b57cec5SDimitry Andric // to being Piece relative.
259*0b57cec5SDimitry Andric unsigned IntraPieceOffset = Offset-PieceOffs;
260*0b57cec5SDimitry Andric
261*0b57cec5SDimitry Andric // We do this by shrinking the RopePiece and then doing an insert of the tail.
262*0b57cec5SDimitry Andric RopePiece Tail(Pieces[i].StrData, Pieces[i].StartOffs+IntraPieceOffset,
263*0b57cec5SDimitry Andric Pieces[i].EndOffs);
264*0b57cec5SDimitry Andric Size -= Pieces[i].size();
265*0b57cec5SDimitry Andric Pieces[i].EndOffs = Pieces[i].StartOffs+IntraPieceOffset;
266*0b57cec5SDimitry Andric Size += Pieces[i].size();
267*0b57cec5SDimitry Andric
268*0b57cec5SDimitry Andric return insert(Offset, Tail);
269*0b57cec5SDimitry Andric }
270*0b57cec5SDimitry Andric
271*0b57cec5SDimitry Andric /// insert - Insert the specified RopePiece into this tree node at the
272*0b57cec5SDimitry Andric /// specified offset. The offset is relative, so "0" is the start of the node.
273*0b57cec5SDimitry Andric ///
274*0b57cec5SDimitry Andric /// If there is no space in this subtree for the extra piece, the extra tree
275*0b57cec5SDimitry Andric /// node is returned and must be inserted into a parent.
insert(unsigned Offset,const RopePiece & R)276*0b57cec5SDimitry Andric RopePieceBTreeNode *RopePieceBTreeLeaf::insert(unsigned Offset,
277*0b57cec5SDimitry Andric const RopePiece &R) {
278*0b57cec5SDimitry Andric // If this node is not full, insert the piece.
279*0b57cec5SDimitry Andric if (!isFull()) {
280*0b57cec5SDimitry Andric // Find the insertion point. We are guaranteed that there is a split at the
281*0b57cec5SDimitry Andric // specified offset so find it.
282*0b57cec5SDimitry Andric unsigned i = 0, e = getNumPieces();
283*0b57cec5SDimitry Andric if (Offset == size()) {
284*0b57cec5SDimitry Andric // Fastpath for a common case.
285*0b57cec5SDimitry Andric i = e;
286*0b57cec5SDimitry Andric } else {
287*0b57cec5SDimitry Andric unsigned SlotOffs = 0;
288*0b57cec5SDimitry Andric for (; Offset > SlotOffs; ++i)
289*0b57cec5SDimitry Andric SlotOffs += getPiece(i).size();
290*0b57cec5SDimitry Andric assert(SlotOffs == Offset && "Split didn't occur before insertion!");
291*0b57cec5SDimitry Andric }
292*0b57cec5SDimitry Andric
293*0b57cec5SDimitry Andric // For an insertion into a non-full leaf node, just insert the value in
294*0b57cec5SDimitry Andric // its sorted position. This requires moving later values over.
295*0b57cec5SDimitry Andric for (; i != e; --e)
296*0b57cec5SDimitry Andric Pieces[e] = Pieces[e-1];
297*0b57cec5SDimitry Andric Pieces[i] = R;
298*0b57cec5SDimitry Andric ++NumPieces;
299*0b57cec5SDimitry Andric Size += R.size();
300*0b57cec5SDimitry Andric return nullptr;
301*0b57cec5SDimitry Andric }
302*0b57cec5SDimitry Andric
303*0b57cec5SDimitry Andric // Otherwise, if this is leaf is full, split it in two halves. Since this
304*0b57cec5SDimitry Andric // node is full, it contains 2*WidthFactor values. We move the first
305*0b57cec5SDimitry Andric // 'WidthFactor' values to the LHS child (which we leave in this node) and
306*0b57cec5SDimitry Andric // move the last 'WidthFactor' values into the RHS child.
307*0b57cec5SDimitry Andric
308*0b57cec5SDimitry Andric // Create the new node.
309*0b57cec5SDimitry Andric RopePieceBTreeLeaf *NewNode = new RopePieceBTreeLeaf();
310*0b57cec5SDimitry Andric
311*0b57cec5SDimitry Andric // Move over the last 'WidthFactor' values from here to NewNode.
312*0b57cec5SDimitry Andric std::copy(&Pieces[WidthFactor], &Pieces[2*WidthFactor],
313*0b57cec5SDimitry Andric &NewNode->Pieces[0]);
314*0b57cec5SDimitry Andric // Replace old pieces with null RopePieces to drop refcounts.
315*0b57cec5SDimitry Andric std::fill(&Pieces[WidthFactor], &Pieces[2*WidthFactor], RopePiece());
316*0b57cec5SDimitry Andric
317*0b57cec5SDimitry Andric // Decrease the number of values in the two nodes.
318*0b57cec5SDimitry Andric NewNode->NumPieces = NumPieces = WidthFactor;
319*0b57cec5SDimitry Andric
320*0b57cec5SDimitry Andric // Recompute the two nodes' size.
321*0b57cec5SDimitry Andric NewNode->FullRecomputeSizeLocally();
322*0b57cec5SDimitry Andric FullRecomputeSizeLocally();
323*0b57cec5SDimitry Andric
324*0b57cec5SDimitry Andric // Update the list of leaves.
325*0b57cec5SDimitry Andric NewNode->insertAfterLeafInOrder(this);
326*0b57cec5SDimitry Andric
327*0b57cec5SDimitry Andric // These insertions can't fail.
328*0b57cec5SDimitry Andric if (this->size() >= Offset)
329*0b57cec5SDimitry Andric this->insert(Offset, R);
330*0b57cec5SDimitry Andric else
331*0b57cec5SDimitry Andric NewNode->insert(Offset - this->size(), R);
332*0b57cec5SDimitry Andric return NewNode;
333*0b57cec5SDimitry Andric }
334*0b57cec5SDimitry Andric
335*0b57cec5SDimitry Andric /// erase - Remove NumBytes from this node at the specified offset. We are
336*0b57cec5SDimitry Andric /// guaranteed that there is a split at Offset.
erase(unsigned Offset,unsigned NumBytes)337*0b57cec5SDimitry Andric void RopePieceBTreeLeaf::erase(unsigned Offset, unsigned NumBytes) {
338*0b57cec5SDimitry Andric // Since we are guaranteed that there is a split at Offset, we start by
339*0b57cec5SDimitry Andric // finding the Piece that starts there.
340*0b57cec5SDimitry Andric unsigned PieceOffs = 0;
341*0b57cec5SDimitry Andric unsigned i = 0;
342*0b57cec5SDimitry Andric for (; Offset > PieceOffs; ++i)
343*0b57cec5SDimitry Andric PieceOffs += getPiece(i).size();
344*0b57cec5SDimitry Andric assert(PieceOffs == Offset && "Split didn't occur before erase!");
345*0b57cec5SDimitry Andric
346*0b57cec5SDimitry Andric unsigned StartPiece = i;
347*0b57cec5SDimitry Andric
348*0b57cec5SDimitry Andric // Figure out how many pieces completely cover 'NumBytes'. We want to remove
349*0b57cec5SDimitry Andric // all of them.
350*0b57cec5SDimitry Andric for (; Offset+NumBytes > PieceOffs+getPiece(i).size(); ++i)
351*0b57cec5SDimitry Andric PieceOffs += getPiece(i).size();
352*0b57cec5SDimitry Andric
353*0b57cec5SDimitry Andric // If we exactly include the last one, include it in the region to delete.
354*0b57cec5SDimitry Andric if (Offset+NumBytes == PieceOffs+getPiece(i).size()) {
355*0b57cec5SDimitry Andric PieceOffs += getPiece(i).size();
356*0b57cec5SDimitry Andric ++i;
357*0b57cec5SDimitry Andric }
358*0b57cec5SDimitry Andric
359*0b57cec5SDimitry Andric // If we completely cover some RopePieces, erase them now.
360*0b57cec5SDimitry Andric if (i != StartPiece) {
361*0b57cec5SDimitry Andric unsigned NumDeleted = i-StartPiece;
362*0b57cec5SDimitry Andric for (; i != getNumPieces(); ++i)
363*0b57cec5SDimitry Andric Pieces[i-NumDeleted] = Pieces[i];
364*0b57cec5SDimitry Andric
365*0b57cec5SDimitry Andric // Drop references to dead rope pieces.
366*0b57cec5SDimitry Andric std::fill(&Pieces[getNumPieces()-NumDeleted], &Pieces[getNumPieces()],
367*0b57cec5SDimitry Andric RopePiece());
368*0b57cec5SDimitry Andric NumPieces -= NumDeleted;
369*0b57cec5SDimitry Andric
370*0b57cec5SDimitry Andric unsigned CoverBytes = PieceOffs-Offset;
371*0b57cec5SDimitry Andric NumBytes -= CoverBytes;
372*0b57cec5SDimitry Andric Size -= CoverBytes;
373*0b57cec5SDimitry Andric }
374*0b57cec5SDimitry Andric
375*0b57cec5SDimitry Andric // If we completely removed some stuff, we could be done.
376*0b57cec5SDimitry Andric if (NumBytes == 0) return;
377*0b57cec5SDimitry Andric
378*0b57cec5SDimitry Andric // Okay, now might be erasing part of some Piece. If this is the case, then
379*0b57cec5SDimitry Andric // move the start point of the piece.
380*0b57cec5SDimitry Andric assert(getPiece(StartPiece).size() > NumBytes);
381*0b57cec5SDimitry Andric Pieces[StartPiece].StartOffs += NumBytes;
382*0b57cec5SDimitry Andric
383*0b57cec5SDimitry Andric // The size of this node just shrunk by NumBytes.
384*0b57cec5SDimitry Andric Size -= NumBytes;
385*0b57cec5SDimitry Andric }
386*0b57cec5SDimitry Andric
387*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
388*0b57cec5SDimitry Andric // RopePieceBTreeInterior Class
389*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
390*0b57cec5SDimitry Andric
391*0b57cec5SDimitry Andric namespace {
392*0b57cec5SDimitry Andric
393*0b57cec5SDimitry Andric /// RopePieceBTreeInterior - This represents an interior node in the B+Tree,
394*0b57cec5SDimitry Andric /// which holds up to 2*WidthFactor pointers to child nodes.
395*0b57cec5SDimitry Andric class RopePieceBTreeInterior : public RopePieceBTreeNode {
396*0b57cec5SDimitry Andric /// NumChildren - This holds the number of children currently active in the
397*0b57cec5SDimitry Andric /// Children array.
398*0b57cec5SDimitry Andric unsigned char NumChildren = 0;
399*0b57cec5SDimitry Andric
400*0b57cec5SDimitry Andric RopePieceBTreeNode *Children[2*WidthFactor];
401*0b57cec5SDimitry Andric
402*0b57cec5SDimitry Andric public:
RopePieceBTreeInterior()403*0b57cec5SDimitry Andric RopePieceBTreeInterior() : RopePieceBTreeNode(false) {}
404*0b57cec5SDimitry Andric
RopePieceBTreeInterior(RopePieceBTreeNode * LHS,RopePieceBTreeNode * RHS)405*0b57cec5SDimitry Andric RopePieceBTreeInterior(RopePieceBTreeNode *LHS, RopePieceBTreeNode *RHS)
406*0b57cec5SDimitry Andric : RopePieceBTreeNode(false) {
407*0b57cec5SDimitry Andric Children[0] = LHS;
408*0b57cec5SDimitry Andric Children[1] = RHS;
409*0b57cec5SDimitry Andric NumChildren = 2;
410*0b57cec5SDimitry Andric Size = LHS->size() + RHS->size();
411*0b57cec5SDimitry Andric }
412*0b57cec5SDimitry Andric
~RopePieceBTreeInterior()413*0b57cec5SDimitry Andric ~RopePieceBTreeInterior() {
414*0b57cec5SDimitry Andric for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
415*0b57cec5SDimitry Andric Children[i]->Destroy();
416*0b57cec5SDimitry Andric }
417*0b57cec5SDimitry Andric
isFull() const418*0b57cec5SDimitry Andric bool isFull() const { return NumChildren == 2*WidthFactor; }
419*0b57cec5SDimitry Andric
getNumChildren() const420*0b57cec5SDimitry Andric unsigned getNumChildren() const { return NumChildren; }
421*0b57cec5SDimitry Andric
getChild(unsigned i) const422*0b57cec5SDimitry Andric const RopePieceBTreeNode *getChild(unsigned i) const {
423*0b57cec5SDimitry Andric assert(i < NumChildren && "invalid child #");
424*0b57cec5SDimitry Andric return Children[i];
425*0b57cec5SDimitry Andric }
426*0b57cec5SDimitry Andric
getChild(unsigned i)427*0b57cec5SDimitry Andric RopePieceBTreeNode *getChild(unsigned i) {
428*0b57cec5SDimitry Andric assert(i < NumChildren && "invalid child #");
429*0b57cec5SDimitry Andric return Children[i];
430*0b57cec5SDimitry Andric }
431*0b57cec5SDimitry Andric
432*0b57cec5SDimitry Andric /// FullRecomputeSizeLocally - Recompute the Size field of this node by
433*0b57cec5SDimitry Andric /// summing up the sizes of the child nodes.
FullRecomputeSizeLocally()434*0b57cec5SDimitry Andric void FullRecomputeSizeLocally() {
435*0b57cec5SDimitry Andric Size = 0;
436*0b57cec5SDimitry Andric for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
437*0b57cec5SDimitry Andric Size += getChild(i)->size();
438*0b57cec5SDimitry Andric }
439*0b57cec5SDimitry Andric
440*0b57cec5SDimitry Andric /// split - Split the range containing the specified offset so that we are
441*0b57cec5SDimitry Andric /// guaranteed that there is a place to do an insertion at the specified
442*0b57cec5SDimitry Andric /// offset. The offset is relative, so "0" is the start of the node.
443*0b57cec5SDimitry Andric ///
444*0b57cec5SDimitry Andric /// If there is no space in this subtree for the extra piece, the extra tree
445*0b57cec5SDimitry Andric /// node is returned and must be inserted into a parent.
446*0b57cec5SDimitry Andric RopePieceBTreeNode *split(unsigned Offset);
447*0b57cec5SDimitry Andric
448*0b57cec5SDimitry Andric /// insert - Insert the specified ropepiece into this tree node at the
449*0b57cec5SDimitry Andric /// specified offset. The offset is relative, so "0" is the start of the
450*0b57cec5SDimitry Andric /// node.
451*0b57cec5SDimitry Andric ///
452*0b57cec5SDimitry Andric /// If there is no space in this subtree for the extra piece, the extra tree
453*0b57cec5SDimitry Andric /// node is returned and must be inserted into a parent.
454*0b57cec5SDimitry Andric RopePieceBTreeNode *insert(unsigned Offset, const RopePiece &R);
455*0b57cec5SDimitry Andric
456*0b57cec5SDimitry Andric /// HandleChildPiece - A child propagated an insertion result up to us.
457*0b57cec5SDimitry Andric /// Insert the new child, and/or propagate the result further up the tree.
458*0b57cec5SDimitry Andric RopePieceBTreeNode *HandleChildPiece(unsigned i, RopePieceBTreeNode *RHS);
459*0b57cec5SDimitry Andric
460*0b57cec5SDimitry Andric /// erase - Remove NumBytes from this node at the specified offset. We are
461*0b57cec5SDimitry Andric /// guaranteed that there is a split at Offset.
462*0b57cec5SDimitry Andric void erase(unsigned Offset, unsigned NumBytes);
463*0b57cec5SDimitry Andric
classof(const RopePieceBTreeNode * N)464*0b57cec5SDimitry Andric static bool classof(const RopePieceBTreeNode *N) {
465*0b57cec5SDimitry Andric return !N->isLeaf();
466*0b57cec5SDimitry Andric }
467*0b57cec5SDimitry Andric };
468*0b57cec5SDimitry Andric
469*0b57cec5SDimitry Andric } // namespace
470*0b57cec5SDimitry Andric
471*0b57cec5SDimitry Andric /// split - Split the range containing the specified offset so that we are
472*0b57cec5SDimitry Andric /// guaranteed that there is a place to do an insertion at the specified
473*0b57cec5SDimitry Andric /// offset. The offset is relative, so "0" is the start of the node.
474*0b57cec5SDimitry Andric ///
475*0b57cec5SDimitry Andric /// If there is no space in this subtree for the extra piece, the extra tree
476*0b57cec5SDimitry Andric /// node is returned and must be inserted into a parent.
split(unsigned Offset)477*0b57cec5SDimitry Andric RopePieceBTreeNode *RopePieceBTreeInterior::split(unsigned Offset) {
478*0b57cec5SDimitry Andric // Figure out which child to split.
479*0b57cec5SDimitry Andric if (Offset == 0 || Offset == size())
480*0b57cec5SDimitry Andric return nullptr; // If we have an exact offset, we're already split.
481*0b57cec5SDimitry Andric
482*0b57cec5SDimitry Andric unsigned ChildOffset = 0;
483*0b57cec5SDimitry Andric unsigned i = 0;
484*0b57cec5SDimitry Andric for (; Offset >= ChildOffset+getChild(i)->size(); ++i)
485*0b57cec5SDimitry Andric ChildOffset += getChild(i)->size();
486*0b57cec5SDimitry Andric
487*0b57cec5SDimitry Andric // If already split there, we're done.
488*0b57cec5SDimitry Andric if (ChildOffset == Offset)
489*0b57cec5SDimitry Andric return nullptr;
490*0b57cec5SDimitry Andric
491*0b57cec5SDimitry Andric // Otherwise, recursively split the child.
492*0b57cec5SDimitry Andric if (RopePieceBTreeNode *RHS = getChild(i)->split(Offset-ChildOffset))
493*0b57cec5SDimitry Andric return HandleChildPiece(i, RHS);
494*0b57cec5SDimitry Andric return nullptr; // Done!
495*0b57cec5SDimitry Andric }
496*0b57cec5SDimitry Andric
497*0b57cec5SDimitry Andric /// insert - Insert the specified ropepiece into this tree node at the
498*0b57cec5SDimitry Andric /// specified offset. The offset is relative, so "0" is the start of the
499*0b57cec5SDimitry Andric /// node.
500*0b57cec5SDimitry Andric ///
501*0b57cec5SDimitry Andric /// If there is no space in this subtree for the extra piece, the extra tree
502*0b57cec5SDimitry Andric /// node is returned and must be inserted into a parent.
insert(unsigned Offset,const RopePiece & R)503*0b57cec5SDimitry Andric RopePieceBTreeNode *RopePieceBTreeInterior::insert(unsigned Offset,
504*0b57cec5SDimitry Andric const RopePiece &R) {
505*0b57cec5SDimitry Andric // Find the insertion point. We are guaranteed that there is a split at the
506*0b57cec5SDimitry Andric // specified offset so find it.
507*0b57cec5SDimitry Andric unsigned i = 0, e = getNumChildren();
508*0b57cec5SDimitry Andric
509*0b57cec5SDimitry Andric unsigned ChildOffs = 0;
510*0b57cec5SDimitry Andric if (Offset == size()) {
511*0b57cec5SDimitry Andric // Fastpath for a common case. Insert at end of last child.
512*0b57cec5SDimitry Andric i = e-1;
513*0b57cec5SDimitry Andric ChildOffs = size()-getChild(i)->size();
514*0b57cec5SDimitry Andric } else {
515*0b57cec5SDimitry Andric for (; Offset > ChildOffs+getChild(i)->size(); ++i)
516*0b57cec5SDimitry Andric ChildOffs += getChild(i)->size();
517*0b57cec5SDimitry Andric }
518*0b57cec5SDimitry Andric
519*0b57cec5SDimitry Andric Size += R.size();
520*0b57cec5SDimitry Andric
521*0b57cec5SDimitry Andric // Insert at the end of this child.
522*0b57cec5SDimitry Andric if (RopePieceBTreeNode *RHS = getChild(i)->insert(Offset-ChildOffs, R))
523*0b57cec5SDimitry Andric return HandleChildPiece(i, RHS);
524*0b57cec5SDimitry Andric
525*0b57cec5SDimitry Andric return nullptr;
526*0b57cec5SDimitry Andric }
527*0b57cec5SDimitry Andric
528*0b57cec5SDimitry Andric /// HandleChildPiece - A child propagated an insertion result up to us.
529*0b57cec5SDimitry Andric /// Insert the new child, and/or propagate the result further up the tree.
530*0b57cec5SDimitry Andric RopePieceBTreeNode *
HandleChildPiece(unsigned i,RopePieceBTreeNode * RHS)531*0b57cec5SDimitry Andric RopePieceBTreeInterior::HandleChildPiece(unsigned i, RopePieceBTreeNode *RHS) {
532*0b57cec5SDimitry Andric // Otherwise the child propagated a subtree up to us as a new child. See if
533*0b57cec5SDimitry Andric // we have space for it here.
534*0b57cec5SDimitry Andric if (!isFull()) {
535*0b57cec5SDimitry Andric // Insert RHS after child 'i'.
536*0b57cec5SDimitry Andric if (i + 1 != getNumChildren())
537*0b57cec5SDimitry Andric memmove(&Children[i+2], &Children[i+1],
538*0b57cec5SDimitry Andric (getNumChildren()-i-1)*sizeof(Children[0]));
539*0b57cec5SDimitry Andric Children[i+1] = RHS;
540*0b57cec5SDimitry Andric ++NumChildren;
541*0b57cec5SDimitry Andric return nullptr;
542*0b57cec5SDimitry Andric }
543*0b57cec5SDimitry Andric
544*0b57cec5SDimitry Andric // Okay, this node is full. Split it in half, moving WidthFactor children to
545*0b57cec5SDimitry Andric // a newly allocated interior node.
546*0b57cec5SDimitry Andric
547*0b57cec5SDimitry Andric // Create the new node.
548*0b57cec5SDimitry Andric RopePieceBTreeInterior *NewNode = new RopePieceBTreeInterior();
549*0b57cec5SDimitry Andric
550*0b57cec5SDimitry Andric // Move over the last 'WidthFactor' values from here to NewNode.
551*0b57cec5SDimitry Andric memcpy(&NewNode->Children[0], &Children[WidthFactor],
552*0b57cec5SDimitry Andric WidthFactor*sizeof(Children[0]));
553*0b57cec5SDimitry Andric
554*0b57cec5SDimitry Andric // Decrease the number of values in the two nodes.
555*0b57cec5SDimitry Andric NewNode->NumChildren = NumChildren = WidthFactor;
556*0b57cec5SDimitry Andric
557*0b57cec5SDimitry Andric // Finally, insert the two new children in the side the can (now) hold them.
558*0b57cec5SDimitry Andric // These insertions can't fail.
559*0b57cec5SDimitry Andric if (i < WidthFactor)
560*0b57cec5SDimitry Andric this->HandleChildPiece(i, RHS);
561*0b57cec5SDimitry Andric else
562*0b57cec5SDimitry Andric NewNode->HandleChildPiece(i-WidthFactor, RHS);
563*0b57cec5SDimitry Andric
564*0b57cec5SDimitry Andric // Recompute the two nodes' size.
565*0b57cec5SDimitry Andric NewNode->FullRecomputeSizeLocally();
566*0b57cec5SDimitry Andric FullRecomputeSizeLocally();
567*0b57cec5SDimitry Andric return NewNode;
568*0b57cec5SDimitry Andric }
569*0b57cec5SDimitry Andric
570*0b57cec5SDimitry Andric /// erase - Remove NumBytes from this node at the specified offset. We are
571*0b57cec5SDimitry Andric /// guaranteed that there is a split at Offset.
erase(unsigned Offset,unsigned NumBytes)572*0b57cec5SDimitry Andric void RopePieceBTreeInterior::erase(unsigned Offset, unsigned NumBytes) {
573*0b57cec5SDimitry Andric // This will shrink this node by NumBytes.
574*0b57cec5SDimitry Andric Size -= NumBytes;
575*0b57cec5SDimitry Andric
576*0b57cec5SDimitry Andric // Find the first child that overlaps with Offset.
577*0b57cec5SDimitry Andric unsigned i = 0;
578*0b57cec5SDimitry Andric for (; Offset >= getChild(i)->size(); ++i)
579*0b57cec5SDimitry Andric Offset -= getChild(i)->size();
580*0b57cec5SDimitry Andric
581*0b57cec5SDimitry Andric // Propagate the delete request into overlapping children, or completely
582*0b57cec5SDimitry Andric // delete the children as appropriate.
583*0b57cec5SDimitry Andric while (NumBytes) {
584*0b57cec5SDimitry Andric RopePieceBTreeNode *CurChild = getChild(i);
585*0b57cec5SDimitry Andric
586*0b57cec5SDimitry Andric // If we are deleting something contained entirely in the child, pass on the
587*0b57cec5SDimitry Andric // request.
588*0b57cec5SDimitry Andric if (Offset+NumBytes < CurChild->size()) {
589*0b57cec5SDimitry Andric CurChild->erase(Offset, NumBytes);
590*0b57cec5SDimitry Andric return;
591*0b57cec5SDimitry Andric }
592*0b57cec5SDimitry Andric
593*0b57cec5SDimitry Andric // If this deletion request starts somewhere in the middle of the child, it
594*0b57cec5SDimitry Andric // must be deleting to the end of the child.
595*0b57cec5SDimitry Andric if (Offset) {
596*0b57cec5SDimitry Andric unsigned BytesFromChild = CurChild->size()-Offset;
597*0b57cec5SDimitry Andric CurChild->erase(Offset, BytesFromChild);
598*0b57cec5SDimitry Andric NumBytes -= BytesFromChild;
599*0b57cec5SDimitry Andric // Start at the beginning of the next child.
600*0b57cec5SDimitry Andric Offset = 0;
601*0b57cec5SDimitry Andric ++i;
602*0b57cec5SDimitry Andric continue;
603*0b57cec5SDimitry Andric }
604*0b57cec5SDimitry Andric
605*0b57cec5SDimitry Andric // If the deletion request completely covers the child, delete it and move
606*0b57cec5SDimitry Andric // the rest down.
607*0b57cec5SDimitry Andric NumBytes -= CurChild->size();
608*0b57cec5SDimitry Andric CurChild->Destroy();
609*0b57cec5SDimitry Andric --NumChildren;
610*0b57cec5SDimitry Andric if (i != getNumChildren())
611*0b57cec5SDimitry Andric memmove(&Children[i], &Children[i+1],
612*0b57cec5SDimitry Andric (getNumChildren()-i)*sizeof(Children[0]));
613*0b57cec5SDimitry Andric }
614*0b57cec5SDimitry Andric }
615*0b57cec5SDimitry Andric
616*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
617*0b57cec5SDimitry Andric // RopePieceBTreeNode Implementation
618*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
619*0b57cec5SDimitry Andric
Destroy()620*0b57cec5SDimitry Andric void RopePieceBTreeNode::Destroy() {
621*0b57cec5SDimitry Andric if (auto *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
622*0b57cec5SDimitry Andric delete Leaf;
623*0b57cec5SDimitry Andric else
624*0b57cec5SDimitry Andric delete cast<RopePieceBTreeInterior>(this);
625*0b57cec5SDimitry Andric }
626*0b57cec5SDimitry Andric
627*0b57cec5SDimitry Andric /// split - Split the range containing the specified offset so that we are
628*0b57cec5SDimitry Andric /// guaranteed that there is a place to do an insertion at the specified
629*0b57cec5SDimitry Andric /// offset. The offset is relative, so "0" is the start of the node.
630*0b57cec5SDimitry Andric ///
631*0b57cec5SDimitry Andric /// If there is no space in this subtree for the extra piece, the extra tree
632*0b57cec5SDimitry Andric /// node is returned and must be inserted into a parent.
split(unsigned Offset)633*0b57cec5SDimitry Andric RopePieceBTreeNode *RopePieceBTreeNode::split(unsigned Offset) {
634*0b57cec5SDimitry Andric assert(Offset <= size() && "Invalid offset to split!");
635*0b57cec5SDimitry Andric if (auto *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
636*0b57cec5SDimitry Andric return Leaf->split(Offset);
637*0b57cec5SDimitry Andric return cast<RopePieceBTreeInterior>(this)->split(Offset);
638*0b57cec5SDimitry Andric }
639*0b57cec5SDimitry Andric
640*0b57cec5SDimitry Andric /// insert - Insert the specified ropepiece into this tree node at the
641*0b57cec5SDimitry Andric /// specified offset. The offset is relative, so "0" is the start of the
642*0b57cec5SDimitry Andric /// node.
643*0b57cec5SDimitry Andric ///
644*0b57cec5SDimitry Andric /// If there is no space in this subtree for the extra piece, the extra tree
645*0b57cec5SDimitry Andric /// node is returned and must be inserted into a parent.
insert(unsigned Offset,const RopePiece & R)646*0b57cec5SDimitry Andric RopePieceBTreeNode *RopePieceBTreeNode::insert(unsigned Offset,
647*0b57cec5SDimitry Andric const RopePiece &R) {
648*0b57cec5SDimitry Andric assert(Offset <= size() && "Invalid offset to insert!");
649*0b57cec5SDimitry Andric if (auto *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
650*0b57cec5SDimitry Andric return Leaf->insert(Offset, R);
651*0b57cec5SDimitry Andric return cast<RopePieceBTreeInterior>(this)->insert(Offset, R);
652*0b57cec5SDimitry Andric }
653*0b57cec5SDimitry Andric
654*0b57cec5SDimitry Andric /// erase - Remove NumBytes from this node at the specified offset. We are
655*0b57cec5SDimitry Andric /// guaranteed that there is a split at Offset.
erase(unsigned Offset,unsigned NumBytes)656*0b57cec5SDimitry Andric void RopePieceBTreeNode::erase(unsigned Offset, unsigned NumBytes) {
657*0b57cec5SDimitry Andric assert(Offset+NumBytes <= size() && "Invalid offset to erase!");
658*0b57cec5SDimitry Andric if (auto *Leaf = dyn_cast<RopePieceBTreeLeaf>(this))
659*0b57cec5SDimitry Andric return Leaf->erase(Offset, NumBytes);
660*0b57cec5SDimitry Andric return cast<RopePieceBTreeInterior>(this)->erase(Offset, NumBytes);
661*0b57cec5SDimitry Andric }
662*0b57cec5SDimitry Andric
663*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
664*0b57cec5SDimitry Andric // RopePieceBTreeIterator Implementation
665*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
666*0b57cec5SDimitry Andric
getCN(const void * P)667*0b57cec5SDimitry Andric static const RopePieceBTreeLeaf *getCN(const void *P) {
668*0b57cec5SDimitry Andric return static_cast<const RopePieceBTreeLeaf*>(P);
669*0b57cec5SDimitry Andric }
670*0b57cec5SDimitry Andric
671*0b57cec5SDimitry Andric // begin iterator.
RopePieceBTreeIterator(const void * n)672*0b57cec5SDimitry Andric RopePieceBTreeIterator::RopePieceBTreeIterator(const void *n) {
673*0b57cec5SDimitry Andric const auto *N = static_cast<const RopePieceBTreeNode *>(n);
674*0b57cec5SDimitry Andric
675*0b57cec5SDimitry Andric // Walk down the left side of the tree until we get to a leaf.
676*0b57cec5SDimitry Andric while (const auto *IN = dyn_cast<RopePieceBTreeInterior>(N))
677*0b57cec5SDimitry Andric N = IN->getChild(0);
678*0b57cec5SDimitry Andric
679*0b57cec5SDimitry Andric // We must have at least one leaf.
680*0b57cec5SDimitry Andric CurNode = cast<RopePieceBTreeLeaf>(N);
681*0b57cec5SDimitry Andric
682*0b57cec5SDimitry Andric // If we found a leaf that happens to be empty, skip over it until we get
683*0b57cec5SDimitry Andric // to something full.
684*0b57cec5SDimitry Andric while (CurNode && getCN(CurNode)->getNumPieces() == 0)
685*0b57cec5SDimitry Andric CurNode = getCN(CurNode)->getNextLeafInOrder();
686*0b57cec5SDimitry Andric
687*0b57cec5SDimitry Andric if (CurNode)
688*0b57cec5SDimitry Andric CurPiece = &getCN(CurNode)->getPiece(0);
689*0b57cec5SDimitry Andric else // Empty tree, this is an end() iterator.
690*0b57cec5SDimitry Andric CurPiece = nullptr;
691*0b57cec5SDimitry Andric CurChar = 0;
692*0b57cec5SDimitry Andric }
693*0b57cec5SDimitry Andric
MoveToNextPiece()694*0b57cec5SDimitry Andric void RopePieceBTreeIterator::MoveToNextPiece() {
695*0b57cec5SDimitry Andric if (CurPiece != &getCN(CurNode)->getPiece(getCN(CurNode)->getNumPieces()-1)) {
696*0b57cec5SDimitry Andric CurChar = 0;
697*0b57cec5SDimitry Andric ++CurPiece;
698*0b57cec5SDimitry Andric return;
699*0b57cec5SDimitry Andric }
700*0b57cec5SDimitry Andric
701*0b57cec5SDimitry Andric // Find the next non-empty leaf node.
702*0b57cec5SDimitry Andric do
703*0b57cec5SDimitry Andric CurNode = getCN(CurNode)->getNextLeafInOrder();
704*0b57cec5SDimitry Andric while (CurNode && getCN(CurNode)->getNumPieces() == 0);
705*0b57cec5SDimitry Andric
706*0b57cec5SDimitry Andric if (CurNode)
707*0b57cec5SDimitry Andric CurPiece = &getCN(CurNode)->getPiece(0);
708*0b57cec5SDimitry Andric else // Hit end().
709*0b57cec5SDimitry Andric CurPiece = nullptr;
710*0b57cec5SDimitry Andric CurChar = 0;
711*0b57cec5SDimitry Andric }
712*0b57cec5SDimitry Andric
713*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
714*0b57cec5SDimitry Andric // RopePieceBTree Implementation
715*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
716*0b57cec5SDimitry Andric
getRoot(void * P)717*0b57cec5SDimitry Andric static RopePieceBTreeNode *getRoot(void *P) {
718*0b57cec5SDimitry Andric return static_cast<RopePieceBTreeNode*>(P);
719*0b57cec5SDimitry Andric }
720*0b57cec5SDimitry Andric
RopePieceBTree()721*0b57cec5SDimitry Andric RopePieceBTree::RopePieceBTree() {
722*0b57cec5SDimitry Andric Root = new RopePieceBTreeLeaf();
723*0b57cec5SDimitry Andric }
724*0b57cec5SDimitry Andric
RopePieceBTree(const RopePieceBTree & RHS)725*0b57cec5SDimitry Andric RopePieceBTree::RopePieceBTree(const RopePieceBTree &RHS) {
726*0b57cec5SDimitry Andric assert(RHS.empty() && "Can't copy non-empty tree yet");
727*0b57cec5SDimitry Andric Root = new RopePieceBTreeLeaf();
728*0b57cec5SDimitry Andric }
729*0b57cec5SDimitry Andric
~RopePieceBTree()730*0b57cec5SDimitry Andric RopePieceBTree::~RopePieceBTree() {
731*0b57cec5SDimitry Andric getRoot(Root)->Destroy();
732*0b57cec5SDimitry Andric }
733*0b57cec5SDimitry Andric
size() const734*0b57cec5SDimitry Andric unsigned RopePieceBTree::size() const {
735*0b57cec5SDimitry Andric return getRoot(Root)->size();
736*0b57cec5SDimitry Andric }
737*0b57cec5SDimitry Andric
clear()738*0b57cec5SDimitry Andric void RopePieceBTree::clear() {
739*0b57cec5SDimitry Andric if (auto *Leaf = dyn_cast<RopePieceBTreeLeaf>(getRoot(Root)))
740*0b57cec5SDimitry Andric Leaf->clear();
741*0b57cec5SDimitry Andric else {
742*0b57cec5SDimitry Andric getRoot(Root)->Destroy();
743*0b57cec5SDimitry Andric Root = new RopePieceBTreeLeaf();
744*0b57cec5SDimitry Andric }
745*0b57cec5SDimitry Andric }
746*0b57cec5SDimitry Andric
insert(unsigned Offset,const RopePiece & R)747*0b57cec5SDimitry Andric void RopePieceBTree::insert(unsigned Offset, const RopePiece &R) {
748*0b57cec5SDimitry Andric // #1. Split at Offset.
749*0b57cec5SDimitry Andric if (RopePieceBTreeNode *RHS = getRoot(Root)->split(Offset))
750*0b57cec5SDimitry Andric Root = new RopePieceBTreeInterior(getRoot(Root), RHS);
751*0b57cec5SDimitry Andric
752*0b57cec5SDimitry Andric // #2. Do the insertion.
753*0b57cec5SDimitry Andric if (RopePieceBTreeNode *RHS = getRoot(Root)->insert(Offset, R))
754*0b57cec5SDimitry Andric Root = new RopePieceBTreeInterior(getRoot(Root), RHS);
755*0b57cec5SDimitry Andric }
756*0b57cec5SDimitry Andric
erase(unsigned Offset,unsigned NumBytes)757*0b57cec5SDimitry Andric void RopePieceBTree::erase(unsigned Offset, unsigned NumBytes) {
758*0b57cec5SDimitry Andric // #1. Split at Offset.
759*0b57cec5SDimitry Andric if (RopePieceBTreeNode *RHS = getRoot(Root)->split(Offset))
760*0b57cec5SDimitry Andric Root = new RopePieceBTreeInterior(getRoot(Root), RHS);
761*0b57cec5SDimitry Andric
762*0b57cec5SDimitry Andric // #2. Do the erasing.
763*0b57cec5SDimitry Andric getRoot(Root)->erase(Offset, NumBytes);
764*0b57cec5SDimitry Andric }
765*0b57cec5SDimitry Andric
766*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
767*0b57cec5SDimitry Andric // RewriteRope Implementation
768*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
769*0b57cec5SDimitry Andric
770*0b57cec5SDimitry Andric /// MakeRopeString - This copies the specified byte range into some instance of
771*0b57cec5SDimitry Andric /// RopeRefCountString, and return a RopePiece that represents it. This uses
772*0b57cec5SDimitry Andric /// the AllocBuffer object to aggregate requests for small strings into one
773*0b57cec5SDimitry Andric /// allocation instead of doing tons of tiny allocations.
MakeRopeString(const char * Start,const char * End)774*0b57cec5SDimitry Andric RopePiece RewriteRope::MakeRopeString(const char *Start, const char *End) {
775*0b57cec5SDimitry Andric unsigned Len = End-Start;
776*0b57cec5SDimitry Andric assert(Len && "Zero length RopePiece is invalid!");
777*0b57cec5SDimitry Andric
778*0b57cec5SDimitry Andric // If we have space for this string in the current alloc buffer, use it.
779*0b57cec5SDimitry Andric if (AllocOffs+Len <= AllocChunkSize) {
780*0b57cec5SDimitry Andric memcpy(AllocBuffer->Data+AllocOffs, Start, Len);
781*0b57cec5SDimitry Andric AllocOffs += Len;
782*0b57cec5SDimitry Andric return RopePiece(AllocBuffer, AllocOffs-Len, AllocOffs);
783*0b57cec5SDimitry Andric }
784*0b57cec5SDimitry Andric
785*0b57cec5SDimitry Andric // If we don't have enough room because this specific allocation is huge,
786*0b57cec5SDimitry Andric // just allocate a new rope piece for it alone.
787*0b57cec5SDimitry Andric if (Len > AllocChunkSize) {
788*0b57cec5SDimitry Andric unsigned Size = End-Start+sizeof(RopeRefCountString)-1;
789*0b57cec5SDimitry Andric auto *Res = reinterpret_cast<RopeRefCountString *>(new char[Size]);
790*0b57cec5SDimitry Andric Res->RefCount = 0;
791*0b57cec5SDimitry Andric memcpy(Res->Data, Start, End-Start);
792*0b57cec5SDimitry Andric return RopePiece(Res, 0, End-Start);
793*0b57cec5SDimitry Andric }
794*0b57cec5SDimitry Andric
795*0b57cec5SDimitry Andric // Otherwise, this was a small request but we just don't have space for it
796*0b57cec5SDimitry Andric // Make a new chunk and share it with later allocations.
797*0b57cec5SDimitry Andric
798*0b57cec5SDimitry Andric unsigned AllocSize = offsetof(RopeRefCountString, Data) + AllocChunkSize;
799*0b57cec5SDimitry Andric auto *Res = reinterpret_cast<RopeRefCountString *>(new char[AllocSize]);
800*0b57cec5SDimitry Andric Res->RefCount = 0;
801*0b57cec5SDimitry Andric memcpy(Res->Data, Start, Len);
802*0b57cec5SDimitry Andric AllocBuffer = Res;
803*0b57cec5SDimitry Andric AllocOffs = Len;
804*0b57cec5SDimitry Andric
805*0b57cec5SDimitry Andric return RopePiece(AllocBuffer, 0, Len);
806*0b57cec5SDimitry Andric }
807