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  }
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  }
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  void reset();
54 
55  // For now, just check the most basic: a default constructed
56  // ParameterSetID is not valid. This is very crude: we are
57  // assuming that nobody created a ParameterSetID from an empty
58  // string, nor from any string that is not a valid string
59  // representation of an MD5 checksum.
60  bool isValid() const;
61 
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  bool operator!=(Hash<I> const& other) const;
66  std::ostream& print(std::ostream& os) const;
67  void toString(std::string& result) const;
68  void toDigest(cms::Digest& digest) const;
69  void swap(Hash<I>& other);
70 
71  // Return the 16-byte (non-printable) string form.
72  value_type compactForm() const;
73 
74  bool isCompactForm() const;
75 
77  size_t smallHash() const;
78 
79  //Used by ROOT storage
80  // CMS_CLASS_VERSION(11) // This macro is not defined here, so expand it.
81  static short Class_Version() {return 11;}
82 
83  private:
84 
87  void throwIfIllFormed() const;
88 
89  template<typename Op>
90  bool
91  compareUsing(Hash<I> const& iOther, Op op) const {
92  bool meCF = hash_detail::isCompactForm_(hash_);
93  bool otherCF = hash_detail::isCompactForm_(iOther.hash_);
94  if(meCF == otherCF) {
95  return op(this->hash_,iOther.hash_);
96  }
97  //copy constructor will do compact form conversion
98  if(meCF) {
99  Hash<I> temp(iOther);
100  return op(this->hash_,temp.hash_);
101  }
102  Hash<I> temp(*this);
103  return op(temp.hash_,iOther.hash_);
104  }
105 
106  value_type hash_;
107  };
108 
109 
110  //--------------------------------------------------------------------
111  //
112  // Implementation details follow...
113  //--------------------------------------------------------------------
114 
115 
116  template <int I>
117  inline
118  Hash<I>::Hash() : hash_(detail::InvalidHash()) {}
119 
120  template <int I>
121  inline
122  Hash<I>::Hash(typename Hash<I>::value_type const& v) : hash_(v) {
124  }
125 
126  template <int I>
127  inline
128  Hash<I>::Hash(Hash<I> const& iOther) : hash_(iOther.hash_) {
130  }
131 
132  template <int I>
133  inline
134  Hash<I>&
136  hash_ = iRHS.hash_;
138  return *this;
139  }
140 
141  template <int I>
142  inline
143  void
146  }
147 
148  template <int I>
149  inline
150  bool
153  }
154 
155  template <int I>
156  inline
157  bool
159  return this->compareUsing(other, std::less<std::string>());
160  }
161 
162  template <int I>
163  inline
164  bool
166  return this->compareUsing(other, std::greater<std::string>());
167  }
168 
169  template <int I>
170  inline
171  bool
173  return this->compareUsing(other, std::equal_to<std::string>());
174  }
175 
176  template <int I>
177  inline
178  bool
180  return this->compareUsing(other, std::not_equal_to<std::string>());
181  }
182 
183  template <int I>
184  inline
185  std::ostream&
186  Hash<I>::print(std::ostream& os) const {
187  return hash_detail::print_(os, hash_);
188  }
189 
190  template <int I>
191  inline
192  void
194  hash_detail::toString_(result, hash_);
195  }
196 
197  template <int I>
198  inline
199  void
201  hash_detail::toDigest_(digest, hash_);
202  }
203 
204  template <int I>
205  inline
206  void
208  hash_.swap(other.hash_);
209  }
210 
211  template <int I>
212  inline
213  typename Hash<I>::value_type
216  }
217 
218  template<int I>
219  inline
220  size_t
223  }
224 
225  // Note: this template is not declared 'inline' because of the
226  // switch statement.
227 
228  template <int I>
229  inline
230  bool Hash<I>::isCompactForm() const {
232  }
233 
234 
235  // Free swap function
236  template <int I>
237  inline
238  void
240  a.swap(b);
241  }
242 
243  template <int I>
244  inline
245  std::ostream&
246  operator<<(std::ostream& os, Hash<I> const& h) {
247  return h.print(os);
248  }
249 
250 }
251 #endif
bool operator==(Hash< I > const &other) const
Definition: Hash.h:172
value_type hash_
Definition: Hash.h:106
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:214
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:81
bool compareUsing(Hash< I > const &iOther, Op op) const
Definition: Hash.h:91
Definition: Hash.h:43
void toString_(std::string &result, value_type const &hash)
Definition: Hash.cc:87
std::string print(const Track &, edm::Verbosity=edm::Concise)
Track print utility.
Definition: print.cc:10
Hash()
Definition: Hash.h:118
bool isCompactForm() const
Definition: Hash.h:230
void swap(Hash< I > &other)
Definition: Hash.h:207
std::ostream & print(std::ostream &os) const
Definition: Hash.h:186
bool operator<(Hash< I > const &other) const
Definition: Hash.h:158
size_t smallHash_(value_type const &hash)
Definition: Hash.cc:57
std::string const & InvalidHash()
Definition: Hash.cc:11
void swap(Hash< I > &a, Hash< I > &b)
Definition: Hash.h:239
Hash< I > & operator=(Hash< I > const &iRHS)
Definition: Hash.h:135
std::string value_type
Definition: Hash.h:30
value_type compactForm_(value_type const &hash)
Definition: Hash.cc:19
std::string toString(const char *format,...)
Definition: xdaq_compat.cc:4
bool operator>(View< T > const &, View< T > const &)
Definition: View.h:382
bool operator!=(Hash< I > const &other) const
Definition: Hash.h:179
std::string Hash
Definition: Types.h:45
bool operator==(MD5Result const &a, MD5Result const &b)
Definition: Digest.cc:148
bool operator>(Hash< I > const &other) const
Definition: Hash.h:165
size_t smallHash() const
returns a short hash which can be used with hashing containers
Definition: Hash.h:221
double b
Definition: hdecay.h:120
bool operator!=(MD5Result const &a, MD5Result const &b)
Definition: Digest.h:40
void toDigest(cms::Digest &digest) const
Definition: Hash.h:200
HLT enums.
void toString(std::string &result) const
Definition: Hash.h:193
bool isValid() const
Definition: Hash.h:151
double a
Definition: hdecay.h:121
hash_detail::value_type value_type
Definition: Hash.h:45
void throwIfIllFormed(value_type const &hash)
Definition: Hash.cc:77
void reset(double vett[256])
Definition: TPedValues.cc:11
void reset()
Definition: Hash.h:144