Commit e2ac3e14 authored by Samuel GAIST's avatar Samuel GAIST

[algorithms] Cleanup and modernized cxx examples

parent 56a0bad2
Pipeline #25078 passed with stage
in 11 minutes and 45 seconds
......@@ -497,7 +497,7 @@ An example code showing how to implement an algorithm in this configuration is s
class Algorithm: public IAlgorithmSequential
{
public:
virtual bool process(const InputList& inputs, const DataloaderList& data_load_list, const OutputList& outputs)
bool process(const InputList& inputs, const DataloaderList& data_load_list, const OutputList& outputs) override
{
// to read the field "value" on the "in" input, use "data"
// a possible declaration of "user/format/1" would be:
......@@ -514,10 +514,10 @@ An example code showing how to implement an algorithm in this configuration is s
// {
// "value": ...
// }
auto result = new user::other_1();
result->value = output;
user::other_1 result;
result.value = output;
outputs["out"]->write(result);
outputs["out"]->write(&result);
# No error occurred, so return true
return true;
......@@ -567,20 +567,20 @@ shown below:
.. code-block:: c++
:linenos:
class Algorithm: public IAlgorithm
class Algorithm: public IAlgorithmSequential
{
public:
virtual bool process(const InputList& inputs, const DataloaderList& data_load_list, const OutputList& outputs)
bool process(const InputList& inputs, const DataloaderList& data_load_list, const OutputList& outputs) override
{
auto i1 = inputs["in"]->data<user::format_1>()->value;
auto i2 = inputs["in2"]->data<user::format_1>()->value;
auto out = magical_processing(i1, i2);
auto result = new user::other_1();
result->value = out;
user::other_1 result;
result.value = out;
outputs["out"]->write(result);
outputs["out"]->write(&result);
return true;
}
......@@ -636,7 +636,7 @@ The example below illustrates how such an algorithm could be implemented:
class Algorithm: public IAlgorithmSequential
{
public:
virtual bool process(const InputList& inputs, const DataloaderList& data_load_list, const OutputList& outputs)
bool process(const InputList& inputs, const DataloaderList& data_load_list, const OutputList& outputs) override
{
objs.push_back(inputs["in"]->data<user::format_1>()->value); // accumulates
......@@ -644,10 +644,10 @@ The example below illustrates how such an algorithm could be implemented:
{
auto out = magical_processing(objs);
auto result = new user::other_1();
result->value = out;
user::other_1 result;
result.value = out;
outputs["out"]->write(result);
outputs["out"]->write(&result);
objs.clear(); // reset accumulator for next label
}
......@@ -727,31 +727,34 @@ unsynchronized input (``in3``).
class Algorithm: public IAlgorithmSequential
{
public:
virtual bool process(const InputList& inputs, const DataloaderList& data_load_list, const OutputList& outputs)
bool prepare(const beat::backend::cxx::DataLoaderList& data_load_list) override
{
// N.B.: this will be called for every unit in `in'
// Please add the code using dataloader with c++
// Loads the "model" data at the beginning, once
if (models.empty())
{
while (inputs["in3"]->hasMoreData())
{
inputs["in3"]->next();
auto model = inputs["in3"]->data<user::model_1>();
models.push_back(*model);
}
auto loader = data_load_list["in3"];
for (int i = 0 ; i < loader->count() ; ++i) {
auto view = loader->view("in3", i);
std::map<std::string, beat::backend::cxx::Data *> data;
std::tie(data, std::ignore, std::ignore) = (*view)[0];
auto model = static_cast<user::model*>(data["in3"]);
models.append(*model);
}
return true;
}
bool process(const InputList& inputs, const DataloaderList& data_load_list, const OutputList& outputs) override
{
# N.B.: this will be called for every unit in `in'
// Processes the current input in `in' and `in2', apply the model/models
auto out = magical_processing(inputs["in"]->data<user::format_1>()->value,
inputs["in2"]->data<user::format_1>()->value,
models);
// Writes the output
auto result = new user::other_1();
result->value = out;
user::other_1 result;
result.value = out;
outputs["out"]->write(result);
outputs["out"]->write(&result);
return true;
}
......@@ -809,35 +812,37 @@ it is safer to treat inputs using their *group*. For example:
.. code-block:: c++
:linenos:
class Algorithm: public IAlgorithm
class Algorithm: public IAlgorithmSequential
{
public:
virtual bool process(const InputList& inputs, const DataloaderList& data_load_list, const OutputList& outputs)
bool prepare(const beat::backend::cxx::DataLoaderList& data_load_list) override
{
// N.B.: this will be called for every unit in `in'
// Loads the "model" data at the beginning, once
if (models.empty())
{
auto group = inputs->groupOf("in3");
while (group->hasMoreData())
{
group->next(); // synchronously advances the data
auto model = group["in3"]->data<user::model_1>();
models.push_back(*model);
}
auto loader = data_load_list["in3"];
for (int i = 0 ; i < loader->count() ; ++i) {
auto view = loader->view("in3", i);
std::map<std::string, beat::backend::cxx::Data *> data;
std::tie(data, std::ignore, std::ignore) = (*view)[0];
auto model = static_cast<user::model*>(data["in3"]);
models.append(*model);
}
return true;
}
bool process(const InputList& inputs, const DataloaderList& data_load_list, const OutputList& outputs) override
{
// N.B.: this will be called for every unit in `in'
// Processes the current input in `in' and `in2', apply the model/models
auto out = magical_processing(inputs["in"]->data<user::format_1>()->value,
inputs["in2"]->data<user::format_1>()->value,
models);
// Writes the output
auto result = new user::other_1();
result->value = out;
user::other_1 result;
result.value = out;
outputs["out"]->write(result);
outputs["out"]->write(&result);
return true;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment