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
Expand Up @@ -405,6 +405,8 @@ Class changes
[itk::FilterWatcher](../Modules/Core/TestKernel/include/itkFilterWatcher.h) was deleted. [itk::FilterWatcher](../Modules/Core/TestKernel/include/itkFilterWatcher.h) was deleted.
It should be replaced by [itk::SimpleFilterWatcher](../Modules/Core/Common/include/itkSimpleFilterWatcher.h). 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, Since `itk::ProgressReporter` does not work well with the new threading model,
it should be replaced by `itk::ProgressTransformer`. it should be replaced by `itk::ProgressTransformer`.
This only applies to classes which use `GenerateData()` method, and either 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
Expand Up @@ -20,7 +20,7 @@




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


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


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


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


#include "itksys/hash_set.hxx" #include <unordered_set>
#include "itksys/hash_map.hxx" #include <unordered_map>
#include <iostream> #include <iostream>
#include <cstring> #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) 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); = Set.find(word);
std::cout << word << ": " std::cout << word << ": "
<< (it != Set.end() ? "present" : "not present") << (it != Set.end() ? "present" : "not present")
Expand All @@ -52,19 +52,19 @@ inline void println(const char *s)


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


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


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


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


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


/** hash_map type alias. */ /** hash_map type alias. */


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


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


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


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


Expand Down
Expand Up @@ -21,7 +21,7 @@
#include "itkImageToImageFilter.h" #include "itkImageToImageFilter.h"
#include "itkNumericTraits.h" #include "itkNumericTraits.h"


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


