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