Skip to content

feature/spaceship: Clause 22: Container Iterators #1648

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2ffcdb6
Implement operator spaceship for container iterators
miscco Feb 15, 2021
99999c6
Fancy that
miscco Feb 16, 2021
fe09d08
No address of local variables
miscco Feb 16, 2021
700a941
Forgot to check for compatability
miscco Feb 16, 2021
47235f9
Debug performance!
miscco Feb 16, 2021
7e46870
Add spaceship for string iterators
miscco Feb 16, 2021
0c55524
Apply suggestions from code review
CaseyCarter Feb 16, 2021
6165612
Forgot about constexpr
miscco Feb 16, 2021
cf706cb
Apply suggestions from code review
CaseyCarter Feb 16, 2021
c6bdeca
Merge branch 'feature/spaceship' into spaceship_container_iterators
StephanTLavavej Feb 19, 2021
a34a99c
Fix typo.
StephanTLavavej Feb 19, 2021
d0849f9
deque isn't constexpr.
StephanTLavavej Feb 19, 2021
d25040b
Mark vector/vector<bool> iterator spaceships as _CONSTEXPR20_CONTAINER.
StephanTLavavej Feb 19, 2021
1a51bd7
Fix _STL_VERIFY message: spaceship isn't equality.
StephanTLavavej Feb 19, 2021
0496062
Skip _Tree_const_iterator operator!= for C++20.
StephanTLavavej Feb 19, 2021
16c7ce5
Spaceships for checked_array_iterator and unchecked_array_iterator.
StephanTLavavej Feb 19, 2021
4382c52
Skip _Reinterpret_move_iter operator!= for C++20.
StephanTLavavej Feb 19, 2021
c57087e
Activate string tests.
StephanTLavavej Feb 19, 2021
98f6f00
deque iterators were tested above.
StephanTLavavej Feb 19, 2021
a1721c0
Reorder and comment "string iterators", "string_view iterators" tests.
StephanTLavavej Feb 19, 2021
f65f20f
Test checked_array_iterator, unchecked_array_iterator.
StephanTLavavej Feb 19, 2021
6b92a98
In !_HAS_CXX20 mode, drop _CONSTEXPR20 and _CONSTEXPR20_CONTAINER.
StephanTLavavej Feb 19, 2021
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
15 changes: 14 additions & 1 deletion stl/inc/array
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public:
return _Ptr == _Right._Ptr;
}

#if _HAS_CXX20
_NODISCARD constexpr strong_ordering operator<=>(const _Array_const_iterator& _Right) const noexcept {
return _Ptr <=> _Right._Ptr;
}
#else // ^^^ _HAS_CXX20 ^^^ / vvv !_HAS_CXX20 vvv
_NODISCARD _CONSTEXPR17 bool operator!=(const _Array_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}
Expand All @@ -130,6 +135,7 @@ public:
_NODISCARD _CONSTEXPR17 bool operator>=(const _Array_const_iterator& _Right) const noexcept {
return !(*this < _Right);
}
#endif // !_HAS_CXX20

using _Prevent_inheriting_unwrap = _Array_const_iterator;

Expand Down Expand Up @@ -235,6 +241,12 @@ private:
return _Idx == _Right._Idx;
}

#if _HAS_CXX20
_NODISCARD constexpr strong_ordering operator<=>(const _Array_const_iterator& _Right) const noexcept {
_Compat(_Right);
return _Idx <=> _Right._Idx;
}
#else // ^^^ _HAS_CXX20 ^^^ / vvv !_HAS_CXX20 vvv
_NODISCARD _CONSTEXPR17 bool operator!=(const _Array_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}
Expand All @@ -255,6 +267,7 @@ private:
_NODISCARD _CONSTEXPR17 bool operator>=(const _Array_const_iterator& _Right) const noexcept {
return !(*this < _Right);
}
#endif // !_HAS_CXX20

