Skip to content

Commit

Permalink
std::copy -> std::ranges::copy (#3299)
Browse files Browse the repository at this point in the history
  • Loading branch information
schnellerhase committed Jul 12, 2024
1 parent c4acb41 commit aab5c42
Show file tree
Hide file tree
Showing 20 changed files with 46 additions and 55 deletions.
3 changes: 1 addition & 2 deletions cpp/demo/poisson_matrix_free/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ void solver(MPI_Comm comm)
y.set(0.0);

// Update coefficient ui (just copy data from x to ui)
std::copy(x.array().begin(), x.array().end(),
ui->x()->mutable_array().begin());
std::ranges::copy(x.array(), ui->x()->mutable_array().begin());

// Compute action of A on x
fem::pack_coefficients(*M, coeff);
Expand Down
3 changes: 1 addition & 2 deletions cpp/dolfinx/common/IndexMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,8 +931,7 @@ std::vector<std::int64_t> IndexMap::global_indices() const
std::vector<std::int64_t> global(local_size + num_ghosts);
std::iota(global.begin(), std::next(global.begin(), local_size),
global_offset);
std::copy(_ghosts.cbegin(), _ghosts.cend(),
std::next(global.begin(), local_size));
std::ranges::copy(_ghosts, std::next(global.begin(), local_size));
return global;
}
//-----------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions cpp/dolfinx/common/sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void radix_sort(std::span<T> array)

// Copy data back to array
if (its % 2 != 0)
std::copy(buffer.begin(), buffer.end(), array.begin());
std::ranges::copy(buffer, array.begin());
}

/// Returns the indices that would sort (lexicographic) a vector of
Expand Down Expand Up @@ -159,7 +159,7 @@ void argsort_radix(std::span<const T> array, std::span<std::int32_t> perm)
}

if (its % 2 == 1)
std::copy(perm2.begin(), perm2.end(), perm.begin());
std::ranges::copy(perm2, perm.begin());
}

