[llvm-commits] [PATCH 01/20] Add DenseMapInfo for std::string

Eli Friedman eli.friedman at gmail.com
Tue Jul 19 13:33:12 PDT 2011


On Tue, Jul 19, 2011 at 1:11 PM, David Greene <dag at cray.com> wrote:
> Add a DenseMapInfo implementation for std::string.  This is useful
> when mapping pairs that include a string component.
> ---
>  include/llvm/ADT/StringExtras.h |   19 +++++++++++++++++++
>  1 files changed, 19 insertions(+), 0 deletions(-)
>
> diff --git oldinclude/llvm/ADT/StringExtras.h newinclude/llvm/ADT/StringExtras.h
> index 5f5c041..d30e225 100644
> --- oldinclude/llvm/ADT/StringExtras.h
> +++ newinclude/llvm/ADT/StringExtras.h
> @@ -16,6 +16,7 @@
>
>  #include "llvm/Support/DataTypes.h"
>  #include "llvm/ADT/APFloat.h"
> +#include "llvm/ADT/DenseMapInfo.h"
>  #include "llvm/ADT/StringRef.h"
>  #include <cctype>
>  #include <cstdio>
> @@ -165,6 +166,24 @@ static inline unsigned HashString(StringRef Str, unsigned Result = 0) {
>   return Result;
>  }
>
> +// Provide DenseMapInfo for strings.
> +template<> struct DenseMapInfo<std::string> {
> +  static inline std::string getEmptyKey() {
> +    std::string Empty("<<<EMPTY KEY>>>");
> +    return Empty;
> +  }
> +  static inline std::string getTombstoneKey() {
> +    std::string Tombstone("<<<TOMBSTONE KEY>>>");
> +    return Tombstone;
> +  }
> +  static unsigned getHashValue(const std::string& Val) {
> +    return HashString(Val);
> +  }
> +  static bool isEqual(const std::string& LHS, const std::string& RHS) {
> +    return LHS == RHS;
> +  }
> +};
> +
>  } // End llvm namespace
>
>  #endif
> --
> 1.7.6

This is unsafe in general; TableGen might never see the string
"<<<EMPTY KEY>>>", but you can't assume arbitrary code using this
header will not see it.

-Eli




More information about the llvm-commits mailing list