From ab80c5bbc86d45032564b73757e8b09c5ad4f798 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 20 Jul 2018 23:34:50 +0200 Subject: Initial commit --- .gitignore | 4 ++ README.md | 2 +- include/sibs/Functional.hpp | 113 ++++++++++++++++++++++++++++++++++++ project.conf | 9 +++ tests/.vscode/c_cpp_properties.json | 17 ++++++ tests/.vscode/launch.json | 27 +++++++++ tests/.vscode/settings.json | 8 +++ tests/main.cpp | 47 +++++++++++++++ 8 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 include/sibs/Functional.hpp create mode 100644 project.conf create mode 100644 tests/.vscode/c_cpp_properties.json create mode 100644 tests/.vscode/launch.json create mode 100644 tests/.vscode/settings.json create mode 100644 tests/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..36d858d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +# Compiled sibs files +sibs-build/ +tests/sibs-build/ +compile_commands.json diff --git a/README.md b/README.md index dcb0f70..c1202bb 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # sibs-functional - +Library to perform operations such as merge, filter etc on collections without creating copies 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; + }; +} diff --git a/project.conf b/project.conf new file mode 100644 index 0000000..178feae --- /dev/null +++ b/project.conf @@ -0,0 +1,9 @@ +[package] +name = "sibs-functional" +type = "dynamic" +version = "0.1.0" +platforms = ["any"] +tests = "tests" + +[config] +expose_include_dirs = ["include"] diff --git a/tests/.vscode/c_cpp_properties.json b/tests/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..6ffabe5 --- /dev/null +++ b/tests/.vscode/c_cpp_properties.json @@ -0,0 +1,17 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/opt/cuda/bin/gcc", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "clang-x64", + "compileCommands": "${workspaceFolder}/compile_commands.json" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/tests/.vscode/launch.json b/tests/.vscode/launch.json new file mode 100644 index 0000000..a1e2431 --- /dev/null +++ b/tests/.vscode/launch.json @@ -0,0 +1,27 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Launch", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/sibs-build/debug/test", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": true, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/.vscode/settings.json b/tests/.vscode/settings.json new file mode 100644 index 0000000..b8354d0 --- /dev/null +++ b/tests/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "files.associations": { + "array": "cpp", + "initializer_list": "cpp", + "string_view": "cpp", + "utility": "cpp" + } +} \ No newline at end of file diff --git a/tests/main.cpp b/tests/main.cpp new file mode 100644 index 0000000..dfcd357 --- /dev/null +++ b/tests/main.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +void assertEquals(int expected, int actual) +{ + if(actual != expected) + { + fprintf(stderr, "Assert failed: Expected %d, got %d\n", expected, actual); + exit(1); + } +} + +int main(int argc, char **argv) +{ + std::vector vec1 = { 1, 2, 3, 4, 5 }; + std::vector vec2 = { 6, 7, 8, 9, 10 }; + int sum = 0; + + sibs::Function func1(vec1.data(), vec1.data() + vec1.size()); + for(auto value : func1) + { + printf("func1 value: %d\n", value); + sum += value; + } + assertEquals(15, sum); + sum = 0; + + sibs::Function func2(vec2); + for(auto value : func2) + { + printf("func2 value: %d\n", value); + sum += value; + } + assertEquals(40, sum); + sum = 0; + + for(auto value : func1.merge(func2)) + { + printf("func1 and func2 value: %d\n", value); + sum += value; + } + assertEquals(55, sum); + + return 0; +} -- cgit v1.2.3