如何在C ++ 20中实现shift_right()?


9

在C ++ 20中,<algorithm>标头获得了两个新算法:shift_left()shift_right()。他们两个都接受任何LegacyForwardIterator。对于shift_left(),指定了``移动i​0'' 开始以递增顺序执行; 对于shift_right(),指定为“如果ForwardIt满足LegacyBidirectionalIterator要求,则以i从开始的降序执行移动last - first - n - 1。”

我可以想到一种相当容易实现的方法shift_left()

template <typename ForwardIt>
constexpr inline ForwardIt shift_left(ForwardIt first, ForwardIt last, typename std::iterator_traits<ForwardIt>::difference_type n) {
    if (n <= 0) return last;
    ForwardIt it = first;
    for (; n > 0; --n, ++it) {
        if (it == last) return first;
    }
    return std::move(it, last, first);
}

如果ForwardIt满足LegacyBidirectionalIterator的要求,我可以看到它的shift_right()实现方式与极为相似shift_left()。但是,目前还不清楚如何实现shift_right()非双向正向迭代器。

我已经找到了一种算法,该算法使用atat处的空间[first, first+n)作为元素交换的暂存空间,但似乎比shift_left()上面的算法浪费很多:

template <typename ForwardIt>
constexpr inline ForwardIt shift_right(ForwardIt first, ForwardIt last, typename std::iterator_traits<ForwardIt>::difference_type n) {
    if (n <= 0) return first;
    ForwardIt it = first;
    for (; n > 0; --n, ++it) {
        if (it == last) return last;
    }
    ForwardIt ret = it;
    ForwardIt ret_it = first;
    for (; it != last; ++it) {
        std::iter_swap(ret_it, it);
        ret_it++;
        if (ret_it == ret) ret_it = first;
    }
    return ret;
}

是否会有更好的或“预期的”实施方式shift_right()


宁愿用实现std::move代替而不是std::copy...
Aconcagua

@Aconcagua糟糕,是的,我将编辑问题。
伯纳德

Answers:


6

这是班次的示例实现:https : //github.com/danra/shift_proposal/blob/master/shift_proposal.h

从提案文档中:http : //www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0769r0.pdf

#include <algorithm>
#include <iterator>
#include <type_traits>
#include <utility>

template<class I>
using difference_type_t = typename std::iterator_traits<I>::difference_type;

template<class I>
using iterator_category_t = typename std::iterator_traits<I>::iterator_category;

template<class I, class Tag, class = void>
constexpr bool is_category = false;
template<class I, class Tag>
constexpr bool is_category<I, Tag, std::enable_if_t<
    std::is_convertible_v<iterator_category_t<I>, Tag>>> = true;

/// Increment (decrement for negative n) i |n| times or until i == bound,
/// whichever comes first. Returns n - the difference between i's final position
/// and its initial position. (Note: "advance" has overloads with this behavior
/// in the Ranges TS.)
template<class I>
constexpr difference_type_t<I> bounded_advance(
    I& i, difference_type_t<I> n, I const bound)
{
    if constexpr (is_category<I, std::bidirectional_iterator_tag>) {
        for (; n < 0 && i != bound; ++n, void(--i)) {
            ;
        }
    }

    for(; n > 0 && i != bound; --n, void(++i)) {
        ;
    }

    return n;
}

template<class ForwardIt>
ForwardIt shift_left(ForwardIt first, ForwardIt last, difference_type_t<ForwardIt> n)
{
    if (n <= 0) {
        return last;
    }

    auto mid = first;
    if (::bounded_advance(mid, n, last)) {
        return first;
    }

    return std::move(std::move(mid), std::move(last), std::move(first));
}

template<class ForwardIt>
ForwardIt shift_right(ForwardIt first, ForwardIt last, difference_type_t<ForwardIt> n)
{
    if (n <= 0) {
        return first;
    }

    if constexpr (is_category<ForwardIt, std::bidirectional_iterator_tag>) {
        auto mid = last;
        if (::bounded_advance(mid, -n, first)) {
            return last;
        }
        return std::move_backward(std::move(first), std::move(mid), std::move(last));
    } else {
        auto result = first;
        if (::bounded_advance(result, n, last)) {
            return last;
        }

        // Invariant: next(first, n) == result
        // Invariant: next(trail, n) == lead

        auto lead = result;
        auto trail = first;

        for (; trail != result; ++lead, void(++trail)) {
            if (lead == last) {
                // The range looks like:
                //
                //   |-- (n - k) elements --|-- k elements --|-- (n - k) elements --|
                //   ^-first          trail-^                ^-result          last-^
                //
                // Note that distance(first, trail) == distance(result, last)
                std::move(std::move(first), std::move(trail), std::move(result));
                return result;
            }
        }

        for (;;) {
            for (auto mid = first; mid != result; ++lead, void(++trail), ++mid) {
                if (lead == last) {
                    // The range looks like:
                    //
                    //   |-- (n - k) elements --|-- k elements --|-- ... --|-- n elements --|
                    //   ^-first            mid-^         result-^         ^-trail     last-^
                    //
                    trail = std::move(mid, result, std::move(trail));
                    std::move(std::move(first), std::move(mid), std::move(trail));
                    return result;
                }
                std::iter_swap(mid, trail);
            }
        }
    }
}

3
我不知道为什么void(++trail)...
YSC

@YSC防范过度的“丢弃结果”警告
Caleth

@vll这就是我的想法。
YSC

5
@YSC可能要防止不应调用的逗号运算符过载。
核桃

@walnut ho您可能是对的!
YSC
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.