namespace itk { 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 */ /** 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 MapIterator = typename MapType::iterator;
using MapConstIterator = typename MapType::const_iterator; using MapConstIterator = typename MapType::const_iterator;


Expand Down
Expand Up @@ -23,7 +23,7 @@


#include "itkConceptChecking.h" #include "itkConceptChecking.h"


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


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


using OutputMapPointIdentifier = itksys::hash_map< OutputPointIdentifier, OutputPointIdentifier >; using OutputMapPointIdentifier = std::unordered_map< OutputPointIdentifier, OutputPointIdentifier >;
using OutputMapPointIdentifierIterator = typename OutputMapPointIdentifier::iterator; using OutputMapPointIdentifierIterator = typename OutputMapPointIdentifier::iterator;
using OutputMapPointIdentifierConstIterator = typename OutputMapPointIdentifier::const_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; using ConstraintMapConstIterator = typename ConstraintMapType::const_iterator;


struct HashOutputQEPrimal 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 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 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 RowIterator = typename RowType::iterator;
using RowConstIterator = typename RowType::const_iterator; using RowConstIterator = typename RowType::const_iterator;


Expand Down
Expand Up @@ -110,7 +110,7 @@ class ITK_TEMPLATE_EXPORT LaplacianDeformationQuadEdgeMeshFilterWithSoftConstrai
OutputCoordRepType m_Lambda; OutputCoordRepType m_Lambda;
OutputCoordRepType m_LambdaSquare; OutputCoordRepType m_LambdaSquare;


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


}; };
} // end namespace itk } // end namespace itk
Expand Down
Expand Up @@ -154,7 +154,7 @@ LaplacianDeformationQuadEdgeMeshFilterWithSoftConstraints< TInputMesh, TOutputMe


OutputCoordRepType l2 = m_LambdaSquare; 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() ) if( lambdaIt != this->m_LocalLambdaSquare.end() )
{ {
l2 = lambdaIt->second; l2 = lambdaIt->second;
Expand Down
8 changes: 4 additions & 4 deletions Modules/Nonunit/Review/include/itkLabelGeometryImageFilter.h
Expand Up @@ -22,7 +22,7 @@
#include "itkNumericTraits.h" #include "itkNumericTraits.h"
#include "itkArray.h" #include "itkArray.h"
#include "itkSimpleDataObjectDecorator.h" #include "itkSimpleDataObjectDecorator.h"
#include "itksys/hash_map.hxx" #include <map>
#include <vector> #include <vector>
#include "vnl/algo/vnl_symmetric_eigensystem.h" #include "vnl/algo/vnl_symmetric_eigensystem.h"
#include "vnl/vnl_det.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 */ /** Type of the map used to store data per label */
// Map from the label to the class storing all of the geometry information. // Map from the label to the class storing all of the geometry information.
using MapType = itksys::hash_map< LabelPixelType, LabelGeometry >; using MapType = std::map< LabelPixelType, LabelGeometry >;
using MapIterator = typename itksys::hash_map< LabelPixelType, LabelGeometry >::iterator; using MapIterator = typename std::map< LabelPixelType, LabelGeometry >::iterator;
using MapConstIterator = typename itksys::hash_map< LabelPixelType, LabelGeometry >::const_iterator; using MapConstIterator = typename std::map< LabelPixelType, LabelGeometry >::const_iterator;


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


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


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


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


itkSetMacro(UseClusterLabels, bool); itkSetMacro(UseClusterLabels, bool);
itkGetConstMacro(UseClusterLabels, bool); itkGetConstMacro(UseClusterLabels, bool);
Expand Down
Expand Up @@ -404,7 +404,7 @@ KdTreeBasedKmeansEstimator< TKdTree >
{ {
m_GenerateClusterLabels = true; m_GenerateClusterLabels = true;
m_ClusterLabels.clear(); 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++ ) for ( i = 0; i < (unsigned int)( m_Parameters.size() / m_MeasurementVectorSize ); i++ )
{ {
validIndexes.push_back(i); validIndexes.push_back(i);
Expand Down
4 changes: 2 additions & 2 deletions Modules/Numerics/Statistics/include/itkMembershipSample.h
Expand Up @@ -18,7 +18,7 @@
#ifndef itkMembershipSample_h #ifndef itkMembershipSample_h
#define itkMembershipSample_h #define itkMembershipSample_h


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


namespace itk 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. /** Typedef for the storage that holds a class label for each instance.
* The relationship between instances and class label is one-to-one */ * 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 /** Typedef for each subsample that stores instance identifiers of instances
* that belong to a class */ * that belong to a class */
Expand Down
Expand Up @@ -22,7 +22,6 @@
#include "itkImageRegionIterator.h" #include "itkImageRegionIterator.h"
#include "itkNumericTraits.h" #include "itkNumericTraits.h"
#include "itkProgressReporter.h" #include "itkProgressReporter.h"
#include "itksys/hash_map.hxx"
#include <map> #include <map>


namespace itk 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 // Use a map to keep track of the size of each object. Object
// number -> ObjectType (which has Object number and the two sizes) // 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; MapType sizeMap;
typename MapType::iterator mapIt; typename MapType::iterator mapIt;
using MapValueType = typename MapType::value_type; using MapValueType = typename MapType::value_type;
Expand Down
Expand Up @@ -37,6 +37,7 @@ int itkRelabelComponentImageFilterTest(int argc, char* argv[] )
return EXIT_FAILURE; return EXIT_FAILURE;
} }


bool success = true;
using InternalPixelType = unsigned short; using InternalPixelType = unsigned short;
using LabelPixelType = unsigned long; using LabelPixelType = unsigned long;
using WritePixelType = unsigned char; 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 << "Exception caught !" << std::endl;
std::cerr << excep << 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::cerr << "Exception caught during statistics calculation!"
<< std::endl; << std::endl;
std::cerr << excep << std::endl; std::cerr << excep << std::endl;
success = false;
} }
try try
{ {
Expand Down Expand Up @@ -220,6 +223,7 @@ int itkRelabelComponentImageFilterTest(int argc, char* argv[] )
catch (...) catch (...)
{ {
std::cerr << "Exception caught while printing statistics" << std::endl; 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 // 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 << "Comparing label size to reference value." << std::endl;
std::cerr << "Got " << relabel->GetSizeOfObjectsInPixels()[i] << ", expected " << ref1[i] << 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(); relabel->Update();


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

0 comments on commit 0e2d4f2

Please sign in to comment.