@@ -44,8 +44,8 @@ namespace llvm {
44
44
45
45
template <typename KeyT, typename ValueT, typename Config>
46
46
class ValueMapCallbackVH ;
47
- template <typename DenseMapT, typename KeyT> class ValueMapIterator ;
48
- template < typename DenseMapT, typename KeyT> class ValueMapConstIterator ;
47
+ template <typename DenseMapT, typename KeyT, bool IsConst>
48
+ class ValueMapIteratorImpl ;
49
49
50
50
// / This class defines the default behavior for configurable aspects of
51
51
// / ValueMap<>. User Configs should inherit from this class to be as compatible
@@ -132,8 +132,8 @@ class ValueMap {
132
132
return Where->second .get ();
133
133
}
134
134
135
- using iterator = ValueMapIterator <MapT, KeyT>;
136
- using const_iterator = ValueMapConstIterator <MapT, KeyT>;
135
+ using iterator = ValueMapIteratorImpl <MapT, KeyT, false >;
136
+ using const_iterator = ValueMapIteratorImpl <MapT, KeyT, true >;
137
137
138
138
inline iterator begin () { return iterator (Map.begin ()); }
139
139
inline iterator end () { return iterator (Map.end ()); }
@@ -318,8 +318,10 @@ struct DenseMapInfo<ValueMapCallbackVH<KeyT, ValueT, Config>> {
318
318
}
319
319
};
320
320
321
- template <typename DenseMapT, typename KeyT> class ValueMapIterator {
322
- using BaseT = typename DenseMapT::iterator;
321
+ template <typename DenseMapT, typename KeyT, bool IsConst>
322
+ class ValueMapIteratorImpl {
323
+ using BaseT = std::conditional_t <IsConst, typename DenseMapT::const_iterator,
324
+ typename DenseMapT::iterator>;
323
325
using ValueT = typename DenseMapT::mapped_type;
324
326
325
327
BaseT I;
@@ -331,14 +333,20 @@ template <typename DenseMapT, typename KeyT> class ValueMapIterator {
331
333
using pointer = value_type *;
332
334
using reference = value_type &;
333
335
334
- ValueMapIterator () : I() {}
335
- ValueMapIterator (BaseT I) : I(I) {}
336
+ ValueMapIteratorImpl () = default ;
337
+ ValueMapIteratorImpl (BaseT I) : I(I) {}
338
+
339
+ // Allow conversion from iterator to const_iterator.
340
+ template <bool C = IsConst, typename = std::enable_if_t <C>>
341
+ ValueMapIteratorImpl (
342
+ const ValueMapIteratorImpl<DenseMapT, KeyT, false > &Other)
343
+ : I(Other.base()) {}
336
344
337
345
BaseT base () const { return I; }
338
346
339
347
struct ValueTypeProxy {
340
348
const KeyT first;
341
- ValueT &second;
349
+ std:: conditional_t <IsConst, const ValueT &, ValueT &> second;
342
350
343
351
ValueTypeProxy *operator ->() { return this ; }
344
352
@@ -354,69 +362,25 @@ template <typename DenseMapT, typename KeyT> class ValueMapIterator {
354
362
355
363
ValueTypeProxy operator ->() const { return operator *(); }
356
364
357
- bool operator ==(const ValueMapIterator &RHS) const { return I == RHS.I ; }
358
- bool operator !=(const ValueMapIterator &RHS) const { return I != RHS.I ; }
365
+ bool operator ==(const ValueMapIteratorImpl &RHS) const { return I == RHS.I ; }
366
+ bool operator !=(const ValueMapIteratorImpl &RHS) const { return I != RHS.I ; }
359
367
360
- inline ValueMapIterator &operator ++() { // Preincrement
368
+ inline ValueMapIteratorImpl &operator ++() { // Preincrement
361
369
++I;
362
370
return *this ;
363
371
}
364
- ValueMapIterator operator ++(int ) { // Postincrement
365
- ValueMapIterator tmp = *this ;
372
+ ValueMapIteratorImpl operator ++(int ) { // Postincrement
373
+ ValueMapIteratorImpl tmp = *this ;
366
374
++*this ;
367
375
return tmp;
368
376
}
369
377
};
370
378
371
- template <typename DenseMapT, typename KeyT> class ValueMapConstIterator {
372
- using BaseT = typename DenseMapT::const_iterator;
373
- using ValueT = typename DenseMapT::mapped_type;
374
-
375
- BaseT I;
376
-
377
- public:
378
- using iterator_category = std::forward_iterator_tag;
379
- using value_type = std::pair<KeyT, typename DenseMapT::mapped_type>;
380
- using difference_type = std::ptrdiff_t ;
381
- using pointer = value_type *;
382
- using reference = value_type &;
383
-
384
- ValueMapConstIterator () : I() {}
385
- ValueMapConstIterator (BaseT I) : I(I) {}
386
- ValueMapConstIterator (ValueMapIterator<DenseMapT, KeyT> Other)
387
- : I(Other.base()) {}
388
-
389
- BaseT base () const { return I; }
379
+ template <typename DenseMapT, typename KeyT>
380
+ using ValueMapIterator = ValueMapIteratorImpl<DenseMapT, KeyT, false >;
390
381
391
- struct ValueTypeProxy {
392
- const KeyT first;
393
- const ValueT &second;
394
- ValueTypeProxy *operator ->() { return this ; }
395
- operator std::pair<KeyT, ValueT>() const {
396
- return std::make_pair (first, second);
397
- }
398
- };
399
-
400
- ValueTypeProxy operator *() const {
401
- ValueTypeProxy Result = {I->first .Unwrap (), I->second };
402
- return Result;
403
- }
404
-
405
- ValueTypeProxy operator ->() const { return operator *(); }
406
-
407
- bool operator ==(const ValueMapConstIterator &RHS) const { return I == RHS.I ; }
408
- bool operator !=(const ValueMapConstIterator &RHS) const { return I != RHS.I ; }
409
-
410
- inline ValueMapConstIterator &operator ++() { // Preincrement
411
- ++I;
412
- return *this ;
413
- }
414
- ValueMapConstIterator operator ++(int ) { // Postincrement
415
- ValueMapConstIterator tmp = *this ;
416
- ++*this ;
417
- return tmp;
418
- }
419
- };
382
+ template <typename DenseMapT, typename KeyT>
383
+ using ValueMapConstIterator = ValueMapIteratorImpl<DenseMapT, KeyT, true >;
420
384
421
385
} // end namespace llvm
422
386
0 commit comments