ATLAS Offline Software
Public Types | Public Member Functions | Static Public Attributes | Private Attributes | List of all members
SG::StringPool Class Reference

#include <StringPool.h>

Collaboration diagram for SG::StringPool:

Public Types

typedef uint32_t sgkey_t
 Type of the integer keys. More...
 
typedef unsigned int sgaux_t
 Type of auxiliary data. More...
 

Public Member Functions

 StringPool ()
 Constructor. More...
 
 ~StringPool ()
 Destructor. More...
 
 StringPool (const StringPool &other)
 Copy/move constructors. More...
 
 StringPool (StringPool &&other)
 Move constructor. More...
 
StringPooloperator= (const StringPool &other)
 Assignment/move operators. More...
 
StringPooloperator= (StringPool &&other)
 Move operator. More...
 
sgkey_t stringToKey (const std::string &str, sgaux_t aux=0)
 Find the key for a string. More...
 
const std::string * keyToString (sgkey_t key) const
 Find the string corresponding to a given key. More...
 
const std::string * keyToString (sgkey_t key, sgaux_t &aux) const
 Find the string corresponding to a given key. More...
 
bool registerKey (sgkey_t key, const std::string &str, sgaux_t aux=0)
 Remember an additional mapping from key to string. More...
 
size_t size () const
 Number of registered mappings. More...
 
void dump () const
 Debugging dump. More...
 
void clear ()
 Empty the pool. More...
 
bool merge (const StringPool &other)
 Merge another pool into this one. More...
 

Static Public Attributes

static const int sgkey_t_nbits = 30
 Number of bits we'll use in the keys. More...
 
static const sgkey_t sgkey_t_max
 

Private Attributes

std::unique_ptr< StringPoolImplm_impl
 

Detailed Description

Definition at line 34 of file StringPool.h.

Member Typedef Documentation

◆ sgaux_t

typedef unsigned int SG::StringPool::sgaux_t

Type of auxiliary data.

Definition at line 41 of file StringPool.h.

◆ sgkey_t

typedef uint32_t SG::StringPool::sgkey_t

Type of the integer keys.

Definition at line 38 of file StringPool.h.

Constructor & Destructor Documentation

◆ StringPool() [1/3]

SG::StringPool::StringPool ( )

Constructor.

Definition at line 176 of file StringPool.cxx.

177  : m_impl (std::make_unique<StringPoolImpl>())
178 {
179 }

◆ ~StringPool()

SG::StringPool::~StringPool ( )

Destructor.

Definition at line 235 of file StringPool.cxx.

236 {
237  clear();
238 }

◆ StringPool() [2/3]

SG::StringPool::StringPool ( const StringPool other)

Copy/move constructors.

Copy constructor.

other Object from which to copy.

Definition at line 186 of file StringPool.cxx.

187  : m_impl (std::make_unique<StringPoolImpl> (*other.m_impl))
188 {
189 }

◆ StringPool() [3/3]

SG::StringPool::StringPool ( StringPool &&  other)

Move constructor.

other Object from which to move.

Definition at line 196 of file StringPool.cxx.

197  : m_impl (std::move (other.m_impl))
198 {
199  // False positive:
200  // cppcheck-suppress useInitializationList
201  other.m_impl = std::make_unique<StringPoolImpl>();
202 }

Member Function Documentation

◆ clear()

void SG::StringPool::clear ( )

Empty the pool.

Definition at line 352 of file StringPool.cxx.

353 {
354  m_impl->clear();
355 }

◆ dump()

void SG::StringPool::dump ( ) const

Debugging dump.

Write to cout.

Definition at line 343 of file StringPool.cxx.

344 {
345  m_impl->dump();
346 }

◆ keyToString() [1/2]

const std::string * SG::StringPool::keyToString ( sgkey_t  key) const

Find the string corresponding to a given key.

Parameters
keyThe key to look up.
Returns
Pointer to the string found, or null. We can find keys as long as the corresponding string was given to either stringToKey() or registerKey().

Definition at line 288 of file StringPool.cxx.

289 {
290  sgaux_t aux;
291  return m_impl->keyToString (key, aux);
292 }

◆ keyToString() [2/2]

const std::string * SG::StringPool::keyToString ( sgkey_t  key,
sgaux_t aux 
) const

Find the string corresponding to a given key.

Parameters
keyThe key to look up.
aux[out]Auxiliary data associated with the key.
Returns
Pointer to the string found, or null. We can find keys as long as the corresponding string was given to either stringToKey() or registerKey().

Definition at line 303 of file StringPool.cxx.

305 {
306  return m_impl->keyToString (key, aux);
307 }

◆ merge()

bool SG::StringPool::merge ( const StringPool other)

Merge another pool into this one.

Parameters
otherThe other pool to merge into this one.

In case of collisions, the colliding entries are skipped, and false is returned. If no collisions, then true is returned.

Definition at line 365 of file StringPool.cxx.

366 {
367  return m_impl->merge (*other.m_impl);
368 }

◆ operator=() [1/2]

StringPool & SG::StringPool::operator= ( const StringPool other)

Assignment/move operators.

Assignment operator.

other Object from which to copy.

Definition at line 209 of file StringPool.cxx.

