From 8c74e60f548ba8217c7aab42884ed0db652556ce Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Sun, 9 Feb 2025 21:53:03 +0100 Subject: [PATCH] basic functionallity for is odd/even --- example.py | 3 ++ pyproject.toml | 12 +++++++ setup.py | 30 ++++++++++++++++ src/fabelous_math/__init__.py | 3 ++ src/fabelous_math/__main__.py | 0 src/fabelous_math/cpp/functions/bindings.cpp | 36 +++++++++++++++++++ .../cpp/functions/simple_functions.cpp | 11 ++++++ .../include/simple_functions.hpp | 6 ++++ 8 files changed, 101 insertions(+) create mode 100644 example.py create mode 100644 pyproject.toml create mode 100644 setup.py create mode 100644 src/fabelous_math/__init__.py create mode 100644 src/fabelous_math/__main__.py create mode 100644 src/fabelous_math/cpp/functions/bindings.cpp create mode 100644 src/fabelous_math/cpp/functions/simple_functions.cpp create mode 100644 src/fabelous_math/include/simple_functions.hpp diff --git a/example.py b/example.py new file mode 100644 index 0000000..ee0f1ff --- /dev/null +++ b/example.py @@ -0,0 +1,3 @@ +from fabelous_math import is_even, is_odd + +print(is_even(5)) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..91c3f0e --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,12 @@ +[build-system] +requires = ["setuptools>=42", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "fabelous_math" +version = "0.0.1" +description = "Math functions written in C++ for faster code" +authors = [ + {name = "Falko Habel", email = "falko.habel@fabelous.app"} +] +requires-python = ">=3.7" diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..c59254f --- /dev/null +++ b/setup.py @@ -0,0 +1,30 @@ +from setuptools import setup, Extension +import platform + +extra_compile_args = [] +extra_link_args = [] + +# Set C++17 flag based on the compiler +if platform.system() == "Windows": + extra_compile_args.append('/std:c++17') +else: + extra_compile_args.append('-std=c++17') + +module = Extension( + 'fabelous_math.simple_functions', + sources=[ + 'src/fabelous_math/cpp/functions/simple_functions.cpp', + 'src/fabelous_math/cpp/functions/bindings.cpp' + ], + include_dirs=['src/fabelous_math/include'], + extra_compile_args=extra_compile_args, + extra_link_args=extra_link_args, +) + +setup( + name='fabelous_math', + description='Math functions written in C++ for faster code', + ext_modules=[module], + author="Falko Habel", + author_email="falko.habel@fabelous.app" +) \ No newline at end of file diff --git a/src/fabelous_math/__init__.py b/src/fabelous_math/__init__.py new file mode 100644 index 0000000..40c1e47 --- /dev/null +++ b/src/fabelous_math/__init__.py @@ -0,0 +1,3 @@ +from fabelous_math.simple_functions import is_even, is_odd + +__all__ = ["is_even", "is_odd"] \ No newline at end of file diff --git a/src/fabelous_math/__main__.py b/src/fabelous_math/__main__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/fabelous_math/cpp/functions/bindings.cpp b/src/fabelous_math/cpp/functions/bindings.cpp new file mode 100644 index 0000000..aef16dc --- /dev/null +++ b/src/fabelous_math/cpp/functions/bindings.cpp @@ -0,0 +1,36 @@ +#include +#include "simple_functions.hpp" + +static PyObject* is_even_wrapper(PyObject* self, PyObject* args) { + long long number; + if (!PyArg_ParseTuple(args, "L", &number)) { + return NULL; + } + return PyBool_FromLong(simple_functions::is_even(number)); +} + +static PyObject* is_odd_wrapper(PyObject* self, PyObject* args) { + long long number; + if (!PyArg_ParseTuple(args, "L", &number)) { + return NULL; + } + return PyBool_FromLong(simple_functions::is_odd(number)); +} + +static PyMethodDef NumberUtilsMethods[] = { + {"is_even", is_even_wrapper, METH_VARARGS, "Check if a number is even"}, + {"is_odd", is_odd_wrapper, METH_VARARGS, "Check if a number is odd"}, + {NULL, NULL, 0, NULL} +}; + +static struct PyModuleDef number_utils_module = { + PyModuleDef_HEAD_INIT, + "simple_functions", + "Module for checking if numbers are even or odd", + -1, + NumberUtilsMethods +}; + +PyMODINIT_FUNC PyInit_simple_functions(void) { + return PyModule_Create(&number_utils_module); +} \ No newline at end of file diff --git a/src/fabelous_math/cpp/functions/simple_functions.cpp b/src/fabelous_math/cpp/functions/simple_functions.cpp new file mode 100644 index 0000000..6638dae --- /dev/null +++ b/src/fabelous_math/cpp/functions/simple_functions.cpp @@ -0,0 +1,11 @@ +#include "simple_functions.hpp" + +namespace simple_functions { + bool is_even(long long number) { + return (number & 1) == 0; + } + + bool is_odd(long long number) { + return (number & 1) == 1; + } +} \ No newline at end of file diff --git a/src/fabelous_math/include/simple_functions.hpp b/src/fabelous_math/include/simple_functions.hpp new file mode 100644 index 0000000..4678dd8 --- /dev/null +++ b/src/fabelous_math/include/simple_functions.hpp @@ -0,0 +1,6 @@ +#pragma once + +namespace simple_functions { + bool is_even(long long number); + bool is_odd(long long number); +} \ No newline at end of file