Back to Documentations

Signature Description Parameters
template<typename T, typename MT = char>
[[nodiscard]] StlVecType<MT>
mask(const char *col_name,
     std::function<MT(const T &val)> &&mfunc) const;
This is the generalization of a masking function. Every element of the given column will be passed to the mfunc and the output of mfunc will be stored in the result vector. A typical example would be a Boolean result vector, although the default result is a vector<char>. That’s because std::vector<bool> has an odd implementation that I don’t like.

NOTE: Type MT must have default constructor
NOTE: mfunc must be stateless, because it is used in multithreading
NOTE: StlVecType is just a std::vector with the applicable allocator for the DataFrame. For example, if you have declared the DataFrame to allocate memory on a particular boundary, then StlVecType is std::vector with an allocator that does so. Otherwise it is just a default std::vector.
T: Type of the "new index" column
MT: Masking function return type
col_name: Name of the given column
mfunc: The masking function
static void test_mask()  {

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

    StrDataFrame    ibm;

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

    ibm.load_column("close_mask",
                    ibm.mask<double, char>("IBM_Close",
                                           [](const double &close) -> char {
                                               return (close > 180.0);
                                           })
                    );
    ibm.load_column("close_diff",
                    ibm.mask<double, double>("IBM_Close",
                                             [](const double &close) -> double {
                                                 return (180.0 - close);
                                             })
                    );

    const auto  &close_mask = ibm.get_column<char>("close_mask");
    const auto  &close_diff = ibm.get_column<double>("close_diff");

    assert(close_mask[0] == 0);
    assert(close_mask[10] == 0);
    assert(close_mask[247] == 0);
    assert(close_mask[1180] == 0);
    assert(close_mask[2761] == 1);
    assert(close_mask[2806] == 1);
    assert(close_mask[3372] == 1);
    assert(close_mask[4106] == 1);
    assert(close_mask[4966] == 0);
    assert(close_mask[5030] == 0);

    assert((std::fabs(close_diff[0] - 81.4375) < 0.0001));
    assert((std::fabs(close_diff[10] - 80.625) < 0.0001));
    assert((std::fabs(close_diff[247] - 71.93) < 0.0001));
    assert((std::fabs(close_diff[1180] - 98.19) < 0.0001));
    assert((std::fabs(close_diff[2761] - -0.360001) < 0.000001));
    assert((std::fabs(close_diff[2806] - -6.17999) < 0.0001));
    assert((std::fabs(close_diff[3372] - -13.55) < 0.0001));
    assert((std::fabs(close_diff[4106] - -0.529999) < 0.000001));
    assert((std::fabs(close_diff[4966] - 57.06) < 0.0001));
    assert((std::fabs(close_diff[5030] - 68.34) < 0.0001));
}

C++ DataFrame