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 {
31  value_type compactForm_(value_type const& hash);
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);
36  void toString_(std::string& result, 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 {
93  bool meCF = hash_detail::isCompactForm_(hash_);
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 
107  value_type hash_;
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) {
121  }
122 
123  template <int I>
124  inline Hash<I>::Hash(Hash<I> const& iOther) : hash_(iOther.hash_) {
126  }
127 
128  template <int I>
129  inline Hash<I>& Hash<I>::operator=(Hash<I> const& iRHS) {
130  hash_ = iRHS.hash_;
132  return *this;
133  }
134 
135  template <int I>
136  inline void Hash<I>::reset() {
138  }
139 
140  template <int I>
141  inline bool Hash<I>::isValid() const {
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 {
172  hash_detail::toString_(result, hash_);
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 {
188  }
189 
190  template <int I>
191  inline size_t Hash<I>::smallHash() const {
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 {
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
bool operator==(Hash< I > const &other) const
Definition: Hash.h:156
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
value_type hash_
Definition: Hash.h:107
void toDigest_(cms::Digest &digest, value_type const &hash)
Definition: Hash.cc:83
bool isCompactForm_(value_type const &hash)
Definition: Hash.cc:61
bool isValid_(value_type const &hash)
Definition: Hash.cc:63
value_type compactForm() const
Definition: Hash.h:186
void fixup_(value_type &hash)
Definition: Hash.cc:30
std::ostream & print_(std::ostream &os, value_type const &hash)
Definition: Hash.cc:92
static short Class_Version()
Definition: Hash.h:84
bool compareUsing(Hash< I > const &iOther, Op op) const
Definition: Hash.h:92
Definition: Hash.h:43
void toString_(std::string &result, value_type const &hash)
Definition: Hash.cc:75
Hash()
Definition: Hash.h:116
bool operator>(DTCELinkId const &lhs, DTCELinkId const &rhs)
Definition: DTCELinkId.h:73
bool isCompactForm() const
Definition: Hash.h:199
void swap(Hash< I > &other)
Definition: Hash.h:181
std::ostream & print(std::ostream &os) const
Definition: Hash.h:166
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
bool operator<(Hash< I > const &other) const
Definition: Hash.h:146
size_t smallHash_(value_type const &hash)
Definition: Hash.cc:52
std::string const & InvalidHash()
Definition: Hash.cc:11
void swap(Hash< I > &a, Hash< I > &b)
Definition: Hash.h:205
Hash< I > & operator=(Hash< I > const &iRHS)
Definition: Hash.h:129
std::string value_type
Definition: Hash.h:30
value_type compactForm_(value_type const &hash)
Definition: Hash.cc:18
std::string toString(const char *format,...)
Definition: xdaq_compat.cc:4
bool operator!=(Hash< I > const &other) const
Definition: Hash.h:161
std::string Hash
Definition: Types.h:43
bool operator==(MD5Result const &a, MD5Result const &b)
Definition: Digest.cc:141
constexpr unsigned int hash(const char *str, int h=0)
Namespace of DDCMS conversion namespace.
bool operator>(Hash< I > const &other) const
Definition: Hash.h:151
size_t smallHash() const
returns a short hash which can be used with hashing containers
Definition: Hash.h:191
double b
Definition: hdecay.h:118
bool operator!=(MD5Result const &a, MD5Result const &b)
Definition: Digest.h:37
void toDigest(cms::Digest &digest) const
Definition: Hash.h:176
HLT enums.
void toString(std::string &result) const
Definition: Hash.h:171
bool isValid() const
Definition: Hash.h:141
double a
Definition: hdecay.h:119
hash_detail::value_type value_type
Definition: Hash.h:45
void throwIfIllFormed(value_type const &hash)
Definition: Hash.cc:67
void reset(double vett[256])
Definition: TPedValues.cc:11
void reset()
Definition: Hash.h:136