CMS 3D CMS Logo

Hash.h
Go to the documentation of this file.
1 #ifndef DataFormats_Provenance_Hash_h
2 #define DataFormats_Provenance_Hash_h
3 
4 #include <string>
5 #include <functional>
6 
7 /*----------------------------------------------------------------------
8 
9 Hash:
10 
11  Note: The call to 'fixup' in every member function is a temporary
12  measure for backwards compatibility. It is necessary in every function
13  because Root creates instances of the class *without* using the
14  interface of the class, thus making it insufficient to assure that
15  all constructors make corrected instances.
16 
17 ----------------------------------------------------------------------*/
18 namespace cms {
19  class Digest;
20 }
21 
22 namespace edm {
23 
24  namespace detail {
25  // This string is the 16-byte, non-printable version.
26  std::string const& InvalidHash();
27  } // namespace detail
28 
29  namespace hash_detail {
32  void fixup_(value_type& hash);
33  bool isCompactForm_(value_type const& hash);
34  bool isValid_(value_type const& hash);
35  void throwIfIllFormed(value_type const& hash);
37  void toDigest_(cms::Digest& digest, value_type const& hash);
38  std::ostream& print_(std::ostream& os, value_type const& hash);
39  size_t smallHash_(value_type const& hash);
40  } // namespace hash_detail
41 
42  template <int I>
43  class Hash {
44  public:
46 
47  Hash();
48  explicit Hash(value_type const& v);
49 
50  Hash(Hash<I> const&);
51  Hash<I>& operator=(Hash<I> const& iRHS);
52 
53  Hash(Hash<I>&&) = default;
54  Hash<I>& operator=(Hash<I>&&) = default;
55 
56  void reset();
57 
58  // For now, just check the most basic: a default constructed
59  // ParameterSetID is not valid. This is very crude: we are
60  // assuming that nobody created a ParameterSetID from an empty
61  // string, nor from any string that is not a valid string
62  // representation of an MD5 checksum.
63  bool isValid() const;
64 
65  bool operator<(Hash<I> const& other) const;
66  bool operator>(Hash<I> const& other) const;
67  bool operator==(Hash<I> const& other) const;
68  bool operator!=(Hash<I> const& other) const;
69  std::ostream& print(std::ostream& os) const;
70  void toString(std::string& result) const;
71  void toDigest(cms::Digest& digest) const;
72  void swap(Hash<I>& other);
73 
74  // Return the 16-byte (non-printable) string form.
75  value_type compactForm() const;
76 
77  bool isCompactForm() const;
78 
80  size_t smallHash() const;
81 
82  //Used by ROOT storage
83  // CMS_CLASS_VERSION(11) // This macro is not defined here, so expand it.
84  static short Class_Version() { return 11; }
85 
86  private:
89  void throwIfIllFormed() const;
90 
91  template <typename Op>
92  bool compareUsing(Hash<I> const& iOther, Op op) const {
94  bool otherCF = hash_detail::isCompactForm_(iOther.hash_);
95  if (meCF == otherCF) {
96  return op(this->hash_, iOther.hash_);
97  }
98  //copy constructor will do compact form conversion
99  if (meCF) {
100  Hash<I> temp(iOther);
101  return op(this->hash_, temp.hash_);
102  }
103  Hash<I> temp(*this);
104  return op(temp.hash_, iOther.hash_);
105  }
106 
108  };
109 
110  //--------------------------------------------------------------------
111  //
112  // Implementation details follow...
113  //--------------------------------------------------------------------
114 
115  template <int I>
116  inline Hash<I>::Hash() : hash_(detail::InvalidHash()) {}
117 
118  template <int I>
119  inline Hash<I>::Hash(typename Hash<I>::value_type const& v) : hash_(v) {
120  hash_detail::fixup_(hash_);
121  }
122 
123  template <int I>
124  inline Hash<I>::Hash(Hash<I> const& iOther) : hash_(iOther.hash_) {
125  hash_detail::fixup_(hash_);
126  }
127 
128  template <int I>
129  inline Hash<I>& Hash<I>::operator=(Hash<I> const& iRHS) {
130  hash_ = iRHS.hash_;
131  hash_detail::fixup_(hash_);
132  return *this;
133  }
134 
135  template <int I>
136  inline void Hash<I>::reset() {
137  hash_ = detail::InvalidHash();
138  }
139 
140  template <int I>
141  inline bool Hash<I>::isValid() const {
142  return hash_detail::isValid_(hash_);
143  }
144 
145  template <int I>
146  inline bool Hash<I>::operator<(Hash<I> const& other) const {
147  return this->compareUsing(other, std::less<std::string>());
148  }
149 
150  template <int I>
151  inline bool Hash<I>::operator>(Hash<I> const& other) const {
152  return this->compareUsing(other, std::greater<std::string>());
153  }
154 
155  template <int I>
156  inline bool Hash<I>::operator==(Hash<I> const& other) const {
157  return this->compareUsing(other, std::equal_to<std::string>());
158  }
159 
160  template <int I>
161  inline bool Hash<I>::operator!=(Hash<I> const& other) const {
162  return this->compareUsing(other, std::not_equal_to<std::string>());
163  }
164 
165  template <int I>
166  inline std::ostream& Hash<I>::print(std::ostream& os) const {
167  return hash_detail::print_(os, hash_);
168  }
169 
170  template <int I>
171  inline void Hash<I>::toString(std::string& result) const {
173  }
174 
175  template <int I>
176  inline void Hash<I>::toDigest(cms::Digest& digest) const {
177  hash_detail::toDigest_(digest, hash_);
178  }
179 
180  template <int I>
181  inline void Hash<I>::swap(Hash<I>& other) {
182  hash_.swap(other.hash_);
183  }
184 
185  template <int I>
186  inline typename Hash<I>::value_type Hash<I>::compactForm() const {
187  return hash_detail::compactForm_(hash_);
188  }
189 
190  template <int I>
191  inline size_t Hash<I>::smallHash() const {
192  return hash_detail::smallHash_(hash_);
193  }
194 
195  // Note: this template is not declared 'inline' because of the
196  // switch statement.
197 
198  template <int I>
199  inline bool Hash<I>::isCompactForm() const {
200  return hash_detail::isCompactForm_(hash_);
201  }
202 
203  // Free swap function
204  template <int I>
205  inline void swap(Hash<I>& a, Hash<I>& b) {
206  a.swap(b);
207  }
208 
209  template <int I>
210  inline std::ostream& operator<<(std::ostream& os, Hash<I> const& h) {
211  return h.print(os);
212  }
213 
214 } // namespace edm
215 #endif
edm::swap
void swap(Hash< I > &a, Hash< I > &b)
Definition: Hash.h:205
edm::Hash::Hash
Hash()
Definition: Hash.h:116
edm::Hash::operator<
bool operator<(Hash< I > const &other) const
Definition: Hash.h:146
edm::Hash::isCompactForm
bool isCompactForm() const
Definition: Hash.h:199
cond::Hash
std::string Hash
Definition: Types.h:43
edm::Hash::swap
void swap(Hash< I > &other)
Definition: Hash.h:181
edm::hash_detail::toString_
void toString_(std::string &result, value_type const &hash)
Definition: Hash.cc:75
cond::hash
Definition: Time.h:19
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::hash_detail::fixup_
void fixup_(value_type &hash)
Definition: Hash.cc:30
edm::hash_detail::toDigest_
void toDigest_(cms::Digest &digest, value_type const &hash)
Definition: Hash.cc:83
edm::Hash::hash_
value_type hash_
Definition: Hash.h:107
edm::hash_detail::isValid_
bool isValid_(value_type const &hash)
Definition: Hash.cc:63
edm::Hash::throwIfIllFormed
void throwIfIllFormed() const
operator>
bool operator>(DTCELinkId const &lhs, DTCELinkId const &rhs)
Definition: DTCELinkId.h:74
detail
Definition: ConvertingESProducerWithDependenciesT.h:23
findQualityFiles.v
v
Definition: findQualityFiles.py:179
edm::Hash::Class_Version
static short Class_Version()
Definition: Hash.h:84
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
edm::hash_detail::value_type
std::string value_type
Definition: Hash.h:30
edm::Hash::toDigest
void toDigest(cms::Digest &digest) const
Definition: Hash.h:176
edm::Hash::smallHash
size_t smallHash() const
returns a short hash which can be used with hashing containers
Definition: Hash.h:191
edm::Hash::isValid
bool isValid() const
Definition: Hash.h:141
toolbox::toString
std::string toString(const char *format,...)
Definition: xdaq_compat.cc:4
edm::Hash::operator=
Hash< I > & operator=(Hash< I > const &iRHS)
Definition: Hash.h:129
cms::Digest
Definition: Digest.h:46
edm::hash_detail::smallHash_
size_t smallHash_(value_type const &hash)
Definition: Hash.cc:52
edm::Hash
Definition: Hash.h:43
trackingPlots.other
other
Definition: trackingPlots.py:1464
h
sistrip::SpyUtilities::isValid
const bool isValid(const Frame &aFrame, const FrameQuality &aQuality, const uint16_t aExpectedPos)
Definition: SiStripSpyUtilities.cc:124
b
double b
Definition: hdecay.h:118
operator==
bool operator==(const QGLikelihoodParameters &lhs, const QGLikelihoodCategory &rhs)
Test if parameters are compatible with category.
Definition: QGLikelihoodObject.h:49
operator!=
bool operator!=(DTCELinkId const &lhs, DTCELinkId const &rhs)
Definition: DTCELinkId.h:81
a
double a
Definition: hdecay.h:119
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
edm::hash_detail::compactForm_
value_type compactForm_(value_type const &hash)
Definition: Hash.cc:18
edm::Hash::toString
void toString(std::string &result) const
Definition: Hash.h:171
edm::Hash::compactForm
value_type compactForm() const
Definition: Hash.h:186
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::Hash::compareUsing
bool compareUsing(Hash< I > const &iOther, Op op) const
Definition: Hash.h:92
edm::hash_detail::throwIfIllFormed
void throwIfIllFormed(value_type const &hash)
Definition: Hash.cc:67
edm::Hash::operator!=
bool operator!=(Hash< I > const &other) const
Definition: Hash.h:161
edm::operator<<
std::ostream & operator<<(std::ostream &os, Hash< I > const &h)
Definition: Hash.h:210
edm::Hash::print
std::ostream & print(std::ostream &os) const
Definition: Hash.h:166
reset
void reset(double vett[256])
Definition: TPedValues.cc:11
edm::Hash::value_type
hash_detail::value_type value_type
Definition: Hash.h:45
mps_fire.result
result
Definition: mps_fire.py:311
edm::Hash::reset
void reset()
Definition: Hash.h:136
operator<
bool operator<(DTCELinkId const &lhs, DTCELinkId const &rhs)
Definition: DTCELinkId.h:70
edm::Hash::operator>
bool operator>(Hash< I > const &other) const
Definition: Hash.h:151
edm::hash_detail::print_
std::ostream & print_(std::ostream &os, value_type const &hash)
Definition: Hash.cc:92
edm::hash_detail::isCompactForm_
bool isCompactForm_(value_type const &hash)
Definition: Hash.cc:61
cms
Namespace of DDCMS conversion namespace.
Definition: ProducerAnalyzer.cc:21
edm::Hash::operator==
bool operator==(Hash< I > const &other) const
Definition: Hash.h:156
edm::detail::InvalidHash
std::string const & InvalidHash()
Definition: Hash.cc:11