Skip to content

Commit 8869a6e

Browse files
[ADT] Modernize IndexedMap with llvm::identity_cxx20 (NFC)
C++ is evolving in the direction of removing type alias "argument_type" from functors. This patch changes the default functor type to llvm::identity_cxx20, backported from C++20, while templatizing those methods that use toIndex_ with IndexT. This way we no longer need to take argument_type from ToIndexT. Now, identity_cxx20()(int) is of type int, causing mixed sign comparisons, so this patch first assigns the result to a temporary variable of type unsigned before feeding it to assert.
1 parent 1c04cee commit 8869a6e

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

llvm/include/llvm/ADT/IndexedMap.h

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,14 @@
2020
#ifndef LLVM_ADT_INDEXEDMAP_H
2121
#define LLVM_ADT_INDEXEDMAP_H
2222

23-
#include "llvm/ADT/STLExtras.h"
2423
#include "llvm/ADT/STLForwardCompat.h"
2524
#include "llvm/ADT/SmallVector.h"
2625
#include <cassert>
27-
#include <type_traits>
2826

2927
namespace llvm {
3028

31-
namespace detail {
32-
// Helper to compute the IndexT for IndexedMap.
33-
template <typename ToIndexT, typename = void> struct DeduceIndexType {
34-
using type =
35-
std::conditional_t<std::is_same_v<ToIndexT, llvm::identity_cxx20>,
36-
unsigned, void>;
37-
};
38-
39-
template <typename ToIndexT>
40-
struct DeduceIndexType<ToIndexT,
41-
std::void_t<typename ToIndexT::argument_type>> {
42-
using type = typename ToIndexT::argument_type;
43-
};
44-
} // namespace detail
45-
46-
template <typename T, typename ToIndexT = llvm::identity_cxx20,
47-
typename IndexT = typename detail::DeduceIndexType<ToIndexT>::type>
29+
template <typename T, typename ToIndexT = llvm::identity_cxx20>
4830
class IndexedMap {
49-
static_assert(!std::is_same_v<IndexT, void>,
50-
"Could not deduce index type from the provided functor.");
5131
// Prefer SmallVector with zero inline storage over std::vector. IndexedMaps
5232
// can grow very large and SmallVector grows more efficiently as long as T
5333
// is trivially copyable.
@@ -62,14 +42,17 @@ class IndexedMap {
6242

6343
explicit IndexedMap(const T &val) : nullVal_(val) {}
6444

65-
typename StorageT::reference operator[](IndexT n) {
66-
assert(toIndex_(n) < storage_.size() && "index out of bounds!");
67-
return storage_[toIndex_(n)];
45+
template <typename IndexT> typename StorageT::reference operator[](IndexT n) {
46+
unsigned Index = toIndex_(n);
47+
assert(Index < storage_.size() && "index out of bounds!");
48+
return storage_[Index];
6849
}
6950

51+
template <typename IndexT>
7052
typename StorageT::const_reference operator[](IndexT n) const {
71-
assert(toIndex_(n) < storage_.size() && "index out of bounds!");
72-
return storage_[toIndex_(n)];
53+
unsigned Index = toIndex_(n);
54+
assert(Index < storage_.size() && "index out of bounds!");
55+
return storage_[Index];
7356
}
7457

7558
void reserve(typename StorageT::size_type s) { storage_.reserve(s); }
@@ -78,13 +61,16 @@ class IndexedMap {
7861

7962
void clear() { storage_.clear(); }
8063

81-
void grow(IndexT n) {
64+
template <typename IndexT> void grow(IndexT n) {
8265
unsigned NewSize = toIndex_(n) + 1;
8366
if (NewSize > storage_.size())
8467
resize(NewSize);
8568
}
8669

87-
bool inBounds(IndexT n) const { return toIndex_(n) < storage_.size(); }
70+
template <typename IndexT> bool inBounds(IndexT n) const {
71+
unsigned Index = toIndex_(n);
72+
return Index < storage_.size();
73+
}
8874

8975
typename StorageT::size_type size() const { return storage_.size(); }
9076
};

0 commit comments

Comments
 (0)