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