forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc++] [P2848R0] Implement
is_uniqued
- Loading branch information
1 parent
cbe3761
commit 490536e
Showing
25 changed files
with
477 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef _LIBCPP___ALGORITHM_IS_UNIQUED_H | ||
#define _LIBCPP___ALGORITHM_IS_UNIQUED_H | ||
|
||
#include <__algorithm/comp.h> | ||
#include <__algorithm/adjacent_find.h> | ||
#include <__config> | ||
#include <__iterator/iterator_traits.h> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
#endif | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
template <class _ForwardIterator, class _BinaryPredicate> | ||
_LIBCPP_NODISCARD_EXT inline | ||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 | ||
bool | ||
is_uniqued(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred) | ||
{ | ||
return std::__adjacent_find(__first, __last, __pred) == __last; | ||
} | ||
|
||
template<class _ForwardIterator> | ||
_LIBCPP_NODISCARD_EXT inline | ||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 | ||
bool | ||
is_uniqued(_ForwardIterator __first, _ForwardIterator __last) | ||
{ | ||
return std::__adjacent_find(__first, __last, __equal_to()) == __last; | ||
} | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
#endif // _LIBCPP___ALGORITHM_IS_UNIQUED_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef _LIBCPP__ALGORITHM_RANGES_IS_UNIQUED_H | ||
#define _LIBCPP__ALGORITHM_RANGES_IS_UNIQUED_H | ||
|
||
#include <__algorithm/ranges_adjacent_find.h> | ||
#include <__config> | ||
#include <__functional/identity.h> | ||
#include <__functional/ranges_operations.h> | ||
#include <__iterator/concepts.h> | ||
#include <__iterator/projected.h> | ||
#include <__ranges/access.h> | ||
#include <__ranges/concepts.h> | ||
#include <__utility/move.h> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
#endif | ||
|
||
#if _LIBCPP_STD_VER >= 20 | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
namespace ranges { | ||
namespace __is_uniqued { | ||
struct __fn { | ||
template <forward_iterator _Iter, sentinel_for<_Iter> _Sent, | ||
class _Proj = identity, | ||
indirect_binary_predicate<projected<_Iter, _Proj>, | ||
projected<_Iter, _Proj>> _Pred = ranges::equal_to> | ||
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr | ||
bool operator()(_Iter __first, _Sent __last, _Pred __pred = {}, _Proj __proj = {}) const { | ||
return ranges::__adjacent_find_impl(std::move(__first), __last, __pred, __proj) == __last; | ||
} | ||
|
||
template <forward_range _Range, | ||
class _Proj = identity, | ||
indirect_binary_predicate<projected<iterator_t<_Range>, _Proj>, | ||
projected<iterator_t<_Range>, _Proj>> _Pred = ranges::equal_to> | ||
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr | ||
bool operator()(_Range&& __range, _Pred __pred = {}, _Proj __proj = {}) const { | ||
auto __last = ranges::end(__range); | ||
return ranges::__adjacent_find_impl(ranges::begin(__range), __last, __pred, __proj) == __last; | ||
} | ||
}; | ||
} // namespace __is_uniqued | ||
|
||
inline namespace __cpo { | ||
inline constexpr auto is_uniqued = __is_uniqued::__fn{}; | ||
} // namespace __cpo | ||
} // namespace ranges | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
#endif // _LIBCPP_STD_VER >= 20 | ||
|
||
#endif // _LIBCPP__ALGORITHM_RANGES_IS_UNIQUED_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
libcxx/test/std/algorithms/alg.nonmodifying/alg.is.uniqued/is_uniqued.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// <algorithm> | ||
|
||
// template<ForwardIterator Iter> | ||
// constexpr bool is_uniqued(Iter first, Iter last); | ||
|
||
#include <algorithm> | ||
#include <cassert> | ||
|
||
#include "test_macros.h" | ||
#include "test_iterators.h" | ||
|
||
TEST_CONSTEXPR_CXX20 bool test_constexpr() { | ||
int ia[] = {0, 1, 2, 2, 0, 1, 2, 3}; | ||
int ib[] = {0, 1, 2, 7, 0, 1, 2, 3}; | ||
assert(std::is_uniqued(ia, ia + 8) == false); | ||
assert(std::is_uniqued(ib, ib + 8) == true); | ||
return true; | ||
} | ||
|
||
int main(int, char**) | ||
{ | ||
{ | ||
int ia[] = {0, 1, 2, 2, 0, 1, 2, 3}; | ||
assert(!std::is_uniqued(ia, ia + 8)); | ||
assert(std::is_uniqued(ia, ia + 3)); | ||
assert(!std::is_uniqued(ia, ia + 4)); | ||
assert(std::is_uniqued(ia, ia)); | ||
assert(!std::is_uniqued(ia + 2, ia + 8)); | ||
assert(std::is_uniqued(ia + 3, ia + 8)); | ||
} | ||
{ | ||
using It = forward_iterator<const int*>; | ||
int ia[] = {0, 1, 2, 2, 0, 1, 2, 3}; | ||
assert(!std::is_uniqued(It(ia), It(ia + 8))); | ||
assert(std::is_uniqued(It(ia), It(ia + 3))); | ||
assert(!std::is_uniqued(It(ia), It(ia + 4))); | ||
assert(std::is_uniqued(It(ia), It(ia))); | ||
assert(!std::is_uniqued(It(ia + 2), It(ia + 8))); | ||
assert(std::is_uniqued(It(ia + 3), It(ia + 8))); | ||
} | ||
|
||
test_constexpr(); | ||
#if TEST_STD_VER >= 20 | ||
static_assert(test_constexpr()); | ||
#endif | ||
|
||
return 0; | ||
} |
Oops, something went wrong.