]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/include/clang/Rewrite/Core/RewriteRope.h
MFC r244628:
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / include / clang / Rewrite / Core / RewriteRope.h
1 //===--- RewriteRope.h - 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 defines the RewriteRope class, which is a powerful string class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_REWRITEROPE_H
15 #define LLVM_CLANG_REWRITEROPE_H
16
17 #include "llvm/Support/Compiler.h"
18
19 #include <cstring>
20 #include <cassert>
21 #include <cstddef>
22 #include <iterator>
23
24 namespace clang {
25   //===--------------------------------------------------------------------===//
26   // RopeRefCountString Class
27   //===--------------------------------------------------------------------===//
28
29   /// RopeRefCountString - This struct is allocated with 'new char[]' from the
30   /// heap, and represents a reference counted chunk of string data.  When its
31   /// ref count drops to zero, it is delete[]'d.  This is primarily managed
32   /// through the RopePiece class below.
33   struct RopeRefCountString {
34     unsigned RefCount;
35     char Data[1];  //  Variable sized.
36
37     void addRef() {
38       ++RefCount;
39     }
40
41     void dropRef() {
42       if (--RefCount == 0)
43         delete [] (char*)this;
44     }
45   };
46
47   //===--------------------------------------------------------------------===//
48   // RopePiece Class
49   //===--------------------------------------------------------------------===//
50
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.
54   ///
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.
59   struct RopePiece {
60     RopeRefCountString *StrData;
61     unsigned StartOffs;
62     unsigned EndOffs;
63
64     RopePiece() : StrData(0), StartOffs(0), EndOffs(0) {}
65
66     RopePiece(RopeRefCountString *Str, unsigned Start, unsigned End)
67       : StrData(Str), StartOffs(Start), EndOffs(End) {
68       if (StrData)
69         StrData->addRef();
70     }
71     RopePiece(const RopePiece &RP)
72       : StrData(RP.StrData), StartOffs(RP.StartOffs), EndOffs(RP.EndOffs) {
73       if (StrData)
74         StrData->addRef();
75     }
76
77     ~RopePiece() {
78       if (StrData)
79         StrData->dropRef();
80     }
81
82     void operator=(const RopePiece &RHS) {
83       if (StrData != RHS.StrData) {
84         if (StrData)
85           StrData->dropRef();
86         StrData = RHS.StrData;
87         if (StrData)
88           StrData->addRef();
89       }
90       StartOffs = RHS.StartOffs;
91       EndOffs = RHS.EndOffs;
92     }
93
94     const char &operator[](unsigned Offset) const {
95       return StrData->Data[Offset+StartOffs];
96     }
97     char &operator[](unsigned Offset) {
98       return StrData->Data[Offset+StartOffs];
99     }
100
101     unsigned size() const { return EndOffs-StartOffs; }
102   };
103
104   //===--------------------------------------------------------------------===//
105   // RopePieceBTreeIterator Class
106   //===--------------------------------------------------------------------===//
107
108   /// RopePieceBTreeIterator - This class provides read-only forward iteration
109   /// over bytes that are in a RopePieceBTree.  This first iterates over bytes
110   /// in a RopePiece, then iterates over RopePiece's in a RopePieceBTreeLeaf,
111   /// then iterates over RopePieceBTreeLeaf's in a RopePieceBTree.
112   class RopePieceBTreeIterator :
113       public std::iterator<std::forward_iterator_tag, const char, ptrdiff_t> {
114     /// CurNode - The current B+Tree node that we are inspecting.
115     const void /*RopePieceBTreeLeaf*/ *CurNode;
116     /// CurPiece - The current RopePiece in the B+Tree node that we're
117     /// inspecting.
118     const RopePiece *CurPiece;
119     /// CurChar - The current byte in the RopePiece we are pointing to.
120     unsigned CurChar;
121   public:
122     // begin iterator.
123     RopePieceBTreeIterator(const void /*RopePieceBTreeNode*/ *N);
124     // end iterator
125     RopePieceBTreeIterator() : CurNode(0), CurPiece(0), CurChar(0) {}
126
127     char operator*() const {
128       return (*CurPiece)[CurChar];
129     }
130
131     bool operator==(const RopePieceBTreeIterator &RHS) const {
132       return CurPiece == RHS.CurPiece && CurChar == RHS.CurChar;
133     }
134     bool operator!=(const RopePieceBTreeIterator &RHS) const {
135       return !operator==(RHS);
136     }
137
138     RopePieceBTreeIterator& operator++() {   // Preincrement
139       if (CurChar+1 < CurPiece->size())
140         ++CurChar;
141       else
142         MoveToNextPiece();
143       return *this;
144     }
145     inline RopePieceBTreeIterator operator++(int) { // Postincrement
146       RopePieceBTreeIterator tmp = *this; ++*this; return tmp;
147     }
148   private:
149     void MoveToNextPiece();
150   };
151
152   //===--------------------------------------------------------------------===//
153   // RopePieceBTree Class
154   //===--------------------------------------------------------------------===//
155
156   class RopePieceBTree {
157     void /*RopePieceBTreeNode*/ *Root;
158     void operator=(const RopePieceBTree &) LLVM_DELETED_FUNCTION;
159   public:
160     RopePieceBTree();
161     RopePieceBTree(const RopePieceBTree &RHS);
162     ~RopePieceBTree();
163
164     typedef RopePieceBTreeIterator iterator;
165     iterator begin() const { return iterator(Root); }
166     iterator end() const { return iterator(); }
167     unsigned size() const;
168     unsigned empty() const { return size() == 0; }
169
170     void clear();
171
172     void insert(unsigned Offset, const RopePiece &R);
173
174     void erase(unsigned Offset, unsigned NumBytes);
175   };
176
177   //===--------------------------------------------------------------------===//
178   // RewriteRope Class
179   //===--------------------------------------------------------------------===//
180
181 /// RewriteRope - A powerful string class.  This class supports extremely
182 /// efficient insertions and deletions into the middle of it, even for
183 /// ridiculously long strings.
184 class RewriteRope {
185   RopePieceBTree Chunks;
186
187   /// We allocate space for string data out of a buffer of size AllocChunkSize.
188   /// This keeps track of how much space is left.
189   RopeRefCountString *AllocBuffer;
190   unsigned AllocOffs;
191   enum { AllocChunkSize = 4080 };
192
193 public:
194   RewriteRope() :  AllocBuffer(0), AllocOffs(AllocChunkSize) {}
195   RewriteRope(const RewriteRope &RHS)
196     : Chunks(RHS.Chunks), AllocBuffer(0), AllocOffs(AllocChunkSize) {
197   }
198
199   ~RewriteRope() {
200     // If we had an allocation buffer, drop our reference to it.
201     if (AllocBuffer)
202       AllocBuffer->dropRef();
203   }
204
205   typedef RopePieceBTree::iterator iterator;
206   typedef RopePieceBTree::iterator const_iterator;
207   iterator begin() const { return Chunks.begin(); }
208   iterator end() const  { return Chunks.end(); }
209   unsigned size() const { return Chunks.size(); }
210
211   void clear() {
212     Chunks.clear();
213   }
214
215   void assign(const char *Start, const char *End) {
216     clear();
217     if (Start != End)
218       Chunks.insert(0, MakeRopeString(Start, End));
219   }
220
221   void insert(unsigned Offset, const char *Start, const char *End) {
222     assert(Offset <= size() && "Invalid position to insert!");
223     if (Start == End) return;
224     Chunks.insert(Offset, MakeRopeString(Start, End));
225   }
226
227   void erase(unsigned Offset, unsigned NumBytes) {
228     assert(Offset+NumBytes <= size() && "Invalid region to erase!");
229     if (NumBytes == 0) return;
230     Chunks.erase(Offset, NumBytes);
231   }
232
233 private:
234   RopePiece MakeRopeString(const char *Start, const char *End);
235 };
236
237 } // end namespace clang
238
239 #endif