_CONSTEXPR17 void _Compat(const _Array_const_iterator& _Right) const noexcept { // test for compatible iterator pair
_STL_VERIFY(_Ptr == _Right._Ptr, "array iterators incompatible");
Expand Down Expand Up @@ -784,7 +797,7 @@ _NODISCARD _CONSTEXPR20 bool operator==(const array<_Ty, _Size>& _Left, const ar

#if !_HAS_CXX20
template <class _Ty, size_t _Size>
_NODISCARD _CONSTEXPR20 bool operator!=(const array<_Ty, _Size>& _Left, const array<_Ty, _Size>& _Right) {
_NODISCARD bool operator!=(const array<_Ty, _Size>& _Left, const array<_Ty, _Size>& _Right) {
return !(_Left == _Right);
}
#endif // !_HAS_CXX20
Expand Down
13 changes: 13 additions & 0 deletions stl/inc/deque
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public:
return _Myoff == _Right._Myoff;
}

#if _HAS_CXX20
_NODISCARD strong_ordering operator<=>(const _Deque_unchecked_const_iterator& _Right) const noexcept {
return _Myoff <=> _Right._Myoff;
}
#else // ^^^ _HAS_CXX20 ^^^ / vvv !_HAS_CXX20 vvv
_NODISCARD bool operator!=(const _Deque_unchecked_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}
Expand All @@ -126,6 +131,7 @@ public:
_NODISCARD bool operator>=(const _Deque_unchecked_const_iterator& _Right) const noexcept {
return !(*this < _Right);
}
#endif // !_HAS_CXX20

const _Container_base12* _Getcont() const noexcept { // get container pointer
return _Mycont;
Expand Down Expand Up @@ -345,6 +351,12 @@ public:
return this->_Myoff == _Right._Myoff;
}

#if _HAS_CXX20
_NODISCARD strong_ordering operator<=>(const _Deque_const_iterator& _Right) const noexcept {
_Compat(_Right);
return this->_Myoff <=> _Right._Myoff;
}
#else // ^^^ _HAS_CXX20 ^^^ / vvv !_HAS_CXX20 vvv
_NODISCARD bool operator!=(const _Deque_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}
Expand All @@ -365,6 +377,7 @@ public:
_NODISCARD bool operator>=(const _Deque_const_iterator& _Right) const noexcept {
return !(*this < _Right);
}
#endif // !_HAS_CXX20

void _Compat(const _Deque_const_iterator& _Right) const noexcept { // test for compatible iterator pair
#if _ITERATOR_DEBUG_LEVEL == 0
Expand Down
6 changes: 6 additions & 0 deletions stl/inc/filesystem
Original file line number Diff line number Diff line change
Expand Up @@ -1603,9 +1603,11 @@ namespace filesystem {
return _Lhs._Position == _Rhs._Position;
}

#if !_HAS_CXX20
_NODISCARD friend bool operator!=(const _Path_iterator& _Lhs, const _Path_iterator& _Rhs) {
return _Lhs._Position != _Rhs._Position;
}
#endif // !_HAS_CXX20

#if _ITERATOR_DEBUG_LEVEL != 0
friend void _Verify_range(const _Path_iterator& _Lhs, const _Path_iterator& _Rhs) {
Expand Down Expand Up @@ -2728,9 +2730,11 @@ namespace filesystem {
return _Impl == _Rhs._Impl;
}

#if !_HAS_CXX20
_NODISCARD bool operator!=(const directory_iterator& _Rhs) const noexcept /* strengthened */ {
return _Impl != _Rhs._Impl;
}
#endif // !_HAS_CXX20

_Directory_entry_proxy operator++(int) {
_Directory_entry_proxy _Proxy(**this);
Expand Down Expand Up @@ -2977,9 +2981,11 @@ namespace filesystem {
return _Impl == _Rhs._Impl;
}

#if !_HAS_CXX20
_NODISCARD bool operator!=(const recursive_directory_iterator& _Rhs) const noexcept {
return _Impl != _Rhs._Impl;
}
#endif // !_HAS_CXX20

_Directory_entry_proxy operator++(int) {
_Directory_entry_proxy _Proxy(**this);
Expand Down
6 changes: 6 additions & 0 deletions stl/inc/forward_list
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,21 @@ public:
return _Ptr == _Right._Ptr;
}

#if !_HAS_CXX20
_NODISCARD bool operator!=(const _Flist_unchecked_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}
#endif // !_HAS_CXX20

_NODISCARD bool operator==(_Default_sentinel) const noexcept {
return _Ptr == nullptr;
}

#if !_HAS_CXX20
_NODISCARD bool operator!=(_Default_sentinel) const noexcept {
return _Ptr != nullptr;
}
#endif // !_HAS_CXX20

_Nodeptr _Ptr; // pointer to node
};
Expand Down Expand Up @@ -161,9 +165,11 @@ public:
return this->_Ptr == _Right._Ptr;
}

#if !_HAS_CXX20
_NODISCARD bool operator!=(const _Flist_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}
#endif // !_HAS_CXX20

#if _ITERATOR_DEBUG_LEVEL == 2
friend void _Verify_range(const _Flist_const_iterator& _First, const _Flist_const_iterator& _Last) noexcept {
Expand Down
14 changes: 14 additions & 0 deletions stl/inc/iterator
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,13 @@ public:
return _Myindex == _Right._Myindex;
}

#if _HAS_CXX20
_NODISCARD constexpr _STD strong_ordering operator<=>(const checked_array_iterator& _Right) const noexcept {
_STL_VERIFY(_Myarray == _Right._Myarray && _Mysize == _Right._Mysize,
"cannot compare incompatible checked_array_iterators");
return _Myindex <=> _Right._Myindex;
}
#else // ^^^ _HAS_CXX20 ^^^ / vvv !_HAS_CXX20 vvv
_NODISCARD constexpr bool operator!=(const checked_array_iterator& _Right) const noexcept {
return !(*this == _Right);
}
Expand All @@ -1517,6 +1524,7 @@ public:
_NODISCARD constexpr bool operator>=(const checked_array_iterator& _Right) const noexcept {
return !(*this < _Right);
}
#endif // !_HAS_CXX20

friend constexpr void _Verify_range(
const checked_array_iterator& _First, const checked_array_iterator& _Last) noexcept {
Expand Down Expand Up @@ -1650,6 +1658,11 @@ public:
return _Myptr == _Right._Myptr;
}

#if _HAS_CXX20
_NODISCARD constexpr _STD strong_ordering operator<=>(const unchecked_array_iterator& _Right) const noexcept {
return _Myptr <=> _Right._Myptr;
}
#else // ^^^ _HAS_CXX20 ^^^ / vvv !_HAS_CXX20 vvv
_NODISCARD constexpr bool operator!=(const unchecked_array_iterator& _Right) const noexcept {
return !(*this == _Right);
}
Expand All @@ -1669,6 +1682,7 @@ public:
_NODISCARD constexpr bool operator>=(const unchecked_array_iterator& _Right) const noexcept {
return !(*this < _Right);
}
#endif // !_HAS_CXX20

#if _ITERATOR_DEBUG_LEVEL != 0
friend constexpr void _Verify_range(
Expand Down
4 changes: 4 additions & 0 deletions stl/inc/list
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ public:
return _Ptr == _Right._Ptr;
}

#if !_HAS_CXX20
_NODISCARD bool operator!=(const _List_unchecked_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}
#endif // !_HAS_CXX20

_Nodeptr _Ptr; // pointer to node
};
Expand Down Expand Up @@ -199,9 +201,11 @@ public:
return this->_Ptr == _Right._Ptr;
}

#if !_HAS_CXX20
_NODISCARD bool operator!=(const _List_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}
#endif // !_HAS_CXX20

#if _ITERATOR_DEBUG_LEVEL == 2
friend void _Verify_range(const _List_const_iterator& _First, const _List_const_iterator& _Last) noexcept {
Expand Down
39 changes: 28 additions & 11 deletions stl/inc/vector
Original file line number Diff line number Diff line change
Expand Up @@ -152,26 +152,33 @@ public:
return _Ptr == _Right._Ptr;
}

_NODISCARD _CONSTEXPR20_CONTAINER bool operator!=(const _Vector_const_iterator& _Right) const noexcept {
#if _HAS_CXX20
_NODISCARD _CONSTEXPR20_CONTAINER strong_ordering operator<=>(const _Vector_const_iterator& _Right) const noexcept {
_Compat(_Right);
return _Unfancy(_Ptr) <=> _Unfancy(_Right._Ptr);
}
#else // ^^^ _HAS_CXX20 ^^^ / vvv !_HAS_CXX20 vvv
_NODISCARD bool operator!=(const _Vector_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}

_NODISCARD _CONSTEXPR20_CONTAINER bool operator<(const _Vector_const_iterator& _Right) const noexcept {
_NODISCARD bool operator<(const _Vector_const_iterator& _Right) const noexcept {
_Compat(_Right);
return _Ptr < _Right._Ptr;
}

_NODISCARD _CONSTEXPR20_CONTAINER bool operator>(const _Vector_const_iterator& _Right) const noexcept {
_NODISCARD bool operator>(const _Vector_const_iterator& _Right) const noexcept {
return _Right < *this;
}

_NODISCARD _CONSTEXPR20_CONTAINER bool operator<=(const _Vector_const_iterator& _Right) const noexcept {
_NODISCARD bool operator<=(const _Vector_const_iterator& _Right) const noexcept {
return !(_Right < *this);
}

_NODISCARD _CONSTEXPR20_CONTAINER bool operator>=(const _Vector_const_iterator& _Right) const noexcept {
_NODISCARD bool operator>=(const _Vector_const_iterator& _Right) const noexcept {
return !(*this < _Right);
}
#endif // !_HAS_CXX20

_CONSTEXPR20_CONTAINER void _Compat(const _Vector_const_iterator& _Right) const noexcept {
// test for compatible iterator pair
Expand Down Expand Up @@ -1851,7 +1858,7 @@ _NODISCARD _CONSTEXPR20_CONTAINER bool operator==(const vector<_Ty, _Alloc>& _Le

#if !_HAS_CXX20
template <class _Ty, class _Alloc>
_NODISCARD _CONSTEXPR20_CONTAINER bool operator!=(const vector<_Ty, _Alloc>& _Left, const vector<_Ty, _Alloc>& _Right) {
_NODISCARD bool operator!=(const vector<_Ty, _Alloc>& _Left, const vector<_Ty, _Alloc>& _Right) {
return !(_Left == _Right);
}
#endif // !_HAS_CXX20
Expand Down Expand Up @@ -2204,26 +2211,36 @@ public:
return this->_Myptr == _Right._Myptr && this->_Myoff == _Right._Myoff;
}

_NODISCARD _CONSTEXPR20_CONTAINER bool operator!=(const _Vb_const_iterator& _Right) const noexcept {
#if _HAS_CXX20
_NODISCARD _CONSTEXPR20_CONTAINER strong_ordering operator<=>(const _Vb_const_iterator& _Right) const noexcept {
_Compat(_Right);
if (const auto _CmpResult = this->_Myptr <=> _Right._Myptr; _CmpResult != 0) {
return _CmpResult;
}
return this->_Myoff <=> _Right._Myoff;
}
#else // ^^^ _HAS_CXX20 ^^^ / vvv !_HAS_CXX20 vvv
_NODISCARD bool operator!=(const _Vb_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}

_NODISCARD _CONSTEXPR20_CONTAINER bool operator<(const _Vb_const_iterator& _Right) const noexcept {
_NODISCARD bool operator<(const _Vb_const_iterator& _Right) const noexcept {
_Compat(_Right);
return this->_Myptr < _Right._Myptr || (this->_Myptr == _Right._Myptr && this->_Myoff < _Right._Myoff);
}

_NODISCARD _CONSTEXPR20_CONTAINER bool operator>(const _Vb_const_iterator& _Right) const noexcept {
_NODISCARD bool operator>(const _Vb_const_iterator& _Right) const noexcept {
return _Right < *this;
}

_NODISCARD _CONSTEXPR20_CONTAINER bool operator<=(const _Vb_const_iterator& _Right) const noexcept {
_NODISCARD bool operator<=(const _Vb_const_iterator& _Right) const noexcept {
return !(_Right < *this);
}

_NODISCARD _CONSTEXPR20_CONTAINER bool operator>=(const _Vb_const_iterator& _Right) const noexcept {
_NODISCARD bool operator>=(const _Vb_const_iterator& _Right) const noexcept {
return !(*this < _Right);
}
#endif // !_HAS_CXX20

_CONSTEXPR20_CONTAINER void _Compat(const _Vb_const_iterator& _Right) const noexcept {
// test for compatible iterator pair
Expand Down
2 changes: 2 additions & 0 deletions stl/inc/xhash
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,14 @@ struct _Reinterpret_move_iter {
return _Lhs._Base == _Rhs._Base;
}

#if !_HAS_CXX20
#ifndef __CUDACC__ // TRANSITION, VSO-568006
_NODISCARD
#endif // TRANSITION, VSO-568006
friend bool operator!=(const _Reinterpret_move_iter& _Lhs, const _Reinterpret_move_iter& _Rhs) {
return _Lhs._Base != _Rhs._Base;
}
#endif // !_HAS_CXX20
};

// STRUCT TEMPLATE _List_head_construct_ptr
Expand Down
19 changes: 19 additions & 0 deletions stl/inc/xstring
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,17 @@ public:
#endif // _ITERATOR_DEBUG_LEVEL
}

#if _HAS_CXX20
_NODISCARD constexpr strong_ordering operator<=>(const _String_view_iterator& _Right) const noexcept {
#if _ITERATOR_DEBUG_LEVEL >= 1
_STL_VERIFY(_Mydata == _Right._Mydata && _Mysize == _Right._Mysize,
"cannot compare incompatible string_view iterators");
return _Myoff <=> _Right._Myoff;
#else // ^^^ _ITERATOR_DEBUG_LEVEL >= 1 ^^^ // vvv _ITERATOR_DEBUG_LEVEL == 0 vvv
return _Myptr <=> _Right._Myptr;
#endif // _ITERATOR_DEBUG_LEVEL
}
#else // ^^^ _HAS_CXX20 ^^^ / vvv !_HAS_CXX20 vvv
_NODISCARD constexpr bool operator!=(const _String_view_iterator& _Right) const noexcept {
return !(*this == _Right);
}
Expand All @@ -1149,6 +1160,7 @@ public:
_NODISCARD constexpr bool operator>=(const _String_view_iterator& _Right) const noexcept {
return !(*this < _Right);
}
#endif // !_HAS_CXX20

#if _ITERATOR_DEBUG_LEVEL >= 1
friend constexpr void _Verify_range(const _String_view_iterator& _First, const _String_view_iterator& _Last) {
Expand Down Expand Up @@ -2009,6 +2021,12 @@ public:
return _Ptr == _Right._Ptr;
}

#if _HAS_CXX20
_NODISCARD strong_ordering operator<=>(const _String_const_iterator& _Right) const noexcept {
_Compat(_Right);
return _Unfancy(_Ptr) <=> _Unfancy(_Right._Ptr);
}
#else // ^^^ _HAS_CXX20 ^^^ / vvv !_HAS_CXX20 vvv
_NODISCARD bool operator!=(const _String_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}
Expand All @@ -2029,6 +2047,7 @@ public:
_NODISCARD bool operator>=(const _String_const_iterator& _Right) const noexcept {
return !(*this < _Right);
}
#endif // !_HAS_CXX20

void _Compat(const _String_const_iterator& _Right) const noexcept { // test for compatible iterator pair
#if _ITERATOR_DEBUG_LEVEL >= 1
Expand Down
Loading