"Tuples" of fused types

16 views
Skip to first unread message

Bernie Roesler

unread,
Nov 4, 2025, 1:04:00 PMNov 4
to cython-users
I have the following fused types and function:

ctypedef fused index_t:
    int32_t
    int64_t

ctypedef fused common_t:
    klu_common
    klu_l_common

ctypedef fused symbolic_t:
    klu_symbolic
    klu_l_symbolic

cdef int _copy_symbolic(
    symbolic_t* dest,
    const symbolic_t* src,
    const common_t* cm,
    index_t _dummy=0
) except -1:
    """Deep copy a KLU symbolic struct."""
    if src is NULL or dest is NULL:
        raise ValueError("Source and destination pointers must not be NULL.")

    # Copy the top-level data and pointers
    memcpy(dest, src, sizeof(symbolic_t))

    cdef size_t n = src.n

    # Deep copy internal arrays
    dest.Lnz = <double*>_malloc_copy(src.Lnz, n, sizeof(double), cm)
    dest.P = <index_t*>_malloc_copy(src.P, n, sizeof(index_t), cm)
    dest.Q = <index_t*>_malloc_copy(src.Q, n, sizeof(index_t), cm)
    dest.R = <index_t*>_malloc_copy(src.R, n + 1, sizeof(index_t), cm)

    return 0

It does not compile, because it tries to make all combinations of the types, but the problem is the "klu_symbolic" only goes with "int32_t", and "klu_l_symbolic" with "int64_t", etc. Is there a way that I can tell the compiler a "tuple" of allowed combinations of fused types? such that "(klu_symbolic, klu_common, int32_t)" and "(klu_l_symbolic, klu_l_common, int64_t)" are the only compiled versions?

copy/pasting this function isn't all that bad, but I have a much longer version for "klu_numeric" that also allows for either double or double complex, so has 4 versions.

da-woods

unread,
Nov 5, 2025, 3:35:28 PM (13 days ago) Nov 5
to cython...@googlegroups.com

You might be able to do something like

    # at the start of the function
    if index_t is int32_t and common_t is not klu_common:
        assert False
        return

Whether that works or not depends on exactly when the dead-code elimination happens for fused functions (and whether that happens before or after your compile error). But it's probably worth a try.

I don't know another way to do it off the top of my head.

David

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/cython-users/83b568ef-58f2-49da-b411-3558ea81e442n%40googlegroups.com.

Bernie Roesler

unread,
Nov 7, 2025, 1:03:01 AM (12 days ago) Nov 7
to cython...@googlegroups.com
That works! Thanks.

You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/aPPa_4QGzyI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/cython-users/a29c2e30-39e2-4d16-ae2b-6415705ed79e%40d-woods.co.uk.

Reply all
Reply to author
Forward
0 new messages