From 69adf26aa3e853418002562f623c42a9c7008271 Mon Sep 17 00:00:00 2001 From: Antonio Sanchez Date: Fri, 9 Apr 2021 19:50:19 -0700 Subject: Modify googlehash use to account for namespace issues. The namespace declaration for googlehash is a configurable macro that can be disabled. In particular, it is disabled within google, causing compile errors since `dense_hash_map`/`sparse_hash_map` are then in the global namespace instead of in `::google`. Here we play a bit of gynastics to allow for both `google::*_hash_map` and `*_hash_map`, while limiting namespace polution. Symbols within the `::google` namespace are imported into `Eigen::google`. We also remove checks based on `_SPARSE_HASH_MAP_H_`, as this is fragile, and instead require `EIGEN_GOOGLEHASH_SUPPORT` to be defined. --- unsupported/Eigen/SparseExtra | 1 + unsupported/Eigen/src/SparseExtra/RandomSetter.h | 48 +++++++++++++++++------- unsupported/test/sparse_extra.cpp | 4 +- 3 files changed, 37 insertions(+), 16 deletions(-) (limited to 'unsupported') diff --git a/unsupported/Eigen/SparseExtra b/unsupported/Eigen/SparseExtra index 819cffa27..ba5cbd661 100644 --- a/unsupported/Eigen/SparseExtra +++ b/unsupported/Eigen/SparseExtra @@ -24,6 +24,7 @@ #ifdef EIGEN_GOOGLEHASH_SUPPORT #include + #include #endif /** diff --git a/unsupported/Eigen/src/SparseExtra/RandomSetter.h b/unsupported/Eigen/src/SparseExtra/RandomSetter.h index 7542cf764..985702b5f 100644 --- a/unsupported/Eigen/src/SparseExtra/RandomSetter.h +++ b/unsupported/Eigen/src/SparseExtra/RandomSetter.h @@ -10,7 +10,13 @@ #ifndef EIGEN_RANDOMSETTER_H #define EIGEN_RANDOMSETTER_H -namespace Eigen { +#if defined(EIGEN_GOOGLEHASH_SUPPORT) +// Ensure the ::google namespace exists, required for checking existence of +// ::google::dense_hash_map and ::google::sparse_hash_map. +namespace google {} +#endif + +namespace Eigen { /** Represents a std::map * @@ -56,7 +62,26 @@ template struct StdUnorderedMapTraits }; #endif // EIGEN_UNORDERED_MAP_SUPPORT -#ifdef _DENSE_HASH_MAP_H_ +#if defined(EIGEN_GOOGLEHASH_SUPPORT) + +namespace google { + +// Namespace work-around, since sometimes dense_hash_map and sparse_hash_map +// are in the global namespace, and other times they are under ::google. +using namespace ::google; + +template +struct DenseHashMap { + typedef dense_hash_map type; +}; + +template +struct SparseHashMap { + typedef sparse_hash_map type; +}; + +} // namespace google + /** Represents a google::dense_hash_map * * \see RandomSetter @@ -64,7 +89,7 @@ template struct StdUnorderedMapTraits template struct GoogleDenseHashMapTraits { typedef int KeyType; - typedef google::dense_hash_map Type; + typedef typename google::DenseHashMap::type Type; enum { IsSorted = 0 }; @@ -72,9 +97,7 @@ template struct GoogleDenseHashMapTraits static void setInvalidKey(Type& map, const KeyType& k) { map.set_empty_key(k); } }; -#endif -#ifdef _SPARSE_HASH_MAP_H_ /** Represents a google::sparse_hash_map * * \see RandomSetter @@ -82,7 +105,7 @@ template struct GoogleDenseHashMapTraits template struct GoogleSparseHashMapTraits { typedef int KeyType; - typedef google::sparse_hash_map Type; + typedef typename google::SparseHashMap::type Type; enum { IsSorted = 0 }; @@ -134,18 +157,17 @@ template struct GoogleSparseHashMapTraits * GoogleSparseHashMapTraits, GnuHashMapTraits, and finally StdMapTraits. * * For performance and memory consumption reasons it is highly recommended to use one of - * the Google's hash_map implementation. To enable the support for them, you have two options: - * - \#include yourself \b before Eigen/Sparse header - * - define EIGEN_GOOGLEHASH_SUPPORT - * In the later case the inclusion of is made for you. + * Google's hash_map implementations. To enable the support for them, you must define + * EIGEN_GOOGLEHASH_SUPPORT. This will include both and + * for you. * - * \see http://code.google.com/p/google-sparsehash/ + * \see https://github.com/sparsehash/sparsehash */ template class MapTraits = -#if defined _DENSE_HASH_MAP_H_ +#if defined(EIGEN_GOOGLEHASH_SUPPORT) GoogleDenseHashMapTraits -#elif defined _HASH_MAP +#elif defined(_HASH_MAP) GnuHashMapTraits #else StdMapTraits diff --git a/unsupported/test/sparse_extra.cpp b/unsupported/test/sparse_extra.cpp index bc681e3b1..cdfd10ca4 100644 --- a/unsupported/test/sparse_extra.cpp +++ b/unsupported/test/sparse_extra.cpp @@ -123,10 +123,8 @@ template void sparse_extra(const SparseMatrixType& re #ifdef EIGEN_UNORDERED_MAP_SUPPORT VERIFY(( test_random_setter >(m,refMat,nonzeroCoords) )); #endif - #ifdef _DENSE_HASH_MAP_H_ + #ifdef EIGEN_GOOGLEHASH_SUPPORT VERIFY(( test_random_setter >(m,refMat,nonzeroCoords) )); - #endif - #ifdef _SPARSE_HASH_MAP_H_ VERIFY(( test_random_setter >(m,refMat,nonzeroCoords) )); #endif -- cgit v1.2.3