Update hash functions to use folly:#️⃣:hash_combine

Summary: Following hashing best practices.

Reviewed By: shergin

Differential Revision: D13827893

fbshipit-source-id: 3786f1e42b176a973890989be7b33efce4825ac6
This commit is contained in:
Joshua Gross 2019-01-28 14:07:46 -08:00 committed by Facebook Github Bot
parent 6418e30e90
commit 88bc80c518
6 changed files with 83 additions and 85 deletions

View File

@ -91,10 +91,12 @@ struct hash<facebook::react::AttributedString::Fragment> {
size_t operator()( size_t operator()(
const facebook::react::AttributedString::Fragment &fragment) const { const facebook::react::AttributedString::Fragment &fragment) const {
auto seed = size_t{0}; auto seed = size_t{0};
folly::hash::hash_combine(seed, fragment.string); folly::hash::hash_combine(
folly::hash::hash_combine(seed, fragment.textAttributes); seed,
folly::hash::hash_combine(seed, fragment.shadowView); fragment.string,
folly::hash::hash_combine(seed, fragment.parentShadowView); fragment.textAttributes,
fragment.shadowView,
fragment.parentShadowView);
return seed; return seed;
} }
}; };

View File

@ -73,15 +73,15 @@ template <>
struct hash<facebook::react::ParagraphAttributes> { struct hash<facebook::react::ParagraphAttributes> {
size_t operator()( size_t operator()(
const facebook::react::ParagraphAttributes &attributes) const { const facebook::react::ParagraphAttributes &attributes) const {
size_t seed = 0; auto seed = size_t{0};
folly::hash::hash_combine(seed, attributes.maximumNumberOfLines);
folly::hash::hash_combine(seed, attributes.ellipsizeMode);
folly::hash::hash_combine(seed, attributes.adjustsFontSizeToFit);
folly::hash::hash_combine( folly::hash::hash_combine(
seed, std::hash<float>{}(attributes.minimumFontSize)); seed,
folly::hash::hash_combine( attributes.maximumNumberOfLines,
seed, std::hash<float>{}(attributes.maximumFontSize)); attributes.ellipsizeMode,
return hash<int>()(seed); attributes.adjustsFontSizeToFit,
attributes.minimumFontSize,
attributes.maximumFontSize);
return seed;
} }
}; };
} // namespace std } // namespace std

View File

