ATLAS Offline Software
Loading...
Searching...
No Matches
CxxUtils::TransparentStringHash Struct Reference

Transparent hash functor for string-like keys. More...

#include <transparent_string_hash.h>

Collaboration diagram for CxxUtils::TransparentStringHash:

Public Types

using is_transparent = void

Public Member Functions

std::size_t operator() (std::string_view s) const
 Hash a string view.
std::size_t operator() (const std::string &s) const
 Hash a string.
std::size_t operator() (const char *s) const
 Hash a C string.

Detailed Description

Transparent hash functor for string-like keys.

This hash functor allows an associative container with std::string keys to be searched using other string-like types, such as std::string_view or const char*, without first constructing a temporary std::string.

The nested is_transparent type marks this functor as a transparent hash functor, allowing heterogeneous lookup in containers such as std::unordered_map.

Example:

#include <functional>
#include <string>
#include <string_view>
#include <unordered_map>
using CLID = unsigned int;
using NameMap = std::unordered_map<
std::string,
std::equal_to<> >;
NameMap nameMap;
nameMap.emplace("MyClass", 123);
const std::string key = "MyClass";
const std::string_view view = "MyClass";
const char* cstr = "MyClass";
auto fromString = nameMap.find(key);
auto fromStringView = nameMap.find(view);
auto fromCString = nameMap.find(cstr);
uint32_t CLID
The Class ID type.
Transparent hash functor for string-like keys.
Warning
The const char* overload expects a non-null, null-terminated string. For character buffers that are not null-terminated, use std::string_view.

Definition at line 60 of file transparent_string_hash.h.

Member Typedef Documentation

◆ is_transparent

Member Function Documentation

◆ operator()() [1/3]

std::size_t CxxUtils::TransparentStringHash::operator() ( const char * s) const
inline

Hash a C string.

Parameters
sNull-terminated C string to hash.
Returns
Hash value for s.

Definition at line 89 of file transparent_string_hash.h.

89 {
90 return (*this)(std::string_view{s});
91 }

◆ operator()() [2/3]

std::size_t CxxUtils::TransparentStringHash::operator() ( const std::string & s) const
inline

Hash a string.

Parameters
sString to hash.
Returns
Hash value for s.

Definition at line 79 of file transparent_string_hash.h.

79 {
80 return (*this)(std::string_view{s});
81 }

◆ operator()() [3/3]

std::size_t CxxUtils::TransparentStringHash::operator() ( std::string_view s) const
inline

Hash a string view.

Parameters
sString view to hash.
Returns
Hash value for s.

Definition at line 69 of file transparent_string_hash.h.

69 {
70 return std::hash<std::string_view>{}(s);
71 }

The documentation for this struct was generated from the following file: