diff options
author | dec05eba <0xdec05eba@gmail.com> | 2018-07-20 23:34:50 +0200 |
---|---|---|
committer | dec05eba <0xdec05eba@gmail.com> | 2018-07-20 23:39:23 +0200 |
commit | 142a28cb61c530227833c092273ce9d188faf6a8 (patch) | |
tree | 1c9c30626f12f3d0f3d021934c350c0556b7401f /include/sibs | |
parent | e1250f0525a16c5c409680f7c6acf735b4a8a416 (diff) |
Initial commit
Diffstat (limited to 'include/sibs')
-rw-r--r-- | include/sibs/Functional.hpp | 113 |
1 files changed, 113 insertions, 0 deletions
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 <vector> + +namespace sibs +{ + template <typename T> + class Function; + + template <typename T> + class FunctionIterator + { + public: + FunctionIterator(T *_it, Function<T> *_func, std::size_t _funcIndex) : + it(_it), + func(_func), + funcIndex(_funcIndex) + { + + } + + bool operator == (FunctionIterator<T> &rhs) const + { + return it == rhs.it && funcIndex == rhs.funcIndex; + } + + bool operator != (FunctionIterator<T> &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<T> *func; + std::size_t funcIndex; + }; + + template <typename T> + class Function + { + friend class FunctionIterator<T>; + public: + typedef FunctionIterator<T> iterator; + typedef const FunctionIterator<T> const_iterator; + + Function(T *begin, T *end) : + beginIt(begin), + endIt(end) + { + + } + + Function(std::vector<T> &vec) : + beginIt(vec.data()), + endIt(vec.data() + vec.size()) + { + + } + + Function& merge(Function &otherFunc) + { + connections.push_back(otherFunc); + return *this; + } + + iterator begin() + { + return FunctionIterator<T>(beginIt, this, 0); + } + + iterator end() + { + T *it = endIt; + if(!connections.empty()) + it = connections.back().endIt; + return FunctionIterator<T>(it, this, connections.size()); + } + + const_iterator begin() const + { + return FunctionIterator<T>(beginIt, this, 0); + } + + const_iterator end() const + { + T *it = endIt; + if(!connections.empty()) + it = connections.back().endIt; + return FunctionIterator<T>(it, this, connections.size()); + } + private: + T *beginIt; + T *endIt; + std::vector<Function> connections; + }; +} |