ATLAS Offline Software
Loading...
Searching...
No Matches
IdentifierHashTable.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef MUONCALIBIDENTIFIER_IDENTIFIERHASHTABLE_H
6#define MUONCALIBIDENTIFIER_IDENTIFIERHASHTABLE_H
7
8/***************************************************************************
9 * Identifier utility
10 * -----------------------------------------
11 *
12 * Author : Martin Woudstra
13 * Creation Date: 28 April 2004
14 * Last Update : 07 May 2004
15 ***************************************************************************/
16
18#include "GaudiKernel/MsgStream.h"
20
21#include <iostream>
22#include <sstream>
23#include <limits.h>
24
36template <class T>
38public:
39 //
40 // nested types
41 //
43 typedef typename T::IdentifierType IdType;
45 typedef typename T::HashType HashType;
50 //
51 // Member functions
52 //
56 explicit IdentifierHashTable( const T& idFields );
58 void clear();
60 unsigned int size() const;
62 bool checkValidity() const;
65 HashType addEntry( const IdType& id );
67 HashType getHash( const IdType& id ) const;
69 IdType getIdentifier( const HashType& hash ) const;
71 void dump( std::ostream& os = std::cout ) const;
73 std::string dumpToString() const;
74private:
76 std::vector<IdType> m_hashToId;
77};
78
79template <class T>
81{}
82
83template <class T>
84inline IdentifierHashTable<T>::IdentifierHashTable( const T& idFields )
85 : m_idToHash(idFields)
86{}
87
88template <class T>
91#ifdef IDENTIFIERHASHTABLE_DEBUG
92 MsgStream log(Athena::getMessageSvc(),"IdentifierHashTable");
93 log<<MSG::DEBUG<<"IdentifierHashTable<T>::addEntry(0x" << std::hex << id << std::dec << ")"<<endmsg;
94#endif
95 if ( !T::isValid( id ) ) {
96#ifdef IDENTIFIERHASHTABLE_DEBUG
97 log<<MSG::DEBUG<<": id is invalid. Nothing added."<<endmsg;
98#endif
99 return defaultHashValue();
100 }
101#ifdef IDENTIFIERHASHTABLE_DEBUG
102 log<<MSG::DEBUG<<"::IdToHashTable"<<endmsg;
103#endif
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;
108#endif
109 return defaultHashValue();
110 } else {
111 m_hashToId.push_back( id );
112#ifdef IDENTIFIERHASHTABLE_DEBUG
113 log<<MSG::DEBUG<<" ADDED at hash=" << theHash<<endmsg;
114#endif
115 }
116
117 return theHash;
118}
119
120template <class T>
123 return m_idToHash.getHash( id );
124}
125
126template <class T>
127inline typename IdentifierHashTable<T>::IdType
129 unsigned int idx = hash;
130 if ( idx >= m_hashToId.size() ) return IdType();
131 return m_hashToId[ idx ];
132}
133
134template <class T>
135inline void IdentifierHashTable<T>::clear() {
136 m_idToHash.clear();
137 m_hashToId.clear();
138}
139
140template <class T>
141inline unsigned int IdentifierHashTable<T>::size() const {
142 return m_hashToId.size();
143}
144
145template <class T>
147 // check that both tables are same size
148 unsigned int idToHashSize = m_idToHash.size();
149 if ( idToHashSize != m_hashToId.size() ) {
150 MsgStream log(Athena::getMessageSvc(),"IdentifierHashTable");
151 log<<MSG::WARNING<<"IdentifierHashTable<T>::checkValidity() idToHash size (" << idToHashSize << ") not equal to hashToId size (" << m_hashToId.size() << ")"<<endmsg;
152 return false;
153 }
154 // check that table is non-empty
155 if ( !m_hashToId.size() ) {
156 MsgStream log(Athena::getMessageSvc(),"IdentifierHashTable");
157 log<<MSG::WARNING<<"IdentifierHashTable<T>::checkValidity() Table is empty."<<endmsg;
158 return false;
159 }
160#ifdef IDENTIFIERHASHTABLE_DEBUG
161 MsgStream log(Athena::getMessageSvc(),"IdentifierHashTable");
162#endif
163 // check that tables are each other's inverse
164 unsigned int nErrors = 0;
165 for ( unsigned int i = 0; i < m_hashToId.size(); ++i ) {
166 HashType tryHash(i);
167 IdType id = getIdentifier( tryHash );
168 HashType gotHash = getHash( id );
169 if ( gotHash != tryHash ) {
170#ifndef IDENTIFIERHASHTABLE_DEBUG
171 MsgStream log(Athena::getMessageSvc(),"IdentifierHashTable");
172#endif
173 log<<MSG::WARNING<<"IdentifierHashTable<T>::checkValidity() getIdentifier(" << tryHash << ")=0x" << std::hex << id << std::dec << " whereas getHash(" << std::hex << id << std::dec << ")=" << gotHash<<endmsg;
174 ++nErrors;
175 } else {
176#ifdef IDENTIFIERHASHTABLE_DEBUG
177 log<<MSG::DEBUG<<"hash=" << i << " <--> id=0x" << std::hex << id << std::dec << ": OK" <<endmsg;
178#endif
179 }
180 }
181#ifdef IDENTIFIERHASHTABLE_DEBUG
182 if ( nErrors ) {
183 log<<MSG::WARNING<<"IdentifierHashTable<T>::checkValidity() table contains " << nErrors << " errors." << endmsg;
184 } else {
185 log<<MSG::DEBUG<<"IdentifierHashTable<T>::checkValidity(): table OK" << endmsg;
186 }
187#endif
188
189 if ( nErrors ) return false;
190
191 // if we get here, everything is OK!
192 return true;
193}
194
195template <class T>
196void IdentifierHashTable<T>::dump( std::ostream& os ) const {
197 int n = size();
198 for ( int i = 0; i < n; ++i ) {
199 HashType iHash(i);
200 IdType id = getIdentifier( iHash );
201 os << "ID=0x" << std::hex << id << std::dec << " fields";
202 os << m_idToHash.dumpOneEntryToString( id ) << "\n";
203 }
204 int nTotal = m_idToHash.totalSize();
205 os << "IdentifierHashTable has " << n << " valid entries and "
206 << nTotal - n << " wasted entries" << std::endl;
207}
208
209template <class T>
210inline std::string IdentifierHashTable<T>::dumpToString() const {
211 std::ostringstream oss;
212 dump( oss );
213 return oss.str();
214}
215
216#endif // MUONCALIBIDENTIFIER_IDENTIFIERHASHTABLE_H
#define endmsg
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)
Definition hash.h:13
-event-from-file