Brett 2023-12-08 20:44:45 -05:00
parent a4220e3c0d
commit 704e77419f
2 changed files with 71 additions and 16 deletions

View File

@ -54,14 +54,10 @@ namespace blt
namespace blt namespace blt
{ {
template<typename TYPE_ITR> template<typename TYPE_ITR>
class enumerate class enumerator
{ {
private:
size_t index = 0;
TYPE_ITR begin;
TYPE_ITR end;
public: public:
class enumerate_itr class iterator
{ {
public: public:
using iterator_category = std::input_iterator_tag; using iterator_category = std::input_iterator_tag;
@ -71,10 +67,62 @@ namespace blt
using reference = typename TYPE_ITR::reference; using reference = typename TYPE_ITR::reference;
private: private:
size_t index = 0; size_t index = 0;
TYPE_ITR current;
public: public:
explicit iterator(TYPE_ITR current): current(current)
{};
iterator& operator++()
{
index++;
++current;
return *this;
}
iterator operator++(int)
{
iterator retval = *this;
++(*this);
return retval;
}
bool operator==(iterator other) const
{ return current == other.current; }
bool operator!=(iterator other) const
{ return !(*this == other); }
std::pair<size_t, const reference> operator*() const
{
return {index, *current};
}; };
}; };
explicit enumerator(TYPE_ITR begin, TYPE_ITR end): begin_(begin), end_(end)
{}
iterator begin()
{
return begin_;
}
iterator end()
{
return end_;
}
private:
iterator begin_;
iterator end_;
};
template<typename T>
static inline enumerator<typename T::iterator> enumerate(T container)
{
return enumerator{container.begin(), container.end()};
}
#if defined(__GNUC__) || defined(__llvm__) #if defined(__GNUC__) || defined(__llvm__)
#define BLT_ATTRIB_NO_INLINE __attribute__ ((noinline)) #define BLT_ATTRIB_NO_INLINE __attribute__ ((noinline))
#else #else

View File

@ -36,15 +36,21 @@ void printLines(const std::vector<std::string>& lines)
std::cout << v << "\n"; std::cout << v << "\n";
} }
void testEnumerate(const std::vector<std::string>& test){ void testEnumerate(const std::vector<std::string>& test)
{
for (auto pair : blt::enumerate(test))
{
std::cout << pair.first << ": " << pair.second << std::endl;
}
} }
void getfucked(){ void getfucked()
{
BLT_ASSERT(false); BLT_ASSERT(false);
} }
void fuckered(){ void fuckered()
{
getfucked(); getfucked();
} }
@ -90,7 +96,8 @@ void blt::test::utility::run()
// left // left
(new string::BinaryTreeFormatter::Node("member")) (new string::BinaryTreeFormatter::Node("member"))
->with((new string::BinaryTreeFormatter::Node("total -= total * 0.15")) ->with((new string::BinaryTreeFormatter::Node("total -= total * 0.15"))
->with((new string::BinaryTreeFormatter::Node("total > 500"))->with(new string::BinaryTreeFormatter::Node("total -= 25"))), ->with((new string::BinaryTreeFormatter::Node("total > 500"))
->with(new string::BinaryTreeFormatter::Node("total -= 25"))),
(new string::BinaryTreeFormatter::Node("total -= total * 0.05"))), (new string::BinaryTreeFormatter::Node("total -= total * 0.05"))),
// right // right
(new string::BinaryTreeFormatter::Node("quality")) (new string::BinaryTreeFormatter::Node("quality"))