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_REWRITE_CORE_REWRITEROPE_H
15 #define LLVM_CLANG_REWRITE_CORE_REWRITEROPE_H
17 #include "llvm/ADT/IntrusiveRefCntPtr.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/Compiler.h"
26 //===--------------------------------------------------------------------===//
27 // RopeRefCountString Class
28 //===--------------------------------------------------------------------===//
30 /// RopeRefCountString - This struct is allocated with 'new char[]' from the
31 /// heap, and represents a reference counted chunk of string data. When its
32 /// ref count drops to zero, it is delete[]'d. This is primarily managed
33 /// through the RopePiece class below.
34 struct RopeRefCountString {
36 char Data[1]; // Variable sized.
38 void Retain() { ++RefCount; }
41 assert(RefCount > 0 && "Reference count is already zero.");
43 delete [] (char*)this;
47 //===--------------------------------------------------------------------===//
49 //===--------------------------------------------------------------------===//
51 /// RopePiece - This class represents a view into a RopeRefCountString object.
52 /// This allows references to string data to be efficiently chopped up and
53 /// moved around without having to push around the string data itself.
55 /// For example, we could have a 1M RopePiece and want to insert something
56 /// into the middle of it. To do this, we split it into two RopePiece objects
57 /// that both refer to the same underlying RopeRefCountString (just with
58 /// different offsets) which is a nice constant time operation.
60 llvm::IntrusiveRefCntPtr<RopeRefCountString> StrData;
64 RopePiece() : StrData(nullptr), StartOffs(0), EndOffs(0) {}
66 RopePiece(llvm::IntrusiveRefCntPtr<RopeRefCountString> Str, unsigned Start,
68 : StrData(std::move(Str)), StartOffs(Start), EndOffs(End) {}
70 const char &operator[](unsigned Offset) const {
71 return StrData->Data[Offset+StartOffs];
73 char &operator[](unsigned Offset) {
74 return StrData->Data[Offset+StartOffs];
77 unsigned size() const { return EndOffs-StartOffs; }
80 //===--------------------------------------------------------------------===//
81 // RopePieceBTreeIterator Class
82 //===--------------------------------------------------------------------===//
84 /// RopePieceBTreeIterator - This class provides read-only forward iteration
85 /// over bytes that are in a RopePieceBTree. This first iterates over bytes
86 /// in a RopePiece, then iterates over RopePiece's in a RopePieceBTreeLeaf,
87 /// then iterates over RopePieceBTreeLeaf's in a RopePieceBTree.
88 class RopePieceBTreeIterator :
89 public std::iterator<std::forward_iterator_tag, const char, ptrdiff_t> {
90 /// CurNode - The current B+Tree node that we are inspecting.
91 const void /*RopePieceBTreeLeaf*/ *CurNode;
92 /// CurPiece - The current RopePiece in the B+Tree node that we're
94 const RopePiece *CurPiece;
95 /// CurChar - The current byte in the RopePiece we are pointing to.
99 RopePieceBTreeIterator(const void /*RopePieceBTreeNode*/ *N);
101 RopePieceBTreeIterator()
102 : CurNode(nullptr), CurPiece(nullptr), CurChar(0) {}
104 char operator*() const {
105 return (*CurPiece)[CurChar];
108 bool operator==(const RopePieceBTreeIterator &RHS) const {
109 return CurPiece == RHS.CurPiece && CurChar == RHS.CurChar;
111 bool operator!=(const RopePieceBTreeIterator &RHS) const {
112 return !operator==(RHS);
115 RopePieceBTreeIterator& operator++() { // Preincrement
116 if (CurChar+1 < CurPiece->size())
122 inline RopePieceBTreeIterator operator++(int) { // Postincrement
123 RopePieceBTreeIterator tmp = *this; ++*this; return tmp;
126 llvm::StringRef piece() const {
127 return llvm::StringRef(&(*CurPiece)[0], CurPiece->size());
130 void MoveToNextPiece();
133 //===--------------------------------------------------------------------===//
134 // RopePieceBTree Class
135 //===--------------------------------------------------------------------===//
137 class RopePieceBTree {
138 void /*RopePieceBTreeNode*/ *Root;
139 void operator=(const RopePieceBTree &) LLVM_DELETED_FUNCTION;
142 RopePieceBTree(const RopePieceBTree &RHS);
145 typedef RopePieceBTreeIterator iterator;
146 iterator begin() const { return iterator(Root); }
147 iterator end() const { return iterator(); }
148 unsigned size() const;
149 unsigned empty() const { return size() == 0; }
153 void insert(unsigned Offset, const RopePiece &R);
155 void erase(unsigned Offset, unsigned NumBytes);
158 //===--------------------------------------------------------------------===//
160 //===--------------------------------------------------------------------===//
162 /// RewriteRope - A powerful string class. This class supports extremely
163 /// efficient insertions and deletions into the middle of it, even for
164 /// ridiculously long strings.
166 RopePieceBTree Chunks;
168 /// We allocate space for string data out of a buffer of size AllocChunkSize.
169 /// This keeps track of how much space is left.
170 llvm::IntrusiveRefCntPtr<RopeRefCountString> AllocBuffer;
172 enum { AllocChunkSize = 4080 };
175 RewriteRope() : AllocBuffer(nullptr), AllocOffs(AllocChunkSize) {}
176 RewriteRope(const RewriteRope &RHS)
177 : Chunks(RHS.Chunks), AllocBuffer(nullptr), AllocOffs(AllocChunkSize) {
180 typedef RopePieceBTree::iterator iterator;
181 typedef RopePieceBTree::iterator const_iterator;
182 iterator begin() const { return Chunks.begin(); }
183 iterator end() const { return Chunks.end(); }
184 unsigned size() const { return Chunks.size(); }
190 void assign(const char *Start, const char *End) {
193 Chunks.insert(0, MakeRopeString(Start, End));
196 void insert(unsigned Offset, const char *Start, const char *End) {
197 assert(Offset <= size() && "Invalid position to insert!");
198 if (Start == End) return;
199 Chunks.insert(Offset, MakeRopeString(Start, End));
202 void erase(unsigned Offset, unsigned NumBytes) {
203 assert(Offset+NumBytes <= size() && "Invalid region to erase!");
204 if (NumBytes == 0) return;
205 Chunks.erase(Offset, NumBytes);
209 RopePiece MakeRopeString(const char *Start, const char *End);
212 } // end namespace clang