diff --git a/include/blt/std/utility.h b/include/blt/std/utility.h index 04e55d3..60c1a43 100644 --- a/include/blt/std/utility.h +++ b/include/blt/std/utility.h @@ -54,14 +54,10 @@ namespace blt namespace blt { template - class enumerate + class enumerator { - private: - size_t index = 0; - TYPE_ITR begin; - TYPE_ITR end; public: - class enumerate_itr + class iterator { public: using iterator_category = std::input_iterator_tag; @@ -71,9 +67,61 @@ namespace blt using reference = typename TYPE_ITR::reference; private: size_t index = 0; + TYPE_ITR current; 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 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 + static inline enumerator enumerate(T container) + { + return enumerator{container.begin(), container.end()}; + } + #if defined(__GNUC__) || defined(__llvm__) #define BLT_ATTRIB_NO_INLINE __attribute__ ((noinline)) diff --git a/tests/src/utility_test.cpp b/tests/src/utility_test.cpp index f1ceb9a..f1fc01f 100644 --- a/tests/src/utility_test.cpp +++ b/tests/src/utility_test.cpp @@ -36,15 +36,21 @@ void printLines(const std::vector& lines) std::cout << v << "\n"; } -void testEnumerate(const std::vector& test){ - +void testEnumerate(const std::vector& test) +{ + for (auto pair : blt::enumerate(test)) + { + std::cout << pair.first << ": " << pair.second << std::endl; + } } -void getfucked(){ +void getfucked() +{ BLT_ASSERT(false); } -void fuckered(){ +void fuckered() +{ getfucked(); } @@ -89,14 +95,15 @@ void blt::test::utility::run() assign1.getRoot()->with( // left (new string::BinaryTreeFormatter::Node("member")) - ->with((new string::BinaryTreeFormatter::Node("total -= total * 0.15")) - ->with((new string::BinaryTreeFormatter::Node("total > 500"))->with(new string::BinaryTreeFormatter::Node("total -= 25"))), - (new string::BinaryTreeFormatter::Node("total -= total * 0.05"))), + ->with((new string::BinaryTreeFormatter::Node("total -= total * 0.15")) + ->with((new string::BinaryTreeFormatter::Node("total > 500")) + ->with(new string::BinaryTreeFormatter::Node("total -= 25"))), + (new string::BinaryTreeFormatter::Node("total -= total * 0.05"))), // right (new string::BinaryTreeFormatter::Node("quality")) - ->with((new string::BinaryTreeFormatter::Node("total -= total * 0.02")), - (new string::BinaryTreeFormatter::Node("total -= total * 0.05"))) - ); + ->with((new string::BinaryTreeFormatter::Node("total -= total * 0.02")), + (new string::BinaryTreeFormatter::Node("total -= total * 0.05"))) + ); printLines(assign1.construct()); blt::string::TableFormatter tableQ2i1("Iteration 0");