summaryrefslogtreecommitdiffstats
path: root/src/sage/structure/richcmp.pxd
diff options
context:
space:
mode:
Diffstat (limited to 'src/sage/structure/richcmp.pxd')
-rw-r--r--src/sage/structure/richcmp.pxd146
1 files changed, 146 insertions, 0 deletions
diff --git a/src/sage/structure/richcmp.pxd b/src/sage/structure/richcmp.pxd
new file mode 100644
index 00000000..ac7a6d4
--- /dev/null
+++ b/src/sage/structure/richcmp.pxd
@@ -0,0 +1,146 @@
+from libc.stdint cimport uint32_t
+from cpython.object cimport Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, Py_GE
+from cpython.object cimport PyObject_RichCompare as richcmp
+
+
+cpdef inline richcmp_not_equal(x, y, int op):
+ """
+ Like ``richcmp(x, y, op)`` but assuming that `x` is not equal to `y`.
+
+ INPUT:
+
+ - ``op`` -- a rich comparison operation (e.g. ``Py_EQ``)
+
+ OUTPUT:
+
+ If ``op`` is not ``op_EQ`` or ``op_NE``, the result of
+ ``richcmp(x, y, op)``. If ``op`` is ``op_EQ``, return
+ ``False``. If ``op`` is ``op_NE``, return ``True``.
+
+ This is useful to compare lazily two objects A and B according to 2
+ (or more) different parameters, say width and height for example.
+ One could use::
+
+ return richcmp((A.width(), A.height()), (B.width(), B.height()), op)
+
+ but this will compute both width and height in all cases, even if
+ A.width() and B.width() are enough to decide the comparison.
+
+ Instead one can do::
+
+ wA = A.width()
+ wB = B.width()
+ if wA != wB:
+ return richcmp_not_equal(wA, wB, op)
+ return richcmp(A.height(), B.height(), op)
+
+ The difference with ``richcmp`` is that ``richcmp_not_equal``
+ assumes that its arguments are not equal, which is excluding the case
+ where the comparison cannot be decided so far, without
+ knowing the rest of the parameters.
+
+ EXAMPLES::
+
+ sage: from sage.structure.richcmp import (richcmp_not_equal,
+ ....: op_EQ, op_NE, op_LT, op_LE, op_GT, op_GE)
+ sage: for op in (op_LT, op_LE, op_EQ, op_NE, op_GT, op_GE):