-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[libc++] Introduce __product_iterator_traits
and optimise flat_map::insert
#139454
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
757d24e
[libc++] Introduce and optimise
huixie90 e47e222
CI
huixie90 860d9bc
CI
huixie90 921ace8
CI
huixie90 1ec6d72
Apply suggestions from code review
huixie90 78c0fdb
review comments
huixie90 f8e545f
ci
huixie90 f7bf9e8
CI
huixie90 57afa88
review
huixie90 7314fe0
CI
huixie90 038de7d
CI
huixie90 b39029c
release notes and gcc 15 filter out
huixie90 f6e6418
ci
huixie90 01bfe42
rebase
huixie90 6b57b90
constexpr fix
huixie90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,76 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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___ITERATOR_PRODUCT_ITERATOR_H | ||
#define _LIBCPP___ITERATOR_PRODUCT_ITERATOR_H | ||
|
||
// Product iterators are iterators that contain two or more underlying iterators. | ||
// | ||
// For example, std::flat_map stores its data into two separate containers, and its iterator | ||
// is a proxy over two separate underlying iterators. The concept of product iterators | ||
// allows algorithms to operate over these underlying iterators separately, opening the | ||
// door to various optimizations. | ||
// | ||
// If __product_iterator_traits can be instantiated, the following functions and associated types must be provided: | ||
huixie90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// - static constexpr size_t Traits::__size | ||
// The number of underlying iterators inside the product iterator. | ||
// | ||
// - template <size_t _N> | ||
huixie90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// static decltype(auto) Traits::__get_iterator_element(It&& __it) | ||
// Returns the _Nth iterator element of the given product iterator. | ||
// | ||
// - template <class... _Iters> | ||
// static _Iterator __make_product_iterator(_Iters&&...); | ||
// Creates a product iterator from the given underlying iterators. | ||
|
||
#include <__config> | ||
#include <__cstddef/size_t.h> | ||
#include <__type_traits/enable_if.h> | ||
#include <__type_traits/integral_constant.h> | ||
#include <__utility/declval.h> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
#endif | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
template <class _Iterator> | ||
struct __product_iterator_traits; | ||
/* exposition-only: | ||
{ | ||
static constexpr size_t __size = ...; | ||
|
||
template <size_t _N, class _Iter> | ||
static decltype(auto) __get_iterator_element(_Iter&&); | ||
|
||
template <class... _Iters> | ||
static _Iterator __make_product_iterator(_Iters&&...); | ||
}; | ||
*/ | ||
|
||
template <class _Tp, size_t = 0> | ||
struct __is_product_iterator : false_type {}; | ||
|
||
template <class _Tp> | ||
struct __is_product_iterator<_Tp, sizeof(__product_iterator_traits<_Tp>) * 0> : true_type {}; | ||
|
||
template <class _Tp, size_t _Size, class = void> | ||
struct __is_product_iterator_of_size : false_type {}; | ||
|
||
template <class _Tp, size_t _Size> | ||
struct __is_product_iterator_of_size<_Tp, _Size, __enable_if_t<__product_iterator_traits<_Tp>::__size == _Size> > | ||
: true_type {}; | ||
|
||
template <class _Iterator, size_t _Nth> | ||
using __product_iterator_element_t _LIBCPP_NODEBUG = | ||
decltype(__product_iterator_traits<_Iterator>::template __get_iterator_element<_Nth>(std::declval<_Iterator>())); | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
#endif // _LIBCPP___ITERATOR_PRODUCT_ITERATOR_H |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,66 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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, c++20 | ||
// gcc 15 does not seem to recognize the __product_iterator_traits specializations | ||
// UNSUPPORTED: gcc | ||
|
||
#include <flat_map> | ||
#include <ranges> | ||
#include <type_traits> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "test_macros.h" | ||
#include "test_iterators.h" | ||
|
||
constexpr bool test() { | ||
{ | ||
// Test that the __get_iterator_element can handle a non-copyable iterator | ||
int Date[] = {1, 2, 3, 4}; | ||
cpp20_input_iterator<int*> iter(Date); | ||
sentinel_wrapper<cpp20_input_iterator<int*>> sent{cpp20_input_iterator<int*>(Date + 4)}; | ||
std::ranges::subrange r1(std::move(iter), std::move(sent)); | ||
auto v = std::views::zip(std::move(r1), std::views::iota(0, 4)); | ||
auto it = v.begin(); | ||
|
||
using Iter = decltype(it); | ||
|
||
static_assert(!std::is_copy_constructible_v<Iter>); | ||
|
||
static_assert(std::__product_iterator_traits<Iter>::__size == 2); | ||
std::same_as<cpp20_input_iterator<int*>&> decltype(auto) it1 = | ||
huixie90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::__product_iterator_traits<Iter>::__get_iterator_element<0>(it); | ||
|
||
assert(*it1 == 1); | ||
} | ||
if (!std::is_constant_evaluated()) { | ||
// Test __make_product_iterator | ||
using M = std::flat_map<int, int>; | ||
M m{{1, 1}, {2, 2}, {3, 3}}; | ||
using Iter = std::ranges::iterator_t<const M>; | ||
const auto& keys = m.keys(); | ||
const auto& values = m.values(); | ||
|
||
auto it_keys = std::ranges::begin(keys); | ||
auto it_values = std::ranges::begin(values); | ||
|
||
auto it = std::__product_iterator_traits<Iter>::__make_product_iterator(it_keys, it_values); | ||
assert(it->first == 1); | ||
assert(it->second == 1); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
int main(int, char**) { | ||
test(); | ||
static_assert(test()); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.