Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions Modules/Core/Mesh/test/itkCellInterfaceTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
*=========================================================================*/

#include <iostream>
#include <numeric>
#include <string_view>
#include <vector>

#include "itkMesh.h"
#include "itkPolyLineCell.h"
Expand Down Expand Up @@ -88,15 +90,12 @@ TestCellInterface(const std::string_view name, TCell * aCell)
std::cout << " SetPointIds" << std::endl;

using PointIdentifier = typename TCell::PointIdentifier;
const unsigned int numberOfPointIds = cell->GetNumberOfPoints() * 2;

auto * pointIds = new PointIdentifier[cell->GetNumberOfPoints() * 2];
std::vector<PointIdentifier> pointIds(numberOfPointIds);
std::iota(pointIds.begin(), pointIds.end(), 0);

for (unsigned int i = 0; i < cell->GetNumberOfPoints() * 2; ++i)
{
pointIds[i] = i;
}

cell->SetPointIds(pointIds);
cell->SetPointIds(pointIds.data());
if (cell->GetNumberOfPoints() > 0)
{
cell->SetPointId(0, 100);
Expand All @@ -112,7 +111,7 @@ TestCellInterface(const std::string_view name, TCell * aCell)
}
std::cout << std::endl;

cell->SetPointIds(&pointIds[cell->GetNumberOfPoints()], &pointIds[cell->GetNumberOfPoints() * 2]);
cell->SetPointIds(pointIds.data() + cell->GetNumberOfPoints(), pointIds.data() + numberOfPointIds);
std::cout << " Iterator test: PointIds for populated cell: ";
typename TCell::PointIdIterator pxpointId = cell->PointIdsBegin();
typename TCell::PointIdIterator pxendId = cell->PointIdsEnd();
Expand All @@ -136,8 +135,6 @@ TestCellInterface(const std::string_view name, TCell * aCell)
}
std::cout << std::endl;


delete[] pointIds;
return EXIT_SUCCESS;
}
int
Expand Down
10 changes: 6 additions & 4 deletions Modules/Nonunit/Review/include/itkVoxBoCUBImageIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <fstream>
#include <string>
#include <map>
#include <memory>
#include "itkImageIOBase.h"
#include "itkSpatialOrientation.h"
#include <cstdio>
Expand Down Expand Up @@ -95,21 +96,22 @@ class VoxBoCUBImageIO : public ImageIOBase
Write(const void * buffer) override;

VoxBoCUBImageIO();
~VoxBoCUBImageIO() override;
~VoxBoCUBImageIO() override = default;
void
PrintSelf(std::ostream & os, Indent indent) const override;

private:
bool
CheckExtension(const char *, bool & isCompressed);

GenericCUBFileAdaptor *
std::unique_ptr<GenericCUBFileAdaptor>
CreateReader(const char * filename);

GenericCUBFileAdaptor *
std::unique_ptr<GenericCUBFileAdaptor>
CreateWriter(const char * filename);

GenericCUBFileAdaptor *m_Reader{ nullptr }, *m_Writer{ nullptr };
std::unique_ptr<GenericCUBFileAdaptor> m_Reader{};
std::unique_ptr<GenericCUBFileAdaptor> m_Writer{};
Comment on lines +113 to +114
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the past, the use of {} appeared problematic for smart pointers to a forward-declared class:

I don't know if that is still relevant nowadays. 🤷 For GCC std::unique_ptr, it was fixed with GCC 9.2. Do we require GCC >= 9.2?

If the {} do still cause a problem, simply remove them. Default-initialization of std::unique_ptr will work fine anyway!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If memory serves me well, we support a rather old GCC version.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GCC 7, according to:

