Pārlūkot izejas kodu

add `vec2i.__floordiv__`

blueloveTH 1 gadu atpakaļ
vecāks
revīzija
dc8df160d6
3 mainītis faili ar 16 papildinājumiem un 0 dzēšanām
  1. 1 0
      include/typings/linalg.pyi
  2. 12 0
      src/modules/linalg.c
  3. 3 0
      tests/80_linalg.py

+ 1 - 0
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: ...
 

+ 12 - 0
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__);

+ 3 - 0
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