|
|
@@ -22,7 +22,13 @@ namespace Carbon {
|
|
|
// `KeyT` can optionally be different from `ValueT`, and if so is used for the
|
|
|
// argument to `Lookup`. It must be valid to use both `KeyT` and `ValueT` as
|
|
|
// lookup types in the underlying `Set`.
|
|
|
-template <typename IdT, typename KeyT, typename ValueT = KeyT>
|
|
|
+template <typename IdT, typename KeyT, typename ValueT = KeyT,
|
|
|
+ // Parentheses around the lambda to help clang-format.
|
|
|
+ auto ValueToKeyFn =
|
|
|
+ ([](typename ValueStoreTypes<ValueT>::ConstRefType value) ->
|
|
|
+ typename ValueStoreTypes<ValueT>::ConstRefType {
|
|
|
+ return value;
|
|
|
+ })>
|
|
|
class CanonicalValueStore {
|
|
|
public:
|
|
|
using KeyType = std::remove_cvref_t<KeyT>;
|
|
|
@@ -75,8 +81,8 @@ class CanonicalValueStore {
|
|
|
Set<IdT, /*SmallSize=*/0, KeyContext> set_;
|
|
|
};
|
|
|
|
|
|
-template <typename IdT, typename KeyT, typename ValueT>
|
|
|
-class CanonicalValueStore<IdT, KeyT, ValueT>::KeyContext
|
|
|
+template <typename IdT, typename KeyT, typename ValueT, auto ValueToKeyFn>
|
|
|
+class CanonicalValueStore<IdT, KeyT, ValueT, ValueToKeyFn>::KeyContext
|
|
|
: public TranslatingKeyContext<KeyContext> {
|
|
|
public:
|
|
|
explicit KeyContext(const ValueStore<IdT, ValueType>* values)
|
|
|
@@ -84,28 +90,34 @@ class CanonicalValueStore<IdT, KeyT, ValueT>::KeyContext
|
|
|
|
|
|
// Note that it is safe to return a `const` reference here as the underlying
|
|
|
// object's lifetime is provided by the `ValueStore`.
|
|
|
- auto TranslateKey(IdT id) const -> ConstRefType { return values_->Get(id); }
|
|
|
+ auto TranslateKey(IdT id) const
|
|
|
+ -> llvm::function_traits<decltype(ValueToKeyFn)>::result_t {
|
|
|
+ return ValueToKeyFn(values_->Get(id));
|
|
|
+ }
|
|
|
|
|
|
private:
|
|
|
const ValueStore<IdT, ValueType>* values_;
|
|
|
};
|
|
|
|
|
|
-template <typename IdT, typename KeyT, typename ValueT>
|
|
|
-auto CanonicalValueStore<IdT, KeyT, ValueT>::Add(ValueType value) -> IdT {
|
|
|
+template <typename IdT, typename KeyT, typename ValueT, auto ValueToKeyFn>
|
|
|
+auto CanonicalValueStore<IdT, KeyT, ValueT, ValueToKeyFn>::Add(ValueType value)
|
|
|
+ -> IdT {
|
|
|
auto make_key = [&] { return IdT(values_.Add(std::move(value))); };
|
|
|
- return set_.Insert(value, make_key, KeyContext(&values_)).key();
|
|
|
+ return set_.Insert(ValueToKeyFn(value), make_key, KeyContext(&values_)).key();
|
|
|
}
|
|
|
|
|
|
-template <typename IdT, typename KeyT, typename ValueT>
|
|
|
-auto CanonicalValueStore<IdT, KeyT, ValueT>::Lookup(KeyType key) const -> IdT {
|
|
|
+template <typename IdT, typename KeyT, typename ValueT, auto ValueToKeyFn>
|
|
|
+auto CanonicalValueStore<IdT, KeyT, ValueT, ValueToKeyFn>::Lookup(
|
|
|
+ KeyType key) const -> IdT {
|
|
|
if (auto result = set_.Lookup(key, KeyContext(&values_))) {
|
|
|
return result.key();
|
|
|
}
|
|
|
return IdT::None;
|
|
|
}
|
|
|
|
|
|
-template <typename IdT, typename KeyT, typename ValueT>
|
|
|
-auto CanonicalValueStore<IdT, KeyT, ValueT>::Reserve(size_t size) -> void {
|
|
|
+template <typename IdT, typename KeyT, typename ValueT, auto ValueToKeyFn>
|
|
|
+auto CanonicalValueStore<IdT, KeyT, ValueT, ValueToKeyFn>::Reserve(size_t size)
|
|
|
+ -> void {
|
|
|
// Compute the resulting new insert count using the size of values -- the
|
|
|
// set doesn't have a fast to compute current size.
|
|
|
if (size > values_.size()) {
|