ATLAS Offline Software
Loading...
Searching...
No Matches
IdentifierToHash.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_IDENTIFIERTOHASH_H
6#define MUONCALIBIDENTIFIER_IDENTIFIERTOHASH_H
7
8/***************************************************************************
9 * Identifier utility
10 * -----------------------------------------
11 *
12 * Author : Martin Woudstra
13 * Creation Date: 28 April 2004
14 * Last Update : 27 April 2005
15 ***************************************************************************/
16
18#include "GaudiKernel/MsgStream.h"
20
21#include <iostream>
22#include <sstream>
23#include <string>
24#include <limits.h>
25
38template <class T>
40public:
41 //
42 // nested types
43 //
47 enum { NFIELDS = T::NFIELDS, NMAX=UINT_MAX };
49 typedef typename T::IdentifierType IdType;
51 typedef typename T::HashType HashType;
56 return T::defaultHash();
57 }
58
61 template < unsigned int N, class K >
63 public:
64 static const typename K::ValueType& getConstRef( const K& obj, const T& indices ) {
65 int idx = indices[N-1];
66#ifdef IDENTIFIERTOHASH_DEBUG
67 MsgStream log(Athena::getMessageSvc(),"RecursiveIndexCall");
68 log<<MSG::DEBUG<<"[" << idx << "]"<<endmsg;
69#endif
71 }
72 static typename K::ValueType& getRef( K& obj, const T& indices ) {
73 int idx = indices[N-1];
74#ifdef IDENTIFIERTOHASH_DEBUG
75 log<<MSG::DEBUG<<"{" << idx << "}"<<endmsg;
76#endif
77 return RecursiveIndexCall<N-1,typename K::SubType>::getRef( obj[ idx ], indices );
78 }
79 };
80
81 // specialise for N=1 to end recursion
82 template < class K >
83 class RecursiveIndexCall<1,K> {
84 public:
85 static const typename K::ValueType& getConstRef( const K& obj, const T& indices ) {
86 int idx = indices[0];
87#ifdef IDENTIFIERTOHASH_DEBUG
88 MsgStream log(Athena::getMessageSvc(),"RecursiveIndexCall");
89 log<<MSG::DEBUG<<"[" << idx << "]"<<endmsg;
90#endif
91 return obj[ idx ];
92 }
93 static typename K::ValueType& getRef( K& obj, const T& indices ) {
94 int idx = indices[0];
95#ifdef IDENTIFIERTOHASH_DEBUG
96 log<<MSG::DEBUG<<"{" << idx << "}"<<endmsg;
97#endif
98 return obj[ idx ];
99 }
100 };
101
102 // zero is invalid. Make empty class.
103 template < class K >
105 };
106
109 template < unsigned int N, class K>
111 public:
112 static bool isInRange( const K& obj, const T& indices ) {
113 int idx = indices[N-1];
114 bool bOK = obj.isInRange( idx );
115#ifdef IDENTIFIERTOHASH_DEBUG
116 MsgStream log(Athena::getMessageSvc(),"RecursiveRangeCheck");
117 log<<MSG::DEBUG<<"(" << idx << ")"<<endmsg;
118 if ( !bOK ) {
119 log<<MSG::DEBUG<<" CHECK: " << idx << " Out of range (" << obj.minIndex() << "," << obj.maxIndex() << ")"<<endmsg;
120 }
121#endif
122 return bOK && RecursiveRangeCheck<N-1,typename K::SubType>::isInRange( obj[ idx ], indices );
123 }
124
125 };
126
127 template <class K>
129 public:
130 static bool isInRange( const K& obj, const T& indices ) {
131 int idx = indices[0];
132 bool bOK = obj.isInRange( idx );
133#ifdef IDENTIFIERTOHASH_DEBUG
134 MsgStream log(Athena::getMessageSvc(),"RecursiveRangeCheck");
135 log<<MSG::DEBUG<<"(" << idx << ")"<<endmsg;
136 if ( !bOK ) {
137 log<<MSG::DEBUG<<" CHECK: Out of range (" << obj.minIndex() << "," << obj.maxIndex() << ")"<<endmsg;
138 }else {
139 log<<MSG::DEBUG<<" CHECK: Range OK"<<endmsg;
140 }
141#endif
142 return bOK;
143 }
144 };
145
146 template <class K>
148 // empty class
149 };
150 //
151 // Member functions
152 //
156 explicit IdentifierToHash( const T& idFields );
158 void clear();
160 unsigned int size() const;
162 unsigned int totalSize() const;
171 bool addEntry( const IdType& id, const HashType& aHash );
173 HashType getHash( const IdType& id ) const;
175 void dumpOneEntry( const IdType& id, std::ostream& os ) const;
177 std::string dumpOneEntryToString( const IdType& id ) const;
179 void dump( std::ostream& os = std::cout ) const;
181 std::string dumpToString() const;
182private:
183 mutable T m_idFields;
184 unsigned int m_entries;
186};
187
188template <class T>
192
193template <class T>
194inline IdentifierToHash<T>::IdentifierToHash( const T& idFields )
195 : m_idFields( idFields ), m_entries(0)
196{}
197
198template <class T>
199bool
201 const typename IdentifierToHash<T>::HashType& aHash ) {
202#ifdef IDENTIFIERTOHASH_DEBUG
203 MsgStream log(Athena::getMessageSvc(),"IdentifierToHash<T>");
204 log<<MSG::DEBUG<<"IdentifierToHash<T>::addEntry(0x" << std::hex << id << std::dec << "," << aHash << ")"<<endmsg;
205#endif
206 if ( !T::isValid( id ) ) {
207#ifdef IDENTIFIERTOHASH_DEBUG
208 log<<MSG::DEBUG<<": id is invalid. Nothing added."<<endmsg;
209#endif
210 return false;
211 }
212 m_idFields.setAll( id );
213#ifdef
214 log<<MSG::DEBUG<<"::IdToHashTable"<<endmsg;
215#endif
216
217 HashType& hashRef =
218 RecursiveIndexCall< NFIELDS, ArrayType >::getRef( m_data, m_idFields );
219 // only add hash if entry is not yet taken
220 if ( hashRef != defaultHashValue() ) {
221#ifdef IDENTIFIERTOHASH_DEBUG
222 log<<MSG::WARNING<<"id already taken by hash=" << hashRef << ". Nothing added."<<endmsg;
223#endif
224 return false;
225 } else {
226 // set the hash to the indicated value
227 hashRef = aHash;
228#ifdef IDENTIFIERTOHASH_DEBUG
229 log<<MSG::DEBUG<<" ADDED."<<endmsg;
230#endif
231 }
232
233 ++m_entries;
234 return true;
235}
236
237template <class T>
240 HashType aHash = m_entries;
241 if ( !addEntry( id, aHash ) ) return defaultHashValue();
242 return aHash;
243}
244
245template <class T>
248 if ( !T::isValid( id ) ) return defaultHashValue();
249 m_idFields.setAll( id );
250 // check that fields are in range
252 return defaultHashValue();
253 }
255}
256
257template <class T>
258inline void IdentifierToHash<T>::clear() {
259 m_entries = 0;
260 m_data.clear();
261}
262
263template <class T>
264inline unsigned int IdentifierToHash<T>::size() const {
265 return m_entries;
266}
267
268
269template <class T>
270inline unsigned int IdentifierToHash<T>::totalSize() const {
271 return m_data.totalSize();
272}
273
274template <class T>
276 std::ostream& os ) const {
277 m_idFields.setAll( id );
278 m_data.dumpOneEntry( m_idFields, os );
279}
280
281template <class T>
282inline std::string IdentifierToHash<T>::dumpOneEntryToString( const typename IdentifierToHash<T>::IdType& id ) const {
283 m_idFields.setAll( id );
284 return m_data.dumpOneEntryToString( m_idFields );
285}
286
287template <class T>
288inline void IdentifierToHash<T>::dump( std::ostream& os ) const {
289 m_data.dump( os );
290}
291
292template <class T>
293inline std::string IdentifierToHash<T>::dumpToString() const {
294 return m_data.dumpToString();
295}
296
297
298#endif // MUONCALIBIDENTIFIER_IDENTIFIERTOHASH_H
#define endmsg
static const K::ValueType & getConstRef(const K &obj, const T &indices)
static K::ValueType & getRef(K &obj, const T &indices)
Helper class for IdentifierToHash to automate recursive index call.
static const K::ValueType & getConstRef(const K &obj, const T &indices)
static K::ValueType & getRef(K &obj, const T &indices)
static bool isInRange(const K &obj, const T &indices)
Helper class for one-go recursive range check on all indices.
static bool isInRange(const K &obj, const T &indices)
T::HashType HashType
define type HashType
unsigned int m_entries
HashType addEntry(const IdType &id)
Add an identifier to the table, and return the hash value belonging to it.
T::IdentifierType IdType
define type IdType
void clear()
Clear the hashtable.
static HashType defaultHashValue()
Helper function to get a properly initialised hash.
HashType getHash(const IdType &id) const
Get hash from 0 to size()-1.
void dumpOneEntry(const IdType &id, std::ostream &os) const
Dump one entry for given id to os.
unsigned int size() const
Number of valid hashes in the table.
MultiDimArray< T, NFIELDS > ArrayType
define type ArrayType
std::string dumpToString() const
Dump complete table into a string.
IdentifierToHash()
Default constructor makes empty hash table and creates default idFields object.
unsigned int totalSize() const
Total number of hashes in the table.
std::string dumpOneEntryToString(const IdType &id) const
Dump one entry for given id to a string.
bool addEntry(const IdType &id, const HashType &aHash)
Add an identifier+hash pair to the table.
void dump(std::ostream &os=std::cout) const
Dump complete table to output stream.
Multi-dimensional array with a compile-time number of dimensions, and a run-time complete freedom ove...
singleton-like access to IMessageSvc via open function and helper
IMessageSvc * getMessageSvc(bool quiet=false)
-event-from-file