Skip to content

Commit

Permalink
Comparison operators
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed Nov 2, 2024
1 parent 0b8acd7 commit dd5118d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 99 deletions.
36 changes: 10 additions & 26 deletions include/momo/stdish/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -650,45 +650,29 @@ namespace internal
mTreeMap.MergeFrom(map.get_nested_container());
}

bool operator==(const map_base& right) const
friend bool operator==(const map_base& left, const map_base& right)
{
return size() == right.size() && std::equal(begin(), end(), right.begin());
}

bool operator!=(const map_base& right) const
{
return !(*this == right);
return left.size() == right.size()
&& std::equal(left.begin(), left.end(), right.begin());
}

#ifdef MOMO_HAS_THREE_WAY_COMPARISON
auto operator<=>(const map_base& right) const
friend auto operator<=>(const map_base& left, const map_base& right)
requires requires (const_reference ref) { ref <=> ref; }
{
return std::lexicographical_compare_three_way(begin(), end(),
return std::lexicographical_compare_three_way(left.begin(), left.end(),
right.begin(), right.end());
}
#else
bool operator<(const map_base& right) const
{
return std::lexicographical_compare(begin(), end(), right.begin(), right.end());
}

bool operator>(const map_base& right) const
{
return right < *this;
}

bool operator<=(const map_base& right) const
friend bool operator<(const map_base& left, const map_base& right)
{
return !(right < *this);
}

bool operator>=(const map_base& right) const
{
return right <= *this;
return std::lexicographical_compare(left.begin(), left.end(),
right.begin(), right.end());
}
#endif

MOMO_MORE_COMPARISON_OPERATORS(const map_base&)

protected: //?
void ptAssign(std::initializer_list<value_type> values)
{
Expand Down
10 changes: 6 additions & 4 deletions include/momo/stdish/pool_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,16 @@ class unsynchronized_pool_allocator
momo::internal::ObjectManager<Value, MemManager>::Destroy(mMemPool->GetMemManager(), *ptr);
}

bool operator==(const unsynchronized_pool_allocator& alloc) const noexcept
friend bool operator==(const unsynchronized_pool_allocator& left,
const unsynchronized_pool_allocator& right) noexcept
{
return mMemPool == alloc.mMemPool;
return left.mMemPool == right.mMemPool;
}

bool operator!=(const unsynchronized_pool_allocator& alloc) const noexcept
friend bool operator!=(const unsynchronized_pool_allocator& left,
const unsynchronized_pool_allocator& right) noexcept
{
return !(*this == alloc);
return !(left == right);
}

protected:
Expand Down
34 changes: 8 additions & 26 deletions include/momo/stdish/set.h
Original file line number Diff line number Diff line change
Expand Up @@ -544,47 +544,29 @@ class set
mTreeSet.MergeFrom(set.get_nested_container());
}

bool operator==(const set& right) const
friend bool operator==(const set& left, const set& right)
{
return size() == right.size() && std::equal(begin(), end(), right.begin());
}

bool operator!=(const set& right) const
{
return !(*this == right);
return left.size() == right.size() && std::equal(left.begin(), left.end(), right.begin());
}

