5#ifndef MUONCALIBIDENTIFIER_IDENTIFIERHASHCALC_H
6#define MUONCALIBIDENTIFIER_IDENTIFIERHASHCALC_H
21#include "GaudiKernel/MsgStream.h"
41 typedef typename T::IdentifierType
IdType;
67 void dump( std::ostream& os = std::cout )
const;
87 : m_idFields(idFields)
94 for (
unsigned int i = 0;
i < NFIELDS; ++
i ) {
95 if ( idFields[i] < m_fieldsMin[i] || idFields[i] > m_fieldsMax[i] )
return false;
103 m_idFields.setAll(
id );
104 if ( !isInRange( m_idFields ) )
return T::defaultHash();
105 unsigned int hash = m_idFields[0] - m_fieldsMin[0];
106 for (
unsigned int i = 1;
i < NFIELDS; ++
i ) {
107 hash += m_fieldsFactor[
i-1] * ( m_idFields[
i] - m_fieldsMin[
i] );
109 return HashType(hash);
116 fields[0] = m_fieldsMin[0] + ( idHash % m_fieldsSize[0] );
117 for (
unsigned int i = 1;
i < NFIELDS; ++
i ) {
118 fields[
i] = m_fieldsMin[
i] + ( ( idHash / m_fieldsFactor[
i-1] ) % m_fieldsSize[
i] );
120 m_idFields.setAll( fields );
121 if ( !isInRange( m_idFields ) )
return IdType();
122 return m_idFields.getId();
128 m_fieldsSize[0] = m_fieldsMax[0] - m_fieldsMin[0] + 1;
129 m_fieldsFactor[0] = m_fieldsSize[0];
130 for (
unsigned int i = 1;
i < NFIELDS; ++
i ) {
131 m_fieldsSize[
i] = m_fieldsMax[
i] - m_fieldsMin[
i] + 1;
132 m_fieldsFactor[
i] = m_fieldsFactor[
i-1] * m_fieldsSize[
i];
138 if ( !T::isValid(
id ) )
return false;
140 m_idFields.setAll(
id );
141 bool bChanged =
false;
142 for (
unsigned int i = 0;
i < NFIELDS; ++
i ) {
143 if ( m_idFields[i] < m_fieldsMin[i] ) {
144 m_fieldsMin[
i] = m_idFields[
i];
147 if ( m_idFields[i] > m_fieldsMax[i] ) {
148 m_fieldsMax[
i] = m_idFields[
i];
158 if ( fieldIndex >= NFIELDS )
return;
159 if ( fieldMin > fieldMax )
return;
160 bool bChanged =
false;
161 if ( fieldMin != m_fieldsMin[fieldIndex] ) {
162 m_fieldsMin[fieldIndex] = fieldMin;
165 if ( fieldMax != m_fieldsMax[fieldIndex] ) {
166 m_fieldsMax[fieldIndex] = fieldMax;
174 for (
unsigned int i = 0;
i < NFIELDS; ++
i ) {
175 m_fieldsMin[
i] = INT_MAX;
176 m_fieldsMax[
i] = INT_MIN;
178 m_fieldsFactor[
i] = 0;
184 return m_fieldsFactor[NFIELDS - 1];
192 log<<MSG::WARNING<<
"IdentifierHashCalc<T>::checkValidity() Table is empty."<<
endmsg;
196 unsigned int nErrors = 0;
197#ifdef IDENTIFIERHASHCALC_DEBUG
200 for (
unsigned int i = 0;
i <
size(); ++
i ) {
202 IdType
id = getIdentifier( iHash );
205#ifndef IDENTIFIERHASHCALC_DEBUG
208 log<<MSG::WARNING<<
"IdentifierHashCalc<T>::checkValidity() getIdentifier(" <<
i <<
")=0x" << std::hex <<
id << std::dec <<
" whereas getHash(" << std::hex <<
id << std::dec <<
")=" <<
hash<<
endmsg;
211#ifdef IDENTIFIERHASHCALC_DEBUG
212 log<<MSG::DEBUG<<
"hash=" <<
i <<
" <--> id=0x" << std::hex <<
id << std::dec <<
": OK"<<
endmsg;
216#ifdef IDENTIFIERHASHCALC_DEBUG
218 log<<MSG::WARNING<<
"IdentifierHashCalc<T>::checkValidity() table contains " << nErrors <<
" errors." <<
endmsg;
220 log<<MSG::DEBUG<<
"IdentifierHashCalc<T>::checkValidity(): table OK"<<
endmsg;
224 if ( nErrors )
return false;
232 unsigned int n =
size();
233 for (
unsigned int i = 0;
i <
n; ++
i ) {
235 IdType
id = getIdentifier( iHash );
236 m_idFields.setAll(
id );
237 os <<
"ID=0x" << std::hex <<
id << std::dec <<
" fields=(";
238 for (
int j = NFIELDS-1; j >= 0; --j ) {
240 if ( j != 0 )
os <<
",";
242 os <<
") hash=" <<
i <<
"\n";
244 os <<
"IdentifierHashTable has " <<
n <<
" entries.\n";
245 os <<
"\nSummary:\n";
246 os <<
"field min max size factor\n";
247 for (
unsigned int i = 0;
i < NFIELDS; ++
i ) {
248 os << std::setw(5) <<
i <<
" "
249 << std::setw(3) << m_fieldsMin[
i] <<
" "
250 << std::setw(3) << m_fieldsMax[
i] <<
" "
251 << std::setw(4) << m_fieldsSize[
i] <<
" "
252 << std::setw(6) << m_fieldsFactor[
i] <<
"\n";
259 std::ostringstream oss;
void clear()
Clear the hashtable.
void dump(std::ostream &os=std::cout) const
Dump complete table to output stream.
IdType getIdentifier(const HashType &idHash) const
Get identifier from hash.
T IdFieldsType
define the type IdFieldsType
bool isInRange(const T &idFields) const
MdtTubeInChamberIdFields m_idFields
T::IdentifierType IdType
define the type IdType
T::HashType HashType
define the type HashType
void setFieldLimits(unsigned int fieldIndex, int fieldMin, int fieldMax)
Set the limits of a specific field.
bool addEntry(const IdType &id)
Add an identifier to the table.
unsigned int m_fieldsSize[NFIELDS]
unsigned int size() const
Number of hashes in the table.
HashType getHash(const IdType &id) const
Get hash from 0 to size()-1.
unsigned int m_fieldsFactor[NFIELDS]
IdentifierHashCalc(const T &idFields)
Make empty table with pointer set to external idHelper, which will be transmitted to the constructor ...
bool checkValidity() const
Check that the table is internally consistent.
std::string dumpToString() const
Dump complete table into a string.
IdentifierHashCalc()
Default constructor makes empty hash table.
singleton-like access to IMessageSvc via open function and helper
IMessageSvc * getMessageSvc(bool quiet=false)
std::size_t getHash(const T &obj)