Skip to content

Commit 2689aba

Browse files
[IR] Consolidate ValueMap iterator classes (NFC) (#161777)
This patch consolidates ValueMapIterator and ValueMapConstIterator into ValueMapIteratorImpl. ValueMapIteratorImpl takes a boolean template parameter to determine whether it should act as a const iterator or a non-const one. ValueMapIterator and ValueMapConstIterator are now type aliases of ValueMapIteratorImpl.
1 parent 30f7c5d commit 2689aba

File tree

1 file changed

+26
-62
lines changed

1 file changed

+26
-62
lines changed

llvm/include/llvm/IR/ValueMap.h

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ namespace llvm {
4444

4545
template <typename KeyT, typename ValueT, typename Config>
4646
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;
4949

5050
/// This class defines the default behavior for configurable aspects of
5151
/// ValueMap<>. User Configs should inherit from this class to be as compatible
@@ -132,8 +132,8 @@ class ValueMap {
132132
return Where->second.get();
133133
}
134134

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>;
137137

138138
inline iterator begin() { return iterator(Map.begin()); }
139139
inline iterator end() { return iterator(Map.end()); }
@@ -318,8 +318,10 @@ struct DenseMapInfo<ValueMapCallbackVH<KeyT, ValueT, Config>> {
318318
}
319319
};
320320

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>;
323325
using ValueT = typename DenseMapT::mapped_type;
324326

325327
BaseT I;
@@ -331,14 +333,20 @@ template <typename DenseMapT, typename KeyT> class ValueMapIterator {
331333
using pointer = value_type *;
332334
using reference = value_type &;
333335

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()) {}
336344

337345
BaseT base() const { return I; }
338346

339347
struct ValueTypeProxy {
340348
const KeyT first;
341-
ValueT &second;
349+
std::conditional_t<IsConst, const ValueT &, ValueT &> second;
342350

343351
ValueTypeProxy *operator->() { return this; }
344352

@@ -354,69 +362,25 @@ template <typename DenseMapT, typename KeyT> class ValueMapIterator {
354362

355363
ValueTypeProxy operator->() const { return operator*(); }
356364

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; }
359367

360-
inline ValueMapIterator &operator++() { // Preincrement
368+
inline ValueMapIteratorImpl &operator++() { // Preincrement
361369
++I;
362370
return *this;
363371
}
364-
ValueMapIterator operator++(int) { // Postincrement
365-
ValueMapIterator tmp = *this;
372+
ValueMapIteratorImpl operator++(int) { // Postincrement
373+
ValueMapIteratorImpl tmp = *this;
366374
++*this;
367375
return tmp;
368376
}
369377
};
370378

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>;
390381

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>;
420384

421385
} // end namespace llvm
422386

0 commit comments

Comments
 (0)