#ifdef MOMO_HAS_THREE_WAY_COMPARISON
auto operator<=>(const set& right) const
friend auto operator<=>(const set& left, const set& right)
requires requires (const_reference ref) { std::tie(ref) <=> std::tie(ref); }
{
auto comp = [] (const value_type& value1, const value_type& value2)
{ return std::tie(value1) <=> std::tie(value2); };
return std::lexicographical_compare_three_way(begin(), end(),
return std::lexicographical_compare_three_way(left.begin(), left.end(),
right.begin(), right.end(), comp);
}
#else
bool operator<(const set& right) const
friend bool operator<(const set& left, const set& right)
{
return std::lexicographical_compare(begin(), end(), right.begin(), right.end());
}

bool operator>(const set& right) const
{
return right < *this;
}

bool operator<=(const set& right) const
{
return !(right < *this);
}

bool operator>=(const set& right) const
{
return right <= *this;
return std::lexicographical_compare(left.begin(), left.end(), right.begin(), right.end());
}
#endif

MOMO_MORE_COMPARISON_OPERATORS(const set&)

private:
static TreeSet pvCreateSet(set&& right, const allocator_type& alloc)
{
Expand Down
10 changes: 5 additions & 5 deletions include/momo/stdish/unordered_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -811,11 +811,11 @@ class unordered_map
return static_cast<float>(count) / static_cast<float>(bucketCount);
}

bool operator==(const unordered_map& right) const
friend bool operator==(const unordered_map& left, const unordered_map& right)
{
if (size() != right.size())
if (left.size() != right.size())
return false;
for (const_reference ref : *this)
for (const_reference ref : left)
{
const_iterator iter = right.find(ref.first);
if (iter == right.end())
Expand All @@ -826,9 +826,9 @@ class unordered_map
return true;
}

bool operator!=(const unordered_map& right) const
friend bool operator!=(const unordered_map& left, const unordered_map& right)
{
return !(*this == right);
return !(left == right);
}

private:
Expand Down
15 changes: 8 additions & 7 deletions include/momo/stdish/unordered_multimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -619,17 +619,18 @@ class unordered_multimap
//size_type bucket(const key_type& key) const
//float load_factor() const noexcept

bool operator==(const unordered_multimap& right) const
friend bool operator==(const unordered_multimap& left, const unordered_multimap& right)
{
if (mHashMultiMap.GetKeyCount() != right.mHashMultiMap.GetKeyCount())
if (left.mHashMultiMap.GetKeyCount() != right.mHashMultiMap.GetKeyCount())
return false;
if (mHashMultiMap.GetCount() != right.mHashMultiMap.GetCount())
if (left.mHashMultiMap.GetCount() != right.mHashMultiMap.GetCount())
return false;
for (typename HashMultiMap::ConstKeyIterator::Reference ref : mHashMultiMap.GetKeyBounds())
typedef typename HashMultiMap::ConstKeyIterator ConstKeyIterator;
for (typename ConstKeyIterator::Reference ref : left.mHashMultiMap.GetKeyBounds())
{
if (ref.GetCount() == 0)
continue;
typename HashMultiMap::ConstKeyIterator rightKeyIter = right.mHashMultiMap.Find(ref.key);
ConstKeyIterator rightKeyIter = right.mHashMultiMap.Find(ref.key);
if (!rightKeyIter)
return false;
if (ref.GetCount() != rightKeyIter->GetCount())
Expand All @@ -640,9 +641,9 @@ class unordered_multimap
return true;
}

bool operator!=(const unordered_multimap& right) const
friend bool operator!=(const unordered_multimap& left, const unordered_multimap& right)
{
return !(*this == right);
return !(left == right);
}

private:
Expand Down
10 changes: 5 additions & 5 deletions include/momo/stdish/unordered_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -619,21 +619,21 @@ class unordered_set
return static_cast<float>(count) / static_cast<float>(bucketCount);
}

bool operator==(const unordered_set& right) const
friend bool operator==(const unordered_set& left, const unordered_set& right)
{
if (size() != right.size())
if (left.size() != right.size())
return false;
for (const_reference ref : *this)
for (const_reference ref : left)
{
if (right.find(ref) == right.end())
return false;
}
return true;
}

bool operator!=(const unordered_set& right) const
friend bool operator!=(const unordered_set& left, const unordered_set& right)
{
return !(*this == right);
return !(left == right);
}

private:
Expand Down
34 changes: 8 additions & 26 deletions include/momo/stdish/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,47 +453,29 @@ class vector
assign(values.begin(), values.end());
}

bool operator==(const vector& right) const
friend bool operator==(const vector& left, const vector& right)
{
return mArray.IsEqual(right.mArray);
}

bool operator!=(const vector& right) const
{
return !(*this == right);
return left.mArray.IsEqual(right.mArray);
}

#ifdef MOMO_HAS_THREE_WAY_COMPARISON
auto operator<=>(const vector& right) const
friend auto operator<=>(const vector& left, const vector& right)
requires requires (const_reference ref) { std::tie(ref) <=> std::tie(ref); }
{
auto comp = [] (const value_type& value1, const value_type& value2)
{ return std::tie(value1) <=> std::tie(value2); };
return std::lexicographical_compare_three_way(begin(), end(),
return std::lexicographical_compare_three_way(left.begin(), left.end(),
right.begin(), right.end(), comp);
}
#else
bool operator<(const vector& right) const
friend bool operator<(const vector& left, const vector& right)
{
return std::lexicographical_compare(begin(), end(), right.begin(), right.end());
}

bool operator>(const vector& right) const
{
return right < *this;
}

bool operator<=(const vector& right) const
{
return !(right < *this);
}

bool operator>=(const vector& right) const
{
return right <= *this;
return std::lexicographical_compare(left.begin(), left.end(), right.begin(), right.end());
}
#endif

MOMO_MORE_COMPARISON_OPERATORS(const vector&)

private:
static Array pvCreateArray(vector&& right, const allocator_type& alloc)
{
Expand Down

0 comments on commit dd5118d

Please sign in to comment.