-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc][cpp] reverse_iterator support #85702
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
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//===-- Standalone implementation of iterator -------------------*- C++ -*-===// | ||
// | ||
// 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 LLVM_LIBC_SRC___SUPPORT_CPP_ITERATOR_H | ||
#define LLVM_LIBC_SRC___SUPPORT_CPP_ITERATOR_H | ||
|
||
#include "src/__support/CPP/type_traits/enable_if.h" | ||
#include "src/__support/CPP/type_traits/is_convertible.h" | ||
#include "src/__support/CPP/type_traits/is_same.h" | ||
#include "src/__support/macros/attributes.h" | ||
|
||
namespace LIBC_NAMESPACE { | ||
namespace cpp { | ||
|
||
template <typename T> struct iterator_traits; | ||
template <typename T> struct iterator_traits<T *> { | ||
using reference = T &; | ||
}; | ||
|
||
template <typename Iter> class reverse_iterator { | ||
Iter current; | ||
|
||
public: | ||
using reference = typename iterator_traits<Iter>::reference; | ||
|
||
LIBC_INLINE reverse_iterator() : current() {} | ||
LIBC_INLINE constexpr explicit reverse_iterator(Iter it) : current(it) {} | ||
|
||
template <typename Other, | ||
cpp::enable_if_t<!cpp::is_same_v<Iter, Other> && | ||
cpp::is_convertible_v<const Other &, Iter>, | ||
int> = 0> | ||
LIBC_INLINE constexpr explicit reverse_iterator(const Other &it) | ||
: current(it) {} | ||
|
||
LIBC_INLINE constexpr reference operator*() const { | ||
Iter tmp = current; | ||
return *--tmp; | ||
} | ||
LIBC_INLINE constexpr reverse_iterator operator--() { | ||
++current; | ||
return *this; | ||
} | ||
LIBC_INLINE constexpr reverse_iterator &operator++() { | ||
--current; | ||
return *this; | ||
} | ||
LIBC_INLINE constexpr reverse_iterator operator++(int) { | ||
reverse_iterator tmp(*this); | ||
--current; | ||
return tmp; | ||
} | ||
}; | ||
|
||
} // namespace cpp | ||
} // namespace LIBC_NAMESPACE | ||
|
||
#endif // LLVM_LIBC_SRC___SUPPORT_CPP_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//===-- Unittests for Array -----------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/__support/CPP/array.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
using LIBC_NAMESPACE::cpp::array; | ||
|
||
TEST(LlvmLibcArrayTest, Basic) { | ||
array<int, 3> a = {0, 1, 2}; | ||
|
||
ASSERT_EQ(a.data(), &a.front()); | ||
ASSERT_EQ(a.front(), 0); | ||
ASSERT_EQ(a.back(), 2); | ||
ASSERT_EQ(a.size(), size_t{3}); | ||
ASSERT_EQ(a[1], 1); | ||
ASSERT_FALSE(a.empty()); | ||
ASSERT_NE(a.begin(), a.end()); | ||
ASSERT_EQ(*a.begin(), a.front()); | ||
|
||
auto it = a.rbegin(); | ||
ASSERT_EQ(*it, 2); | ||
ASSERT_EQ(*(++it), 1); | ||
ASSERT_EQ(*(++it), 0); | ||
|
||
for (int &x : a) | ||
ASSERT_GE(x, 0); | ||
} | ||
|
||
// Test const_iterator and const variant methods. | ||
TEST(LlvmLibcArrayTest, Const) { | ||
const array<int, 3> z = {3, 4, 5}; | ||
|
||
ASSERT_EQ(3, z.front()); | ||
ASSERT_EQ(4, z[1]); | ||
ASSERT_EQ(5, z.back()); | ||
ASSERT_EQ(3, *z.data()); | ||
|
||
// begin, cbegin, end, cend | ||
array<int, 3>::const_iterator it2 = z.begin(); | ||
ASSERT_EQ(*it2, z.front()); | ||
it2 = z.cbegin(); | ||
ASSERT_EQ(*it2, z.front()); | ||
it2 = z.end(); | ||
ASSERT_NE(it2, z.begin()); | ||
it2 = z.cend(); | ||
ASSERT_NE(it2, z.begin()); | ||
|
||
// rbegin, crbegin, rend, crend | ||
array<int, 3>::const_reverse_iterator it = z.rbegin(); | ||
ASSERT_EQ(*it, z.back()); | ||
it = z.crbegin(); | ||
ASSERT_EQ(*it, z.back()); | ||
it = z.rend(); | ||
ASSERT_EQ(*--it, z.front()); | ||
it = z.crend(); | ||
ASSERT_EQ(*--it, z.front()); | ||
|
||
for (const int &x : z) | ||
ASSERT_GE(x, 0); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most functions here should be
[[nodiscard]]
I believe.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://en.cppreference.com/w/cpp/container/array/rend also doesn't mention nodiscard.
I guess I'm happy to add it, regardless, since nodiscard should have been the language default IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C++17 S23.3.7.1 Class template array overview also doesn't mention [[nodiscard]] on these methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm...now I'm on the fence. I'm going to land these without, but I would not be opposed to adding them in a future PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coincidentally, I think some WG21 are in Tokyo right now, and this came up for discussion. @lntue pointed me to a discussion in #libcxx channel of llvm discord where Mark de Wever said:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hehe! I didn't mean to confuse you with
[[nodiscard]]
but I'm amazed by the outcome!Tangentially I think we're missing the following functions for
reverse_iterator
to be useful.as in
for (auto itr = value.rbegin(); itr **!=** value.rend(); ++itr)
.