From a786f61860e6a83ef8fe1e0f56352cc47eabceb8 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 17 Dec 2024 17:10:48 +0000 Subject: [PATCH] Added additional transparent comparators to unordered containers --- include/etl/unordered_multiset.h | 74 ++++++++++++++++++++++++++++---- test/test_unordered_map.cpp | 18 ++++---- test/test_unordered_multimap.cpp | 8 ++-- test/test_unordered_multiset.cpp | 54 +++++++++++------------ test/test_unordered_set.cpp | 38 ++++++++-------- 5 files changed, 125 insertions(+), 67 deletions(-) diff --git a/include/etl/unordered_multiset.h b/include/etl/unordered_multiset.h index 76ff60ae5..f2b28a585 100644 --- a/include/etl/unordered_multiset.h +++ b/include/etl/unordered_multiset.h @@ -1164,12 +1164,14 @@ namespace etl return end(); } +#if ETL_USING_CPP11 //********************************************************************* /// Finds an element. ///\param key The key to search for. ///\return An iterator to the element if the key exists, otherwise end(). //********************************************************************* - const_iterator find(key_parameter_t key) const + template ::value, int> = 0> + iterator find(const K& key) { size_t index = get_bucket_index(key); @@ -1197,15 +1199,14 @@ namespace etl return end(); } +#endif -#if ETL_USING_CPP11 //********************************************************************* /// Finds an element. ///\param key The key to search for. ///\return An iterator to the element if the key exists, otherwise end(). //********************************************************************* - template ::value, int> = 0> - iterator find(const K& key) + const_iterator find(key_parameter_t key) const { size_t index = get_bucket_index(key); @@ -1224,7 +1225,7 @@ namespace etl // Do we have this one? if (key_equal_function(key, inode->key)) { - return iterator(pbuckets + number_of_buckets, pbucket, inode); + return iterator((pbuckets + number_of_buckets), pbucket, inode); } ++inode; @@ -1233,7 +1234,6 @@ namespace etl return end(); } -#endif #if ETL_USING_CPP11 //********************************************************************* @@ -1261,7 +1261,7 @@ namespace etl // Do we have this one? if (key_equal_function(key, inode->key)) { - return iterator(pbuckets + number_of_buckets, pbucket, inode); + return iterator((pbuckets + number_of_buckets), pbucket, inode); } ++inode; @@ -1271,7 +1271,7 @@ namespace etl return end(); } #endif - + //********************************************************************* /// Returns a range containing all elements with key key in the container. /// The range is defined by two iterators, the first pointing to the first @@ -1298,6 +1298,35 @@ namespace etl return ETL_OR_STD::pair(f, l); } +#if ETL_USING_CPP11 + //********************************************************************* + /// Returns a range containing all elements with key key in the container. + /// The range is defined by two iterators, the first pointing to the first + /// element of the wanted range and the second pointing past the last + /// element of the range. + ///\param key The key to search for. + ///\return An iterator pair to the range of elements if the key exists, otherwise end(). + //********************************************************************* + template ::value, int> = 0> + ETL_OR_STD::pair equal_range(const K& key) + { + iterator f = find(key); + iterator l = f; + + if (l != end()) + { + ++l; + + while ((l != end()) && key_equal_function(key, *l)) + { + ++l; + } + } + + return ETL_OR_STD::pair(f, l); + } +#endif + //********************************************************************* /// Returns a range containing all elements with key key in the container. /// The range is defined by two iterators, the first pointing to the first @@ -1324,6 +1353,35 @@ namespace etl return ETL_OR_STD::pair(f, l); } +#if ETL_USING_CPP11 + //********************************************************************* + /// Returns a range containing all elements with key key in the container. + /// The range is defined by two iterators, the first pointing to the first + /// element of the wanted range and the second pointing past the last + /// element of the range. + ///\param key The key to search for. + ///\return A const iterator pair to the range of elements if the key exists, otherwise end(). + //********************************************************************* + template ::value, int> = 0> + ETL_OR_STD::pair equal_range(const K& key) const + { + const_iterator f = find(key); + const_iterator l = f; + + if (l != end()) + { + ++l; + + while ((l != end()) && key_equal_function(key, *l)) + { + ++l; + } + } + + return ETL_OR_STD::pair(f, l); + } +#endif + //************************************************************************* /// Gets the size of the unordered_multiset. //************************************************************************* diff --git a/test/test_unordered_map.cpp b/test/test_unordered_map.cpp index b8e01dab1..382653372 100644 --- a/test/test_unordered_map.cpp +++ b/test/test_unordered_map.cpp @@ -149,11 +149,11 @@ namespace } }; - typedef TestDataDC DC; - typedef TestDataNDC NDC; + using DC = TestDataDC; + using NDC = TestDataNDC; - typedef ETL_OR_STD::pair ElementDC; - typedef ETL_OR_STD::pair ElementNDC; + using ElementDC = ETL_OR_STD::pair; + using ElementNDC = ETL_OR_STD::pair; } namespace etl @@ -220,11 +220,11 @@ namespace using ItemM = TestDataM; using DataM = etl::unordered_map>; - typedef etl::unordered_map DataDC; - typedef etl::unordered_map DataNDC; - typedef etl::iunordered_map IDataNDC; - typedef etl::unordered_map> DataNDCTransparent; - typedef etl::unordered_map> DataDCTransparent; + using DataDC = etl::unordered_map; + using DataNDC = etl::unordered_map; + using IDataNDC = etl::iunordered_map; + using DataNDCTransparent = etl::unordered_map>; + using DataDCTransparent = etl::unordered_map>; NDC N0 = NDC("A"); NDC N1 = NDC("B"); diff --git a/test/test_unordered_multimap.cpp b/test/test_unordered_multimap.cpp index 7753a12fe..a5fccb25c 100644 --- a/test/test_unordered_multimap.cpp +++ b/test/test_unordered_multimap.cpp @@ -104,11 +104,11 @@ namespace } }; - typedef TestDataDC DC; - typedef TestDataNDC NDC; + using DC = TestDataDC; + using NDC = TestDataNDC; - typedef ETL_OR_STD::pair ElementDC; - typedef ETL_OR_STD::pair ElementNDC; + using ElementDC = ETL_OR_STD::pair; + using ElementNDC = ETL_OR_STD::pair; //*************************************************************************** struct CustomHashFunction diff --git a/test/test_unordered_multiset.cpp b/test/test_unordered_multiset.cpp index 7989e6bbf..e8e300b6f 100644 --- a/test/test_unordered_multiset.cpp +++ b/test/test_unordered_multiset.cpp @@ -45,8 +45,8 @@ SOFTWARE. namespace { - typedef TestDataDC DC; - typedef TestDataNDC NDC; + using DC = TestDataDC; + using NDC = TestDataNDC; } namespace etl @@ -151,6 +151,7 @@ namespace } }; + //*************************************************************************** SUITE(test_unordered_multiset) { static const size_t SIZE = 10; @@ -177,11 +178,10 @@ namespace using DataM = etl::unordered_multiset; - typedef etl::unordered_multiset DataDC; - typedef etl::unordered_multiset DataNDC; - typedef etl::iunordered_multiset IDataNDC; - - typedef etl::unordered_multiset> DataTransparent; + using DataDC = etl::unordered_multiset; + using DataNDC = etl::unordered_multiset; + using IDataNDC = etl::iunordered_multiset; + using DataTransparent = etl::unordered_multiset>; const char* CK0 = "FF"; // 0 const char* CK1 = "FG"; // 1 @@ -204,26 +204,26 @@ namespace const char* CK18 = "FX"; // 8 const char* CK19 = "FY"; // 9 - NDC N0 = NDC(CK0); - NDC N1 = NDC(CK1); - NDC N2 = NDC(CK2); - NDC N3 = NDC(CK3); - NDC N4 = NDC(CK4); - NDC N5 = NDC(CK5); - NDC N6 = NDC(CK6); - NDC N7 = NDC(CK7); - NDC N8 = NDC(CK8); - NDC N9 = NDC(CK9); - NDC N10 = NDC(CK10); - NDC N11 = NDC(CK11); - NDC N12 = NDC(CK12); - NDC N13 = NDC(CK13); - NDC N14 = NDC(CK14); - NDC N15 = NDC(CK15); - NDC N16 = NDC(CK16); - NDC N17 = NDC(CK17); - NDC N18 = NDC(CK18); - NDC N19 = NDC(CK19); + NDC N0 = NDC("FF"); + NDC N1 = NDC("FG"); + NDC N2 = NDC("FH"); + NDC N3 = NDC("FI"); + NDC N4 = NDC("FJ"); + NDC N5 = NDC("FK"); + NDC N6 = NDC("FL"); + NDC N7 = NDC("FM"); + NDC N8 = NDC("FN"); + NDC N9 = NDC("FO"); + NDC N10 = NDC("FP"); + NDC N11 = NDC("FQ"); + NDC N12 = NDC("FR"); + NDC N13 = NDC("FS"); + NDC N14 = NDC("FT"); + NDC N15 = NDC("FU"); + NDC N16 = NDC("FV"); + NDC N17 = NDC("FW"); + NDC N18 = NDC("FX"); + NDC N19 = NDC("FY"); std::vector initial_data; std::vector excess_data; diff --git a/test/test_unordered_set.cpp b/test/test_unordered_set.cpp index 919608b61..33eecb889 100644 --- a/test/test_unordered_set.cpp +++ b/test/test_unordered_set.cpp @@ -45,8 +45,8 @@ SOFTWARE. namespace { - typedef TestDataDC DC; - typedef TestDataNDC NDC; + using DC = TestDataDC; + using NDC = TestDataNDC; } namespace etl @@ -178,10 +178,10 @@ namespace using DataM = etl::unordered_set; - typedef etl::unordered_set DataDC; - typedef etl::unordered_set DataNDC; - typedef etl::iunordered_set IDataNDC; - typedef etl::unordered_set> DataTransparent; + using DataDC = etl::unordered_set; + using DataNDC = etl::unordered_set; + using IDataNDC = etl::iunordered_set; + using DataTransparent = etl::unordered_set>; const char* CK0 = "FF"; // 0 const char* CK1 = "FG"; // 1 @@ -520,23 +520,23 @@ namespace } } - //************************************************************************* - TEST_FIXTURE(SetupFixture, test_insert_range_using_transparent_comparator) - { - std::array initial = { "AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH" }; + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_insert_range_using_transparent_comparator) + { + std::array initial = { "AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH" }; - DataTransparent data; + DataTransparent data; - data.insert(initial.begin(), initial.end()); + data.insert(initial.begin(), initial.end()); - DataTransparent::iterator idata; + DataTransparent::iterator idata; - for (size_t i = 0UL; i < 8; ++i) - { - idata = data.find(initial[i]); - CHECK(idata != data.end()); - } - } + for (size_t i = 0UL; i < 8; ++i) + { + idata = data.find(initial[i]); + CHECK(idata != data.end()); + } + } //************************************************************************* TEST_FIXTURE(SetupFixture, test_insert_range_excess)