The Gaudi Framework  master (fcd9f667)
Loading...
Searching...
No Matches
Gaudi::StringKey Class Reference

Helper class for efficient "key" access for strings. More...

#include </builds/gaudi/Gaudi/GaudiKernel/include/GaudiKernel/StringKey.h>

Collaboration diagram for Gaudi::StringKey:

Public Member Functions

 StringKey ()
 Default constructor from empty string.
 StringKey (const char *key)
 constructor from plain C-string, perform hashing
 StringKey (std::string_view key)
 constructor from std::string_view, perform hashing
 StringKey (std::string key)
 constructor from std::string, perform hashing
const std::string & str () const
 the actual string
 operator const std::string & () const
 implicit cast to std::string
 operator std::string_view () const
 implicit cast to std::string_view
bool empty () const
 empty key?
bool operator! () const
 empty key?
bool operator== (const StringKey &) const =default
 Compiler generated equality operator first comparing hash, then string (see below).
auto operator<=> (const StringKey &) const =default
 Compiler generated spaceship operator first comparing hash, then string (see below).
bool operator== (const char *rhs) const
 Comparison to other string-like types.
bool operator== (std::string_view rhs) const
bool operator== (const std::string &rhs) const
auto operator<=> (const char *rhs) const
auto operator<=> (std::string_view rhs) const
auto operator<=> (const std::string &rhs) const
std::size_t __hash__ () const
 the actual access to the hash
const std::string & __str__ () const
 the representation of the object
std::string __repr__ () const
 the representation of the object
bool __eq__ (const StringKey &right) const
 equality operator for python
bool __eq__ (std::string_view right) const
 equality operators for python
bool __neq__ (const StringKey &right) const
 non-equality operator for python
bool __neq__ (const std::string_view right) const
 non-equality operator for python
std::string toString () const
 string representation (for properties)

Private Attributes

std::size_t m_hash
 the hash
std::string m_str
 the actual string

Friends

std::string operator+ (const std::string &lhs, const Gaudi::StringKey &rhs)
std::string operator+ (const char *lhs, const Gaudi::StringKey &rhs)
std::string operator+ (const Gaudi::StringKey &lhs, const std::string &rhs)
std::string operator+ (const Gaudi::StringKey &lhs, const char *rhs)

Detailed Description

Helper class for efficient "key" access for strings.

Multiple lookups of the same key can be speed up by using a pre-computed key:

std::map<StringKey, double> m = {...};
const StringKey& key("SomeLongKey");
// EFFICIENT:
auto i1 = m.find ( key ) ;
// CAN BE VERY INEFICIENT:
auto i2 = m_find( "SomeLongKey" );
Helper class for efficient "key" access for strings.
Definition StringKey.h:68

The class also supports heterogeneous lookups without the need to construct a temporary StringKey object:

std::unordered_set<Gaudi::StringKey, Gaudi::StringKeyHash, std::equal_to<>> s = {...};
s.contains("foo");
Attention
NEVER use the actual hash value for anything stored in files, as it is not guaranteed that the hashing scheme will remain the same.
Author
Vanya BELYAEV Ivan..nosp@m.Bely.nosp@m.aev@n.nosp@m.ikhe.nosp@m.f.nl
Gerhard Raven (based on stringKey)
Frank Winklmeier
Date
2009-04-08

Definition at line 68 of file StringKey.h.

Constructor & Destructor Documentation

◆ StringKey() [1/4]

Gaudi::StringKey::StringKey ( )
inline

Default constructor from empty string.

Definition at line 71 of file StringKey.h.

71: StringKey( std::string_view{} ) {}
StringKey()
Default constructor from empty string.
Definition StringKey.h:71

◆ StringKey() [2/4]

Gaudi::StringKey::StringKey ( const char * key)
inline

constructor from plain C-string, perform hashing

Definition at line 73 of file StringKey.h.

73: StringKey( std::string_view{ key } ) {}

◆ StringKey() [3/4]

Gaudi::StringKey::StringKey ( std::string_view key)
inline

constructor from std::string_view, perform hashing

Definition at line 75 of file StringKey.h.

75: m_hash{ StringKeyHash()( key ) }, m_str( key ) {}
std::string m_str
the actual string
Definition StringKey.h:146
std::size_t m_hash
the hash
Definition StringKey.h:143

◆ StringKey() [4/4]

Gaudi::StringKey::StringKey ( std::string key)
inline

constructor from std::string, perform hashing

Definition at line 77 of file StringKey.h.

77: m_hash{ StringKeyHash()( key ) }, m_str( std::move( key ) ) {}

Member Function Documentation

◆ __eq__() [1/2]

bool Gaudi::StringKey::__eq__ ( const StringKey & right) const

equality operator for python

Definition at line 25 of file StringKey.cpp.

25{ return *this == right; }

◆ __eq__() [2/2]

bool Gaudi::StringKey::__eq__ ( std::string_view right) const

equality operators for python

Definition at line 26 of file StringKey.cpp.

26{ return *this == right; }

◆ __hash__()

std::size_t Gaudi::StringKey::__hash__ ( ) const
inline

the actual access to the hash

