From dc8df160d6e47d1170248088eb977b0f8fca2ac9 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 21 Feb 2025 11:38:11 +0800 Subject: [PATCH] add `vec2i.__floordiv__` --- include/typings/linalg.pyi | 1 + src/modules/linalg.c | 12 ++++++++++++ tests/80_linalg.py | 3 +++ 3 files changed, 16 insertions(+) diff --git a/include/typings/linalg.pyi b/include/typings/linalg.pyi index a2106ef4..7a26a8db 100644 --- a/include/typings/linalg.pyi +++ b/include/typings/linalg.pyi @@ -30,6 +30,7 @@ class _vecI[T]: def __mul__(self, other: int) -> T: ... @overload def __mul__(self, other: T) -> T: ... + def __floordiv__(self, other: int) -> T: ... def __hash__(self) -> int: ... diff --git a/src/modules/linalg.c b/src/modules/linalg.c index 2c54035f..3c313d6d 100644 --- a/src/modules/linalg.c +++ b/src/modules/linalg.c @@ -298,6 +298,16 @@ DEF_VECTOR_OPS(3) sum += a.data[i] * b.data[i]; \ py_newint(py_retval(), sum); \ return true; \ + } \ + static bool vec##D##i##__floordiv__(int argc, py_Ref argv) { \ + PY_CHECK_ARGC(2); \ + PY_CHECK_ARG_TYPE(1, tp_int); \ + c11_vec##D##i a = py_tovec##D##i(&argv[0]); \ + py_i64 b = py_toint(&argv[1]); \ + for(int i = 0; i < D; i++) \ + a.data[i] /= b; \ + py_newvec##D##i(py_retval(), a); \ + return true; \ } DEF_VECTOR_INT_OPS(2) @@ -911,6 +921,7 @@ void pk__add_module_linalg() { py_bindmagic(vec2i, __add__, vec2i__add__); py_bindmagic(vec2i, __sub__, vec2i__sub__); py_bindmagic(vec2i, __mul__, vec2i__mul__); + py_bindmagic(vec2i, __floordiv__, vec2i__floordiv__); py_bindmagic(vec2i, __eq__, vec2i__eq__); py_bindmagic(vec2i, __ne__, vec2i__ne__); py_bindmagic(vec2i, __hash__, vec2i__hash__); @@ -935,6 +946,7 @@ void pk__add_module_linalg() { py_bindmagic(vec3i, __add__, vec3i__add__); py_bindmagic(vec3i, __sub__, vec3i__sub__); py_bindmagic(vec3i, __mul__, vec3i__mul__); + py_bindmagic(vec3i, __floordiv__, vec3i__floordiv__); py_bindmagic(vec3i, __eq__, vec3i__eq__); py_bindmagic(vec3i, __ne__, vec3i__ne__); py_bindmagic(vec3i, __hash__, vec3i__hash__); diff --git a/tests/80_linalg.py b/tests/80_linalg.py index fb29237f..01958c32 100644 --- a/tests/80_linalg.py +++ b/tests/80_linalg.py @@ -388,6 +388,9 @@ assert vec3i(1, 2, 3) * vec3i(4, 5, 6) == vec3i(4, 10, 18) assert vec3i(1, 2, 3) * 2 == vec3i(2, 4, 6) assert vec3i(1, 2, 3).dot(vec3i(4, 5, 6)) == 32 +assert vec2i(3, 5) // 2 == vec2i(1, 2) +assert vec3i(3, 5, 8) // 2 == vec3i(1, 2, 4) + a = {} a[vec2i(1, 2)] = 1 assert a[vec2i(1, 2)] == 1