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 174 of file StringPool.cxx.

175  : m_impl (std::make_unique<StringPoolImpl>())
176 {
177 }

◆ ~StringPool()

SG::StringPool::~StringPool ( )

Destructor.

Definition at line 233 of file StringPool.cxx.

234 {
235  clear();
236 }

◆ StringPool() [2/3]

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

Copy/move constructors.

Copy constructor.

other Object from which to copy.

Definition at line 184 of file StringPool.cxx.

185  : m_impl (std::make_unique<StringPoolImpl> (*other.m_impl))
186 {
187 }

◆ StringPool() [3/3]

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

Move constructor.

other Object from which to move.

Definition at line 194 of file StringPool.cxx.

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

Member Function Documentation

◆ clear()

void SG::StringPool::clear ( )

Empty the pool.

Definition at line 350 of file StringPool.cxx.

351 {
352  m_impl->clear();
353 }

◆ dump()

void SG::StringPool::dump ( ) const

Debugging dump.

Write to cout.

Definition at line 341 of file StringPool.cxx.

342 {
343  m_impl->dump();
344 }

◆ 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 286 of file StringPool.cxx.

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

◆ 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 301 of file StringPool.cxx.

303 {
304  return m_impl->keyToString (key, aux);
305 }

◆ 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 363 of file StringPool.cxx.

364 {
365  return m_impl->merge (*other.m_impl);
366 }

◆ operator=() [1/2]

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

Assignment/move operators.

Assignment operator.

other Object from which to copy.

Definition at line 207 of file StringPool.cxx.

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

◆ operator=() [2/2]

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

Move operator.

other Object from which to move.

Definition at line 220 of file StringPool.cxx.

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

◆ 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 319 of file StringPool.cxx.

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

◆ size()

size_t SG::StringPool::size ( ) const

Number of registered mappings.

Definition at line 332 of file StringPool.cxx.

333 {
334  return m_impl->size();
335 }

◆ 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 248 of file StringPool.cxx.

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

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:350
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:286
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:248
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37