5#ifndef MUONCALIBIDENTIFIER_IDENTIFIERHASHTABLE_H
6#define MUONCALIBIDENTIFIER_IDENTIFIERHASHTABLE_H
18#include "GaudiKernel/MsgStream.h"
43 typedef typename T::IdentifierType
IdType;
71 void dump( std::ostream& os = std::cout )
const;
85 : m_idToHash(idFields)
91#ifdef IDENTIFIERHASHTABLE_DEBUG
93 log<<MSG::DEBUG<<
"IdentifierHashTable<T>::addEntry(0x" << std::hex <<
id << std::dec <<
")"<<
endmsg;
95 if ( !T::isValid(
id ) ) {
96#ifdef IDENTIFIERHASHTABLE_DEBUG
97 log<<MSG::DEBUG<<
": id is invalid. Nothing added."<<
endmsg;
99 return defaultHashValue();
101#ifdef IDENTIFIERHASHTABLE_DEBUG
102 log<<MSG::DEBUG<<
"::IdToHashTable"<<
endmsg;
104 HashType theHash( m_hashToId.size() );
105 if ( !m_idToHash.addEntry(
id, theHash ) ) {
106#ifdef IDENTIFIERHASHTABLE_DEBUG
107 log<<MSG::DEBUG<<
" WARNING: could not add to table"<<
endmsg;
109 return defaultHashValue();
111 m_hashToId.push_back(
id );
112#ifdef IDENTIFIERHASHTABLE_DEBUG
113 log<<MSG::DEBUG<<
" ADDED at hash=" << theHash<<
endmsg;
123 return m_idToHash.getHash(
id );
130 if ( idx >= m_hashToId.size() )
return IdType();
131 return m_hashToId[
idx ];
142 return m_hashToId.size();
148 unsigned int idToHashSize = m_idToHash.size();
149 if ( idToHashSize != m_hashToId.size() ) {
151 log<<MSG::WARNING<<
"IdentifierHashTable<T>::checkValidity() idToHash size (" << idToHashSize <<
") not equal to hashToId size (" << m_hashToId.size() <<
")"<<
endmsg;
155 if ( !m_hashToId.size() ) {
157 log<<MSG::WARNING<<
"IdentifierHashTable<T>::checkValidity() Table is empty."<<
endmsg;
160#ifdef IDENTIFIERHASHTABLE_DEBUG
164 unsigned int nErrors = 0;
165 for (
unsigned int i = 0;
i < m_hashToId.size(); ++
i ) {
167 IdType
id = getIdentifier( tryHash );
168 HashType gotHash =
getHash(
id );
169 if ( gotHash != tryHash ) {
170#ifndef IDENTIFIERHASHTABLE_DEBUG
173 log<<MSG::WARNING<<
"IdentifierHashTable<T>::checkValidity() getIdentifier(" << tryHash <<
")=0x" << std::hex <<
id << std::dec <<
" whereas getHash(" << std::hex <<
id << std::dec <<
")=" << gotHash<<
endmsg;
176#ifdef IDENTIFIERHASHTABLE_DEBUG
177 log<<MSG::DEBUG<<
"hash=" <<
i <<
" <--> id=0x" << std::hex <<
id << std::dec <<
": OK" <<
endmsg;
181#ifdef IDENTIFIERHASHTABLE_DEBUG
183 log<<MSG::WARNING<<
"IdentifierHashTable<T>::checkValidity() table contains " << nErrors <<
" errors." <<
endmsg;
185 log<<MSG::DEBUG<<
"IdentifierHashTable<T>::checkValidity(): table OK" <<
endmsg;
189 if ( nErrors )
return false;
198 for (
int i = 0;
i <
n; ++
i ) {
200 IdType
id = getIdentifier( iHash );
201 os <<
"ID=0x" << std::hex <<
id << std::dec <<
" fields";
202 os << m_idToHash.dumpOneEntryToString(
id ) <<
"\n";
204 int nTotal = m_idToHash.totalSize();
205 os <<
"IdentifierHashTable has " <<
n <<
" valid entries and "
206 << nTotal -
n <<
" wasted entries" << std::endl;
211 std::ostringstream oss;
T::HashType HashType
define the type HashType
HashType getHash(const IdType &id) const
Get hash from 0 to size()-1.
void dump(std::ostream &os=std::cout) const
Dump complete table to output stream.
bool checkValidity() const
Check that the table is internally consistent.
unsigned int size() const
Number of hashes in the table.
void clear()
Clear the hashtable.
std::vector< IdType > m_hashToId
IdentifierToHash< T > m_idToHash
IdentifierHashTable()
Default constructor makes empty hash table with default idFields object.
std::string dumpToString() const
Dump complete table into a string.
static HashType defaultHashValue()
Helper function to get a properly initialised hash.
IdType getIdentifier(const HashType &hash) const
Get identifier from hash.
IdentifierHashTable(const T &idFields)
Make empty hash table initialised with idFields object.
HashType addEntry(const IdType &id)
Add an identifier to the table.
T::IdentifierType IdType
define the type IdType
The IdentifierToHash table.
static HashType defaultHashValue()
Helper function to get a properly initialised hash.
singleton-like access to IMessageSvc via open function and helper
IMessageSvc * getMessageSvc(bool quiet=false)
std::size_t getHash(const T &obj)