aboutsummaryrefslogtreecommitdiff
path: root/lib/Rewrite/DeltaTree.cpp
blob: d01de31253f2522a88b55c4b488c6b403f8f2fff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//===--- DeltaTree.cpp - B-Tree for Rewrite Delta tracking ----------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the DeltaTree and related classes.
//
//===----------------------------------------------------------------------===//

#include "clang/Rewrite/DeltaTree.h"
#include "llvm/Support/Casting.h"
#include <cstring>
using namespace clang;
using llvm::cast;
using llvm::dyn_cast;

namespace {
  struct SourceDelta;
  class DeltaTreeNode;
  class DeltaTreeInteriorNode;
}

/// The DeltaTree class is a multiway search tree (BTree) structure with some
/// fancy features.  B-Trees are are generally more memory and cache efficient
/// than binary trees, because they store multiple keys/values in each node.
///
/// DeltaTree implements a key/value mapping from FileIndex to Delta, allowing
/// fast lookup by FileIndex.  However, an added (important) bonus is that it
/// can also efficiently tell us the full accumulated delta for a specific
/// file offset as well, without traversing the whole tree.
///
/// The nodes of the tree are made up of instances of two classes:
/// DeltaTreeNode and DeltaTreeInteriorNode.  The later subclasses the
/// former and adds children pointers.  Each node knows the full delta of all
/// entries (recursively) contained inside of it, which allows us to get the
/// full delta implied by a whole subtree in constant time.
  
namespace {
  /// SourceDelta - As code in the original input buffer is added and deleted,
  /// SourceDelta records are used to keep track of how the input SourceLocation
  /// object is mapped into the output buffer.
  struct SourceDelta {
    unsigned FileLoc;
    int Delta;
    
    static SourceDelta get(unsigned Loc, int D) {
      SourceDelta Delta;
      Delta.FileLoc = Loc;
      Delta.Delta = D;
      return Delta;
    }
  };
} // end anonymous namespace

namespace {
  /// DeltaTreeNode - The common part of all nodes.
  ///
  class DeltaTreeNode {
    friend class DeltaTreeInteriorNode;
    
    /// WidthFactor - This controls the number of K/V slots held in the BTree:
    /// how wide it is.  Each level of the BTree is guaranteed to have at least
    /// WidthFactor-1 K/V pairs (unless the whole tree is less full than that)
    /// and may have at most 2*WidthFactor-1 K/V pairs.
    enum { WidthFactor = 8 };
    
    /// Values - This tracks the SourceDelta's currently in this node.
    ///
    SourceDelta Values[2*WidthFactor-1];
    
    /// NumValuesUsed - This tracks the number of values this node currently
    /// holds.
    unsigned char NumValuesUsed;
    
    /// IsLeaf - This is true if this is a leaf of the btree.  If false, this is
    /// an interior node, and is actually an instance of DeltaTreeInteriorNode.
    bool IsLeaf;
    
    /// FullDelta - This is the full delta of all the values in this node and
    /// all children nodes.
    int FullDelta;
  public:
    DeltaTreeNode(bool isLeaf = true)
    : NumValuesUsed(0), IsLeaf(isLeaf), FullDelta(0) {}
    
    bool isLeaf() const { return IsLeaf; }
    int getFullDelta() const { return FullDelta; }
    bool isFull() const { return NumValuesUsed == 2*WidthFactor-1; }
    
    unsigned getNumValuesUsed() const { return NumValuesUsed; }
    const SourceDelta &getValue(unsigned i) const {
      assert(i < NumValuesUsed && "Invalid value #");
      return Values[i];
    }
    SourceDelta &getValue(unsigned i) {
      assert(i < NumValuesUsed && "Invalid value #");
      return Values[i];
    }
    
    /// AddDeltaNonFull - Add a delta to this tree and/or it's children, knowing
    /// that this node is not currently full.
    void AddDeltaNonFull(unsigned FileIndex, int Delta);
    
    /// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a
    /// local walk over our contained deltas.
    void RecomputeFullDeltaLocally();
    
    void Destroy();
    
    static inline bool classof(const DeltaTreeNode *) { return true; }
  };
} // end anonymous namespace

namespace {
  /// DeltaTreeInteriorNode - When isLeaf = false, a node has child pointers.
  /// This class tracks them.
  class DeltaTreeInteriorNode : public DeltaTreeNode {
    DeltaTreeNode *Children[2*WidthFactor];
    ~DeltaTreeInteriorNode() {
      for (unsigned i = 0, e = NumValuesUsed+1; i != e; ++i)
        Children[i]->Destroy();
    }
    friend class DeltaTreeNode;
  public:
    DeltaTreeInteriorNode() : DeltaTreeNode(false /*nonleaf*/) {}
    
