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