stlab 2.3.0
Modern, modular C++ algorithms, data structures, and concurrency primitives
Loading...
Searching...
No Matches
reverse.hpp
Go to the documentation of this file.
1/*
2 Copyright 2013 Adobe
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5*/
6/**************************************************************************************************/
7
8#ifndef STLAB_ALGORITHM_REVERSE_HPP
9#define STLAB_ALGORITHM_REVERSE_HPP
10
18
19#include <algorithm>
20#include <utility>
21
22#include <stlab/config.hpp>
25
26/**************************************************************************************************/
27
28namespace stlab {
29STLAB_VERSION_NAMESPACE_BEGIN()
30
31
36
37/**************************************************************************************************/
38
39namespace unsafe {
40
41/**************************************************************************************************/
42
45
46template <STLAB_CONCEPT(FORWARD_NODE_ITERATOR) I>
47auto reverse_append(I first, I last, I end) -> I {
48 while (first != last) {
49 I prior(end);
50 ++first;
51 stlab::unsafe::set_next(prior, end);
52 end = prior;
53 }
54 return end;
55}
56
59
60template <typename R, // R models NodeRange
61 typename I> // I models NodeIterator
62inline auto reverse_append(R& range, I end) -> I {
63 return stlab::unsafe::reverse_append(std::begin(range), std::end(range), end);
64}
65
68
69template <typename I> // I models NodeIterator
70inline auto reverse_nodes(I first, I last) -> I {
71 return stlab::unsafe::reverse_append(first, last, last);
72}
73
76
77template <typename R> // R models NodeRange
78inline auto reverse_nodes(R& range) -> typename R::iterator {
79 return stlab::unsafe::reverse_nodes(std::begin(range), std::end(range));
80}
81
82/**************************************************************************************************/
83
84} // namespace unsafe
85
86/**************************************************************************************************/
87
90
91template <class BidirectionalRange>
92inline void reverse(BidirectionalRange& range) {
93 std::reverse(std::begin(range), std::end(range));
94}
95
98template <class BidirectionalRange, class OutputIterator>
99inline void reverse_copy(BidirectionalRange& range, OutputIterator result) {
100 std::reverse_copy(std::begin(range), std::end(range), result);
101}
102
105template <class BidirectionalRange, class OutputIterator>
106inline void reverse_copy(const BidirectionalRange& range, OutputIterator result) {
107 std::reverse_copy(std::begin(range), std::end(range), result);
108}
109
110/**************************************************************************************************/
111
114
115template <typename I> // I models BidirectionalIterator
116auto reverse_until(I f, I m, I l) -> std::pair<I, I> {
117 while (f != m && m != l) {
118 --l;
119
120 std::iter_swap(f, l);
121
122 ++f;
123 }
124
125 return std::pair<I, I>(f, l);
126}
127
128/**************************************************************************************************/
129
131
132STLAB_VERSION_NAMESPACE_END()
133} // namespace stlab
134
135/**************************************************************************************************/
136
137#endif // STLAB_ALGORITHM_REVERSE_HPP
138
139/**************************************************************************************************/
Iterator concepts when the compiler supports C++20 concepts.
void reverse(BidirectionalRange &range)
Range-based reverse, will be deprecated in C++20 in favor of std::ranges::reverse.
Definition reverse.hpp:92
void reverse_copy(BidirectionalRange &range, OutputIterator result)
Range-based reverse_copy algorithm, will be deprecated in C++20 in favor of std::ranges::reverse_copy...
Definition reverse.hpp:99
auto reverse_until(I f, I m, I l) -> std::pair< I, I >
Reverses the range [f, l) until m is reached. Returns a range [a, b) of the un-reversed subrange such...
Definition reverse.hpp:116
void set_next(const I &x, const I &y)
Sets the successor of the node referenced by x to y (via set_next_fn<I>).
Definition set_next.hpp:41
Definition reverse.hpp:39
auto reverse_append(I first, I last, I end) -> I
Reverses the range [first, last) and appends end. Returns the beginning of the reversed range such th...
Definition reverse.hpp:47
auto reverse_nodes(I first, I last) -> I
Reverses the range [first, last) and returns the beginning of the reversed range such that [result,...
Definition reverse.hpp:70
Definition reverse.hpp:28
Intrusive forward/bidirectional iterator helpers (set_next, splice, skip).