Back to Documentations

Signature Description Parameters
template<comparable T>
std::vector<size_type>
permutation_vec(const char *name, sort_spec dir) const;
This doesn’t sort the DataFrame. It leaves self unchanged. It returns the permutation vector for sorting the given column. A permutation vector is a vector of indices that, when applied to the original column, reorders its elements into a sorted sequence.
T: Type of the named column. You always must specify this type, even if it is being sorted by the index.
name: The name of a data column or DF_INDEX_COL_NAME
dir: Direction of sorting, ascending or descending
template<comparable T1, comparable T2>
std::vector<size_type>
permutation_vec(const char *name1, sort_spec dir1,
                const char *name2, sort_spec dir2) const;
This function returns the same permutation vector as above, but based on the ordering of two columns. The length of the returned vector will be the min of the lengths of the columns.
T1: Type of the first named column. You always must specify this type, even if it is being sorted by the index.
T2: Type of the second named column. You always must specify this type, even if it is being sorted by the index.
name1: The name of the first data column or DF_INDEX_COL_NAME
name2: The name of the second data column or DF_INDEX_COL_NAME
dir1: Direction of sorting, ascending or descending, of the first column
dir2: Direction of sorting, ascending or descending, of the second column
template<comparable T1, comparable T2, comparable T3>
std::vector<size_type>
permutation_vec(const char *name1, sort_spec dir1,
                const char *name2, sort_spec dir2,
                const char *name3, sort_spec dir3) const;
This function returns the same permutation vector as above, but based on the ordering of three columns. The length of the returned vector will be the min of the lengths of the columns.
T1: Type of the first named column. You always must specify this type, even if it is being sorted by the index.
T2: Type of the second named column. You always must specify this type, even if it is being sorted by the index.
T3: Type of the third named column. You always must specify this type, even if it is being sorted by the index.
name1: The name of the first data column or DF_INDEX_COL_NAME
name2: The name of the second data column or DF_INDEX_COL_NAME
name3: The name of the third data column or DF_INDEX_COL_NAME
dir1: Direction of sorting, ascending or descending, of the first column
dir2: Direction of sorting, ascending or descending, of the second column
dir3: Direction of sorting, ascending or descending, of the third column
static void test_permutation_vec()  {

    std::cout << "\nTesting permutation_vec( ) ..." << std::endl;

    StrDataFrame    df;

    try  {
        df.read("IBM.csv", io_format::csv2);
    }
    catch (const DataFrameError &ex)  {
        std::cout << ex.what() << std::endl;
        ::exit(-1);
    }

    const auto  res1 = df.permutation_vec<double>("IBM_Close", sort_spec::ascen);
    const auto  &close = df.get_column<double>("IBM_Close");

    assert(close.size() == res1.size());
    for (std::size_t i { 1 }; i < close.size(); ++i)
        assert(close[res1[i - 1]] <= close[res1[i]]);

    const auto  res2 = df.permutation_vec<double>("IBM_Close", sort_spec::desce);

    assert(close.size() == res2.size());
    for (std::size_t i { 1 }; i < close.size(); ++i)
        assert(close[res2[i - 1]] >= close[res2[i]]);

    const auto  res3 = df.permutation_vec<double, long>("IBM_Close", sort_spec::ascen, "IBM_Volume", sort_spec::desce);
    const auto  &volume = df.get_column<long>("IBM_Volume");

    assert(res3.size() == std::min(close.size(), volume.size()));
    for (std::size_t i { 1 }; i < std::min(close.size(), volume.size()); ++i) {
        if (close[res3[i - 1]] == close[res3[i]])
            assert(volume[res3[i - 1]] >= volume[res3[i]]);
        else
            assert(close[res3[i - 1]] <= close[res3[i]]);
    }

    const auto  res4 = df.permutation_vec<double, long, double>("IBM_Close", sort_spec::ascen,
                                                                "IBM_Volume", sort_spec::desce,
                                                                "IBM_Open", sort_spec::desce);
    const auto  &open = df.get_column<double>("IBM_Open");
    const auto  min_size = std::min({ close.size(), volume.size(), open.size() });

    assert(res4.size() == min_size);
    for (std::size_t i { 1 }; i < min_size; ++i) {
        if (close[res4[i - 1]] == close[res4[i]])  {
            if (volume[res4[i - 1]] == volume[res4[i]])
                assert(open[res4[i - 1]] >= open[res4[i]]);
            else
                assert(volume[res4[i - 1]] >= volume[res4[i]]);
        }
        else
            assert(close[res4[i - 1]] <= close[res4[i]]);
    }
}

C++ DataFrame