working on enumerate

v1
Brett 2023-12-09 13:44:16 -05:00
parent ca97850b6f
commit eb9cd030e3
2 changed files with 97 additions and 40 deletions

View File

@ -20,6 +20,8 @@
#define BLT_UTILITY_H #define BLT_UTILITY_H
#include <optional> #include <optional>
#include <blt/std/string.h>
#include <regex>
#if defined(__GNUC__) #if defined(__GNUC__)
@ -53,6 +55,22 @@ namespace blt
namespace blt namespace blt
{ {
template<typename T>
static BLT_CPP20_CONSTEXPR inline std::string type_string()
{
return demangle(typeid(T).name());
}
template<typename T>
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<typename TYPE_ITR> template<typename TYPE_ITR>
class enumerator class enumerator
{ {
@ -69,36 +87,38 @@ namespace blt
size_t index = 0; size_t index = 0;
TYPE_ITR current; TYPE_ITR current;
public: public:
explicit iterator(TYPE_ITR current): current(current) explicit iterator(TYPE_ITR current): current(std::move(current))
{}; {}
iterator& operator++() iterator& operator++()
{ {
index++; ++index;
++current; ++current;
return *this; return *this;
} }
iterator operator++(int) bool operator==(iterator other) const
{ {
iterator retval = *this; return current == other.current;
++(*this);
return retval;
} }
bool operator==(iterator other) const
{ return current == other.current; }
bool operator!=(iterator other) const bool operator!=(iterator other) const
{ return !(*this == other); } {
return current != other.current;
}
std::pair<size_t, const reference> operator*() const std::pair<size_t, const reference> operator*() const
{ {
return {index, *current}; return {index, *current};
}; };
std::pair<size_t, reference> 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() iterator begin()
@ -117,7 +137,13 @@ namespace blt
}; };
template<typename T> template<typename T>
static inline enumerator<typename T::iterator> enumerate(T container) static inline auto enumerate(const T& container)
{
return enumerator{container.begin(), container.end()};
}
template<typename T>
static inline auto enumerate(T& container)
{ {
return enumerator{container.begin(), container.end()}; return enumerator{container.begin(), container.end()};
} }
@ -134,39 +160,22 @@ namespace blt
#endif #endif
template<typename T> template<typename T>
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; hell = (void*) &val;
(void) hell; (void) hell;
} }
template<typename T> template<typename T>
void BLT_ATTRIB_NO_INLINE black_box(T val) const T& BLT_ATTRIB_NO_INLINE black_box_ret(const T& val)
{ {
volatile void* hell2; static volatile void* hell;
hell2 = (void*) &val;
(void) hell2;
}
template<typename T>
const T& BLT_ATTRIB_NO_INLINE black_box_ref_ret(const T& val)
{
volatile void* hell;
hell = (void*) &val; hell = (void*) &val;
(void) hell; (void) hell;
return val; return val;
} }
template<typename T>
T BLT_ATTRIB_NO_INLINE black_box_ret(T val)
{
volatile void* hell2;
hell2 = (void*) &val;
(void) hell2;
return val;
}
} }
#endif //BLT_UTILITY_H #endif //BLT_UTILITY_H

View File

@ -18,6 +18,7 @@
#include <blt/std/utility.h> #include <blt/std/utility.h>
#include <blt/std/format.h> #include <blt/std/format.h>
#include <blt/std/logging.h> #include <blt/std/logging.h>
#include <blt/profiling/profiler_v2.h>
#include <utility_test.h> #include <utility_test.h>
#include <vector> #include <vector>
#include <cstring> #include <cstring>
@ -48,17 +49,56 @@ int testFunc(int a, int b)
return a; return a;
} }
void testEnumerate(const std::vector<std::string>& test) template<typename T>
void e1(const T& test)
{ {
BLT_START_INTERVAL("Enumeration (" + blt::type_string<T>().substr(0, 30) + ":" + std::to_string(test.size()) + ")", "blt::enumerate");
for (auto pair : blt::enumerate(test)) 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<T>().substr(0, 30) + ":" + std::to_string(test.size()) + ")", "blt::enumerate");
}
template<typename T>
void e2(const T& test)
{
BLT_START_INTERVAL("Enumeration (" + blt::type_string<T>().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<T>().substr(0, 30) + ":" + std::to_string(test.size()) + ")", "for index");
}
template<typename T>
void e3(const T& test)
{
BLT_START_INTERVAL("Enumeration (" + blt::type_string<T>().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<T>().substr(0, 30) + ":" + std::to_string(test.size()) + ")", "for range");
}
template<typename T>
void testEnumerate(const T& test)
{
e1(test);
e2(test);
e3(test);
BLT_PRINT_PROFILE("Enumeration (" + blt::type_string<T>().substr(0, 30) + ":" + std::to_string(test.size()) + ")");
BLT_TRACE(blt::type_string<T>());
BLT_TRACE(blt::extract_types<T>());
} }
void getfucked() void getfucked()
{ {
BLT_ASSERT(false); //BLT_ASSERT(false);
} }
void fuckered() void fuckered()
@ -87,8 +127,6 @@ void blt::test::utility::run()
tableTest.addRow({"Sleeping Together (Sexual)", "10,000"}); tableTest.addRow({"Sleeping Together (Sexual)", "10,000"});
tableTest.addRow({"Relationship (I would do anything for you)", "1,000,000,000,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)); printLines(tableTest.createTable(true, true));
blt::string::BinaryTreeFormatter::TreeFormat format; blt::string::BinaryTreeFormatter::TreeFormat format;
@ -225,5 +263,15 @@ void blt::test::utility::run()
printLines(tableQ4i2.createTable(true, true)); printLines(tableQ4i2.createTable(true, true));
for (int gensize = 1; gensize < 8; gensize++){
size_t size = static_cast<size_t>(std::pow(10, gensize));
std::vector<std::string> str;
for (size_t i = 0; i < size; i++)
str.push_back(std::to_string(i));
testEnumerate(str);
}
fuckered(); fuckered();
} }