oven::sorted

sortedは範囲を並べ替えるためのRangeアダプタです。


sortedの特徴としては、


・範囲自体を破壊的に並べ替えない。
・並べ替え済みの範囲に対して破壊的な操作ができる。


といったところでしょうか。
以下はその検証コード

#include <iostream>
#include <vector>
#include <pstade/oven/initial_values.hpp>
#include <pstade/oven/identities.hpp>
#include <pstade/oven/sorted.hpp>
#include <pstade/oven/io.hpp>

using namespace pstade::oven;

int main()
{
    std::vector<int> v = initial_values(3, 1, 4);

    std::cout << (v|identities) << std::endl; // {3,1,4} : 並び替え前
    std::cout << (v|sorted)     << std::endl; // {1,3,4} : 並び替えた範囲
    std::cout << (v|identities) << std::endl; // {3,1,4} : 並び替え後
}

v|sortedではvを書き替えてないことがわかります。
v自体をconstで定義するともっとわかりやすいかもしれません。


それに加えて、sortedされた範囲に対して破壊的な操作を行うと、
並べ替えの対象となる範囲自体を書き替えることができます。

#include <iostream>
#include <vector>
#include <pstade/oven/initial_values.hpp>
#include <pstade/oven/identities.hpp>
#include <pstade/oven/sorted.hpp>
#include <pstade/oven/front.hpp>
#include <pstade/oven/io.hpp>

using namespace pstade::oven;

int main()
{
    std::vector<int> v = initial_values(3, 1, 4);

    v|sorted|front = 0; // vの一番小さい要素を0に書き換える
    std::cout << (v|identities) << std::endl; // {3,0,4}
}

sortedのなかでは、oven::outplacedで並び替え前の範囲へのポインタを持つ範囲を生成して
そっちをstd::sortで並べ替える実装になっています。



【参照】
pstade::oven::sorted

pstade::oven::outplaced