* GCC 7 and later [should be supported](https://www.gnu.org/software/gcc/projects/cxx-status.html).

So unfortunately then, these particular {} should be removed!

(The {} problem is specific for smart pointers to a forward-declared class. In other cases, {} usually just works fine.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working on it now.


// Initialize the orientation map (from strings to ITK)
void
Expand Down
30 changes: 9 additions & 21 deletions Modules/Nonunit/Review/src/itkVoxBoCUBImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,7 @@ VoxBoCUBImageIO::VoxBoCUBImageIO()
m_ByteOrder = IOByteOrderEnum::BigEndian;
}

/** Destructor */
VoxBoCUBImageIO::~VoxBoCUBImageIO()
{
delete m_Reader;
delete m_Writer;
}

GenericCUBFileAdaptor *
std::unique_ptr<GenericCUBFileAdaptor>
VoxBoCUBImageIO::CreateReader(const char * filename)
{
try
Expand All @@ -347,11 +340,11 @@ VoxBoCUBImageIO::CreateReader(const char * filename)
{
if (compressed)
{
return new CompressedCUBFileAdaptor(filename, "rb");
return std::make_unique<CompressedCUBFileAdaptor>(filename, "rb");
}
else
{
return new DirectCUBFileAdaptor(filename, "rb");
return std::make_unique<DirectCUBFileAdaptor>(filename, "rb");
}
}
else
Expand All @@ -365,7 +358,7 @@ VoxBoCUBImageIO::CreateReader(const char * filename)
}
}

GenericCUBFileAdaptor *
std::unique_ptr<GenericCUBFileAdaptor>
VoxBoCUBImageIO::CreateWriter(const char * filename)
{
try
Expand All @@ -375,11 +368,11 @@ VoxBoCUBImageIO::CreateWriter(const char * filename)
{
if (compressed)
{
return new CompressedCUBFileAdaptor(filename, "wb");
return std::make_unique<CompressedCUBFileAdaptor>(filename, "wb");
}
else
{
return new DirectCUBFileAdaptor(filename, "wb");
return std::make_unique<DirectCUBFileAdaptor>(filename, "wb");
}
}
else
Expand All @@ -397,7 +390,7 @@ bool
VoxBoCUBImageIO::CanReadFile(const char * filename)
{
// First check if the file can be read
GenericCUBFileAdaptor * reader = CreateReader(filename);
auto reader = CreateReader(filename);

if (reader == nullptr)
{
Expand Down Expand Up @@ -434,7 +427,6 @@ VoxBoCUBImageIO::CanReadFile(const char * filename)
iscub = false;
}

delete reader;
return iscub;
}

Expand Down Expand Up @@ -468,10 +460,7 @@ VoxBoCUBImageIO::Read(void * buffer)
void
VoxBoCUBImageIO::ReadImageInformation()
{
// Make sure there is no other reader
delete m_Reader;

// Create a reader
// Create a reader (assignment releases any previous reader)
m_Reader = CreateReader(m_FileName.c_str());
if (m_Reader == nullptr)
{
Expand Down Expand Up @@ -741,8 +730,7 @@ VoxBoCUBImageIO::Write(const void * buffer)
m_Writer = CreateWriter(m_FileName.c_str());
WriteImageInformation();
m_Writer->WriteData(buffer, this->GetImageSizeInBytes());
delete m_Writer;
m_Writer = nullptr;
m_Writer.reset();
}

/** Print Self Method */
Expand Down
14 changes: 5 additions & 9 deletions Modules/Numerics/FEM/include/itkFEMLinearSystemWrapperDenseVNL.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ class ITKFEM_EXPORT LinearSystemWrapperDenseVNL : public LinearSystemWrapper
using MatrixRepresentation = vnl_matrix<Float>;

/* matrix holder type alias */
using MatrixHolder = std::vector<MatrixRepresentation *>;
using MatrixHolder = std::vector<std::unique_ptr<MatrixRepresentation>>;

/* constructor & destructor */
LinearSystemWrapperDenseVNL()
: LinearSystemWrapper()
{}
~LinearSystemWrapperDenseVNL() override;
~LinearSystemWrapperDenseVNL() override = default;

/* memory management routines */
void
Expand Down Expand Up @@ -169,15 +169,11 @@ class ITKFEM_EXPORT LinearSystemWrapperDenseVNL : public LinearSystemWrapper
MultiplyMatrixVector(unsigned int resultVectorIndex, unsigned int matrixIndex, unsigned int vectorIndex) override;

private:
/** vector of pointers to VNL sparse matrices */
// std::vector< vnl_sparse_matrix<Float>* > *m_Matrices;
MatrixHolder * m_Matrices{ nullptr };
std::unique_ptr<MatrixHolder> m_Matrices{};

/** vector of pointers to VNL vectors */
std::vector<vnl_vector<Float> *> * m_Vectors{ nullptr };
std::unique_ptr<std::vector<std::unique_ptr<vnl_vector<Float>>>> m_Vectors{};

/** vector of pointers to VNL vectors */
std::vector<vnl_vector<Float> *> * m_Solutions{ nullptr };
std::unique_ptr<std::vector<std::unique_ptr<vnl_vector<Float>>>> m_Solutions{};
};
} // namespace itk::fem

