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