-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow C++20 forward iterators to use fast paths
Some forward iterators (by C++20's definition) are only input iterators in C++17 or before, because they cannot have a language reference for their `reference` type, e.g. a zip iterator. However, we can still use the fast paths for these iterators. E.g. this makes `absl::InlinedVector::insert(iterator, InputIt, InputIt)` faster for this category of forward iterator. `iterator_concept` was added in C++20, but it's reasonable to support this for pre-C++20 code as well. PiperOrigin-RevId: 725791745 Change-Id: I7dd02eef0b94ea6d4bfbda8f7f72db4abea10440
- Loading branch information
1 parent
9e764b4
commit ae4b0c5
Showing
9 changed files
with
211 additions
and
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2025 The Abseil Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef ABSL_BASE_INTERNAL_ITERATOR_TRAITS_TEST_HELPER_H_ | ||
#define ABSL_BASE_INTERNAL_ITERATOR_TRAITS_TEST_HELPER_H_ | ||
|
||
#include <iterator> | ||
#include <utility> | ||
|
||
#include "absl/base/config.h" | ||
|
||
namespace absl { | ||
ABSL_NAMESPACE_BEGIN | ||
namespace base_internal { | ||
|
||
// This would be a forward_iterator in C++20, but it's only an input iterator | ||
// before that, since it has a non-reference `reference`. | ||
template <typename Iterator> | ||
class Cpp20ForwardZipIterator { | ||
using IteratorReference = typename std::iterator_traits<Iterator>::reference; | ||
|
||
public: | ||
Cpp20ForwardZipIterator() = default; | ||
explicit Cpp20ForwardZipIterator(Iterator left, Iterator right) | ||
: left_(left), right_(right) {} | ||
|
||
Cpp20ForwardZipIterator& operator++() { | ||
++left_; | ||
++right_; | ||
return *this; | ||
} | ||
|
||
Cpp20ForwardZipIterator operator++(int) { | ||
Cpp20ForwardZipIterator tmp(*this); | ||
++*this; | ||
return *this; | ||
} | ||
|
||
std::pair<IteratorReference, IteratorReference> operator*() const { | ||
return {*left_, *right_}; | ||
} | ||
|
||
// C++17 input iterators require `operator->`, but this isn't possible to | ||
// implement. C++20 dropped the requirement. | ||
|
||
friend bool operator==(const Cpp20ForwardZipIterator& lhs, | ||
const Cpp20ForwardZipIterator& rhs) { | ||
return lhs.left_ == rhs.left_ && lhs.right_ == rhs.right_; | ||
} | ||
|
||
friend bool operator!=(const Cpp20ForwardZipIterator& lhs, | ||
const Cpp20ForwardZipIterator& rhs) { | ||
return !(lhs == rhs); | ||
} | ||
|
||
private: | ||
Iterator left_{}; | ||
Iterator right_{}; | ||
}; | ||
|
||
} // namespace base_internal | ||
ABSL_NAMESPACE_END | ||
} // namespace absl | ||
|
||
template <typename Iterator> | ||
struct std::iterator_traits< | ||
absl::base_internal::Cpp20ForwardZipIterator<Iterator>> { | ||
private: | ||
using IteratorReference = typename std::iterator_traits<Iterator>::reference; | ||
|
||
public: | ||
using iterator_category = std::input_iterator_tag; | ||
using iterator_concept = std::forward_iterator_tag; | ||
using value_type = std::pair<IteratorReference, IteratorReference>; | ||
using difference_type = | ||
typename std::iterator_traits<Iterator>::difference_type; | ||
using reference = value_type; | ||
using pointer = void; | ||
}; | ||
|
||
#if defined(__cpp_lib_concepts) | ||
static_assert( | ||
std::forward_iterator<absl::base_internal::Cpp20ForwardZipIterator<int*>>); | ||
#endif // defined(__cpp_lib_concepts) | ||
|
||
#endif // ABSL_BASE_INTERNAL_ITERATOR_TRAITS_TEST_HELPER_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