@ -10,6 +10,7 @@
#include <functional> #include <functional>
#include <limits> #include <limits>
#include <folly/Hash.h>
#include <folly/Optional.h> #include <folly/Optional.h>
#include <react/attributedstring/primitives.h> #include <react/attributedstring/primitives.h>
#include <react/core/LayoutPrimitives.h> #include <react/core/LayoutPrimitives.h>
@ -92,55 +93,38 @@ class TextAttributes : public DebugStringConvertible {
} // namespace facebook } // namespace facebook
namespace std { namespace std {
template <> template <>
struct hash<facebook::react::TextAttributes> { struct hash<facebook::react::TextAttributes> {
size_t operator()( size_t operator()(
const facebook::react::TextAttributes &textAttributes) const { const facebook::react::TextAttributes &textAttributes) const {
return std::hash<decltype(textAttributes.foregroundColor)>{}( auto seed = size_t{0};
textAttributes.foregroundColor) + folly::hash::hash_combine(
std::hash<decltype(textAttributes.backgroundColor)>{}( seed,
textAttributes.backgroundColor) + textAttributes.foregroundColor,
std::hash<decltype(textAttributes.opacity)>{}(textAttributes.opacity) + textAttributes.backgroundColor,
std::hash<decltype(textAttributes.fontFamily)>{}( textAttributes.opacity,
textAttributes.fontFamily) + textAttributes.fontFamily,
std::hash<decltype(textAttributes.fontSize)>{}( textAttributes.fontSize,
textAttributes.fontSize) + textAttributes.fontSizeMultiplier,
std::hash<decltype(textAttributes.fontSizeMultiplier)>{}( textAttributes.fontWeight,
textAttributes.fontSizeMultiplier) + textAttributes.fontStyle,
std::hash<decltype(textAttributes.fontWeight)>{}( textAttributes.fontVariant,
textAttributes.fontWeight) + textAttributes.allowFontScaling,
std::hash<decltype(textAttributes.fontStyle)>{}( textAttributes.letterSpacing,
textAttributes.fontStyle) + textAttributes.lineHeight,
std::hash<decltype(textAttributes.fontVariant)>{}( textAttributes.alignment,
textAttributes.fontVariant) + textAttributes.baseWritingDirection,
std::hash<decltype(textAttributes.allowFontScaling)>{}( textAttributes.textDecorationColor,
textAttributes.allowFontScaling) + textAttributes.textDecorationLineType,
std::hash<decltype(textAttributes.letterSpacing)>{}( textAttributes.textDecorationLineStyle,
textAttributes.letterSpacing) + textAttributes.textDecorationLinePattern,
std::hash<decltype(textAttributes.lineHeight)>{}( textAttributes.textShadowOffset,
textAttributes.lineHeight) + textAttributes.textShadowRadius,
std::hash<decltype(textAttributes.alignment)>{}( textAttributes.textShadowColor,
textAttributes.alignment) + textAttributes.isHighlighted,
std::hash<decltype(textAttributes.baseWritingDirection)>{}(
textAttributes.baseWritingDirection) +
std::hash<decltype(textAttributes.textDecorationColor)>{}(
textAttributes.textDecorationColor) +
std::hash<decltype(textAttributes.textDecorationLineType)>{}(
textAttributes.textDecorationLineType) +
std::hash<decltype(textAttributes.textDecorationLineStyle)>{}(
textAttributes.textDecorationLineStyle) +
std::hash<decltype(textAttributes.textDecorationLinePattern)>{}(
textAttributes.textDecorationLinePattern) +
std::hash<decltype(textAttributes.textShadowOffset)>{}(
textAttributes.textShadowOffset) +
std::hash<decltype(textAttributes.textShadowRadius)>{}(
textAttributes.textShadowRadius) +
std::hash<decltype(textAttributes.textShadowColor)>{}(
textAttributes.textShadowColor) +
std::hash<decltype(textAttributes.isHighlighted)>{}(
textAttributes.isHighlighted) +
std::hash<decltype(textAttributes.layoutDirection)>{}(
textAttributes.layoutDirection); textAttributes.layoutDirection);
return seed;
} }
}; };
} // namespace std } // namespace std

View File

@ -36,14 +36,15 @@ inline bool operator==(
namespace std { namespace std {
template <> template <>
struct hash<facebook::react::LayoutConstraints> { struct hash<facebook::react::LayoutConstraints> {
size_t operator()(const facebook::react::LayoutConstraints &v) const { size_t operator()(
size_t seed = 0; const facebook::react::LayoutConstraints &constraints) const {
auto seed = size_t{0};
folly::hash::hash_combine( folly::hash::hash_combine(
seed, std::hash<facebook::react::Size>()(v.minimumSize)); seed,
folly::hash::hash_combine( constraints.minimumSize,
seed, std::hash<facebook::react::Size>()(v.maximumSize)); constraints.maximumSize,
folly::hash::hash_combine(seed, v.layoutDirection); constraints.layoutDirection);
return hash<int>()(seed); return seed;
} }
}; };
} // namespace std } // namespace std

View File

@ -9,6 +9,7 @@
#include <functional> #include <functional>
#include <tuple> #include <tuple>
#include <folly/Hash.h>
#include <react/graphics/Float.h> #include <react/graphics/Float.h>
namespace facebook { namespace facebook {
@ -185,44 +186,51 @@ namespace std {
template <> template <>
struct hash<facebook::react::Point> { struct hash<facebook::react::Point> {
size_t operator()(const facebook::react::Point &point) const { size_t operator()(const facebook::react::Point &point) const {
return hash<decltype(point.x)>{}(point.x) + auto seed = size_t{0};
hash<decltype(point.y)>{}(point.y); folly::hash::hash_combine(seed, point.x, point.y);
return seed;
} }
}; };
template <> template <>
struct hash<facebook::react::Size> { struct hash<facebook::react::Size> {
size_t operator()(const facebook::react::Size &size) const { size_t operator()(const facebook::react::Size &size) const {
return hash<decltype(size.width)>{}(size.width) + auto seed = size_t{0};
hash<decltype(size.height)>{}(size.height); folly::hash::hash_combine(seed, size.width, size.height);
return seed;
} }
}; };
template <> template <>
struct hash<facebook::react::Rect> { struct hash<facebook::react::Rect> {
size_t operator()(const facebook::react::Rect &rect) const { size_t operator()(const facebook::react::Rect &rect) const {
return hash<decltype(rect.origin)>{}(rect.origin) + auto seed = size_t{0};
hash<decltype(rect.size)>{}(rect.size); folly::hash::hash_combine(seed, rect.origin, rect.size);
return seed;
} }
}; };
template <typename T> template <typename T>
struct hash<facebook::react::RectangleEdges<T>> { struct hash<facebook::react::RectangleEdges<T>> {
size_t operator()(const facebook::react::RectangleEdges<T> &edges) const { size_t operator()(const facebook::react::RectangleEdges<T> &edges) const {
return hash<decltype(edges.left)>{}(edges.left) + auto seed = size_t{0};
hash<decltype(edges.right)>{}(edges.right) + folly::hash::hash_combine(
hash<decltype(edges.top)>{}(edges.top) + seed, edges.left, edges.right, edges.top, edges.bottom);
hash<decltype(edges.bottom)>{}(edges.bottom); return seed;
} }
}; };
template <typename T> template <typename T>
struct hash<facebook::react::RectangleCorners<T>> { struct hash<facebook::react::RectangleCorners<T>> {
size_t operator()(const facebook::react::RectangleCorners<T> &corners) const { size_t operator()(const facebook::react::RectangleCorners<T> &corners) const {
return hash<decltype(corners.topLeft)>{}(corners.topLeft) + auto seed = size_t{0};
hash<decltype(corners.bottomLeft)>{}(corners.bottomLeft) + folly::hash::hash_combine(
hash<decltype(corners.topRight)>{}(corners.topRight) + seed,
hash<decltype(corners.bottomRight)>{}(corners.bottomRight); corners.topLeft,
corners.bottomLeft,
corners.topRight,
corners.bottomRight);
return seed;
} }
}; };

View File

@ -5,6 +5,7 @@
#pragma once #pragma once
#include <folly/Hash.h>
#include <react/core/LayoutMetrics.h> #include <react/core/LayoutMetrics.h>
#include <react/core/LocalData.h> #include <react/core/LocalData.h>
#include <react/core/Props.h> #include <react/core/Props.h>
@ -65,13 +66,15 @@ namespace std {
template <> template <>
struct hash<facebook::react::ShadowView> { struct hash<facebook::react::ShadowView> {
size_t operator()(const facebook::react::ShadowView &shadowView) const { size_t operator()(const facebook::react::ShadowView &shadowView) const {
return std::hash<decltype(shadowView.componentHandle)>{}( auto seed = size_t{0};
shadowView.componentHandle) + folly::hash::hash_combine(
std::hash<decltype(shadowView.tag)>{}(shadowView.tag) + seed,
std::hash<decltype(shadowView.props)>{}(shadowView.props) + shadowView.componentHandle,
std::hash<decltype(shadowView.eventEmitter)>{}( shadowView.tag,
shadowView.eventEmitter) + shadowView.props,
std::hash<decltype(shadowView.localData)>{}(shadowView.localData); shadowView.eventEmitter,
shadowView.localData);
return seed;
} }
}; };