Attention
NEVER use the actual hash value for anything stored in files, as it is not guaranteed that the hashing scheme will remain the same. The two reason for this function are:
  • transparent usage of this object for hashmap-like containers
  • Python @reutrn the actual hash value

Definition at line 113 of file StringKey.h.

113{ return m_hash; }

◆ __neq__() [1/2]

bool Gaudi::StringKey::__neq__ ( const std::string_view right) const

non-equality operator for python

Definition at line 28 of file StringKey.cpp.

28{ return *this != right; }

◆ __neq__() [2/2]

bool Gaudi::StringKey::__neq__ ( const StringKey & right) const

non-equality operator for python

Definition at line 27 of file StringKey.cpp.

27{ return *this != right; }

◆ __repr__()

std::string Gaudi::StringKey::__repr__ ( ) const

the representation of the object

Definition at line 24 of file StringKey.cpp.

24{ return toString(); }
std::string toString() const
string representation (for properties)
Definition StringKey.cpp:22

◆ __str__()

const std::string & Gaudi::StringKey::__str__ ( ) const

the representation of the object

Definition at line 23 of file StringKey.cpp.

23{ return m_str; }

◆ empty()

bool Gaudi::StringKey::empty ( ) const
inline

empty key?

Definition at line 86 of file StringKey.h.

86{ return m_str.empty(); }

◆ operator const std::string &()

Gaudi::StringKey::operator const std::string & ( ) const
inline

implicit cast to std::string

Definition at line 82 of file StringKey.h.

82{ return m_str; }

◆ operator std::string_view()

Gaudi::StringKey::operator std::string_view ( ) const
inline

implicit cast to std::string_view

Definition at line 84 of file StringKey.h.

84{ return m_str; }

◆ operator!()

bool Gaudi::StringKey::operator! ( ) const
inline

empty key?

Definition at line 88 of file StringKey.h.

88{ return empty(); }
bool empty() const
empty key?
Definition StringKey.h:86

◆ operator<=>() [1/4]

auto Gaudi::StringKey::operator<=> ( const char * rhs) const
inline

Definition at line 100 of file StringKey.h.

100=>( const char* rhs ) const { return m_str <=> rhs; }

◆ operator<=>() [2/4]

auto Gaudi::StringKey::operator<=> ( const std::string & rhs) const
inline

Definition at line 102 of file StringKey.h.

102=>( const std::string& rhs ) const { return m_str <=> rhs; }

◆ operator<=>() [3/4]

auto Gaudi::StringKey::operator<=> ( const StringKey & ) const
default

Compiler generated spaceship operator first comparing hash, then string (see below).

◆ operator<=>() [4/4]

auto Gaudi::StringKey::operator<=> ( std::string_view rhs) const
inline

Definition at line 101 of file StringKey.h.

101=>( std::string_view rhs ) const { return m_str <=> rhs; }

◆ operator==() [1/4]

bool Gaudi::StringKey::operator== ( const char * rhs) const
inline

Comparison to other string-like types.

Definition at line 96 of file StringKey.h.

96{ return m_str == rhs; }

◆ operator==() [2/4]

bool Gaudi::StringKey::operator== ( const std::string & rhs) const
inline

Definition at line 98 of file StringKey.h.

98{ return m_str == rhs; }

◆ operator==() [3/4]

bool Gaudi::StringKey::operator== ( const StringKey & ) const
default

Compiler generated equality operator first comparing hash, then string (see below).

◆ operator==() [4/4]

bool Gaudi::StringKey::operator== ( std::string_view rhs) const
inline

Definition at line 97 of file StringKey.h.

97{ return m_str == rhs; }

◆ str()

const std::string & Gaudi::StringKey::str ( ) const
inline

the actual string

Definition at line 80 of file StringKey.h.

80{ return m_str; }

◆ toString()

std::string Gaudi::StringKey::toString ( ) const

string representation (for properties)

Definition at line 22 of file StringKey.cpp.

22{ return Gaudi::Utils::toString( m_str ); }
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition ToStream.h:329

◆ operator+ [1/4]

std::string operator+ ( const char * lhs,
const Gaudi::StringKey & rhs )
friend

Definition at line 132 of file StringKey.h.

132{ return lhs + rhs.str(); }
const std::string & str() const
the actual string
Definition StringKey.h:80

◆ operator+ [2/4]

std::string operator+ ( const Gaudi::StringKey & lhs,
const char * rhs )
friend

Definition at line 134 of file StringKey.h.

134{ return lhs.str() + rhs; }

◆ operator+ [3/4]

std::string operator+ ( const Gaudi::StringKey & lhs,
const std::string & rhs )
friend

Definition at line 133 of file StringKey.h.

133{ return lhs.str() + rhs; }

◆ operator+ [4/4]

std::string operator+ ( const std::string & lhs,
const Gaudi::StringKey & rhs )
friend

Definition at line 131 of file StringKey.h.

131{ return lhs + rhs.str(); }

Member Data Documentation

◆ m_hash

std::size_t Gaudi::StringKey::m_hash
private

the hash

Definition at line 143 of file StringKey.h.

◆ m_str

std::string Gaudi::StringKey::m_str
private

the actual string

Definition at line 146 of file StringKey.h.


The documentation for this class was generated from the following files: