Skip to content

Commit

Permalink
Comparison operators
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed Nov 1, 2024
1 parent 841018f commit d56de47
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 16 deletions.
8 changes: 8 additions & 0 deletions include/momo/ArrayUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,19 @@ namespace internal
return iter1.mArray == iter2.mArray && iter1.mIndex == iter2.mIndex;
}

#ifdef MOMO_HAS_THREE_WAY_COMPARISON
friend auto operator<=>(ArrayIndexIterator iter1, ArrayIndexIterator iter2)
{
MOMO_CHECK(iter1.mArray == iter2.mArray);
return iter1.mIndex <=> iter2.mIndex;
}
#else
friend bool operator<(ArrayIndexIterator iter1, ArrayIndexIterator iter2)
{
MOMO_CHECK(iter1.mArray == iter2.mArray);
return iter1.mIndex < iter2.mIndex;
}
#endif

MOMO_MORE_ARRAY_ITERATOR_OPERATORS(ArrayIndexIterator)

Expand Down
16 changes: 16 additions & 0 deletions include/momo/DataIndexes.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,19 @@ namespace internal
return iter1.mRaw == iter2.mRaw && iter1.mRawIndex == iter2.mRawIndex;
}

#ifdef MOMO_HAS_THREE_WAY_COMPARISON
friend auto operator<=>(DataRawUniqueHashIterator iter1, DataRawUniqueHashIterator iter2)
{
MOMO_CHECK(iter1.mRaw == iter2.mRaw);
return iter1.mRawIndex <=> iter2.mRawIndex;
}
#else
friend bool operator<(DataRawUniqueHashIterator iter1, DataRawUniqueHashIterator iter2)
{
MOMO_CHECK(iter1.mRaw == iter2.mRaw);
return iter1.mRawIndex < iter2.mRawIndex;
}
#endif

MOMO_MORE_ARRAY_ITERATOR_OPERATORS(DataRawUniqueHashIterator)

Expand Down Expand Up @@ -218,11 +226,19 @@ namespace internal
return iter1.mRaw0 == iter2.mRaw0 && iter1.mRawIndex == iter2.mRawIndex;
}

#ifdef MOMO_HAS_THREE_WAY_COMPARISON
friend auto operator<=>(DataRawMultiHashIterator iter1, DataRawMultiHashIterator iter2)
{
MOMO_CHECK(iter1.mRaw0 == iter2.mRaw0);
return iter1.mRawIndex <=> iter2.mRawIndex;
}
#else
friend bool operator<(DataRawMultiHashIterator iter1, DataRawMultiHashIterator iter2)
{
MOMO_CHECK(iter1.mRaw0 == iter2.mRaw0);
return iter1.mRawIndex < iter2.mRawIndex;
}
#endif

MOMO_MORE_ARRAY_ITERATOR_OPERATORS(DataRawMultiHashIterator)

Expand Down
24 changes: 24 additions & 0 deletions include/momo/DataSelection.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,19 @@ namespace internal
&& iter1.pvGetIndex() == iter2.pvGetIndex();
}

#ifdef MOMO_HAS_THREE_WAY_COMPARISON
friend auto operator<=>(DataRawIterator iter1, DataRawIterator iter2)
{
MOMO_CHECK(iter1.pvGetRaws() == iter2.pvGetRaws());
return iter1.pvGetIndex() <=> iter2.pvGetIndex();
}
#else
friend bool operator<(DataRawIterator iter1, DataRawIterator iter2)
{
MOMO_CHECK(iter1.pvGetRaws() == iter2.pvGetRaws());
return iter1.pvGetIndex() < iter2.pvGetIndex();
}
#endif

MOMO_MORE_ARRAY_ITERATOR_OPERATORS(DataRawIterator)

Expand Down Expand Up @@ -168,11 +176,19 @@ namespace internal
&& iter1.mRawIterator == iter2.mRawIterator;
}

#ifdef MOMO_HAS_THREE_WAY_COMPARISON
friend auto operator<=>(DataRowIterator iter1, DataRowIterator iter2)
{
MOMO_CHECK(iter1.mColumnList == iter2.mColumnList);
return iter1.mRawIterator <=> iter2.mRawIterator;
}
#else
friend bool operator<(DataRowIterator iter1, DataRowIterator iter2)
{
MOMO_CHECK(iter1.mColumnList == iter2.mColumnList);
return iter1.mRawIterator < iter2.mRawIterator;
}
#endif

MOMO_MORE_ARRAY_ITERATOR_OPERATORS(DataRowIterator)

Expand Down Expand Up @@ -327,11 +343,19 @@ namespace internal
return iter1.mOffset == iter2.mOffset && iter1.mRowIterator == iter2.mRowIterator;
}

#ifdef MOMO_HAS_THREE_WAY_COMPARISON
friend auto operator<=>(DataConstItemIterator iter1, DataConstItemIterator iter2)
{
MOMO_CHECK(iter1.mOffset == iter2.mOffset);
return iter1.mRowIterator <=> iter2.mRowIterator;
}
#else
friend bool operator<(DataConstItemIterator iter1, DataConstItemIterator iter2)
{
MOMO_CHECK(iter1.mOffset == iter2.mOffset);
return iter1.mRowIterator < iter2.mRowIterator;
}
#endif

MOMO_MORE_ARRAY_ITERATOR_OPERATORS(DataConstItemIterator)

Expand Down
17 changes: 1 addition & 16 deletions include/momo/IteratorUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "Utility.h"

#define MOMO_MORE_ARRAY_ITERATOR_OPERATORS(Iterator) \
MOMO_MORE_COMPARISON_OPERATORS(Iterator) \
Iterator& operator++() \
{ \
return *this += 1; \
Expand Down Expand Up @@ -59,22 +60,6 @@
Reference operator[](ptrdiff_t diff) const \
{ \
return *(*this + diff); \
} \
friend bool operator!=(Iterator iter1, Iterator iter2) noexcept \
{ \
return !(iter1 == iter2); \
} \
friend bool operator>(Iterator iter1, Iterator iter2) \
{ \
return iter2 < iter1; \
} \
friend bool operator<=(Iterator iter1, Iterator iter2) \
{ \
return !(iter2 < iter1); \
} \
friend bool operator>=(Iterator iter1, Iterator iter2) \
{ \
return iter2 <= iter1; \
}

#define MOMO_MORE_HASH_ITERATOR_OPERATORS(Iterator) \
Expand Down
22 changes: 22 additions & 0 deletions include/momo/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@
return object.GetEnd(); \
}

#ifdef MOMO_HAS_THREE_WAY_COMPARISON
# define MOMO_MORE_COMPARISON_OPERATORS(ObjectArg)
#else
# define MOMO_MORE_COMPARISON_OPERATORS(ObjectArg) \
friend bool operator!=(ObjectArg obj1, ObjectArg obj2) noexcept(noexcept(obj1 == obj2)) \
{ \
return !(obj1 == obj2); \
} \
friend bool operator>(ObjectArg obj1, ObjectArg obj2) \
{ \
return obj2 < obj1; \
} \
friend bool operator<=(ObjectArg obj1, ObjectArg obj2) \
{ \
return !(obj2 < obj1); \
} \
friend bool operator>=(ObjectArg obj1, ObjectArg obj2) \
{ \
return obj2 <= obj1; \
}
#endif

#define MOMO_STATIC_ASSERT(expr) static_assert((expr), #expr)

#define MOMO_CHECK(expr) \
Expand Down

0 comments on commit d56de47

Please sign in to comment.