update array

This commit is contained in:
张壹 2025-01-10 22:31:43 +08:00
parent 3bc94c1db2
commit 363a4499ca
3 changed files with 34 additions and 8 deletions

View File

@ -29,4 +29,4 @@ add_example(multinary_ex OFF)
add_example(text_io_ex OFF)
add_example(getoption_ex OFF)
add_example(process_ex OFF)
add_example(array_ex OFF)
add_example(array_ex ON)

View File

@ -42,7 +42,7 @@ int main(int argc, char const *argv[]) try
A.linear2log(2);
A.show();
A.parallel_for_each([](double &a, size_t i){a += 1;});
A.for_each([](double &a){a += 1;});
A.show();
A.sequence(1.0, 0.5, 3, 4, 1);
@ -64,6 +64,9 @@ int main(int argc, char const *argv[]) try
P.sequence(point3dc(0, 0, 0), point3dc(2, 1, 0.5));
P.show(std::cout, '\n');
array<double> Px = P.extract<double>([](const point3dc &p)->double{return p.x;});
Px.show();
// create a new 2D array
matrix<int> C(5, 5, 1);
C.sequence(0, 1, 10);

View File

@ -593,22 +593,45 @@ namespace gctl
/**
* @brief
*
*
* [](ArrValType &a, size_t i){do something here...}
*
* [](ArrValType &a){do something here...}
*
* @tparam BiaryOp
* @tparam UnaryOp
* @param op
*/
template<typename BiaryOp>
void parallel_for_each(BiaryOp op)
template <typename UnaryOp>
void for_each(UnaryOp op)
{
#pragma omp parallel for
for (size_t i = 0; i < length_; i++)
{
op(val_[i], i);
op(val_[i]);
}
return;
}
/**
* @brief
*
*
* [](const ArrValType &a)->OutType{do something here...}
*
* @tparam UnaryOp
* @param op
*/
template <typename OutType, typename UnaryOp>
array<OutType> extract(UnaryOp op)
{
array<OutType> arr(length_);
#pragma omp parallel for
for (size_t i = 0; i < length_; i++)
{
arr[i] = op(val_[i]);
}
return arr;
}
/**
* @brief Display the elements.
*