Skip to content

Commit

Permalink
Added additional transparent comparators to unordered containers
Browse files Browse the repository at this point in the history
  • Loading branch information
jwellbelove committed Dec 19, 2024
1 parent 0f487fa commit 784d95a
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 67 deletions.
74 changes: 66 additions & 8 deletions include/etl/unordered_multiset.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename K, typename KE = TKeyEqual, etl::enable_if_t<comparator_is_transparent<KE>::value, int> = 0>
iterator find(const K& key)
{
size_t index = get_bucket_index(key);

Expand Down Expand Up @@ -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 <typename K, typename KE = TKeyEqual, etl::enable_if_t<comparator_is_transparent<KE>::value, int> = 0>
iterator find(const K& key)
const_iterator find(key_parameter_t key) const
{
size_t index = get_bucket_index(key);

Expand All @@ -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;
Expand All @@ -1233,7 +1234,6 @@ namespace etl

return end();
}
#endif

#if ETL_USING_CPP11
//*********************************************************************
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -1298,6 +1298,35 @@ namespace etl
return ETL_OR_STD::pair<iterator, iterator>(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 <typename K, typename KE = TKeyEqual, etl::enable_if_t<comparator_is_transparent<KE>::value, int> = 0>
ETL_OR_STD::pair<iterator, iterator> 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<iterator, iterator>(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
Expand All @@ -1324,6 +1353,35 @@ namespace etl
return ETL_OR_STD::pair<const_iterator, const_iterator>(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 <typename K, typename KE = TKeyEqual, etl::enable_if_t<comparator_is_transparent<KE>::value, int> = 0>
ETL_OR_STD::pair<const_iterator, const_iterator> 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<const_iterator, const_iterator>(f, l);
}
#endif

//*************************************************************************
/// Gets the size of the unordered_multiset.
//*************************************************************************
Expand Down
18 changes: 9 additions & 9 deletions test/test_unordered_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ namespace
}
};

typedef TestDataDC<std::string> DC;
typedef TestDataNDC<std::string> NDC;
using DC = TestDataDC<std::string>;
using NDC = TestDataNDC<std::string>;

typedef ETL_OR_STD::pair<std::string, DC> ElementDC;
typedef ETL_OR_STD::pair<std::string, NDC> ElementNDC;
using ElementDC = ETL_OR_STD::pair<std::string, DC>;
using ElementNDC = ETL_OR_STD::pair<std::string, NDC>;
}

namespace etl
Expand Down Expand Up @@ -220,11 +220,11 @@ namespace
using ItemM = TestDataM<int>;
using DataM = etl::unordered_map<std::string, ItemM, SIZE, SIZE, std::hash<std::string>>;

typedef etl::unordered_map<std::string, DC, SIZE, SIZE / 2, simple_hash> DataDC;
typedef etl::unordered_map<std::string, NDC, SIZE, SIZE / 2, simple_hash> DataNDC;
typedef etl::iunordered_map<std::string, NDC, simple_hash> IDataNDC;
typedef etl::unordered_map<std::string, NDC, SIZE, SIZE / 2, transparent_hash, etl::equal_to<>> DataNDCTransparent;
typedef etl::unordered_map<std::string, DC, SIZE, SIZE / 2, transparent_hash, etl::equal_to<>> DataDCTransparent;
using DataDC = etl::unordered_map<std::string, DC, SIZE, SIZE / 2, simple_hash>;
using DataNDC = etl::unordered_map<std::string, NDC, SIZE, SIZE / 2, simple_hash>;
using IDataNDC = etl::iunordered_map<std::string, NDC, simple_hash>;
using DataNDCTransparent = etl::unordered_map<std::string, NDC, SIZE, SIZE / 2, transparent_hash, etl::equal_to<>>;
using DataDCTransparent = etl::unordered_map<std::string, DC, SIZE, SIZE / 2, transparent_hash, etl::equal_to<>>;

NDC N0 = NDC("A");
NDC N1 = NDC("B");
Expand Down
8 changes: 4 additions & 4 deletions test/test_unordered_multimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ namespace
}
};