Expand Down
68 changes: 14 additions & 54 deletions Modules/Numerics/FEM/src/itkFEMLinearSystemWrapperDenseVNL.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ LinearSystemWrapperDenseVNL::InitializeMatrix(unsigned int matrixIndex)
// allocate if necessary
if (m_Matrices == nullptr)
{
m_Matrices = new MatrixHolder(m_NumberOfMatrices);
m_Matrices = std::make_unique<MatrixHolder>(m_NumberOfMatrices);
}

// out with old, in with new
delete (*m_Matrices)[matrixIndex];

(*m_Matrices)[matrixIndex] = new MatrixRepresentation(this->GetSystemOrder(), this->GetSystemOrder());
(*m_Matrices)[matrixIndex] = std::make_unique<MatrixRepresentation>(this->GetSystemOrder(), this->GetSystemOrder());
(*m_Matrices)[matrixIndex]->fill(0.0);
}

Expand All @@ -56,8 +53,7 @@ LinearSystemWrapperDenseVNL::DestroyMatrix(unsigned int matrixIndex)
{
if (m_Matrices)
{
delete (*m_Matrices)[matrixIndex];
(*m_Matrices)[matrixIndex] = nullptr;
(*m_Matrices)[matrixIndex].reset();
}
}

Expand All @@ -67,13 +63,10 @@ LinearSystemWrapperDenseVNL::InitializeVector(unsigned int vectorIndex)
// allocate if necessary
if (m_Vectors == nullptr)
{
m_Vectors = new std::vector<vnl_vector<Float> *>(m_NumberOfVectors);
m_Vectors = std::make_unique<std::vector<std::unique_ptr<vnl_vector<Float>>>>(m_NumberOfVectors);
}

// out with old, in with new
delete (*m_Vectors)[vectorIndex];

(*m_Vectors)[vectorIndex] = new vnl_vector<Float>(this->GetSystemOrder());
(*m_Vectors)[vectorIndex] = std::make_unique<vnl_vector<Float>>(this->GetSystemOrder());
(*m_Vectors)[vectorIndex]->fill(0.0);
}

Expand All @@ -97,8 +90,7 @@ LinearSystemWrapperDenseVNL::DestroyVector(unsigned int vectorIndex)
{
if (m_Vectors)
{
delete (*m_Vectors)[vectorIndex];
(*m_Vectors)[vectorIndex] = nullptr;
(*m_Vectors)[vectorIndex].reset();
}
}

Expand All @@ -108,13 +100,10 @@ LinearSystemWrapperDenseVNL::InitializeSolution(unsigned int solutionIndex)
// allocate if necessary
if (m_Solutions == nullptr)
{
m_Solutions = new std::vector<vnl_vector<Float> *>(m_NumberOfSolutions);
m_Solutions = std::make_unique<std::vector<std::unique_ptr<vnl_vector<Float>>>>(m_NumberOfSolutions);
}

// out with old, in with new
delete (*m_Solutions)[solutionIndex];

