Skip to content

Commit b13df77

Browse files
committed
Algorithms for ranges shift left and shift right
1 parent 5c8fefb commit b13df77

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___ALGORITHM_RANGES_SHIFT_LEFT_H
10+
#define _LIBCPP___ALGORITHM_RANGES_SHIFT_LEFT_H
11+
12+
#include <__config>
13+
#include <__iterator/concepts.h>
14+
#include <__iterator/iterator_traits.h>
15+
#include <__ranges/access.h>
16+
#include <__ranges/concepts.h>
17+
#include <__ranges/subrange.h>
18+
#include <__utility/move.h>
19+
#include <__utility/forward.h>
20+
#include <__iterator/advance.h>
21+
#include <__iterator/distance.h>
22+
23+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24+
# pragma GCC system_header
25+
#endif
26+
27+
_LIBCPP_PUSH_MACROS
28+
#include <__undef_macros>
29+
30+
_LIBCPP_BEGIN_NAMESPACE_STD
31+
32+
namespace ranges {
33+
34+
namespace __shift_left {
35+
36+
struct __fn {
37+
template <std::permutable _Iter, std::sentinel_for<_Iter> _Sent>
38+
_LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
39+
operator()(_Iter __first, _Sent __last, iter_difference_t<_Iter> __n) const {
40+
if (__n <= 0) {
41+
return {__first, __first};
42+
}
43+
44+
auto __dist = std::ranges::distance(__first, __last);
45+
if (__n >= __dist) {
46+
return {__first, __first};
47+
}
48+
49+
auto __mid = std::ranges::next(__first, __n);
50+
auto __new_last = std::move(__mid, __last, __first);
51+
return {__first, __new_last};
52+
}
53+
54+
template <std::ranges::forward_range _Range>
55+
requires std::permutable<iterator_t<_Range>>
56+
_LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range>
57+
operator()(_Range&& __range, range_difference_t<_Range> __n) const {
58+
return (*this)(std::ranges::begin(__range), std::ranges::end(__range), __n);
59+
}
60+
};
61+
62+
} // namespace __shift_left
63+
64+
inline namespace __cpo {
65+
inline constexpr auto shift_left = __shift_left::__fn{};
66+
} // namespace __cpo
67+
68+
} // namespace ranges
69+
70+
_LIBCPP_END_NAMESPACE_STD
71+
72+
_LIBCPP_POP_MACROS
73+
74+
#endif // _LIBCPP___ALGORITHM_RANGES_SHIFT_LEFT_H
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___ALGORITHM_RANGES_SHIFT_RIGHT_H
10+
#define _LIBCPP___ALGORITHM_RANGES_SHIFT_RIGHT_H
11+
12+
#include <__config>
13+
#include <__iterator/concepts.h>
14+
#include <__iterator/iterator_traits.h>
15+
#include <__ranges/access.h>
16+
#include <__ranges/concepts.h>
17+
#include <__ranges/subrange.h>
18+
#include <__utility/move.h>
19+
#include <__utility/forward.h>
20+
#include <__iterator/advance.h>
21+
#include <__iterator/distance.h>
22+
#include <algorithm>
23+
24+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25+
# pragma GCC system_header
26+
#endif
27+
28+
_LIBCPP_PUSH_MACROS
29+
#include <__undef_macros>
30+
31+
_LIBCPP_BEGIN_NAMESPACE_STD
32+
33+
namespace ranges {
34+
35+
namespace __shift_right {
36+
37+
struct __fn {
38+
template <std::permutable _Iter, std::sentinel_for<_Iter> _Sent>
39+
_LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
40+
operator()(_Iter __first, _Sent __last, iter_difference_t<_Iter> __n) const {
41+
if (__n <= 0) {
42+
return {__last, __last};
43+
}
44+
45+
auto __dist = std::ranges::distance(__first, __last);
46+
if (__n >= __dist) {
47+
return {__last, __last};
48+
}
49+
50+
auto __new_first = std::ranges::next(__first, __dist - __n);
51+
std::move_backward(__first, __new_first, __last);
52+
return {__new_first, __last};
53+
}
54+
55+
template <std::ranges::forward_range _Range>
56+
requires std::permutable<iterator_t<_Range>>
57+
_LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range>
58+
operator()(_Range&& __range, range_difference_t<_Range> __n) const {
59+
return (*this)(std::ranges::begin(__range), std::ranges::end(__range), __n);
60+
}
61+
};
62+
63+
} // namespace __shift_right
64+
65+
inline namespace __cpo {
66+
inline constexpr auto shift_right = __shift_right::__fn{};
67+
} // namespace __cpo
68+
69+
} // namespace ranges
70+
71+
_LIBCPP_END_NAMESPACE_STD
72+
73+
_LIBCPP_POP_MACROS
74+
75+
#endif // _LIBCPP___ALGORITHM_RANGES_SHIFT_RIGHT_H

0 commit comments

Comments
 (0)