From 9208caa444771face1f02079b293e942a32dad3a Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 14 Feb 2016 13:09:07 +0000 Subject: [PATCH 1/2] Removed unnecessary checks. --- checksum.h | 8 -------- crc16.h | 3 --- crc16_ccitt.h | 3 --- crc16_kermit.h | 3 --- crc32.h | 3 --- crc64_ecma.h | 3 --- crc8_ccitt.h | 3 --- 7 files changed, 26 deletions(-) diff --git a/checksum.h b/checksum.h index 61a6c24d..154b68a0 100644 --- a/checksum.h +++ b/checksum.h @@ -29,8 +29,6 @@ SOFTWARE. #include -#include "static_assert.h" -#include "type_traits.h" #include "binary.h" #include "frame_check_sequence.h" @@ -135,8 +133,6 @@ namespace etl template checksum(TIterator begin, const TIterator end) { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - reset(); add(begin, end); } @@ -166,8 +162,6 @@ namespace etl template bsd_checksum(TIterator begin, const TIterator end) { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - reset(); add(begin, end); } @@ -197,8 +191,6 @@ namespace etl template xor_checksum(TIterator begin, const TIterator end) { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - reset(); add(begin, end); } diff --git a/crc16.h b/crc16.h index 96688d97..3d0f02b4 100644 --- a/crc16.h +++ b/crc16.h @@ -34,7 +34,6 @@ SOFTWARE. #include #include -#include "static_assert.h" #include "frame_check_sequence.h" #if defined(COMPILER_KEIL) @@ -99,8 +98,6 @@ namespace etl template crc16(TIterator begin, const TIterator end) { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - reset(); add(begin, end); } diff --git a/crc16_ccitt.h b/crc16_ccitt.h index b8ab06a8..2f52cfcb 100644 --- a/crc16_ccitt.h +++ b/crc16_ccitt.h @@ -34,7 +34,6 @@ SOFTWARE. #include #include -#include "static_assert.h" #include "frame_check_sequence.h" #if defined(COMPILER_KEIL) @@ -99,8 +98,6 @@ namespace etl template crc16_ccitt(TIterator begin, const TIterator end) { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - reset(); add(begin, end); } diff --git a/crc16_kermit.h b/crc16_kermit.h index 94d94adb..e635fa0f 100644 --- a/crc16_kermit.h +++ b/crc16_kermit.h @@ -34,7 +34,6 @@ SOFTWARE. #include #include -#include "static_assert.h" #include "frame_check_sequence.h" #if defined(COMPILER_KEIL) @@ -99,8 +98,6 @@ namespace etl template crc16_kermit(TIterator begin, const TIterator end) { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - reset(); add(begin, end); } diff --git a/crc32.h b/crc32.h index 7359ac45..95d896f2 100644 --- a/crc32.h +++ b/crc32.h @@ -34,7 +34,6 @@ SOFTWARE. #include #include -#include "static_assert.h" #include "frame_check_sequence.h" #if defined(COMPILER_KEIL) @@ -99,8 +98,6 @@ namespace etl template crc32(TIterator begin, const TIterator end) { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - reset(); add(begin, end); } diff --git a/crc64_ecma.h b/crc64_ecma.h index 19ae9900..679b0acb 100644 --- a/crc64_ecma.h +++ b/crc64_ecma.h @@ -34,7 +34,6 @@ SOFTWARE. #include #include -#include "static_assert.h" #include "frame_check_sequence.h" #if defined(COMPILER_KEIL) @@ -99,8 +98,6 @@ namespace etl template crc64_ecma(TIterator begin, const TIterator end) { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - reset(); add(begin, end); } diff --git a/crc8_ccitt.h b/crc8_ccitt.h index 2500490a..5168acad 100644 --- a/crc8_ccitt.h +++ b/crc8_ccitt.h @@ -34,7 +34,6 @@ SOFTWARE. #include #include -#include "static_assert.h" #include "frame_check_sequence.h" #if defined(COMPILER_KEIL) @@ -99,8 +98,6 @@ namespace etl template crc8_ccitt(TIterator begin, const TIterator end) { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Type not supported"); - reset(); add(begin, end); } From d89f0714cb1a549431f5ccd0a675859346557bac Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 14 Feb 2016 14:41:15 +0000 Subject: [PATCH 2/2] Fixed a bug in find() when key was less than any value in the set or map. --- iflat_map.h | 34 ++++++++++++++++++++++++++--- iflat_multimap.h | 32 +++++++++++++++++++++++++-- iflat_multiset.h | 32 +++++++++++++++++++++++++-- iflat_set.h | 34 ++++++++++++++++++++++++++--- test/test_flat_map.cpp | 41 +++++++++++++++++++++++++++++++++-- test/test_flat_multimap.cpp | 40 ++++++++++++++++++++++++++++++++++ test/test_flat_multiset.cpp | 43 +++++++++++++++++++++++++++++++++++++ test/test_flat_set.cpp | 41 +++++++++++++++++++++++++++++++++-- 8 files changed, 283 insertions(+), 14 deletions(-) diff --git a/iflat_map.h b/iflat_map.h index 34d3e790..8b55541e 100644 --- a/iflat_map.h +++ b/iflat_map.h @@ -405,7 +405,21 @@ namespace etl //********************************************************************* iterator find(key_value_parameter_t key) { - return lower_bound(key); + iterator itr = lower_bound(key); + + if (itr != end()) + { + if (itr->first == key) + { + return itr; + } + else + { + return end(); + } + } + + return end(); } //********************************************************************* @@ -415,7 +429,21 @@ namespace etl //********************************************************************* const_iterator find(key_value_parameter_t key) const { - return lower_bound(key); + const_iterator itr = lower_bound(key); + + if (itr != end()) + { + if (itr->first == key) + { + return itr; + } + else + { + return end(); + } + } + + return end(); } //********************************************************************* @@ -425,7 +453,7 @@ namespace etl //********************************************************************* size_t count(key_value_parameter_t key) const { - return (find(key == end()) ? 0 : 1); + return (find(key) == end()) ? 0 : 1; } //********************************************************************* diff --git a/iflat_multimap.h b/iflat_multimap.h index a6f4e08a..b7a18016 100644 --- a/iflat_multimap.h +++ b/iflat_multimap.h @@ -350,7 +350,21 @@ namespace etl //********************************************************************* iterator find(key_value_parameter_t key) { - return lower_bound(key); + iterator itr = lower_bound(key); + + if (itr != end()) + { + if (itr->first == key) + { + return itr; + } + else + { + return end(); + } + } + + return end(); } //********************************************************************* @@ -360,7 +374,21 @@ namespace etl //********************************************************************* const_iterator find(key_value_parameter_t key) const { - return lower_bound(key); + const_iterator itr = lower_bound(key); + + if (itr != end()) + { + if (itr->first == key) + { + return itr; + } + else + { + return end(); + } + } + + return end(); } //********************************************************************* diff --git a/iflat_multiset.h b/iflat_multiset.h index 8e178c53..5760583d 100644 --- a/iflat_multiset.h +++ b/iflat_multiset.h @@ -327,7 +327,21 @@ namespace etl //********************************************************************* iterator find(parameter_t key) { - return std::lower_bound(begin(), end(), key, TKeyCompare()); + iterator itr = std::lower_bound(begin(), end(), key, TKeyCompare()); + + if (itr != end()) + { + if (*itr != key) + { + return end(); + } + else + { + return itr; + } + } + + return end(); } //********************************************************************* @@ -337,7 +351,21 @@ namespace etl //********************************************************************* const_iterator find(parameter_t key) const { - return std::lower_bound(begin(), end(), key, TKeyCompare()); + const_iterator itr = std::lower_bound(begin(), end(), key, TKeyCompare()); + + if (itr != end()) + { + if (*itr != key) + { + return end(); + } + else + { + return itr; + } + } + + return end(); } //********************************************************************* diff --git a/iflat_set.h b/iflat_set.h index 81f70dbe..b656eb33 100644 --- a/iflat_set.h +++ b/iflat_set.h @@ -334,7 +334,21 @@ namespace etl //********************************************************************* iterator find(parameter_t key) { - return std::lower_bound(begin(), end(), key, TKeyCompare()); + iterator itr = std::lower_bound(begin(), end(), key, TKeyCompare()); + + if (itr != end()) + { + if (*itr == key) + { + return itr; + } + else + { + return end(); + } + } + + return end(); } //********************************************************************* @@ -344,7 +358,21 @@ namespace etl //********************************************************************* const_iterator find(parameter_t key) const { - return std::lower_bound(begin(), end(), key, TKeyCompare()); + const_iterator itr = std::lower_bound(begin(), end(), key, TKeyCompare()); + + if (itr != end()) + { + if (*itr == key) + { + return itr; + } + else + { + return end(); + } + } + + return end(); } //********************************************************************* @@ -354,7 +382,7 @@ namespace etl //********************************************************************* size_t count(parameter_t key) const { - return (find(key == end()) ? 0 : 1); + return (find(key) == end()) ? 0 : 1; } //********************************************************************* diff --git a/test/test_flat_map.cpp b/test/test_flat_map.cpp index 37dc8dbc..bfb0d9c3 100644 --- a/test/test_flat_map.cpp +++ b/test/test_flat_map.cpp @@ -623,8 +623,17 @@ namespace DataNDC::iterator it = data.find(3); CHECK_EQUAL(N3, it->second); + } - it = data.find(19); + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_find_not_present) + { + DataNDC data(initial_data.begin(), initial_data.end()); + + DataNDC::iterator it = data.find(-1); + CHECK_EQUAL(data.end(), it); + + it = data.find(10); CHECK_EQUAL(data.end(), it); } @@ -635,8 +644,17 @@ namespace DataNDC::const_iterator it = data.find(3); CHECK_EQUAL(N3, it->second); + } - it = data.find(19); + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_find_const_not_present) + { + const DataNDC data(initial_data.begin(), initial_data.end()); + + DataNDC::const_iterator it = data.find(-1); + CHECK_EQUAL(data.end(), it); + + it = data.find(10); CHECK_EQUAL(data.end(), it); } @@ -677,6 +695,25 @@ namespace CHECK_EQUAL(std::distance(compare_data.begin(), i_compare.second), std::distance(data.begin(), i_data.second)); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_equal_range_not_present) + { + Compare_DataNDC compare_data(initial_data.begin(), initial_data.end()); + DataNDC data(initial_data.begin(), initial_data.end()); + + std::pair i_compare; + + std::pair i_data; + + i_data = data.equal_range(-1); + CHECK_EQUAL(data.begin(), i_data.first); + CHECK_EQUAL(data.begin(), i_data.second); + + i_data = data.equal_range(99); + CHECK_EQUAL(data.end(), i_data.first); + CHECK_EQUAL(data.end(), i_data.second); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_equal) { diff --git a/test/test_flat_multimap.cpp b/test/test_flat_multimap.cpp index 51e8cc92..05801437 100644 --- a/test/test_flat_multimap.cpp +++ b/test/test_flat_multimap.cpp @@ -515,6 +515,18 @@ namespace CHECK_EQUAL(data.end(), it); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_find_not_present) + { + DataNDC data(initial_data.begin(), initial_data.end()); + + DataNDC::iterator it = data.find(-1); + CHECK_EQUAL(data.end(), it); + + it = data.find(10); + CHECK_EQUAL(data.end(), it); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_find_const) { @@ -527,6 +539,18 @@ namespace CHECK_EQUAL(data.end(), it); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_find_const_not_present) + { + const DataNDC data(initial_data.begin(), initial_data.end()); + + DataNDC::const_iterator it = data.find(-1); + CHECK_EQUAL(data.end(), it); + + it = data.find(10); + CHECK_EQUAL(data.end(), it); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_lower_bound) { @@ -564,6 +588,22 @@ namespace CHECK_EQUAL(std::distance(compare_data.begin(), i_compare.second), std::distance(data.begin(), i_data.second)); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_equal_range_not_present) + { + DataNDC data(initial_data.begin(), initial_data.end()); + + std::pair i_data; + + i_data = data.equal_range(-1); + CHECK_EQUAL(data.begin(), i_data.first); + CHECK_EQUAL(data.begin(), i_data.second); + + i_data = data.equal_range(99); + CHECK_EQUAL(data.end(), i_data.first); + CHECK_EQUAL(data.end(), i_data.second); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_equal) { diff --git a/test/test_flat_multiset.cpp b/test/test_flat_multiset.cpp index 2cb4e9ad..4966e462 100644 --- a/test/test_flat_multiset.cpp +++ b/test/test_flat_multiset.cpp @@ -56,6 +56,9 @@ namespace typedef std::multiset Compare_DataDC; typedef std::multiset Compare_DataNDC; + NDC NX = NDC("@"); + NDC NY = NDC("["); + NDC N0 = NDC("A"); NDC N1 = NDC("B"); NDC N2 = NDC("C"); @@ -489,6 +492,18 @@ namespace CHECK_EQUAL(data.end(), it); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_find_not_present) + { + DataNDC data(initial_data.begin(), initial_data.end()); + + DataNDC::iterator it = data.find(NX); + CHECK_EQUAL(data.end(), it); + + it = data.find(NY); + CHECK_EQUAL(data.end(), it); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_find_const) { @@ -501,6 +516,18 @@ namespace CHECK_EQUAL(data.end(), it); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_find_const_not_present) + { + const DataNDC data(initial_data.begin(), initial_data.end()); + + DataNDC::const_iterator it = data.find(NX); + CHECK_EQUAL(data.end(), it); + + it = data.find(NY); + CHECK_EQUAL(data.end(), it); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_lower_bound) { @@ -538,6 +565,22 @@ namespace CHECK_EQUAL(std::distance(compare_data.begin(), i_compare.second), std::distance(data.begin(), i_data.second)); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_equal_range_not_present) + { + DataNDC data(initial_data.begin(), initial_data.end()); + + std::pair i_data; + + i_data = data.equal_range(NX); + CHECK_EQUAL(data.begin(), i_data.first); + CHECK_EQUAL(data.begin(), i_data.second); + + i_data = data.equal_range(NY); + CHECK_EQUAL(data.end(), i_data.first); + CHECK_EQUAL(data.end(), i_data.second); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_equal) { diff --git a/test/test_flat_set.cpp b/test/test_flat_set.cpp index 37602f5f..86429f60 100644 --- a/test/test_flat_set.cpp +++ b/test/test_flat_set.cpp @@ -56,6 +56,9 @@ namespace typedef std::set Compare_DataDC; typedef std::set Compare_DataNDC; + NDC NX = NDC("@"); + NDC NY = NDC("["); + NDC N0 = NDC("A"); NDC N1 = NDC("B"); NDC N2 = NDC("C"); @@ -471,8 +474,17 @@ namespace DataNDC::iterator it = data.find(N3); CHECK_EQUAL(N3, *it); + } - it = data.find(N19); + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_find_not_present) + { + DataNDC data(initial_data.begin(), initial_data.end()); + + DataNDC::iterator it = data.find(NX); + CHECK_EQUAL(data.end(), it); + + it = data.find(NY); CHECK_EQUAL(data.end(), it); } @@ -483,8 +495,17 @@ namespace DataNDC::const_iterator it = data.find(N3); CHECK_EQUAL(N3, *it); + } - it = data.find(N19); + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_find_const_not_present) + { + const DataNDC data(initial_data.begin(), initial_data.end()); + + DataNDC::const_iterator it = data.find(NX); + CHECK_EQUAL(data.end(), it); + + it = data.find(NY); CHECK_EQUAL(data.end(), it); } @@ -525,6 +546,22 @@ namespace CHECK_EQUAL(std::distance(compare_data.begin(), i_compare.second), std::distance(data.begin(), i_data.second)); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_equal_range_not_present) + { + DataNDC data(initial_data.begin(), initial_data.end()); + + std::pair i_data; + + i_data = data.equal_range(NX); + CHECK_EQUAL(data.begin(), i_data.first); + CHECK_EQUAL(data.begin(), i_data.second); + + i_data = data.equal_range(NY); + CHECK_EQUAL(data.end(), i_data.first); + CHECK_EQUAL(data.end(), i_data.second); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_equal) {