(*m_Solutions)[solutionIndex] = new vnl_vector<Float>(this->GetSystemOrder());
(*m_Solutions)[solutionIndex] = std::make_unique<vnl_vector<Float>>(this->GetSystemOrder());
(*m_Solutions)[solutionIndex]->fill(0.0);
}

Expand All @@ -138,8 +127,7 @@ LinearSystemWrapperDenseVNL::DestroySolution(unsigned int solutionIndex)
{
if (m_Solutions)
{
delete (*m_Solutions)[solutionIndex];
(*m_Solutions)[solutionIndex] = nullptr;
(*m_Solutions)[solutionIndex].reset();
}
}

Expand Down Expand Up @@ -189,39 +177,31 @@ LinearSystemWrapperDenseVNL::Solve()
void
LinearSystemWrapperDenseVNL::SwapMatrices(unsigned int MatrixIndex1, unsigned int MatrixIndex2)
{
vnl_matrix<Float> * tmp = (*m_Matrices)[MatrixIndex1];
(*m_Matrices)[MatrixIndex1] = (*m_Matrices)[MatrixIndex2];
(*m_Matrices)[MatrixIndex2] = tmp;
std::swap((*m_Matrices)[MatrixIndex1], (*m_Matrices)[MatrixIndex2]);
}

void
LinearSystemWrapperDenseVNL::SwapVectors(unsigned int VectorIndex1, unsigned int VectorIndex2)
{
vnl_vector<Float> tmp = *(*m_Vectors)[VectorIndex1];
*(*m_Vectors)[VectorIndex1] = *(*m_Vectors)[VectorIndex2];
*(*m_Vectors)[VectorIndex2] = tmp;
std::swap((*m_Vectors)[VectorIndex1], (*m_Vectors)[VectorIndex2]);
}

void
LinearSystemWrapperDenseVNL::SwapSolutions(unsigned int SolutionIndex1, unsigned int SolutionIndex2)
{
vnl_vector<Float> * tmp = (*m_Solutions)[SolutionIndex1];
(*m_Solutions)[SolutionIndex1] = (*m_Solutions)[SolutionIndex2];
(*m_Solutions)[SolutionIndex2] = tmp;
std::swap((*m_Solutions)[SolutionIndex1], (*m_Solutions)[SolutionIndex2]);
}

void
LinearSystemWrapperDenseVNL::CopySolution2Vector(unsigned int SolutionIndex, unsigned int VectorIndex)
{
delete (*m_Vectors)[VectorIndex];
(*m_Vectors)[VectorIndex] = new vnl_vector<Float>(*((*m_Solutions)[SolutionIndex]));
(*m_Vectors)[VectorIndex] = std::make_unique<vnl_vector<Float>>(*((*m_Solutions)[SolutionIndex]));
}

void
LinearSystemWrapperDenseVNL::CopyVector2Solution(unsigned int VectorIndex, unsigned int SolutionIndex)
{
delete (*m_Solutions)[SolutionIndex];
(*m_Solutions)[SolutionIndex] = new vnl_vector<Float>(*((*m_Vectors)[VectorIndex]));
(*m_Solutions)[SolutionIndex] = std::make_unique<vnl_vector<Float>>(*((*m_Vectors)[VectorIndex]));
}

void
Expand Down Expand Up @@ -266,24 +246,4 @@ LinearSystemWrapperDenseVNL::ScaleSolution(Float scale, unsigned int solutionInd
(*(*m_Solutions)[solutionIndex]) = (*(*m_Solutions)[solutionIndex]) * scale;
}

LinearSystemWrapperDenseVNL::~LinearSystemWrapperDenseVNL()
{
for (unsigned int i = 0; i < m_NumberOfMatrices; ++i)
{
this->DestroyMatrix(i);
}
for (unsigned int i = 0; i < m_NumberOfVectors; ++i)
{
this->DestroyVector(i);
}
for (unsigned int i = 0; i < m_NumberOfSolutions; ++i)
{
this->DestroySolution(i);
}

delete m_Matrices;
delete m_Vectors;
delete m_Solutions;
}

} // namespace itk::fem
Loading