210 {
211  if (this != &other) {
212  *m_impl = *other.m_impl;
213  }
214  return *this;
215 }

◆ operator=() [2/2]

StringPool & SG::StringPool::operator= ( StringPool &&  other)

Move operator.

other Object from which to move.

Definition at line 222 of file StringPool.cxx.

223 {
224  if (this != &other) {
225  StringPoolImpl& other_impl = *other.m_impl;
226  *m_impl = std::move (other_impl);
227  }
228  return *this;
229 }

◆ registerKey()

bool SG::StringPool::registerKey ( sgkey_t  key,
const std::string &  str,
sgaux_t  aux = 0 
)

Remember an additional mapping from key to string.

Parameters
keyThe key to enter.
strThe string to enter.
auxAuxiliary data to include along with the string.
Returns
True if successful; false if the key already corresponds to a different string.

This registers an additional mapping from a key to a string; it can be found later through lookup() on the string.

Definition at line 321 of file StringPool.cxx.

324 {
325  // Make sure the primary mapping is registered first.
326  stringToKey (str, aux);
327  return m_impl->registerKey (key, str, aux);
328 }

◆ size()

size_t SG::StringPool::size ( ) const

Number of registered mappings.

Definition at line 334 of file StringPool.cxx.

335 {
336  return m_impl->size();
337 }

◆ stringToKey()

StringPool::sgkey_t SG::StringPool::stringToKey ( const std::string &  str,
sgaux_t  aux = 0 
)

Find the key for a string.

Parameters
strThe string to look up.
auxAuxiliary data to include along with the string.
Returns
A key identifying the string. A given string will always return the same key. Will throw ExcSgkeyCollision in case of a hash collision (for a non-transient string)!

Definition at line 250 of file StringPool.cxx.

252 {
253  uint64_t crc = CxxUtils::crc64 (str);
254  if (aux) crc = CxxUtils::crc64addint (crc, aux);
255  sgkey_t key = (crc & sgkey_t_max);
256  if (m_impl->registerKey (key, str, aux)) {
257  return key;
258  }
259 
260  // Hash collision...
261  // If we have a transient key, then adjust the hash to avoid a collision.
262  if (SG::isTransientKey (str)) {
263  sgkey_t new_key = ( (key+1) & sgkey_t_max);
264  while (!m_impl->registerKey (new_key, str, aux)) {
265  new_key = ( (new_key+1) & sgkey_t_max);
266  if (key == new_key) std::abort();
267  }
268  return new_key;
269  }
270 
271  // Otherwise raise an exception.
272  sgaux_t old_aux = 0;
273  const std::string* old_str = keyToString (key, old_aux);
274  if (!old_str) std::abort(); // Shouldn't happen.
275  throw ExcSgkeyCollision (str, aux,
276  *old_str, old_aux,
277  key);
278 }

Member Data Documentation

◆ m_impl

std::unique_ptr<StringPoolImpl> SG::StringPool::m_impl
private

Definition at line 137 of file StringPool.h.

◆ sgkey_t_max

const sgkey_t SG::StringPool::sgkey_t_max
static
Initial value:
=
(static_cast<sgkey_t>(1) << sgkey_t_nbits) - 1

Definition at line 46 of file StringPool.h.

◆ sgkey_t_nbits

const int SG::StringPool::sgkey_t_nbits = 30
static

Number of bits we'll use in the keys.

Leave a few spare bits in case we want to overload them for flags later.

Definition at line 45 of file StringPool.h.


The documentation for this class was generated from the following files:
SG::isTransientKey
bool isTransientKey(const std::string &key)
Test to see if a key is transoent.
Definition: transientKey.h:41
SG::StringPool::m_impl
std::unique_ptr< StringPoolImpl > m_impl
Definition: StringPool.h:137
SG::StringPool::sgkey_t_nbits
static const int sgkey_t_nbits
Number of bits we'll use in the keys.
Definition: StringPool.h:45
SG::StringPool::clear
void clear()
Empty the pool.
Definition: StringPool.cxx:352
SG::StringPool::sgkey_t_max
static const sgkey_t sgkey_t_max
Definition: StringPool.h:46
CxxUtils::crc64addint
uint64_t crc64addint(uint64_t crc, uint64_t x)
Extend a previously-calculated CRC to include an int.
Definition: crc64.cxx:732
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
SG::StringPool::sgaux_t
unsigned int sgaux_t
Type of auxiliary data.
Definition: StringPool.h:41
SG::StringPool::sgkey_t
uint32_t sgkey_t
Type of the integer keys.
Definition: StringPool.h:38
SG::StringPool::keyToString
const std::string * keyToString(sgkey_t key) const
Find the string corresponding to a given key.
Definition: StringPool.cxx:288
CxxUtils::crc64
uint64_t crc64(const CRCTable &table, const char *data, size_t data_len)
Find the CRC-64 of a string,.
Definition: crc64.cxx:696
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
str
Definition: BTagTrackIpAccessor.cxx:11
SG::StringPool::stringToKey
sgkey_t stringToKey(const std::string &str, sgaux_t aux=0)
Find the key for a string.
Definition: StringPool.cxx:250
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37