1 //===--- RewriteRope.h - Rope specialized for rewriter ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the RewriteRope class, which is a powerful string class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_REWRITEROPE_H
15 #define LLVM_CLANG_REWRITEROPE_H
17 #include "llvm/Support/Compiler.h"
24 //===--------------------------------------------------------------------===//
25 // RopeRefCountString Class
26 //===--------------------------------------------------------------------===//
28 /// RopeRefCountString - This struct is allocated with 'new char[]' from the
29 /// heap, and represents a reference counted chunk of string data. When its
30 /// ref count drops to zero, it is delete[]'d. This is primarily managed
31 /// through the RopePiece class below.
32 struct RopeRefCountString {
34 char Data[1]; // Variable sized.
42 delete [] (char*)this;
46 //===--------------------------------------------------------------------===//
48 //===--------------------------------------------------------------------===//
50 /// RopePiece - This class represents a view into a RopeRefCountString object.
51 /// This allows references to string data to be efficiently chopped up and
52 /// moved around without having to push around the string data itself.
54 /// For example, we could have a 1M RopePiece and want to insert something
55 /// into the middle of it. To do this, we split it into two RopePiece objects
56 /// that both refer to the same underlying RopeRefCountString (just with
57 /// different offsets) which is a nice constant time operation.
59 RopeRefCountString *StrData;
63 RopePiece() : StrData(0), StartOffs(0), EndOffs(0) {}
65 RopePiece(RopeRefCountString *Str, unsigned Start, unsigned End)
66 : StrData(Str), StartOffs(Start), EndOffs(End) {
70 RopePiece(const RopePiece &RP)
71 : StrData(RP.StrData), StartOffs(RP.StartOffs), EndOffs(RP.EndOffs) {
81 void operator=(const RopePiece &RHS) {
82 if (StrData != RHS.StrData) {
85 StrData = RHS.StrData;
89 StartOffs = RHS.StartOffs;
90 EndOffs = RHS.EndOffs;
93 const char &operator[](unsigned Offset) const {
94 return StrData->Data[Offset+StartOffs];
96 char &operator[](unsigned Offset) {
97 return StrData->Data[Offset+StartOffs];
100 unsigned size() const { return EndOffs-StartOffs; }
103 //===--------------------------------------------------------------------===//
104 // RopePieceBTreeIterator Class
105 //===--------------------------------------------------------------------===//
107 /// RopePieceBTreeIterator - This class provides read-only forward iteration
108 /// over bytes that are in a RopePieceBTree. This first iterates over bytes
109 /// in a RopePiece, then iterates over RopePiece's in a RopePieceBTreeLeaf,
110 /// then iterates over RopePieceBTreeLeaf's in a RopePieceBTree.
111 class RopePieceBTreeIterator :
112 public std::iterator<std::forward_iterator_tag, const char, ptrdiff_t> {
113 /// CurNode - The current B+Tree node that we are inspecting.
114 const void /*RopePieceBTreeLeaf*/ *CurNode;
115 /// CurPiece - The current RopePiece in the B+Tree node that we're
117 const RopePiece *CurPiece;
118 /// CurChar - The current byte in the RopePiece we are pointing to.
122 RopePieceBTreeIterator(const void /*RopePieceBTreeNode*/ *N);
124 RopePieceBTreeIterator() : CurNode(0), CurPiece(0), CurChar(0) {}
126 char operator*() const {
127 return (*CurPiece)[CurChar];
130 bool operator==(const RopePieceBTreeIterator &RHS) const {
131 return CurPiece == RHS.CurPiece && CurChar == RHS.CurChar;
133 bool operator!=(const RopePieceBTreeIterator &RHS) const {
134 return !operator==(RHS);
137 RopePieceBTreeIterator& operator++() { // Preincrement
138 if (CurChar+1 < CurPiece->size())
144 inline RopePieceBTreeIterator operator++(int) { // Postincrement
145 RopePieceBTreeIterator tmp = *this; ++*this; return tmp;
148 void MoveToNextPiece();
151 //===--------------------------------------------------------------------===//
152 // RopePieceBTree Class
153 //===--------------------------------------------------------------------===//
155 class RopePieceBTree {
156 void /*RopePieceBTreeNode*/ *Root;
157 void operator=(const RopePieceBTree &) LLVM_DELETED_FUNCTION;
160 RopePieceBTree(const RopePieceBTree &RHS);
163 typedef RopePieceBTreeIterator iterator;
164 iterator begin() const { return iterator(Root); }
165 iterator end() const { return iterator(); }
166 unsigned size() const;
167 unsigned empty() const { return size() == 0; }
171 void insert(unsigned Offset, const RopePiece &R);
173 void erase(unsigned Offset, unsigned NumBytes);
176 //===--------------------------------------------------------------------===//
178 //===--------------------------------------------------------------------===//
180 /// RewriteRope - A powerful string class. This class supports extremely
181 /// efficient insertions and deletions into the middle of it, even for
182 /// ridiculously long strings.
184 RopePieceBTree Chunks;
186 /// We allocate space for string data out of a buffer of size AllocChunkSize.
187 /// This keeps track of how much space is left.
188 RopeRefCountString *AllocBuffer;
190 enum { AllocChunkSize = 4080 };
193 RewriteRope() : AllocBuffer(0), AllocOffs(AllocChunkSize) {}
194 RewriteRope(const RewriteRope &RHS)
195 : Chunks(RHS.Chunks), AllocBuffer(0), AllocOffs(AllocChunkSize) {
199 // If we had an allocation buffer, drop our reference to it.
201 AllocBuffer->dropRef();
204 typedef RopePieceBTree::iterator iterator;
205 typedef RopePieceBTree::iterator const_iterator;
206 iterator begin() const { return Chunks.begin(); }
207 iterator end() const { return Chunks.end(); }
208 unsigned size() const { return Chunks.size(); }
214 void assign(const char *Start, const char *End) {
217 Chunks.insert(0, MakeRopeString(Start, End));
220 void insert(unsigned Offset, const char *Start, const char *End) {
221 assert(Offset <= size() && "Invalid position to insert!");
222 if (Start == End) return;
223 Chunks.insert(Offset, MakeRopeString(Start, End));
226 void erase(unsigned Offset, unsigned NumBytes) {
227 assert(Offset+NumBytes <= size() && "Invalid region to erase!");
228 if (NumBytes == 0) return;
229 Chunks.erase(Offset, NumBytes);
233 RopePiece MakeRopeString(const char *Start, const char *End);
236 } // end namespace clang