aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-07-20 23:34:50 +0200
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:50:13 +0200
commitab80c5bbc86d45032564b73757e8b09c5ad4f798 (patch)
treee59f69aa55a486750bee744a80df29b6c19ef88a /include
parent3dd30d705bf9ec6ca8973483100258655cd357f4 (diff)
Initial commit
Diffstat (limited to 'include')
-rw-r--r--include/sibs/Functional.hpp113
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;
+ };
+}