Skip to content

Commit bffeb26

Browse files
authored
Merge pull request #5538 from N-Dekker/Replace-vector-with-array
Use `std::array` instead of `std::vector` for local variables
2 parents 71225e3 + e008f0a commit bffeb26

File tree

8 files changed

+29
-62
lines changed

8 files changed

+29
-62
lines changed

Modules/Core/Common/test/itkConstNeighborhoodIteratorTest.cxx

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "itkNeighborhoodIteratorTestCommon.hxx"
2020
#include "itkConstNeighborhoodIterator.h"
21+
#include <array>
2122

2223
void
2324
println(const char * s)
@@ -356,17 +357,8 @@ itkConstNeighborhoodIteratorTest(int, char *[])
356357
using NeighborhoodIteratorType = itk::ConstNeighborhoodIterator<ChangeRegionTestImageType>;
357358
NeighborhoodIteratorType neighborhoodIterator(neighborhoodRadius, image, region1);
358359

359-
std::vector<int> expectedValuesRegion1(9);
360-
expectedValuesRegion1[0] = 0;
361-
expectedValuesRegion1[1] = 255;
362-
expectedValuesRegion1[2] = 255;
363-
expectedValuesRegion1[3] = 0;
364-
expectedValuesRegion1[4] = 255;
365-
expectedValuesRegion1[5] = 255;
366-
expectedValuesRegion1[6] = 0;
367-
expectedValuesRegion1[7] = 255;
368-
expectedValuesRegion1[8] = 255;
369-
unsigned int counter = 0;
360+
static constexpr std::array<int, 9> expectedValuesRegion1{ 0, 255, 255, 0, 255, 255, 0, 255, 255 };
361+
unsigned int counter = 0;
370362

