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
23 //===--------------------------------------------------------------------===//
24 // RopeRefCountString Class
25 //===--------------------------------------------------------------------===//
27 /// RopeRefCountString - This struct is allocated with 'new char[]' from the
28 /// heap, and represents a reference counted chunk of string data. When its
29 /// ref count drops to zero, it is delete[]'d. This is primarily managed
30 /// through the RopePiece class below.
31 struct RopeRefCountString {
33 char Data[1]; // Variable sized.
40 if (this && --RefCount == 0)
41 delete [] (char*)this;
45 //===--------------------------------------------------------------------===//
47 //===--------------------------------------------------------------------===//
49 /// RopePiece - This class represents a view into a RopeRefCountString object.
50 /// This allows references to string data to be efficiently chopped up and
51 /// moved around without having to push around the string data itself.
53 /// For example, we could have a 1M RopePiece and want to insert something
54 /// into the middle of it. To do this, we split it into two RopePiece objects
55 /// that both refer to the same underlying RopeRefCountString (just with
56 /// different offsets) which is a nice constant time operation.
58 RopeRefCountString *StrData;
62 RopePiece() : StrData(0), StartOffs(0), EndOffs(0) {}
64 RopePiece(RopeRefCountString *Str, unsigned Start, unsigned End)
65 : StrData(Str), StartOffs(Start), EndOffs(End) {
68 RopePiece(const RopePiece &RP)
69 : StrData(RP.StrData), StartOffs(RP.StartOffs), EndOffs(RP.EndOffs) {
77 void operator=(const RopePiece &RHS) {
78 if (StrData != RHS.StrData) {
80 StrData = RHS.StrData;
83 StartOffs = RHS.StartOffs;
84 EndOffs = RHS.EndOffs;
87 const char &operator[](unsigned Offset) const {
88 return StrData->Data[Offset+StartOffs];
90 char &operator[](unsigned Offset) {
91 return StrData->Data[Offset+StartOffs];
94 unsigned size() const { return EndOffs-StartOffs; }
97 //===--------------------------------------------------------------------===//
98 // RopePieceBTreeIterator Class
99 //===--------------------------------------------------------------------===//
101 /// RopePieceBTreeIterator - This class provides read-only forward iteration
102 /// over bytes that are in a RopePieceBTree. This first iterates over bytes
103 /// in a RopePiece, then iterates over RopePiece's in a RopePieceBTreeLeaf,
104 /// then iterates over RopePieceBTreeLeaf's in a RopePieceBTree.
105 class RopePieceBTreeIterator :
106 public std::iterator<std::forward_iterator_tag, const char, ptrdiff_t> {
107 /// CurNode - The current B+Tree node that we are inspecting.
108 const void /*RopePieceBTreeLeaf*/ *CurNode;
109 /// CurPiece - The current RopePiece in the B+Tree node that we're
111 const RopePiece *CurPiece;
112 /// CurChar - The current byte in the RopePiece we are pointing to.
116 RopePieceBTreeIterator(const void /*RopePieceBTreeNode*/ *N);
118 RopePieceBTreeIterator() : CurNode(0), CurPiece(0), CurChar(0) {}
120 char operator*() const {
121 return (*CurPiece)[CurChar];
124 bool operator==(const RopePieceBTreeIterator &RHS) const {
125 return CurPiece == RHS.CurPiece && CurChar == RHS.CurChar;
127 bool operator!=(const RopePieceBTreeIterator &RHS) const {
128 return !operator==(RHS);
131 RopePieceBTreeIterator& operator++() { // Preincrement
132 if (CurChar+1 < CurPiece->size())
138 inline RopePieceBTreeIterator operator++(int) { // Postincrement
139 RopePieceBTreeIterator tmp = *this; ++*this; return tmp;
142 void MoveToNextPiece();
145 //===--------------------------------------------------------------------===//
146 // RopePieceBTree Class
147 //===--------------------------------------------------------------------===//
149 class RopePieceBTree {
150 void /*RopePieceBTreeNode*/ *Root;
151 void operator=(const RopePieceBTree &); // DO NOT IMPLEMENT
154 RopePieceBTree(const RopePieceBTree &RHS);
157 typedef RopePieceBTreeIterator iterator;
158 iterator begin() const { return iterator(Root); }
159 iterator end() const { return iterator(); }
160 unsigned size() const;
161 unsigned empty() const { return size() == 0; }
165 void insert(unsigned Offset, const RopePiece &R);
167 void erase(unsigned Offset, unsigned NumBytes);
170 //===--------------------------------------------------------------------===//
172 //===--------------------------------------------------------------------===//
174 /// RewriteRope - A powerful string class. This class supports extremely
175 /// efficient insertions and deletions into the middle of it, even for
176 /// ridiculously long strings.
178 RopePieceBTree Chunks;
180 /// We allocate space for string data out of a buffer of size AllocChunkSize.
181 /// This keeps track of how much space is left.
182 RopeRefCountString *AllocBuffer;
184 enum { AllocChunkSize = 4080 };
187 RewriteRope() : AllocBuffer(0), AllocOffs(AllocChunkSize) {}
188 RewriteRope(const RewriteRope &RHS)
189 : Chunks(RHS.Chunks), AllocBuffer(0), AllocOffs(AllocChunkSize) {
193 // If we had an allocation buffer, drop our reference to it.
194 AllocBuffer->dropRef();
197 typedef RopePieceBTree::iterator iterator;
198 typedef RopePieceBTree::iterator const_iterator;
199 iterator begin() const { return Chunks.begin(); }
200 iterator end() const { return Chunks.end(); }
201 unsigned size() const { return Chunks.size(); }
207 void assign(const char *Start, const char *End) {
210 Chunks.insert(0, MakeRopeString(Start, End));
213 void insert(unsigned Offset, const char *Start, const char *End) {
214 assert(Offset <= size() && "Invalid position to insert!");
215 if (Start == End) return;
216 Chunks.insert(Offset, MakeRopeString(Start, End));
219 void erase(unsigned Offset, unsigned NumBytes) {
220 assert(Offset+NumBytes <= size() && "Invalid region to erase!");
221 if (NumBytes == 0) return;
222 Chunks.erase(Offset, NumBytes);
226 RopePiece MakeRopeString(const char *Start, const char *End);
229 } // end namespace clang