I want to add hash functionality to unordered_map but having trouble understanding the semantics in cython
I have the following definition
cdef extern from * using namespace "std":
"""
struct pair_hash {
template <class T1, class T2>
std::size_t operator () (const std::pair<T1,T2> &p) const {
auto h1 = std::hash<T1>{}(p.first);
auto h2 = std::hash<T2>{}(p.second);
return h1 ^ h2;
}
};
"""
cdef cppclass pair_hash:
pass
cdef cppclass unordered_map[T, U, H]:
pass
I have browsed around the unordered_map code to see how bindings is done.
My question is how to approach these bindings in general?
So far I have deduced that mapping would involve
Correct name space
tell cython what the object is through cppclass
provide method names to be bound
Here, however I need to bind a template and I am a little lost on how to approach this.
Related
I want to use a C++ shared_ptr as a replacement for raw C pointers. As a simple example the following code seems to work as intended:
from libcpp.memory cimport shared_ptr, allocator
cdef shared_ptr[double] spd
cdef allocator[double] allo
spd.reset(allo.allocate(13))
The size is chosen as 13 here, but in general is not know at compile time.
I'm not sure if this is correct, but I haven't had any errors (no memory leaks and segfaults yet). I'm curious if there is a more optimal solution with make_shared.
But I can't use C++11 arrays because Cython doesn't allow literals as templates, e.g. something like
cdef shared_ptr[array[double]] spd = make_shared[array[double,13]]()
and "normal" arrays which are supposed to work with C++20 compiler (e.g. gcc 10) are causing problems in one way or another:
# Cython error "Expected an identifier or literal"
cdef shared_ptr[double[]] spd = make_shared[double[]](3)
# Can't get ptr to work
ctypedef double[] darr
cdef shared_ptr[darr] spd = make_shared[darr](13)
cdef double* ptr = spd.get() # or spd.get()[0] or <double*> spd.get()[0] or ...
Is the allocator solution the correct and best one or is there another way how to do it?
Here is what I'm going with
cdef extern from *:
"""
template <typename T>
struct Ptr_deleter{
size_t nn;
void (*free_ptr)(T*, size_t);
Ptr_deleter(size_t nIn, void (*free_ptrIn)(T*, size_t)){
this->nn = nIn;
this->free_ptr = free_ptrIn;
};
void operator()(T* ptr){
free_ptr(ptr, nn);
};
};
template <typename T>
std::shared_ptr<T> ptr_to_sptr (T* ptr, size_t nn, void (*free_ptr)(T*, size_t)) {
Ptr_deleter dltr(nn, free_ptr);
std::shared_ptr<T> sp (ptr, dltr);
return sp;
};
"""
shared_ptr[double] d_ptr_to_sptr "ptr_to_sptr"(double* ptr, size_t nn, void (*free_ptr)(double*, size_t) nogil) nogil
cdef void free_d_ptr(double* ptr, size_t nn) nogil:
free(ptr)
cdef shared_ptr[double] sp_d_empty(size_t nn) nogil:
return d_ptr_to_sptr(<double*> nullCheckMalloc(nn*sizeof(double)), nn, &free_d_ptr)
My understanding is that the "right" way to handle malloced arrays is to use a custom deleter like I did. I personally prefer sticking with somewhat-raw C pointers (double* instead of double[] or something), since it's more natural in Cython and my projects.
I think it's reasonably easy to see how to change free_ptr for more complicated data types. For simple data types it could be done in less lines and less convoluted, but I wanted to have the same base.
I like my solution in the regard that I can just "wrap" existing Cython/C code raw pointers in a shared_ptr.
When working with C++ (especially newer standards like C++20) I think verbatim code is pretty often necessary. But I've intentionally defined free_d_ptr in Cython, so it's easy to use existing Cython code to handle the actual work done to free/clear/whatever the array.
I didn't get C++11 std::arrays to work, and it's apparently not "properly" possible in Cython in general (see Interfacing C++11 array with Cython).
I didn't get double[] or similar to work either (is possible in C++20), but with verbatim C++ code I think this should be doable in Cython. I prefer more C-like pointers/arrays anyway as I said.
How can I tell Cython that my C++ class has overloaded operator=? I tried:
cdef extern from "my_source.H":
cdef cppclass MyStatus:
void operator=(const char* status)
cdef public void setStatus(MyStatus& status):
status = "FOO"
but Cython either complains "Assignment to reference status" or (if I make status a non-reference) constructs a python object out of the string "FOO" and then tries to assign the python object to status.
The problem in your code is, that for Cython "FOO" is a Python-object. For expressions like
char *s = "FOO"
Cython is clever enough to understand, what you want and automatically interprets "FOO" as char *.
However, Cython doesn't really "understand"/interpret the signatures of wrapped c++-functions (for that it must be a c++-compiler) and thus cannot know, that you want "FOO" be a char *.
Thus you have to help Cython, for example:
status = <const char *>"FOO"
You also have to work around the problem with reference, for example via:
cdef public void setStatus(MyStatus *status):
status[0] = <const char *>"FOO"
or if you want to have keep the signature of the function intact:
cdef public void setStatus(MyStatus& status):
cdef MyStatus * as_ptr = &status
as_ptr[0] = <const char *>"FOO"
I'm not completely sure the problem with the assigment to reference isn't a bug.
Another observation: the assigment operators aren't part of the "official" wrap of the standard containers, see here or here.
I am working on wrapping a C library using cython. I would like to use a cdef class to wrap a simple struct. The struct does require some parameters to be properly initialized. I put the corresponding code int the __cinit__ method like so:
cdef class Func:
cdef library.Func* func
def __cinit__(self, int value):
library.init_func(&self.func, value)
In python code I can create a function object via f = Func(17). The code then handles the initialization just fine. My question is the following: Say I want to extend the Func class in python (class MyFunc(Func): ...). How do I add a constructor to to MyFunc?
I would like to write a constructor with other parameters which calls
__cinit__(self, int value) with a value derived from constructor parameters. But it does not seem to be possible (likely for good reason) to call __cinit__ from python code.
Is there some way to include constructors in subclasses or should I create a wrapper around the function and delegate methods / properties?
Using Cython to wrap some parts from a C++ API to python, I'm having misunderstandings that I couldn't fix by search similar questions. I like to access a Factory class that has the constructors private and a public method GetInstance.
namespace cpplibrary
{
class CppFactory
{
public:
static CppFactory& GetInstance();
private:
CppFactory(void);
CppFactory( const CppFactory& );
};
}
I'm trying a cython code like:
cdef extern from "cppFactory.h" namespace "cpplibrary":
cdef cppclass CppFactory:
CppFactory() except +
CppFactory& GetInstance()
cdef class PyFactory:
cdef CppFactory* _thisptr
def __cinit__(self):
self._thisptr = GetInstance()
I've check with as many variants as I thought. With and without the declaration of the constructor in the extern. With different ways to define the _thisptr from different posted examples. And so on. But I couldn't find a singleton example like this.
Where is the mistake I can't see?
I think you've got two issues:
1) How to wrap static methods: the recommended way is to declare the function outside the class, and use a string to tell Cython what name it should use in C++
2) The assignment to thisptr, where you just need to use & to get a pointer from the reference.
Code:
cdef extern from "cppFactory.h" namespace "cpplibrary":
cdef cppclass CppFactory:
CppFactory() except +
# declare outside the class and use a string to specify name
# unfortunately this doesn't seem to play well with namespace
# so we need to specify it again
cdef CppFactory& CppFactory_GetInstance "cpplibrary::CppFactory::GetInstance"()
cdef class PyFactory:
cdef CppFactory* _thisptr
def __cinit__(self):
self._thisptr = &CppFactory_GetInstance()
One of the problem is indeed to wrap a static method (here Getinstance()).
You can do that by using the cython #staticmethod decorator (see Cython's doc here):
cdef extern from "cppFactory.h" namespace "cpplibrary":
cdef cppclass CppFactory:
#staticmember
CppFactory& GetInstance()
Then, the .pyx can be:
cdef class PyFactory:
cdef CppFactory* _thisptr
def __cinit__(self):
self._thisptr = new CppFactory()
I wish to implement python wrapper for a bunch of c++ classes. Somewhere in pxd I have:
cdef cppclass FooImpl1:
FooImpl1()
int foo()
cdef cppclass FooImpl2
FooImpl2()
int foo()
I wonder if I can write something like this in pyx python wrapper:
ctypedef fused FooImpl:
FooImpl1*
FooImpl2*
cdef class Foo:
cdef FooImpl impl
def __cinit__(self, int selector):
if selector == 1:
self.impl = new FooImpl1()
else:
self.impl = new FooImpl2()
def func(self):
# depending on the object stored in impl FooImpl2::foo or FooImpl1::foo
# will be called
return self.impl.foo()
Is there a way to accomplish expected behavior? FooImpl1 and FooImpl2 don't share abstract interface, they are template specializations of a class.
As of this version (0.20), Cython doesn't support fused types in classes, only in function parameters and variables. Here are the docs.