test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
6 /*----------------------------------------------------------------------
7 
8 Hash:
9 
10  Note: The call to 'fixup' in every member function is a temporary
11  measure for backwards compatibility. It is necessary in every function
12  because Root creates instances of the class *without* using the
13  interface of the class, thus making it insufficient to assure that
14  all constructors make corrected instances.
15 
16 ----------------------------------------------------------------------*/
17 namespace cms {
18  class Digest;
19 }
20 
21 namespace edm {
22 
23  namespace detail {
24  // This string is the 16-byte, non-printable version.
25  std::string const& InvalidHash();
26  }
27 
28  namespace hash_detail {
31  void fixup_(value_type& hash);
32  bool isCompactForm_(value_type const& hash);
33  bool isValid_(value_type const& hash);
34  void throwIfIllFormed(value_type const& hash);
36  void toDigest_(cms::Digest& digest, value_type const& hash);
37  std::ostream& print_(std::ostream& os, value_type const& hash);
38  size_t smallHash_(value_type const& hash);
39  }
40 
41  template <int I>
42  class Hash {
43  public:
45 
46  Hash();
47  explicit Hash(value_type const& v);
48 
49  Hash(Hash<I> const&);
50  Hash<I>& operator=(Hash<I> const& iRHS);
51 
52  void reset();
53 
54  // For now, just check the most basic: a default constructed
55  // ParameterSetID is not valid. This is very crude: we are
56  // assuming that nobody created a ParameterSetID from an empty
57  // string, nor from any string that is not a valid string
58  // representation of an MD5 checksum.
59  bool isValid() const;
60 
61  bool operator<(Hash<I> const& other) const;
62  bool operator>(Hash<I> const& other) const;
63  bool operator==(Hash<I> const& other) const;
64  bool operator!=(Hash<I> const& other) const;
65  std::ostream& print(std::ostream& os) const;
66  void toString(std::string& result) const;
67  void toDigest(cms::Digest& digest) const;
68  void swap(Hash<I>& other);
69 
70  // Return the 16-byte (non-printable) string form.
71  value_type compactForm() const;
72 
73  bool isCompactForm() const;
74 
76  size_t smallHash() const;
77 
78  //Used by ROOT storage
79  // CMS_CLASS_VERSION(11) // This macro is not defined here, so expand it.
80  static short Class_Version() {return 11;}
81 
82  private:
83 
86  void throwIfIllFormed() const;
87 
88  template<typename Op>
89  bool
90  compareUsing(Hash<I> const& iOther, Op op) const {
92  bool otherCF = hash_detail::isCompactForm_(iOther.hash_);
93  if(meCF == otherCF) {
94  return op(this->hash_,iOther.hash_);
95  }
96  //copy constructor will do compact form conversion
97  if(meCF) {
98  Hash<I> temp(iOther);
99  return op(this->hash_,temp.hash_);
100  }
101  Hash<I> temp(*this);
102  return op(temp.hash_,iOther.hash_);
103  }
104 
106  };
107 
108 
109  //--------------------------------------------------------------------
110  //
111  // Implementation details follow...
112  //--------------------------------------------------------------------
113 
114 
115  template <int I>
116  inline
117  Hash<I>::Hash() : hash_(detail::InvalidHash()) {}
118 
119  template <int I>
120  inline
121  Hash<I>::Hash(typename Hash<I>::value_type const& v) : hash_(v) {
123  }
124 
125  template <int I>
126  inline
127  Hash<I>::Hash(Hash<I> const& iOther) : hash_(iOther.hash_) {
129  }
130 
131  template <int I>
132  inline
133  Hash<I>&
135  hash_ = iRHS.hash_;
136  hash_detail::fixup_(hash_);
137  return *this;
138  }
139 
140  template <int I>
141  inline
142  void
144  hash_ = detail::InvalidHash();
145  }
146 
147  template <int I>
148  inline
149  bool
151  return hash_detail::isValid_(hash_);
152  }
153 
154  template <int I>
155  inline
156  bool
157  Hash<I>::operator<(Hash<I> const& other) const {
158  return this->compareUsing(other, std::less<std::string>());
159  }
160 
161  template <int I>
162  inline
163  bool
164  Hash<I>::operator>(Hash<I> const& other) const {
165  return this->compareUsing(other, std::greater<std::string>());
166  }
167 
168  template <int I>
169  inline
170  bool
171  Hash<I>::operator==(Hash<I> const& other) const {
172  return this->compareUsing(other, std::equal_to<std::string>());
173  }
174 
175  template <int I>
176  inline
177  bool
178  Hash<I>::operator!=(Hash<I> const& other) const {
179  return this->compareUsing(other, std::not_equal_to<std::string>());
180  }
181 
182  template <int I>
183  inline
184  std::ostream&
185  Hash<I>::print(std::ostream& os) const {
186  return hash_detail::print_(os, hash_);
187  }
188 
189  template <int I>
190  inline
191  void
193  hash_detail::toString_(result, hash_);
194  }
195 
196  template <int I>
197  inline
198  void
200  hash_detail::toDigest_(digest, hash_);
201  }
202 
203  template <int I>
204  inline
205  void
207  hash_.swap(other.hash_);
208  }
209 
210  template <int I>
211  inline
212  typename Hash<I>::value_type
214  return hash_detail::compactForm_(hash_);
215  }
216 
217  template<int I>
218  inline
219  size_t
221  return hash_detail::smallHash_(hash_);
222  }
223 
224  // Note: this template is not declared 'inline' because of the
225  // switch statement.
226 
227  template <int I>
228  inline
229  bool Hash<I>::isCompactForm() const {
230  return hash_detail::isCompactForm_(hash_);
231  }
232 
233 
234  // Free swap function
235  template <int I>
236  inline
237  void
239  a.swap(b);
240  }
241 
242  template <int I>
243  inline
244  std::ostream&
245  operator<<(std::ostream& os, Hash<I> const& h) {
246  return h.print(os);
247  }
248 
249 }
250 #endif
bool operator==(Hash< I > const &other) const
Definition: Hash.h:171
value_type hash_
Definition: Hash.h:105
void toDigest_(cms::Digest &digest, value_type const &hash)
Definition: Hash.cc:96
bool isCompactForm_(value_type const &hash)
Definition: Hash.cc:67
bool isValid_(value_type const &hash)
Definition: Hash.cc:72
value_type compactForm() const
Definition: Hash.h:213
void fixup_(value_type &hash)
Definition: Hash.cc:32
std::ostream & print_(std::ostream &os, value_type const &hash)
Definition: Hash.cc:106
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
static short Class_Version()
Definition: Hash.h:80
bool compareUsing(Hash< I > const &iOther, Op op) const
Definition: Hash.h:90
Definition: Hash.h:42
void toString_(std::string &result, value_type const &hash)
Definition: Hash.cc:87
Hash()
Definition: Hash.h:117
bool isCompactForm() const
Definition: Hash.h:229
void swap(Hash< I > &other)
Definition: Hash.h:206
std::ostream & print(std::ostream &os) const
Definition: Hash.h:185
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
tuple result
Definition: mps_fire.py:84
bool operator<(Hash< I > const &other) const
Definition: Hash.h:157
size_t smallHash_(value_type const &hash)
Definition: Hash.cc:57
std::string const & InvalidHash()
Definition: Hash.cc:11
Hash< I > & operator=(Hash< I > const &iRHS)
Definition: Hash.h:134
std::string value_type
Definition: Hash.h:29
value_type compactForm_(value_type const &hash)
Definition: Hash.cc:19
bool operator!=(Hash< I > const &other) const
Definition: Hash.h:178
bool operator>(Hash< I > const &other) const
Definition: Hash.h:164
void throwIfIllFormed() const
size_t smallHash() const
returns a short hash which can be used with hashing containers
Definition: Hash.h:220
double b
Definition: hdecay.h:120
void toDigest(cms::Digest &digest) const
Definition: Hash.h:199
void toString(std::string &result) const
Definition: Hash.h:192
bool isValid() const
Definition: Hash.h:150
double a
Definition: hdecay.h:121
hash_detail::value_type value_type
Definition: Hash.h:44
void throwIfIllFormed(value_type const &hash)
Definition: Hash.cc:77
void reset()
Definition: Hash.h:143