diff --git a/include/blt/std/utility.h b/include/blt/std/utility.h index 60c1a43..1cc9fcc 100644 --- a/include/blt/std/utility.h +++ b/include/blt/std/utility.h @@ -20,6 +20,8 @@ #define BLT_UTILITY_H #include +#include +#include #if defined(__GNUC__) @@ -53,6 +55,22 @@ namespace blt namespace blt { + template + static BLT_CPP20_CONSTEXPR inline std::string type_string() + { + return demangle(typeid(T).name()); + } + + template + static BLT_CPP20_CONSTEXPR inline std::string extract_types() + { + auto name = demangle(typeid(T).name()); + + std::regex replace("(__cxx[0-9]*)::"); + + return std::regex_replace(name, replace, ""); + } + template class enumerator { @@ -69,36 +87,38 @@ namespace blt size_t index = 0; TYPE_ITR current; public: - explicit iterator(TYPE_ITR current): current(current) - {}; + explicit iterator(TYPE_ITR current): current(std::move(current)) + {} iterator& operator++() { - index++; + ++index; ++current; return *this; } - iterator operator++(int) + bool operator==(iterator other) const { - iterator retval = *this; - ++(*this); - return retval; + return current == other.current; } - bool operator==(iterator other) const - { return current == other.current; } - bool operator!=(iterator other) const - { return !(*this == other); } + { + return current != other.current; + } std::pair operator*() const { return {index, *current}; }; + + std::pair operator*() + { + return {index, *current}; + }; }; - explicit enumerator(TYPE_ITR begin, TYPE_ITR end): begin_(begin), end_(end) + explicit enumerator(TYPE_ITR begin, TYPE_ITR end): begin_(std::move(begin)), end_(std::move(end)) {} iterator begin() @@ -117,7 +137,13 @@ namespace blt }; template - static inline enumerator enumerate(T container) + static inline auto enumerate(const T& container) + { + return enumerator{container.begin(), container.end()}; + } + + template + static inline auto enumerate(T& container) { return enumerator{container.begin(), container.end()}; } @@ -134,39 +160,22 @@ namespace blt #endif template - void BLT_ATTRIB_NO_INLINE black_box_ref(const T& val) + void BLT_ATTRIB_NO_INLINE black_box(const T& val) { - volatile void* hell; + static volatile void* hell; hell = (void*) &val; (void) hell; } template - void BLT_ATTRIB_NO_INLINE black_box(T val) + const T& BLT_ATTRIB_NO_INLINE black_box_ret(const T& val) { - volatile void* hell2; - hell2 = (void*) &val; - (void) hell2; - } - - template - const T& BLT_ATTRIB_NO_INLINE black_box_ref_ret(const T& val) - { - volatile void* hell; + static volatile void* hell; hell = (void*) &val; (void) hell; return val; } - template - T BLT_ATTRIB_NO_INLINE black_box_ret(T val) - { - volatile void* hell2; - hell2 = (void*) &val; - (void) hell2; - return val; - } - } #endif //BLT_UTILITY_H diff --git a/tests/src/utility_test.cpp b/tests/src/utility_test.cpp index f1dbe49..b0879dc 100644 --- a/tests/src/utility_test.cpp +++ b/tests/src/utility_test.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -48,17 +49,56 @@ int testFunc(int a, int b) return a; } -void testEnumerate(const std::vector& test) +template +void e1(const T& test) { + BLT_START_INTERVAL("Enumeration (" + blt::type_string().substr(0, 30) + ":" + std::to_string(test.size()) + ")", "blt::enumerate"); for (auto pair : blt::enumerate(test)) { - std::cout << pair.first << ": " << pair.second << std::endl; + blt::black_box(pair); } + BLT_END_INTERVAL("Enumeration (" + blt::type_string().substr(0, 30) + ":" + std::to_string(test.size()) + ")", "blt::enumerate"); +} + +template +void e2(const T& test) +{ + BLT_START_INTERVAL("Enumeration (" + blt::type_string().substr(0, 30) + ":" + std::to_string(test.size()) + ")", "for index"); + for (size_t i = 0; i < test.size(); i++) + { + const auto& v = test[i]; + blt::black_box(std::pair{i, v}); + } + BLT_END_INTERVAL("Enumeration (" + blt::type_string().substr(0, 30) + ":" + std::to_string(test.size()) + ")", "for index"); +} + +template +void e3(const T& test) +{ + BLT_START_INTERVAL("Enumeration (" + blt::type_string().substr(0, 30) + ":" + std::to_string(test.size()) + ")", "for range"); + size_t counter = 0; + for (const auto& s : test) + { + blt::black_box(std::pair{counter, s}); + ++counter; + } + BLT_END_INTERVAL("Enumeration (" + blt::type_string().substr(0, 30) + ":" + std::to_string(test.size()) + ")", "for range"); +} + +template +void testEnumerate(const T& test) +{ + e1(test); + e2(test); + e3(test); + BLT_PRINT_PROFILE("Enumeration (" + blt::type_string().substr(0, 30) + ":" + std::to_string(test.size()) + ")"); + BLT_TRACE(blt::type_string()); + BLT_TRACE(blt::extract_types()); } void getfucked() { - BLT_ASSERT(false); + //BLT_ASSERT(false); } void fuckered() @@ -87,8 +127,6 @@ void blt::test::utility::run() tableTest.addRow({"Sleeping Together (Sexual)", "10,000"}); tableTest.addRow({"Relationship (I would do anything for you)", "1,000,000,000,000"}); - testEnumerate(tableTest.createTable(true, true)); - printLines(tableTest.createTable(true, true)); blt::string::BinaryTreeFormatter::TreeFormat format; @@ -224,6 +262,16 @@ void blt::test::utility::run() tableQ4i2.addRow({"Exit (10)", "{a + b, a * b, a - b}", " -- "}); printLines(tableQ4i2.createTable(true, true)); - + + for (int gensize = 1; gensize < 8; gensize++){ + size_t size = static_cast(std::pow(10, gensize)); + + std::vector str; + for (size_t i = 0; i < size; i++) + str.push_back(std::to_string(i)); + + testEnumerate(str); + } + fuckered(); }