From 142a28cb61c530227833c092273ce9d188faf6a8 Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Fri, 20 Jul 2018 23:34:50 +0200 Subject: Initial commit --- include/sibs/Functional.hpp | 113 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 include/sibs/Functional.hpp (limited to 'include') diff --git a/include/sibs/Functional.hpp b/include/sibs/Functional.hpp new file mode 100644 index 0000000..53d8ff0 --- /dev/null +++ b/include/sibs/Functional.hpp @@ -0,0 +1,113 @@ +#pragma once + +#include + +namespace sibs +{ + template + class Function; + + template + class FunctionIterator + { + public: + FunctionIterator(T *_it, Function *_func, std::size_t _funcIndex) : + it(_it), + func(_func), + funcIndex(_funcIndex) + { + + } + + bool operator == (FunctionIterator &rhs) const + { + return it == rhs.it && funcIndex == rhs.funcIndex; + } + + bool operator != (FunctionIterator &rhs) const + { + return !(*this == rhs); + } + + T& operator * () + { + return *it; + } + + void operator ++ () + { + ++it; + if(it == func->endIt) + { + if(funcIndex < func->connections.size()) + { + it = func->connections[funcIndex].beginIt; + ++funcIndex; + } + } + } + private: + T *it; + Function *func; + std::size_t funcIndex; + }; + + template + class Function + { + friend class FunctionIterator; + public: + typedef FunctionIterator iterator; + typedef const FunctionIterator const_iterator; + + Function(T *begin, T *end) : + beginIt(begin), + endIt(end) + { + + } + + Function(std::vector &vec) : + beginIt(vec.data()), + endIt(vec.data() + vec.size()) + { + + } + + Function& merge(Function &otherFunc) + { + connections.push_back(otherFunc); + return *this; + } + + iterator begin() + { + return FunctionIterator(beginIt, this, 0); + } + + iterator end() + { + T *it = endIt; + if(!connections.empty()) + it = connections.back().endIt; + return FunctionIterator(it, this, connections.size()); + } + + const_iterator begin() const + { + return FunctionIterator(beginIt, this, 0); + } + + const_iterator end() const + { + T *it = endIt; + if(!connections.empty()) + it = connections.back().endIt; + return FunctionIterator(it, this, connections.size()); + } + private: + T *beginIt; + T *endIt; + std::vector connections; + }; +} -- cgit v1.2.3