[PATCH] D13770: Add hashing and DenseMapInfo for ArrayRef

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 16 15:01:44 PDT 2015


Looks good! (optional unit tests - no doubt similar things don't bother
with them - but could be nice)

On Thu, Oct 15, 2015 at 7:06 AM, Rafael Ávila de Espíndola <
llvm-commits at lists.llvm.org> wrote:

> rafael created this revision.
> rafael added a reviewer: chandlerc.
> rafael added a subscriber: llvm-commits.
> rafael set the repository for this revision to rL LLVM.
>
> Sometimes it is more natural to use a ArrayRef<uint8_t> than a StringRef
> to represent a range of bytes that is not, semantically, a string.
>
>
> Repository:
>   rL LLVM
>
> http://reviews.llvm.org/D13770
>
> Files:
>   include/llvm/ADT/ArrayRef.h
>   include/llvm/ADT/DenseMapInfo.h
>
> Index: include/llvm/ADT/DenseMapInfo.h
> ===================================================================
> --- include/llvm/ADT/DenseMapInfo.h
> +++ include/llvm/ADT/DenseMapInfo.h
> @@ -14,6 +14,7 @@
>  #ifndef LLVM_ADT_DENSEMAPINFO_H
>  #define LLVM_ADT_DENSEMAPINFO_H
>
> +#include "llvm/ADT/ArrayRef.h"
>  #include "llvm/ADT/Hashing.h"
>  #include "llvm/ADT/StringRef.h"
>  #include "llvm/Support/PointerLikeTypeTraits.h"
> @@ -190,6 +191,31 @@
>    }
>  };
>
> +// Provide DenseMapInfo for ArrayRefs.
> +template <typename T> struct DenseMapInfo<ArrayRef<T>> {
> +  static inline ArrayRef<T> getEmptyKey() {
> +    return ArrayRef<T>(reinterpret_cast<const T
> *>(~static_cast<uintptr_t>(0)),
> +                       size_t(0));
> +  }
> +  static inline ArrayRef<T> getTombstoneKey() {
> +    return ArrayRef<T>(reinterpret_cast<const T
> *>(~static_cast<uintptr_t>(1)),
> +                       size_t(0));
> +  }
> +  static unsigned getHashValue(ArrayRef<T> Val) {
> +    assert(Val.data() != getEmptyKey().data() && "Cannot hash the empty
> key!");
> +    assert(Val.data() != getTombstoneKey().data() &&
> +           "Cannot hash the tombstone key!");
> +    return (unsigned)(hash_value(Val));
> +  }
> +  static bool isEqual(ArrayRef<T> LHS, ArrayRef<T> RHS) {
> +    if (RHS.data() == getEmptyKey().data())
> +      return LHS.data() == getEmptyKey().data();
> +    if (RHS.data() == getTombstoneKey().data())
> +      return LHS.data() == getTombstoneKey().data();
> +    return LHS == RHS;

+  }
> +};
> +
>  } // end namespace llvm
>
>  #endif
> Index: include/llvm/ADT/ArrayRef.h
> ===================================================================
> --- include/llvm/ADT/ArrayRef.h
> +++ include/llvm/ADT/ArrayRef.h
> @@ -10,6 +10,7 @@
>  #ifndef LLVM_ADT_ARRAYREF_H
>  #define LLVM_ADT_ARRAYREF_H
>
> +#include "llvm/ADT/Hashing.h"
>  #include "llvm/ADT/None.h"
>  #include "llvm/ADT/SmallVector.h"
>  #include <vector>
> @@ -374,6 +375,10 @@
>    template <typename T> struct isPodLike<ArrayRef<T> > {
>      static const bool value = true;
>    };
> +
> +  template <typename T> hash_code hash_value(ArrayRef<T> S) {
> +    return hash_combine_range(S.begin(), S.end());
> +  }
>  }
>
>  #endif
>
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151016/b9e1f7cb/attachment.html>


More information about the llvm-commits mailing list