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