File indexing completed on 2024-11-25 02:29:28
0001 #ifndef DataFormats_Provenance_CompactHash_h
0002 #define DataFormats_Provenance_CompactHash_h
0003
0004 #include <string_view>
0005 #include <array>
0006 #include <functional>
0007
0008 namespace cms {
0009 class Digest;
0010 }
0011
0012 namespace edm {
0013
0014 namespace detail {
0015
0016 std::array<unsigned char, 16> const& InvalidCompactHash();
0017 }
0018
0019 namespace compact_hash_detail {
0020 using value_type = std::array<unsigned char, 16>;
0021 void toString_(std::string& result, value_type const& hash);
0022 void toDigest_(cms::Digest& digest, value_type const& hash);
0023 std::ostream& print_(std::ostream& os, value_type const& hash);
0024 bool isValid_(value_type const& hash);
0025 size_t smallHash_(value_type const& hash);
0026 value_type fromHex_(std::string_view);
0027 void throwIfIllFormed(std::string_view v);
0028 }
0029
0030 template <int I>
0031 class CompactHash {
0032 public:
0033 typedef compact_hash_detail::value_type value_type;
0034
0035 CompactHash();
0036 explicit CompactHash(value_type const& v);
0037 explicit CompactHash(std::string_view v);
0038
0039 CompactHash(CompactHash<I> const&) = default;
0040 CompactHash<I>& operator=(CompactHash<I> const& iRHS) = default;
0041
0042 CompactHash(CompactHash<I>&&) = default;
0043 CompactHash<I>& operator=(CompactHash<I>&&) = default;
0044
0045 void reset();
0046
0047
0048
0049
0050
0051
0052 bool isValid() const;
0053
0054 bool operator<(CompactHash<I> const& other) const;
0055 bool operator>(CompactHash<I> const& other) const;
0056 bool operator==(CompactHash<I> const& other) const;
0057 bool operator!=(CompactHash<I> const& other) const;
0058 std::ostream& print(std::ostream& os) const;
0059 void toString(std::string& result) const;
0060 void toDigest(cms::Digest& digest) const;
0061
0062
0063 value_type const& compactForm() const;
0064
0065
0066 size_t smallHash() const;
0067
0068
0069
0070 static short Class_Version() { return 3; }
0071
0072 private:
0073 template <typename Op>
0074 bool compareUsing(CompactHash<I> const& iOther, Op op) const {
0075 return op(this->hash_, iOther.hash_);
0076 }
0077
0078 value_type hash_;
0079 };
0080
0081
0082
0083
0084
0085
0086 template <int I>
0087 inline CompactHash<I>::CompactHash() : hash_(detail::InvalidCompactHash()) {}
0088
0089 template <int I>
0090 inline CompactHash<I>::CompactHash(value_type const& v) : hash_(v) {}
0091
0092 template <int I>
0093 inline CompactHash<I>::CompactHash(std::string_view v) {
0094 if (v.size() == 32) {
0095 hash_ = compact_hash_detail::fromHex_(v);
0096 } else {
0097 compact_hash_detail::throwIfIllFormed(v);
0098 std::copy(v.begin(), v.end(), hash_.begin());
0099 }
0100 }
0101
0102 template <int I>
0103 inline void CompactHash<I>::reset() {
0104 hash_ = detail::InvalidCompactHash();
0105 }
0106
0107 template <int I>
0108 inline bool CompactHash<I>::isValid() const {
0109 return compact_hash_detail::isValid_(hash_);
0110 }
0111
0112 template <int I>
0113 inline bool CompactHash<I>::operator<(CompactHash<I> const& other) const {
0114 return this->compareUsing(other, std::less<value_type>());
0115 }
0116
0117 template <int I>
0118 inline bool CompactHash<I>::operator>(CompactHash<I> const& other) const {
0119 return this->compareUsing(other, std::greater<value_type>());
0120 }
0121
0122 template <int I>
0123 inline bool CompactHash<I>::operator==(CompactHash<I> const& other) const {
0124 return this->compareUsing(other, std::equal_to<value_type>());
0125 }
0126
0127 template <int I>
0128 inline bool CompactHash<I>::operator!=(CompactHash<I> const& other) const {
0129 return this->compareUsing(other, std::not_equal_to<value_type>());
0130 }
0131
0132 template <int I>
0133 inline std::ostream& CompactHash<I>::print(std::ostream& os) const {
0134 return compact_hash_detail::print_(os, hash_);
0135 }
0136
0137 template <int I>
0138 inline void CompactHash<I>::toString(std::string& result) const {
0139 compact_hash_detail::toString_(result, hash_);
0140 }
0141
0142 template <int I>
0143 inline void CompactHash<I>::toDigest(cms::Digest& digest) const {
0144 compact_hash_detail::toDigest_(digest, hash_);
0145 }
0146
0147 template <int I>
0148 inline typename CompactHash<I>::value_type const& CompactHash<I>::compactForm() const {
0149 return hash_;
0150 }
0151
0152 template <int I>
0153 inline size_t CompactHash<I>::smallHash() const {
0154 return compact_hash_detail::smallHash_(hash_);
0155 }
0156
0157 template <int I>
0158 inline std::ostream& operator<<(std::ostream& os, CompactHash<I> const& h) {
0159 return h.print(os);
0160 }
0161
0162 }
0163 #endif