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