typedef TestDataDC<std::string> DC;
typedef TestDataNDC<std::string> NDC;
using DC = TestDataDC<std::string>;
using NDC = TestDataNDC<std::string>;

typedef ETL_OR_STD::pair<std::string, DC> ElementDC;
typedef ETL_OR_STD::pair<std::string, NDC> ElementNDC;
using ElementDC = ETL_OR_STD::pair<std::string, DC>;
using ElementNDC = ETL_OR_STD::pair<std::string, NDC>;

//***************************************************************************
struct CustomHashFunction
Expand Down
54 changes: 27 additions & 27 deletions test/test_unordered_multiset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ SOFTWARE.

namespace
{
typedef TestDataDC<std::string> DC;
typedef TestDataNDC<std::string> NDC;
using DC = TestDataDC<std::string>;
using NDC = TestDataNDC<std::string>;
}

namespace etl
Expand Down Expand Up @@ -151,6 +151,7 @@ namespace
}
};

//***************************************************************************
SUITE(test_unordered_multiset)
{
static const size_t SIZE = 10;
Expand All @@ -177,11 +178,10 @@ namespace

using DataM = etl::unordered_multiset<ItemM, SIZE, SIZE, simple_hash>;

typedef etl::unordered_multiset<DC, SIZE, SIZE / 2, simple_hash> DataDC;
typedef etl::unordered_multiset<NDC, SIZE, SIZE / 2, simple_hash> DataNDC;
typedef etl::iunordered_multiset<NDC, simple_hash> IDataNDC;

typedef etl::unordered_multiset<std::string, SIZE, SIZE / 2, transparent_hash, etl::equal_to<>> DataTransparent;
using DataDC = etl::unordered_multiset<DC, SIZE, SIZE / 2, simple_hash>;
using DataNDC = etl::unordered_multiset<NDC, SIZE, SIZE / 2, simple_hash>;
using IDataNDC = etl::iunordered_multiset<NDC, simple_hash>;
using DataTransparent = etl::unordered_multiset<std::string, SIZE, SIZE / 2, transparent_hash, etl::equal_to<>>;

const char* CK0 = "FF"; // 0
const char* CK1 = "FG"; // 1
Expand All @@ -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<NDC> initial_data;
std::vector<NDC> excess_data;
Expand Down
38 changes: 19 additions & 19 deletions test/test_unordered_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ SOFTWARE.

namespace
{
typedef TestDataDC<std::string> DC;
typedef TestDataNDC<std::string> NDC;
using DC = TestDataDC<std::string>;
using NDC = TestDataNDC<std::string>;
}

namespace etl
Expand Down Expand Up @@ -178,10 +178,10 @@ namespace

using DataM = etl::unordered_set<ItemM, SIZE, SIZE, simple_hash>;

typedef etl::unordered_set<DC, SIZE, SIZE / 2, simple_hash> DataDC;
typedef etl::unordered_set<NDC, SIZE, SIZE / 2, simple_hash> DataNDC;
typedef etl::iunordered_set<NDC, simple_hash> IDataNDC;
typedef etl::unordered_set<std::string, SIZE, SIZE / 2, transparent_hash, etl::equal_to<>> DataTransparent;
using DataDC = etl::unordered_set<DC, SIZE, SIZE / 2, simple_hash>;
using DataNDC = etl::unordered_set<NDC, SIZE, SIZE / 2, simple_hash>;
using IDataNDC = etl::iunordered_set<NDC, simple_hash>;
using DataTransparent = etl::unordered_set<std::string, SIZE, SIZE / 2, transparent_hash, etl::equal_to<>>;

const char* CK0 = "FF"; // 0
const char* CK1 = "FG"; // 1
Expand Down Expand Up @@ -520,23 +520,23 @@ namespace
}
}

//*************************************************************************
TEST_FIXTURE(SetupFixture, test_insert_range_using_transparent_comparator)
{
std::array<const char*, 8> initial = { "AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH" };
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_insert_range_using_transparent_comparator)
{
std::array<const char*, 8> 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)
Expand Down

0 comments on commit 784d95a

Please sign in to comment.