Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up Selector_List::operator== #2739

Merged
merged 2 commits into from
Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 10 additions & 27 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,34 +459,17 @@ namespace Sass {

bool Selector_List::operator== (const Selector_List& rhs) const
{
// for array access
size_t i = 0, n = 0;
size_t iL = length();
size_t nL = rhs.length();
// create temporary vectors and sort them
std::vector<Complex_Selector_Obj> l_lst = this->elements();
std::vector<Complex_Selector_Obj> r_lst = rhs.elements();
std::sort(l_lst.begin(), l_lst.end(), OrderNodes());
std::sort(r_lst.begin(), r_lst.end(), OrderNodes());
// process loop
while (true)
{
// first check for valid index
if (i == iL) return iL == nL;
else if (n == nL) return iL == nL;
// the access the vector items
Complex_Selector_Obj l = l_lst[i];
Complex_Selector_Obj r = r_lst[n];
// skip nulls
if (!l) ++i;
else if (!r) ++n;
// do the check
else if (*l != *r)
{ return false; }
// advance
++i; ++n;
if (&rhs == this) return true;
if (rhs.length() != length()) return false;
std::unordered_set<const Complex_Selector *, HashPtr, ComparePtrs> lhs_set;
lhs_set.reserve(length());
for (const Complex_Selector_Obj &element : elements()) {
lhs_set.insert(element.ptr());
}
// there is no break?!
for (const Complex_Selector_Obj &element : rhs.elements()) {
if (lhs_set.find(element.ptr()) == lhs_set.end()) return false;
}
return true;
}

bool Selector_List::operator< (const Selector& rhs) const
Expand Down
2 changes: 1 addition & 1 deletion src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2288,7 +2288,7 @@ namespace Sass {
name += ns_ + "|";
return name + name_;
}
size_t hash() const override
virtual size_t hash() const override
{
if (hash_ == 0) {
hash_combine(hash_, std::hash<int>()(SELECTOR));
Expand Down
13 changes: 13 additions & 0 deletions src/ast_fwd_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,19 @@ namespace Sass {
}
};

struct HashPtr {
template <class T>
size_t operator() (const T *ref) const {
return ref->hash();
}
};
struct ComparePtrs {
template <class T>
bool operator() (const T *lhs, const T *rhs) const {
return *lhs == *rhs;
}
};

// ###########################################################################
// some often used typedefs
// ###########################################################################
Expand Down