    DeltaTreeInteriorNode(DeltaTreeNode *FirstChild)
    : DeltaTreeNode(false /*nonleaf*/) {
      FullDelta = FirstChild->FullDelta;
      Children[0] = FirstChild;
    }
    
    const DeltaTreeNode *getChild(unsigned i) const {
      assert(i < getNumValuesUsed()+1 && "Invalid child");
      return Children[i];
    }
    DeltaTreeNode *getChild(unsigned i) {
      assert(i < getNumValuesUsed()+1 && "Invalid child");
      return Children[i];
    }
    
    static inline bool classof(const DeltaTreeInteriorNode *) { return true; }
    static inline bool classof(const DeltaTreeNode *N) { return !N->isLeaf(); }
  private:
    void SplitChild(unsigned ChildNo);
  };
}


/// Destroy - A 'virtual' destructor.
void DeltaTreeNode::Destroy() {
  if (isLeaf())
    delete this;
  else
    delete cast<DeltaTreeInteriorNode>(this);
}

/// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a
/// local walk over our contained deltas.
void DeltaTreeNode::RecomputeFullDeltaLocally() {
  int NewFullDelta = 0;
  for (unsigned i = 0, e = getNumValuesUsed(); i != e; ++i)
    NewFullDelta += Values[i].Delta;
  if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this))
    for (unsigned i = 0, e = getNumValuesUsed()+1; i != e; ++i)
      NewFullDelta += IN->getChild(i)->getFullDelta();
  FullDelta = NewFullDelta;
}


/// AddDeltaNonFull - Add a delta to this tree and/or it's children, knowing
/// that this node is not currently full.
void DeltaTreeNode::AddDeltaNonFull(unsigned FileIndex, int Delta) {
  assert(!isFull() && "AddDeltaNonFull on a full tree?");
  
  // Maintain full delta for this node.
  FullDelta += Delta;
  
  // Find the insertion point, the first delta whose index is >= FileIndex.
  unsigned i = 0, e = getNumValuesUsed();
  while (i != e && FileIndex > getValue(i).FileLoc)
    ++i;
  
  // If we found an a record for exactly this file index, just merge this
  // value into the preexisting record and finish early.
  if (i != e && getValue(i).FileLoc == FileIndex) {
    // NOTE: Delta could drop to zero here.  This means that the next delta
    // entry is useless and could be removed.  Supporting erases is
    // significantly more complex though, so we just leave an entry with
    // Delta=0 in the tree.
    Values[i].Delta += Delta;
    return;
  }
  
  if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this)) {
    // Insertion into an interior node propagates the value down to a child.
    DeltaTreeNode *Child = IN->getChild(i);
    
    // If the child tree is full, split it, pulling an element up into our
    // node.
    if (Child->isFull()) {
      IN->SplitChild(i);
      SourceDelta &MedianVal = getValue(i);
      
      // If the median value we pulled up is exactly our insert position, add
      // the delta and return.
      if (MedianVal.FileLoc == FileIndex) {
        MedianVal.Delta += Delta;
        return;
      }
      
      // If the median value pulled up is less than our current search point,
      // include those deltas and search down the RHS now.
      if (MedianVal.FileLoc < FileIndex)
        Child = IN->getChild(i+1);
    }
    
    Child->AddDeltaNonFull(FileIndex, Delta);
  } else {
    // For an insertion into a non-full leaf node, just insert the value in
    // its sorted position.  This requires moving later values over.
    if (i != e)
      memmove(&Values[i+1], &Values[i], sizeof(Values[0])*(e-i));
    Values[i] = SourceDelta::get(FileIndex, Delta);
    ++NumValuesUsed;
  }
}

/// SplitChild - At this point, we know that the current node is not full and
/// that the specified child of this node is.  Split the child in half at its
/// median, propagating one value up into us.  Child may be either an interior
/// or leaf node.
void DeltaTreeInteriorNode::SplitChild(unsigned ChildNo) {
  DeltaTreeNode *Child = getChild(ChildNo);
  assert(!isFull() && Child->isFull() && "Inconsistent constraints");
  
  // Since the child is full, it contains 2*WidthFactor-1 values.  We move
  // the first 'WidthFactor-1' values to the LHS child (which we leave in the
  // original child), propagate one value up into us, and move the last
  // 'WidthFactor-1' values into thew RHS child.
  
  // Create the new child node.
  DeltaTreeNode *NewNode;
  if (DeltaTreeInteriorNode *CIN = dyn_cast<DeltaTreeInteriorNode>(Child)) {
    // If the child is an interior node, also move over 'WidthFactor' grand
    // children into the new node.
    NewNode = new DeltaTreeInteriorNode();
    memcpy(&((DeltaTreeInteriorNode*)NewNode)->Children[0],
           &CIN->Children[WidthFactor],
           WidthFactor*sizeof(CIN->Children[0]));