Bug 761393 - Add a method to hash an nsStyleCoord. r=dbaron
authorPaul Adenot <paul@paul.cx>
Sun, 26 Aug 2012 21:08:32 -0700
changeset 105650 07a27119d9c983c88687f406e53ce6cabf4ae028
parent 105649 1d702ef4214a0b25064e894151a49e451b7678ef
child 105651 dd28317d64d4715d9b36e5a5f0be31f4cfcef25c
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewersdbaron
bugs761393
milestone18.0a1
Bug 761393 - Add a method to hash an nsStyleCoord. r=dbaron
layout/style/nsStyleCoord.cpp
layout/style/nsStyleCoord.h
--- a/layout/style/nsStyleCoord.cpp
+++ b/layout/style/nsStyleCoord.cpp
@@ -6,16 +6,17 @@
 /* representation of length values in computed style data */
 
 #include "nsStyleCoord.h"
 #include "nsString.h"
 #include "nsCRT.h"
 #include "prlog.h"
 #include "nsMathUtils.h"
 #include "nsStyleContext.h"
+#include "mozilla/HashFunctions.h"
 
 nsStyleCoord::nsStyleCoord(nsStyleUnit aUnit)
   : mUnit(aUnit)
 {
   NS_ASSERTION(aUnit < eStyleUnit_Percent, "not a valueless unit");
   if (aUnit >= eStyleUnit_Percent) {
     mUnit = eStyleUnit_Null;
   }
@@ -74,16 +75,49 @@ bool nsStyleCoord::operator==(const nsSt
       return mValue.mInt == aOther.mValue.mInt;
     case eStyleUnit_Calc:
       return *this->GetCalcValue() == *aOther.GetCalcValue();
   }
   NS_ABORT_IF_FALSE(false, "unexpected unit");
   return false;
 }
 
+uint32_t nsStyleCoord::HashValue(uint32_t aHash = 0) const
+{
+  aHash = mozilla::AddToHash(aHash, mUnit);
+
+  switch (mUnit) {
+    case eStyleUnit_Null:
+    case eStyleUnit_Normal:
+    case eStyleUnit_Auto:
+    case eStyleUnit_None:
+      return mozilla::AddToHash(aHash, true);
+    case eStyleUnit_Percent:
+    case eStyleUnit_Factor:
+    case eStyleUnit_Degree:
+    case eStyleUnit_Grad:
+    case eStyleUnit_Radian:
+    case eStyleUnit_Turn:
+      return mozilla::AddToHash(aHash, mValue.mFloat);
+    case eStyleUnit_Coord:
+    case eStyleUnit_Integer:
+    case eStyleUnit_Enumerated:
+      return mozilla::AddToHash(aHash, mValue.mInt);
+    case eStyleUnit_Calc:
+      Calc* calcValue = GetCalcValue();
+      aHash = mozilla::AddToHash(aHash, calcValue->mLength);
+      if (HasPercent()) {
+        return mozilla::AddToHash(aHash, calcValue->mPercent);
+      }
+      return aHash;
+  }
+  NS_ABORT_IF_FALSE(false, "unexpected unit");
+  return aHash;
+}
+
 void nsStyleCoord::Reset()
 {
   mUnit = eStyleUnit_Null;
   mValue.mInt = 0;
 }
 
 void nsStyleCoord::SetCoordValue(nscoord aValue)
 {
--- a/layout/style/nsStyleCoord.h
+++ b/layout/style/nsStyleCoord.h
@@ -130,16 +130,17 @@ public:
   nscoord     GetCoordValue() const;
   int32_t     GetIntValue() const;
   float       GetPercentValue() const;
   float       GetFactorValue() const;
   float       GetAngleValue() const;
   double      GetAngleValueInRadians() const;
   Calc*       GetCalcValue() const;
   void        GetUnionValue(nsStyleUnion& aValue) const;
+  uint32_t    HashValue(uint32_t aHash) const;
 
   void  Reset();  // sets to null
   void  SetCoordValue(nscoord aValue);
   void  SetIntValue(int32_t aValue, nsStyleUnit aUnit);
   void  SetPercentValue(float aValue);
   void  SetFactorValue(float aValue);
   void  SetAngleValue(float aValue, nsStyleUnit aUnit);
   void  SetNormalValue();