371363
for (NeighborhoodIteratorType::ConstIterator pixelIterator = neighborhoodIterator.Begin();
372364
pixelIterator < neighborhoodIterator.End();
@@ -387,16 +379,7 @@ itkConstNeighborhoodIteratorTest(int, char *[])
387379
neighborhoodIterator.SetRegion(region2);
388380
neighborhoodIterator.GoToBegin();
389381

390-
std::vector<int> expectedValuesRegion2(9);
391-
expectedValuesRegion2[0] = 255;
392-
expectedValuesRegion2[1] = 255;
393-
expectedValuesRegion2[2] = 255;
394-
expectedValuesRegion2[3] = 255;
395-
expectedValuesRegion2[4] = 255;
396-
expectedValuesRegion2[5] = 255;
397-
expectedValuesRegion2[6] = 255;
398-
expectedValuesRegion2[7] = 255;
399-
expectedValuesRegion2[8] = 255;
382+
static constexpr std::array<int, 9> expectedValuesRegion2{ 255, 255, 255, 255, 255, 255, 255, 255, 255 };
400383
counter = 0;
401384
for (NeighborhoodIteratorType::ConstIterator pixelIterator = neighborhoodIterator.Begin();
402385
pixelIterator < neighborhoodIterator.End();

Modules/Core/Common/test/itkConstShapedNeighborhoodIteratorTest.cxx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "itkNeighborhoodIteratorTestCommon.hxx"
2020
#include "itkConstShapedNeighborhoodIterator.h"
21+
#include <array>
2122

2223
void
2324
PrintShapedNeighborhood(const itk::ConstShapedNeighborhoodIterator<TestImageType> & n)
@@ -470,9 +471,7 @@ itkConstShapedNeighborhoodIteratorTest(int, char *[])
470471
shapedNeighborhoodIterator.ActivateOffset(offset_item);
471472
}
472473

473-
std::vector<int> expectedValuesRegion1(2);
474-
expectedValuesRegion1[0] = 0;
475-
expectedValuesRegion1[1] = 255;
474+
static constexpr std::array<int, 2> expectedValuesRegion1{ 0, 255 };
476475

477476
unsigned int counter = 0;
478477
// while(!shapedNeighborhoodIterator.IsAtEnd()) // no need for this loop as we are only iterating over a 1x1 region
@@ -500,9 +499,7 @@ itkConstShapedNeighborhoodIteratorTest(int, char *[])
500499
shapedNeighborhoodIterator.SetRegion(region2);
501500
shapedNeighborhoodIterator.GoToBegin();
502501

503-
std::vector<int> expectedValuesRegion2(2);
504-
expectedValuesRegion2[0] = 255;
505-
expectedValuesRegion2[1] = 255;
502+
static constexpr std::array<int, 2> expectedValuesRegion2{ 255, 255 };
506503

507504
counter = 0;
508505
// while(!shapedNeighborhoodIterator.IsAtEnd()) // no need for this loop as we are only iterating over a 1x1 region

Modules/Core/Common/test/itkImageRegionIteratorTest.cxx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <iostream>
2020

2121
#include "itkImageRegionIterator.h"
22+
#include <array>
2223

2324

2425
// This routine is used to make sure that we call the "const" version
@@ -172,12 +173,8 @@ itkImageRegionIteratorTest(int, char *[])
172173

173174
itk::ImageRegionConstIterator<TestImageType> imageIterator(image, region1);
174175

175-
std::vector<int> expectedValuesRegion1(4);
176-
expectedValuesRegion1[0] = 0;
177-
expectedValuesRegion1[1] = 255;
178-
expectedValuesRegion1[2] = 0;
179-
expectedValuesRegion1[3] = 255;
180-
unsigned int counter = 0;
176+
static constexpr std::array<int, 4> expectedValuesRegion1{ 0, 255, 0, 255 };
177+
unsigned int counter = 0;
181178
while (!imageIterator.IsAtEnd())
182179
{
183180
if (imageIterator.Get() != expectedValuesRegion1[counter])
@@ -196,11 +193,7 @@ itkImageRegionIteratorTest(int, char *[])
196193
imageIterator.SetRegion(region2);
197194
imageIterator.GoToBegin();
198195

199-
std::vector<int> expectedValuesRegion2(4);
200-
expectedValuesRegion2[0] = 255;
201-
expectedValuesRegion2[1] = 255;
202-
expectedValuesRegion2[2] = 255;
203-
expectedValuesRegion2[3] = 255;
196+
static constexpr std::array<int, 4> expectedValuesRegion2{ 255, 255, 255, 255 };
204197
counter = 0;
205198
while (!imageIterator.IsAtEnd())
206199
{

Modules/Core/Common/test/itkImageScanlineIteratorTest1.cxx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <iostream>
2020

2121
#include "itkImageScanlineIterator.h"
22+
#include <array>
2223

2324

2425
// This routine is used to make sure that we call the "const" version
@@ -188,12 +189,8 @@ itkImageScanlineIteratorTest1(int, char *[])
188189

189190
itk::ImageScanlineConstIterator<TestImageType> imageIterator(image, region1);
190191

191-
std::vector<int> expectedValuesRegion1(4);
192-
expectedValuesRegion1[0] = 0;
193-
expectedValuesRegion1[1] = 255;
194-
expectedValuesRegion1[2] = 0;
195-
expectedValuesRegion1[3] = 255;
196-
unsigned int counter = 0;
192+
static constexpr std::array<int, 4> expectedValuesRegion1{ 0, 255, 0, 255 };
193+
unsigned int counter = 0;
197194
while (!imageIterator.IsAtEnd())
198195
{
199196
while (!imageIterator.IsAtEndOfLine())
@@ -216,11 +213,7 @@ itkImageScanlineIteratorTest1(int, char *[])
216213
imageIterator.SetRegion(region2);
217214
imageIterator.GoToBegin();
218215

219-
std::vector<int> expectedValuesRegion2(4);
220-
expectedValuesRegion2[0] = 255;
221-
expectedValuesRegion2[1] = 255;
222-
expectedValuesRegion2[2] = 255;
223-
expectedValuesRegion2[3] = 255;
216+
static constexpr std::array<int, 4> expectedValuesRegion2{ 255, 255, 255, 255 };
224217
counter = 0;
225218
while (!imageIterator.IsAtEnd())
226219
{

Modules/Core/QuadEdgeMesh/include/itkQuadEdgeMeshEulerOperatorsTestHelper.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "itkQuadEdgeMeshTopologyChecker.h"
2222
#include "itkQuadEdgeMeshPolygonCell.h"
23+
#include <array>
2324

2425
using IdentifierType = unsigned long;
2526

@@ -178,7 +179,7 @@ CreateTetraedronMesh(typename TMesh::Pointer mesh)
178179
const int simpleSquareCells[12] = { 0, 1, 2, 1, 0, 3, 1, 3, 2, 2, 3, 0 };
179180

180181
using PointType = typename TMesh::PointType;
181-
std::vector<PointType> pts(4);
182+
std::array<PointType, 4> pts{};
182183
{
183184
int i(0);
184185
pts[i][0] = 0.;
@@ -235,7 +236,7 @@ CreateSamosa(typename TMesh::Pointer mesh)
235236
const int simpleSquareCells[6] = { 0, 1, 2, 1, 0, 2 };
236237

237238
using PointType = typename TMesh::PointType;
238-
std::vector<PointType> pts(3);
239+
std::array<PointType, 3> pts{};
239240
{
240241
int i(0);
241242
pts[i][0] = 0.;

Modules/Filtering/QuadEdgeMeshFiltering/include/itkDelaunayConformingQuadEdgeMeshFilter.hxx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef itkDelaunayConformingQuadEdgeMeshFilter_hxx
1919
#define itkDelaunayConformingQuadEdgeMeshFilter_hxx
2020

21+
#include <array>
2122

2223
namespace itk
2324
{
@@ -124,8 +125,7 @@ DelaunayConformingQuadEdgeMeshFilter<TInputMesh, TOutputMesh>::Process()
124125

125126
m_FlipEdge->SetInput(output);
126127

127-
typename std::vector<OutputQEType *> list_qe(5);
128-
typename std::vector<OutputQEType *>::iterator it;
128+
std::array<OutputQEType *, 5> list_qe{};
129129

130130
while (!m_PriorityQueue->Empty())
131131
{
@@ -161,9 +161,8 @@ DelaunayConformingQuadEdgeMeshFilter<TInputMesh, TOutputMesh>::Process()
161161
++this->m_NumberOfEdgeFlips;
162162
list_qe[4] = qe;
163163

164-
for (it = list_qe.begin(); it != list_qe.end(); ++it)
164+
for (OutputQEType * e_it : list_qe)
165165
{
166-
OutputQEType * e_it = *it;
167166
if (e_it)
168167
{
169168
CriterionValueType value = Dyer07Criterion(output, e_it);

Modules/IO/JPEG/src/itkJPEGImageIO.cxx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "itksys/SystemTools.hxx"
2121

2222
#include "itk_jpeg.h"
23+
24+
#include <array>
2325
#include <csetjmp>
2426

2527
#define JPEGIO_JPEG_MESSAGES 1
@@ -542,13 +544,11 @@ JPEGImageIO::WriteSlice(const std::string & fileName, const void * const buffer)
542544
{
543545
// store the spacing information as pixels per inch or cm, depending on which option
544546
// retains as much precision as possible
545-
std::vector<UINT16> densityPerInch(2);
546-
densityPerInch[0] = static_cast<UINT16>(25.4 / m_Spacing[0] + 0.5);
547-
densityPerInch[1] = static_cast<UINT16>(25.4 / m_Spacing[1] + 0.5);
547+
const std::array<UINT16, 2> densityPerInch{ static_cast<UINT16>(25.4 / m_Spacing[0] + 0.5),
548+
static_cast<UINT16>(25.4 / m_Spacing[1] + 0.5) };
548549

549-
std::vector<UINT16> densityPerCm(2);
550-
densityPerCm[0] = static_cast<UINT16>(10.0 / m_Spacing[0] + 0.5);
551-
densityPerCm[1] = static_cast<UINT16>(10.0 / m_Spacing[1] + 0.5);
550+
const std::array<UINT16, 2> densityPerCm{ static_cast<UINT16>(10.0 / m_Spacing[0] + 0.5),
551+
static_cast<UINT16>(10.0 / m_Spacing[1] + 0.5) };
552552

553553
if (itk::Math::abs(25.4 / m_Spacing[0] - densityPerInch[0]) +
554554
itk::Math::abs(25.4 / m_Spacing[1] - densityPerInch[1]) <=

Modules/IO/MINC/src/itkMINCImageIO.cxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "itk_minc2.h"
2929

30+
#include <array>
3031
#include <memory> // For unique_ptr.
3132

3233

@@ -481,7 +482,7 @@ MINCImageIO::ReadImageInformation()
481482

482483
double _sep = NAN;
483484
miget_dimension_separation(m_MINCPImpl->m_MincApparentDims[usableDimensions], MI_ORDER_APPARENT, &_sep);
484-
std::vector<double> _dir(3);
485+
std::array<double, 3> _dir{};
485486
miget_dimension_cosines(m_MINCPImpl->m_MincApparentDims[usableDimensions], &_dir[0]);
486487
double _start = NAN;
487488
miget_dimension_start(m_MINCPImpl->m_MincApparentDims[usableDimensions], MI_ORDER_APPARENT, &_start);

0 commit comments

Comments
 (0)