Skip to content

Commit

Permalink
[libc++][spaceship] Implement operator<=> for forward_list
Browse files Browse the repository at this point in the history
Implemented `operator<=>` for `forward_list`

Reviewed By: #libc, philnik

Spies: philnik, libcxx-commits, yaxunl

Differential Revision: https://reviews.llvm.org/D145172
  • Loading branch information
Zingam authored and philnik777 committed Mar 3, 2023
1 parent 2ff646f commit 3bc7633
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
2 changes: 1 addition & 1 deletion libcxx/docs/Status/SpaceshipProjects.csv
Expand Up @@ -37,7 +37,7 @@ Section,Description,Dependencies,Assignee,Complete
| `[string.view.comparison] <https://wg21.link/string.view.comparison>`_,| `basic_string_view <https://reviews.llvm.org/D130295>`_,None,Mark de Wever,|Complete|
| `[array.syn] <https://wg21.link/array.syn>`_ (`general <https://wg21.link/container.opt.reqmts>`_),| `array <https://reviews.llvm.org/D132265>`_,[expos.only.func],Adrian Vogelsgesang,|In Progress|
| `[deque.syn] <https://wg21.link/deque.syn>`_ (`general <https://wg21.link/container.opt.reqmts>`_),| deque <https://reviews.llvm.org/D144821>,[expos.only.func],Hristo Hristov,|Complete|
| `[forward.list.syn] <https://wg21.link/forward.list.syn>`_ (`general <https://wg21.link/container.opt.reqmts>`_),| forward_list,[expos.only.func],Unassigned,|Not Started|
| `[forward.list.syn] <https://wg21.link/forward.list.syn>`_ (`general <https://wg21.link/container.opt.reqmts>`_),| `forward_list <https://reviews.llvm.org/D145172>`_,[expos.only.func],Hristo Hristov,|Complete|
| `[list.syn] <https://wg21.link/list.syn>`_ (`general <https://wg21.link/container.opt.reqmts>`_),| `list <https://reviews.llvm.org/D132312>`_,[expos.only.func],Adrian Vogelsgesang,|Complete|
| `[vector.syn] <https://wg21.link/vector.syn>`_ (`general <https://wg21.link/container.opt.reqmts>`_),| `vector <https://reviews.llvm.org/D132268>`_,[expos.only.func],Adrian Vogelsgesang,|In Progress|
| `[associative.map.syn] <https://wg21.link/associative.map.syn>`_ (`general <https://wg21.link/container.opt.reqmts>`_),"| map
Expand Down
31 changes: 26 additions & 5 deletions libcxx/include/forward_list
Expand Up @@ -146,23 +146,27 @@ template <class T, class Allocator>
template <class T, class Allocator>
bool operator< (const forward_list<T, Allocator>& x,
const forward_list<T, Allocator>& y);
const forward_list<T, Allocator>& y); // removed in C++20
template <class T, class Allocator>
bool operator!=(const forward_list<T, Allocator>& x,
const forward_list<T, Allocator>& y);
const forward_list<T, Allocator>& y); // removed in C++20
template <class T, class Allocator>
bool operator> (const forward_list<T, Allocator>& x,
const forward_list<T, Allocator>& y);
const forward_list<T, Allocator>& y); // removed in C++20
template <class T, class Allocator>
bool operator>=(const forward_list<T, Allocator>& x,
const forward_list<T, Allocator>& y);
const forward_list<T, Allocator>& y); // removed in C++20
template <class T, class Allocator>
bool operator<=(const forward_list<T, Allocator>& x,
const forward_list<T, Allocator>& y);
const forward_list<T, Allocator>& y); // removed in C++20
template<class T, class Allocator>
synth-three-way-result<T> operator<=>(const forward_list<T, Allocator>& x,
const forward_list<T, Allocator>& y); // since C++20
template <class T, class Allocator>
void swap(forward_list<T, Allocator>& x, forward_list<T, Allocator>& y)
Expand All @@ -181,6 +185,7 @@ template <class T, class Allocator, class Predicate>

#include <__algorithm/comp.h>
#include <__algorithm/lexicographical_compare.h>
#include <__algorithm/lexicographical_compare_three_way.h>
#include <__algorithm/min.h>
#include <__assert> // all public C++ headers provide the assertion handler
#include <__config>
Expand Down Expand Up @@ -1711,6 +1716,8 @@ bool operator==(const forward_list<_Tp, _Alloc>& __x,
return (__ix == __ex) == (__iy == __ey);
}

#if _LIBCPP_STD_VER <= 17

template <class _Tp, class _Alloc>
inline _LIBCPP_INLINE_VISIBILITY
bool operator!=(const forward_list<_Tp, _Alloc>& __x,
Expand Down Expand Up @@ -1752,6 +1759,20 @@ bool operator<=(const forward_list<_Tp, _Alloc>& __x,
return !(__y < __x);
}

#else // #if _LIBCPP_STD_VER <= 17

template<class _Tp, class _Allocator>
inline _LIBCPP_HIDE_FROM_ABI
__synth_three_way_result<_Tp>
operator<=>(const forward_list<_Tp, _Allocator>& __x,
const forward_list<_Tp, _Allocator>& __y)
{
return std::lexicographical_compare_three_way(
__x.begin(), __x.end(), __y.begin(), __y.end(), __synth_three_way);
}

#endif // #if _LIBCPP_STD_VER <= 17

template <class _Tp, class _Alloc>
inline _LIBCPP_INLINE_VISIBILITY
void
Expand Down
@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17

// <forward_list>

// template<class T, class Allocator>
// synth-three-way-result<T> operator<=>(const forward_list<T, Allocator>& x,
// const forward_list<T, Allocator>& y);

#include <cassert>
#include <forward_list>

#include "test_container_comparisons.h"

int main(int, char**) {
assert(test_ordered_container_spaceship<std::forward_list>());
// `std::forward_list` is not constexpr, so no `static_assert` test here.
return 0;
}

0 comments on commit 3bc7633

Please sign in to comment.