[llvm] r240528 - [ADT] Teach DenseMap to support StringRef keys.
Paweł Bylica
chfast at gmail.com
Wed Jun 24 05:00:08 PDT 2015
clang has the same template specialization defined
in clang/lib/Basic/VirtualFileSystem.cpp.
On Wed, Jun 24, 2015 at 12:13 PM Chandler Carruth <chandlerc at gmail.com>
wrote:
> Author: chandlerc
> Date: Wed Jun 24 05:06:29 2015
> New Revision: 240528
>
> URL: http://llvm.org/viewvc/llvm-project?rev=240528&view=rev
> Log:
> [ADT] Teach DenseMap to support StringRef keys.
>
> While often you want to use something specialized like StringMap, when
> the strings already have persistent storage a normal densemap over them
> can be more efficient.
>
> This can't go into StringRef.h because of really obnoxious header chains
> from the hashing code to the endian detection code to CPU feature
> detection code to StringMap.
>
> Modified:
> llvm/trunk/include/llvm/ADT/DenseMapInfo.h
> llvm/trunk/unittests/ADT/DenseMapTest.cpp
>
> Modified: llvm/trunk/include/llvm/ADT/DenseMapInfo.h
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/DenseMapInfo.h?rev=240528&r1=240527&r2=240528&view=diff
>
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/DenseMapInfo.h (original)
> +++ llvm/trunk/include/llvm/ADT/DenseMapInfo.h Wed Jun 24 05:06:29 2015
> @@ -14,6 +14,8 @@
> #ifndef LLVM_ADT_DENSEMAPINFO_H
> #define LLVM_ADT_DENSEMAPINFO_H
>
> +#include "llvm/ADT/Hashing.h"
> +#include "llvm/ADT/StringRef.h"
> #include "llvm/Support/PointerLikeTypeTraits.h"
> #include "llvm/Support/type_traits.h"
>
> @@ -163,6 +165,31 @@ struct DenseMapInfo<std::pair<T, U> > {
> }
> };
>
> +// Provide DenseMapInfo for StringRefs.
> +template <> struct DenseMapInfo<StringRef> {
> + static inline StringRef getEmptyKey() {
> + return StringRef(reinterpret_cast<const char
> *>(~static_cast<uintptr_t>(0)),
> + 0);
> + }
> + static inline StringRef getTombstoneKey() {
> + return StringRef(reinterpret_cast<const char
> *>(~static_cast<uintptr_t>(1)),
> + 0);
> + }
> + static unsigned getHashValue(StringRef 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(StringRef LHS, StringRef 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
>
> Modified: llvm/trunk/unittests/ADT/DenseMapTest.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/DenseMapTest.cpp?rev=240528&r1=240527&r2=240528&view=diff
>
> ==============================================================================
> --- llvm/trunk/unittests/ADT/DenseMapTest.cpp (original)
> +++ llvm/trunk/unittests/ADT/DenseMapTest.cpp Wed Jun 24 05:06:29 2015
> @@ -323,6 +323,31 @@ TYPED_TEST(DenseMapTest, ConstIteratorTe
> EXPECT_TRUE(cit == cit2);
> }
>
> +// Make sure DenseMap works with StringRef keys.
> +TEST(DenseMapCustomTest, StringRefTest) {
> + DenseMap<StringRef, int> M;
> +
> + M["a"] = 1;
> + M["b"] = 2;
> + M["c"] = 3;
> +
> + EXPECT_EQ(3u, M.size());
> + EXPECT_EQ(1, M.lookup("a"));
> + EXPECT_EQ(2, M.lookup("b"));
> + EXPECT_EQ(3, M.lookup("c"));
> +
> + EXPECT_EQ(0, M.lookup("q"));
> +
> + // Test the empty string, spelled various ways.
> + EXPECT_EQ(0, M.lookup(""));
> + EXPECT_EQ(0, M.lookup(StringRef()));
> + EXPECT_EQ(0, M.lookup(StringRef("a", 0)));
> + M[""] = 42;
> + EXPECT_EQ(42, M.lookup(""));
> + EXPECT_EQ(42, M.lookup(StringRef()));
> + EXPECT_EQ(42, M.lookup(StringRef("a", 0)));
> +}
> +
> // Key traits that allows lookup with either an unsigned or char* key;
> // In the latter case, "a" == 0, "b" == 1 and so on.
> struct TestDenseMapInfo {
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150624/b1d2c08a/attachment.html>
More information about the llvm-commits
mailing list