The Gaudi Framework  master (e3184c44)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Gaudi::StringKey Class Reference

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

Collaboration diagram for Gaudi::StringKey:

Public Member Functions

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

Private Attributes

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

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" );

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 66 of file StringKey.h.

Constructor & Destructor Documentation

◆ StringKey() [1/4]

Gaudi::StringKey::StringKey ( )
inline

Default constructor from empty string.

Definition at line 69 of file StringKey.h.

69 : StringKey( std::string_view{} ) {}

◆ StringKey() [2/4]

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

constructor from plain C-string, perform hashing

Definition at line 71 of file StringKey.h.

71 : 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 73 of file StringKey.h.

73 : m_hash{ StringKeyHash()( key ) }, m_str( key ) {}

◆ StringKey() [4/4]

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

constructor from std::string, perform hashing

Definition at line 75 of file StringKey.h.

75 : 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 107 of file StringKey.h.

107 { 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(); }

◆ __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 84 of file StringKey.h.

84 { return m_str.empty(); }

◆ operator()

auto Gaudi::StringKey::operator ( const StringKey ) const
default

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

◆ operator const std::string &()

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

implicit cast to std::string

Definition at line 80 of file StringKey.h.

80 { return m_str; }

◆ operator std::string_view()

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

implicit cast to std::string_view

Definition at line 82 of file StringKey.h.

82 { return m_str; }

◆ operator!()

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

empty key?

Definition at line 86 of file StringKey.h.

86 { return empty(); }

◆ operator==() [1/4]

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

Comparison to other string-like types.

Definition at line 94 of file StringKey.h.

94 { return m_str == rhs; }

◆ operator==() [2/4]

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

Definition at line 96 of file StringKey.h.

96 { 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 95 of file StringKey.h.

95 { return m_str == rhs; }

◆ str()

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

the actual string

Definition at line 78 of file StringKey.h.

78 { 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 ); }

Friends And Related Function Documentation

◆ operator+ [1/4]

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

Definition at line 126 of file StringKey.h.

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

◆ operator+ [2/4]

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

Definition at line 128 of file StringKey.h.

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

◆ operator+ [3/4]

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

Definition at line 127 of file StringKey.h.

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

◆ operator+ [4/4]

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

Definition at line 125 of file StringKey.h.

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

Member Data Documentation

◆ m_hash

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

the hash

Definition at line 137 of file StringKey.h.

◆ m_str

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

the actual string

Definition at line 140 of file StringKey.h.


The documentation for this class was generated from the following files:
Gaudi::StringKey::m_str
std::string m_str
the actual string
Definition: StringKey.h:140
gaudirun.s
string s
Definition: gaudirun.py:346
Gaudi::StringKey::m_hash
std::size_t m_hash
the hash
Definition: StringKey.h:137
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:107
Gaudi::StringKey::StringKey
StringKey()
Default constructor from empty string.
Definition: StringKey.h:69
Gaudi::Utils::toString
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:326
Gaudi::StringKey::str
const std::string & str() const
the actual string
Definition: StringKey.h:78
Gaudi::StringKey::toString
std::string toString() const
string representation (for properties)
Definition: StringKey.cpp:22
ProduceConsume.key
key
Definition: ProduceConsume.py:84
Gaudi::StringKey::empty
bool empty() const
empty key?
Definition: StringKey.h:84