Skip to content

Commit c8885dc

Browse files
committed
Merge pull request #1090 from mgreter/hotfix/issue_1086
Improve hash map key handling
2 parents 26efca9 + 9684c00 commit c8885dc

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

ast.hpp

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -851,11 +851,34 @@ namespace Sass {
851851
ADD_PROPERTY(Type, type);
852852
ADD_PROPERTY(Expression*, left);
853853
ADD_PROPERTY(Expression*, right);
854+
size_t hash_;
854855
public:
855856
Binary_Expression(ParserState pstate,
856857
Type t, Expression* lhs, Expression* rhs)
857-
: Expression(pstate), type_(t), left_(lhs), right_(rhs)
858+
: Expression(pstate), type_(t), left_(lhs), right_(rhs), hash_(0)
858859
{ }
860+
virtual bool operator==(Expression& rhs) const
861+
{
862+
try
863+
{
864+
Binary_Expression& m = dynamic_cast<Binary_Expression&>(rhs);
865+
if (m == 0) return false;
866+
return type() == m.type() &&
867+
left() == m.left() &&
868+
right() == m.right();
869+
}
870+
catch (std::bad_cast&)
871+
{
872+
return false;
873+
}
874+
catch (...) { throw; }
875+
}
876+
virtual size_t hash()
877+
{
878+
if (hash_ > 0) return hash_;
879+
hash_ = left()->hash() ^ right()->hash() ^ std::hash<size_t>()(type_);
880+
return hash_;
881+
}
859882
ATTACH_OPERATIONS();
860883
};
861884

@@ -868,10 +891,32 @@ namespace Sass {
868891
private:
869892
ADD_PROPERTY(Type, type);
870893
ADD_PROPERTY(Expression*, operand);
894+
size_t hash_;
871895
public:
872896
Unary_Expression(ParserState pstate, Type t, Expression* o)
873-
: Expression(pstate), type_(t), operand_(o)
897+
: Expression(pstate), type_(t), operand_(o), hash_(0)
874898
{ }
899+
virtual bool operator==(Expression& rhs) const
900+
{
901+
try
902+
{
903+
Unary_Expression& m = dynamic_cast<Unary_Expression&>(rhs);
904+
if (m == 0) return false;
905+
return type() == m.type() &&
906+
operand() == m.operand();
907+
}
908+
catch (std::bad_cast&)
909+
{
910+
return false;
911+
}
912+
catch (...) { throw; }
913+
}
914+
virtual size_t hash()
915+
{
916+
if (hash_ > 0) return hash_;
917+
hash_ = operand()->hash() ^ std::hash<size_t>()(type_);
918+
return hash_;
919+
}
875920
ATTACH_OPERATIONS();
876921
};
877922

@@ -899,7 +944,7 @@ namespace Sass {
899944
{
900945
Argument& m = dynamic_cast<Argument&>(rhs);
901946
if (!(m && name() == m.name())) return false;
902-
return *value() == *value();
947+
return *value() == *m.value();
903948
}
904949
catch (std::bad_cast&)
905950
{

0 commit comments

Comments
 (0)