Skip to content

Commit

Permalink
ENH: replace itksys::hash_map and hash_set by std equivalents
Browse files Browse the repository at this point in the history
itksys::hash_map and hash_set were deprecated.
Replacing them by std::unordered_map and  std::unordered_set.
Some filters assumed ascending ordering within the hash_map.
In those instances itksys::hash_map was replaced by std::map.
Updating new baseline for itkIsolatedWatershedImageFilterTest.png,
as watershed image filter is sensitive to details of computation.
  • Loading branch information
dzenanz committed Apr 11, 2019
1 parent 78338e2 commit 0e2d4f2
Show file tree
Hide file tree
Showing 24 changed files with 78 additions and 68 deletions.
2 changes: 2 additions & 0 deletions Documentation/ITK5MigrationGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ Class changes
[itk::FilterWatcher](../Modules/Core/TestKernel/include/itkFilterWatcher.h) was deleted.
It should be replaced by [itk::SimpleFilterWatcher](../Modules/Core/Common/include/itkSimpleFilterWatcher.h).

`itksys::hash_map` has been removed. It should be replaced by `std::unordered_map`.

Since `itk::ProgressReporter` does not work well with the new threading model,
it should be replaced by `itk::ProgressTransformer`.
This only applies to classes which use `GenerateData()` method, and either
Expand Down
6 changes: 3 additions & 3 deletions Modules/Core/Common/include/itkEquivalencyTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


#include "itkProcessObject.h"
#include "itksys/hash_map.hxx"
#include <unordered_map>

namespace itk
{
Expand Down Expand Up @@ -55,8 +55,8 @@ class ITKCommon_EXPORT EquivalencyTable:public DataObject
itkTypeMacro(EquivalencyTable, DataObject);

/** Define the container type for the table. */
using HashTableType = itksys::hash_map< unsigned long, unsigned long,
itksys::hash< unsigned long > >;
using HashTableType = std::unordered_map< unsigned long, unsigned long,
std::hash< unsigned long > >;

using Iterator = HashTableType::iterator;
using ConstIterator = HashTableType::const_iterator;
Expand Down
28 changes: 14 additions & 14 deletions Modules/Core/Common/test/itkHashTableTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*
*=========================================================================*/

#include "itksys/hash_set.hxx"
#include "itksys/hash_map.hxx"
#include <unordered_set>
#include <unordered_map>
#include <iostream>
#include <cstring>

Expand All @@ -37,10 +37,10 @@ struct eqstr
}
};

