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