/// @brief Compute the permutation array that sorts a 2D array by row.
Expand Down
12 changes: 6 additions & 6 deletions cpp/dolfinx/fem/assemble_matrix_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,16 +397,16 @@ void assemble_interior_facets(
std::span<const std::int32_t> dmap0_cell0 = dmap0.cell_dofs(cells0[0]);
std::span<const std::int32_t> dmap0_cell1 = dmap0.cell_dofs(cells0[1]);
dmapjoint0.resize(dmap0_cell0.size() + dmap0_cell1.size());
std::copy(dmap0_cell0.begin(), dmap0_cell0.end(), dmapjoint0.begin());
std::copy(dmap0_cell1.begin(), dmap0_cell1.end(),
std::next(dmapjoint0.begin(), dmap0_cell0.size()));
std::ranges::copy(dmap0_cell0, dmapjoint0.begin());
std::ranges::copy(dmap0_cell1,
std::next(dmapjoint0.begin(), dmap0_cell0.size()));

std::span<const std::int32_t> dmap1_cell0 = dmap1.cell_dofs(cells1[0]);
std::span<const std::int32_t> dmap1_cell1 = dmap1.cell_dofs(cells1[1]);
dmapjoint1.resize(dmap1_cell0.size() + dmap1_cell1.size());
std::copy(dmap1_cell0.begin(), dmap1_cell0.end(), dmapjoint1.begin());
std::copy(dmap1_cell1.begin(), dmap1_cell1.end(),
std::next(dmapjoint1.begin(), dmap1_cell0.size()));
std::ranges::copy(dmap1_cell0, dmapjoint1.begin());
std::ranges::copy(dmap1_cell1,
std::next(dmapjoint1.begin(), dmap1_cell0.size()));

const int num_rows = bs0 * dmapjoint0.size();
const int num_cols = bs1 * dmapjoint1.size();
Expand Down
12 changes: 6 additions & 6 deletions cpp/dolfinx/fem/assemble_vector_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,19 +479,19 @@ void _lift_bc_interior_facets(
= std::span(dmap0.data_handle() + cells0[1] * num_dofs0, num_dofs0);

dmapjoint0.resize(dmap0_cell0.size() + dmap0_cell1.size());
std::copy(dmap0_cell0.begin(), dmap0_cell0.end(), dmapjoint0.begin());
std::copy(dmap0_cell1.begin(), dmap0_cell1.end(),
std::next(dmapjoint0.begin(), dmap0_cell0.size()));
std::ranges::copy(dmap0_cell0, dmapjoint0.begin());
std::ranges::copy(dmap0_cell1,
std::next(dmapjoint0.begin(), dmap0_cell0.size()));

auto dmap1_cell0
= std::span(dmap1.data_handle() + cells1[0] * num_dofs1, num_dofs1);
auto dmap1_cell1
= std::span(dmap1.data_handle() + cells1[1] * num_dofs1, num_dofs1);

dmapjoint1.resize(dmap1_cell0.size() + dmap1_cell1.size());
std::copy(dmap1_cell0.begin(), dmap1_cell0.end(), dmapjoint1.begin());
std::copy(dmap1_cell1.begin(), dmap1_cell1.end(),
std::next(dmapjoint1.begin(), dmap1_cell0.size()));
std::ranges::copy(dmap1_cell0, dmapjoint1.begin());
std::ranges::copy(dmap1_cell1,
std::next(dmapjoint1.begin(), dmap1_cell0.size()));

// Check if bc is applied to cell0
bool has_bc = false;
Expand Down
2 changes: 1 addition & 1 deletion cpp/dolfinx/fem/discreteoperators.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void discrete_gradient(mesh::Topology& topology,
std::vector<T> Ae(Ab.size());
for (std::int32_t c = 0; c < num_cells; ++c)
{
std::copy(Ab.cbegin(), Ab.cend(), Ae.begin());
std::ranges::copy(Ab, Ae.begin());
apply_inverse_dof_transform(Ae, cell_info, c, ndofs0);
mat_set(dofmap1.cell_dofs(c), dofmap0.cell_dofs(c), Ae);
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/dolfinx/fem/interpolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ void interpolate(Function<T, U>& u1, std::span<const std::int32_t> cells1,
// Same function spaces and on whole mesh
std::span<T> u1_array = u1.x()->mutable_array();
std::span<const T> u0_array = u0.x()->array();
std::copy(u0_array.begin(), u0_array.end(), u1_array.begin());
std::ranges::copy(u0_array, u1_array.begin());
}
else
{
Expand Down
10 changes: 4 additions & 6 deletions cpp/dolfinx/fem/sparsitybuild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,15 @@ void sparsitybuild::interior_facets(
auto dofs00 = dofmap0.cell_dofs(cells0[f]);
auto dofs01 = dofmap0.cell_dofs(cells0[f + 1]);
macro_dofs0.resize(dofs00.size() + dofs01.size());
std::copy(dofs00.begin(), dofs00.end(), macro_dofs0.begin());
std::copy(dofs01.begin(), dofs01.end(),
std::next(macro_dofs0.begin(), dofs00.size()));
std::ranges::copy(dofs00, macro_dofs0.begin());
std::ranges::copy(dofs01, std::next(macro_dofs0.begin(), dofs00.size()));

// Trial function dofs (sparsity pattern columns)
auto dofs10 = dofmap1.cell_dofs(cells1[f]);
auto dofs11 = dofmap1.cell_dofs(cells1[f + 1]);
macro_dofs1.resize(dofs10.size() + dofs11.size());
std::copy(dofs10.begin(), dofs10.end(), macro_dofs1.begin());
std::copy(dofs11.begin(), dofs11.end(),
std::next(macro_dofs1.begin(), dofs10.size()));
std::ranges::copy(dofs10, macro_dofs1.begin());
std::ranges::copy(dofs11, std::next(macro_dofs1.begin(), dofs10.size()));

pattern.insert(macro_dofs0, macro_dofs1);
}
Expand Down
3 changes: 1 addition & 2 deletions cpp/dolfinx/fem/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1313,8 +1313,7 @@ std::vector<typename U::scalar_type> pack_constants(const U& u)
for (auto& constant : constants)
{
const std::vector<T>& value = constant->value;
std::copy(value.begin(), value.end(),
std::next(constant_values.begin(), offset));
std::ranges::copy(value, std::next(constant_values.begin(), offset));
offset += value.size();
}

Expand Down
6 changes: 3 additions & 3 deletions cpp/dolfinx/geometry/gjk.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ nearest_simplex(std::span<const T> s)
qnorm += v[k] * v[k];
if (qnorm < qmin)
{
std::copy(v.begin(), v.end(), vmin.begin());
std::ranges::copy(v, vmin.begin());
qmin = qnorm;
smin.resize(2 * 3);
std::span<T, 3> smin0(smin.data(), 3);
std::copy(s0.begin(), s0.end(), smin0.begin());
std::ranges::copy(s0, smin0.begin());
std::span<T, 3> smin1(smin.data() + 3, 3);
std::copy(s1.begin(), s1.end(), smin1.begin());
std::ranges::copy(s1, smin1.begin());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions cpp/dolfinx/graph/ordering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ gps_reorder_unlabelled(const graph::AdjacencyList<std::int32_t>& graph,
{
std::transform(ls.begin(), ls.end(), wn.begin(),
[](const std::vector<int>& vec) { return vec.size(); });
std::copy(wn.begin(), wn.end(), wh.begin());
std::copy(wn.begin(), wn.end(), wl.begin());
std::ranges::copy(wn, wh.begin());
std::ranges::copy(wn, wl.begin());
for (int w : r)
{
++wh[lvp[w][0]];
Expand Down
11 changes: 5 additions & 6 deletions cpp/dolfinx/graph/partition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ graph::build::distribute(MPI_Comm comm,

std::span b(send_buffer.data() + i * buffer_shape1, buffer_shape1);
auto row = list.links(pos);
std::copy(row.begin(), row.end(), b.begin());
std::ranges::copy(row, b.begin());

auto info = b.last(3);
info[0] = row.size(); // Number of edges for node
Expand Down Expand Up @@ -316,7 +316,7 @@ graph::build::distribute(MPI_Comm comm, std::span<const std::int64_t> list,

std::span b(send_buffer.data() + i * buffer_shape1, buffer_shape1);
std::span row(list.data() + pos * shape[1], shape[1]);
std::copy(row.begin(), row.end(), b.begin());
std::ranges::copy(row, b.begin());

auto info = b.last(2);
info[0] = dest_data[2]; // Owning rank
Expand Down Expand Up @@ -372,15 +372,14 @@ graph::build::distribute(MPI_Comm comm, std::span<const std::int64_t> list,
auto edges = row.first(shape[1]);
if (owner == rank)
{
std::copy(edges.begin(), edges.end(),
std::next(data.begin(), i_owned * shape[1]));
std::ranges::copy(edges, std::next(data.begin(), i_owned * shape[1]));
global_indices[i_owned] = orig_global_index;
++i_owned;
}
else
{
std::copy(edges.begin(), edges.end(),
std::next(data.begin(), (i_ghost + num_owned_r) * shape[1]));
std::ranges::copy(
edges, std::next(data.begin(), (i_ghost + num_owned_r) * shape[1]));
global_indices[i_ghost + num_owned_r] = orig_global_index;
ghost_index_owner[i_ghost] = owner;
++i_ghost;
Expand Down
4 changes: 2 additions & 2 deletions cpp/dolfinx/io/ADIOS2Writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ void vtx_write_mesh(adios2::IO& io, adios2::Engine& engine,
{
std::span vtkcell(vtkcells.data() + c * shape[1], shape[1]);
std::span cell(cells.data() + c * (shape[1] + 1), shape[1] + 1);
std::copy(vtkcell.begin(), vtkcell.end(), std::next(cell.begin()));
std::ranges::copy(vtkcell, std::next(cell.begin()));
}

// Put topology (nodes)
Expand Down Expand Up @@ -835,7 +835,7 @@ vtx_write_mesh_from_space(adios2::IO& io, adios2::Engine& engine,
{
std::span vtkcell(vtk.data() + c * vtkshape[1], vtkshape[1]);
std::span cell(cells.data() + c * (vtkshape[1] + 1), vtkshape[1] + 1);
std::copy(vtkcell.begin(), vtkcell.end(), std::next(cell.begin()));
std::ranges::copy(vtkcell, std::next(cell.begin()));
}

// Define ADIOS2 variables for geometry, topology, celltypes and
Expand Down
2 changes: 1 addition & 1 deletion cpp/dolfinx/io/vtk_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ tabulate_lagrange_dof_coordinates(const fem::FunctionSpace<T>& V)
std::int32_t size_local = range[1] - range[0];
std::iota(x_id.begin(), std::next(x_id.begin(), size_local), range[0]);
std::span ghosts = map_dofs->ghosts();
std::copy(ghosts.begin(), ghosts.end(), std::next(x_id.begin(), size_local));
std::ranges::copy(ghosts, std::next(x_id.begin(), size_local));

// Ghosts
std::vector<std::uint8_t> id_ghost(num_nodes, 0);
Expand Down
3 changes: 1 addition & 2 deletions cpp/dolfinx/la/SparsityPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ std::vector<std::int64_t> SparsityPattern::column_indices() const
const std::int32_t num_ghosts = _col_ghosts.size();
std::vector<std::int64_t> global(local_size + num_ghosts);
std::iota(global.begin(), std::next(global.begin(), local_size), range[0]);
std::copy(_col_ghosts.begin(), _col_ghosts.end(),
global.begin() + local_size);
std::ranges::copy(_col_ghosts, global.begin() + local_size);
return global;
}
//-----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion cpp/dolfinx/la/petsc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ la::petsc::create_vectors(MPI_Comm comm,
VecCreateMPI(comm, x[i].size(), PETSC_DETERMINE, &v[i]);
PetscScalar* data;
VecGetArray(v[i], &data);
std::copy(x[i].begin(), x[i].end(), data);
std::ranges::copy(x[i], data);
VecRestoreArray(v[i], &data);
}

Expand Down
10 changes: 4 additions & 6 deletions cpp/dolfinx/la/petsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,8 @@ class Matrix : public Operator
PetscErrorCode ierr;
#ifdef PETSC_USE_64BIT_INDICES
cache.resize(rows.size() + cols.size());
std::copy(rows.begin(), rows.end(), cache.begin());
std::copy(cols.begin(), cols.end(),
std::next(cache.begin(), rows.size()));
std::ranges::copy(rows, cache.begin());
std::ranges::copy(cols, std::next(cache.begin(), rows.size()));
const PetscInt* _rows = cache.data();
const PetscInt* _cols = cache.data() + rows.size();
ierr = MatSetValuesLocal(A, rows.size(), _rows, cols.size(), _cols,
Expand Down Expand Up @@ -333,9 +332,8 @@ class Matrix : public Operator
PetscErrorCode ierr;
#ifdef PETSC_USE_64BIT_INDICES
cache.resize(rows.size() + cols.size());
std::copy(rows.begin(), rows.end(), cache.begin());
std::copy(cols.begin(), cols.end(),
std::next(cache.begin(), rows.size()));
std::ranges::copy(rows, cache.begin());
std::ranges::copy(cols, std::next(cache.begin(), rows.size()));
const PetscInt* _rows = cache.data();
const PetscInt* _cols = cache.data() + rows.size();
ierr = MatSetValuesBlockedLocal(A, rows.size(), _rows, cols.size(), _cols,
Expand Down
4 changes: 2 additions & 2 deletions cpp/dolfinx/mesh/Topology.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,7 @@ mesh::entities_to_index(const Topology& topology, int dim,
for (int e = 0; e < num_entities_mesh; ++e)
{
auto vertices = e_to_v->links(e);
std::copy(vertices.begin(), vertices.end(), key.begin());
std::ranges::copy(vertices, key.begin());
std::ranges::sort(key);
auto ins = entity_key_to_index.insert({key, e});
if (!ins.second)
Expand All @@ -1461,7 +1461,7 @@ mesh::entities_to_index(const Topology& topology, int dim,
for (std::size_t e = 0; e < entities.size(); e += num_vertices_per_entity)
{
auto v = entities.subspan(e, num_vertices_per_entity);
std::copy(v.begin(), v.end(), vertices.begin());
std::ranges::copy(v, vertices.begin());
std::ranges::sort(vertices);
if (auto it = entity_key_to_index.find(vertices);
it != entity_key_to_index.end())
Expand Down
2 changes: 1 addition & 1 deletion cpp/dolfinx/mesh/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void reorder_list(std::span<T> list, std::span<const std::int32_t> nodemap)
{
auto links_old = std::span(orig.data() + n * degree, degree);
auto links_new = list.subspan(nodemap[n] * degree, degree);
std::copy(links_old.begin(), links_old.end(), links_new.begin());
std::ranges::copy(links_old, links_new.begin());
}
}

Expand Down
2 changes: 1 addition & 1 deletion python/dolfinx/wrappers/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ auto as_nbarray_copy(const V& x, std::size_t ndim, const std::size_t* shape)
using _V = std::decay_t<V>;
using T = typename _V::value_type;
T* ptr = new T[x.size()];
std::copy(x.begin(), x.end(), ptr);
std::ranges::copy(x, ptr);
return nb::ndarray<T, nb::numpy>(
ptr, ndim, shape,
nb::capsule(ptr, [](void* p) noexcept { delete[] (T*)p; }));
Expand Down

0 comments on commit aab5c42

Please sign in to comment.