void lookup(const itksys::hash_set<const char*, itksys::hash<const char*>, eqstr>& Set,
void lookup(const std::unordered_set<const char*, std::hash<const char*>, eqstr>& Set,
const char* word)
{
itksys::hash_set<const char*, itksys::hash<const char*>, eqstr>::const_iterator it
std::unordered_set<const char*, std::hash<const char*>, eqstr>::const_iterator it
= Set.find(word);
std::cout << word << ": "
<< (it != Set.end() ? "present" : "not present")
Expand All @@ -52,19 +52,19 @@ inline void println(const char *s)

int itkHashTableTest(int, char* [] )
{
println("Testing itksys::hash");
itksys::hash<const char*> H;
println("Testing std::hash");
std::hash<const char*> H;
std::cout << "foo -> " << H("foo") << std::endl;
std::cout << "bar -> " << H("bar") << std::endl;
itksys::hash<int> H1;
std::hash<int> H1;
std::cout << "1 -> " << H1(1) << std::endl;
std::cout << "234 -> " << H1(234) << std::endl;
itksys::hash<char> H2;
std::hash<char> H2;
std::cout << "a -> " << H2('a') << std::endl;
std::cout << "Z -> " << H2('Z') << std::endl;

println("Testing itksys::hash_set");
using HashSetType = itksys::hash_set<const char*, itksys::hash<const char*>, eqstr>;
println("Testing std::unordered_set");
using HashSetType = std::unordered_set<const char*, std::hash<const char*>, eqstr>;
HashSetType Set;
Set.insert("kiwi");
Set.insert("plum");
Expand All @@ -85,7 +85,7 @@ int itkHashTableTest(int, char* [] )
// This is to prevent the user from calling empty() when they mean clear().
if (Set.empty()) std::cout << "Set is empty." << std::endl;
Set.bucket_count();
Set.resize(50);
Set.rehash(50);
Set.insert("the horror");
Set.count("apple");
Set.find("kiwi");
Expand All @@ -95,8 +95,8 @@ int itkHashTableTest(int, char* [] )
HashSetType SetCopy;
SetCopy = Set;

println("Testing itksys::hash_map");
using HashMapType = itksys::hash_map<const char*, int, itksys::hash<const char*>, eqstr>;
println("Testing std::unordered_map");
using HashMapType = std::unordered_map<const char*, int, std::hash<const char*>, eqstr>;

HashMapType months;
months["january"] = 31;
Expand Down Expand Up @@ -125,7 +125,7 @@ int itkHashTableTest(int, char* [] )
// This is to prevent the user from calling empty() when they mean clear().
if (months.empty()) std::cout << "Set is empty." << std::endl;
months.bucket_count();
months.resize(50);
months.rehash(50);
HashMapType::value_type p("psychotic break", 2);
months.insert(p);
months.count("january");
Expand Down
6 changes: 3 additions & 3 deletions Modules/Core/Mesh/include/itkAutomaticTopologyMeshSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "itkArray.h"
#include "itkDefaultStaticMeshTraits.h"
#include "itksys/hash_map.hxx"
#include <unordered_map>
#include "itkHexahedronCell.h"
#include "itkIntTypes.h"
#include "itkMesh.h"
Expand Down Expand Up @@ -145,7 +145,7 @@ class ITK_TEMPLATE_EXPORT AutomaticTopologyMeshSource:public MeshSource< TOutput

/** hash_map type alias. */

using PointHashMap = itksys::hash_map<
using PointHashMap = std::unordered_map<
PointType,
IdentifierType,
StructHashFunction< PointHashType > >;
Expand Down Expand Up @@ -388,7 +388,7 @@ class ITK_TEMPLATE_EXPORT AutomaticTopologyMeshSource:public MeshSource< TOutput
// are controlled manually

private:
using CellHashMap = itksys::hash_map<
using CellHashMap = std::unordered_map<
Array< IdentifierType >,
IdentifierType,
IdentifierArrayHashFunction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef itkAdaptiveEqualizationHistogram_h
#define itkAdaptiveEqualizationHistogram_h

#include "itksys/hash_map.hxx"
#include <unordered_map>
#include "itkStructHashFunction.h"
#include "itkMath.h"
#include <cmath>
Expand Down Expand Up @@ -122,7 +122,7 @@ class AdaptiveEqualizationHistogram
}

private:
using MapType = typename itksys::hash_map< TInputPixel,
using MapType = typename std::unordered_map< TInputPixel,
size_t,
StructHashFunction< TInputPixel > >;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "itkImageToImageFilter.h"
#include "itkNumericTraits.h"

#include "itksys/hash_map.hxx"
#include <unordered_map>

namespace itk {

Expand Down Expand Up @@ -116,7 +116,7 @@ class ITK_TEMPLATE_EXPORT LabelOverlapMeasuresImageFilter :
};

/** Type of the map used to store data per label */
using MapType = itksys::hash_map<LabelType, LabelSetMeasures>;
using MapType = std::unordered_map<LabelType, LabelSetMeasures>;
using MapIterator = typename MapType::iterator;
using MapConstIterator = typename MapType::const_iterator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include "itkConceptChecking.h"

#include "itksys/hash_map.hxx"
#include <unordered_map>

namespace itk {
/** \class LaplacianDeformationQuadEdgeMeshFilter
Expand Down Expand Up @@ -180,11 +180,11 @@ class ITK_TEMPLATE_EXPORT LaplacianDeformationQuadEdgeMeshFilter:
LaplacianDeformationQuadEdgeMeshFilter();
~LaplacianDeformationQuadEdgeMeshFilter() override = default;

using OutputMapPointIdentifier = itksys::hash_map< OutputPointIdentifier, OutputPointIdentifier >;
using OutputMapPointIdentifier = std::unordered_map< OutputPointIdentifier, OutputPointIdentifier >;
using OutputMapPointIdentifierIterator = typename OutputMapPointIdentifier::iterator;
using OutputMapPointIdentifierConstIterator = typename OutputMapPointIdentifier::const_iterator;

using ConstraintMapType = itksys::hash_map< OutputPointIdentifier, OutputVectorType >;
using ConstraintMapType = std::unordered_map< OutputPointIdentifier, OutputVectorType >;
using ConstraintMapConstIterator = typename ConstraintMapType::const_iterator;

struct HashOutputQEPrimal
Expand All @@ -195,13 +195,13 @@ class ITK_TEMPLATE_EXPORT LaplacianDeformationQuadEdgeMeshFilter:
}
};

using CoefficientMapType = itksys::hash_map< OutputQEPrimal*, OutputCoordRepType, HashOutputQEPrimal >;
using CoefficientMapType = std::unordered_map< OutputQEPrimal*, OutputCoordRepType, HashOutputQEPrimal >;
using CoefficientMapConstIterator = typename CoefficientMapType::const_iterator;

using AreaMapType = itksys::hash_map< OutputPointIdentifier, OutputCoordRepType >;
using AreaMapType = std::unordered_map< OutputPointIdentifier, OutputCoordRepType >;
using AreaMapConstIterator = typename AreaMapType::const_iterator;

using RowType = itksys::hash_map< OutputPointIdentifier, OutputCoordRepType >;
using RowType = std::unordered_map< OutputPointIdentifier, OutputCoordRepType >;
using RowIterator = typename RowType::iterator;
using RowConstIterator = typename RowType::const_iterator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class ITK_TEMPLATE_EXPORT LaplacianDeformationQuadEdgeMeshFilterWithSoftConstrai
OutputCoordRepType m_Lambda;
OutputCoordRepType m_LambdaSquare;

itksys::hash_map< OutputPointIdentifier, OutputCoordRepType > m_LocalLambdaSquare;
std::unordered_map< OutputPointIdentifier, OutputCoordRepType > m_LocalLambdaSquare;

};
} // end namespace itk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ LaplacianDeformationQuadEdgeMeshFilterWithSoftConstraints< TInputMesh, TOutputMe

OutputCoordRepType l2 = m_LambdaSquare;

typename itksys::hash_map< OutputPointIdentifier, OutputCoordRepType >::const_iterator lambdaIt = this->m_LocalLambdaSquare.find( vId );
typename std::unordered_map< OutputPointIdentifier, OutputCoordRepType >::const_iterator lambdaIt = this->m_LocalLambdaSquare.find( vId );
if( lambdaIt != this->m_LocalLambdaSquare.end() )
{
l2 = lambdaIt->second;
Expand Down
8 changes: 4 additions & 4 deletions Modules/Nonunit/Review/include/itkLabelGeometryImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "itkNumericTraits.h"
#include "itkArray.h"
#include "itkSimpleDataObjectDecorator.h"
#include "itksys/hash_map.hxx"
#include <map>
#include <vector>
#include "vnl/algo/vnl_symmetric_eigensystem.h"
#include "vnl/vnl_det.h"
Expand Down Expand Up @@ -245,9 +245,9 @@ class ITK_TEMPLATE_EXPORT LabelGeometryImageFilter:

/** Type of the map used to store data per label */
// Map from the label to the class storing all of the geometry information.
using MapType = itksys::hash_map< LabelPixelType, LabelGeometry >;
using MapIterator = typename itksys::hash_map< LabelPixelType, LabelGeometry >::iterator;
using MapConstIterator = typename itksys::hash_map< LabelPixelType, LabelGeometry >::const_iterator;
using MapType = std::map< LabelPixelType, LabelGeometry >;
using MapIterator = typename std::map< LabelPixelType, LabelGeometry >::iterator;
using MapConstIterator = typename std::map< LabelPixelType, LabelGeometry >::const_iterator;

// Macros for enabling the calculation of additional features.
itkGetMacro(CalculatePixelIndices, bool);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define itkKdTreeBasedKmeansEstimator_h

#include <vector>
#include "itksys/hash_map.hxx"
#include <unordered_map>

#include "itkObject.h"
#include "itkEuclideanDistanceMetric.h"
Expand Down Expand Up @@ -152,7 +152,7 @@ class ITK_TEMPLATE_EXPORT KdTreeBasedKmeansEstimator:
* of changes in centroid positions) */
void StartOptimization();

using ClusterLabelsType = itksys::hash_map< InstanceIdentifier, unsigned int >;
using ClusterLabelsType = std::unordered_map< InstanceIdentifier, unsigned int >;

itkSetMacro(UseClusterLabels, bool);
itkGetConstMacro(UseClusterLabels, bool);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ KdTreeBasedKmeansEstimator< TKdTree >
{
m_GenerateClusterLabels = true;
m_ClusterLabels.clear();
m_ClusterLabels.resize( m_KdTree->GetSample()->Size() );
m_ClusterLabels.rehash( m_KdTree->GetSample()->Size() );
for ( i = 0; i < (unsigned int)( m_Parameters.size() / m_MeasurementVectorSize ); i++ )
{
validIndexes.push_back(i);
Expand Down
4 changes: 2 additions & 2 deletions Modules/Numerics/Statistics/include/itkMembershipSample.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef itkMembershipSample_h
#define itkMembershipSample_h

#include "itksys/hash_map.hxx"
#include <unordered_map>
#include "itkSubsample.h"

namespace itk
Expand Down Expand Up @@ -86,7 +86,7 @@ class ITK_TEMPLATE_EXPORT MembershipSample:public DataObject

/** Typedef for the storage that holds a class label for each instance.
* The relationship between instances and class label is one-to-one */
using ClassLabelHolderType = itksys::hash_map< InstanceIdentifier, ClassLabelType >;
using ClassLabelHolderType = std::unordered_map< InstanceIdentifier, ClassLabelType >;

/** Typedef for each subsample that stores instance identifiers of instances
* that belong to a class */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "itkImageRegionIterator.h"
#include "itkNumericTraits.h"
#include "itkProgressReporter.h"
#include "itksys/hash_map.hxx"
#include <map>

namespace itk
Expand Down Expand Up @@ -52,7 +51,7 @@ RelabelComponentImageFilter< TInputImage, TOutputImage >

// Use a map to keep track of the size of each object. Object
// number -> ObjectType (which has Object number and the two sizes)
using MapType = itksys::hash_map< LabelType, RelabelComponentObjectType >;
using MapType = std::map< LabelType, RelabelComponentObjectType >;
MapType sizeMap;
typename MapType::iterator mapIt;
using MapValueType = typename MapType::value_type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ int itkRelabelComponentImageFilterTest(int argc, char* argv[] )
return EXIT_FAILURE;
}

bool success = true;
using InternalPixelType = unsigned short;
using LabelPixelType = unsigned long;
using WritePixelType = unsigned char;
Expand Down Expand Up @@ -133,6 +134,7 @@ int itkRelabelComponentImageFilterTest(int argc, char* argv[] )
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
success = false;
}


Expand All @@ -149,6 +151,7 @@ int itkRelabelComponentImageFilterTest(int argc, char* argv[] )
std::cerr << "Exception caught during statistics calculation!"
<< std::endl;
std::cerr << excep << std::endl;
success = false;
}
try
{
Expand Down Expand Up @@ -220,6 +223,7 @@ int itkRelabelComponentImageFilterTest(int argc, char* argv[] )
catch (...)
{
std::cerr << "Exception caught while printing statistics" << std::endl;
success = false;
}

// Check for the sizes of the 7 first labels which should be sorted by default
Expand All @@ -230,7 +234,7 @@ int itkRelabelComponentImageFilterTest(int argc, char* argv[] )
{
std::cerr << "Comparing label size to reference value." << std::endl;
std::cerr << "Got " << relabel->GetSizeOfObjectsInPixels()[i] << ", expected " << ref1[i] << std::endl;
return EXIT_FAILURE;
success = false;
}
}

Expand All @@ -239,16 +243,25 @@ int itkRelabelComponentImageFilterTest(int argc, char* argv[] )
relabel->Update();

// Check for the sizes of the 7 first labels which are no more sorted
unsigned long ref2 [7] = { 1491, 2, 1, 906, 3, 40, 1 };
for ( int i=0; i<6; ++i )
unsigned long ref2[7] = { 1491, 2, 1, 906, 3, 40, 1 };
for ( int i=0; i<7; ++i )
{
if ( relabel->GetSizeOfObjectsInPixels()[i] != ref2[i] )
{
std::cerr << "Comparing label size to reference value." << std::endl;
std::cerr << "Got " << relabel->GetSizeOfObjectsInPixels()[i] << ", expected " << ref2[i] << std::endl;
return EXIT_FAILURE;
success = false;
}
}

return EXIT_SUCCESS;
if (success)
{
std::cout << "Test PASSED!" << std::endl;
return EXIT_SUCCESS;
}
else
{
std::cout << "Test FAILED!" << std::endl;
return EXIT_FAILURE;
}
}
Loading

0 comments on commit 0e2d4f2

Please sign in to comment.