Ok, I now have this:

cythonHash.pxd:

cdef extern from "hash-table.h":
    ctypedef struct HashTable
    ctypedef void *HashTableKey
    ctypedef unsigned long HashTableHashFunc(HashTableKey value)
    ctypedef unsigned long HashTableEqualFunc(HashTableKey value)
    HashTable *hash_table_new(HashTableHashFunc hash_func,
HashTableEqualFunc equal_func)
    
cdef inline unsigned long c_hash_func(HashTableKey value):
    return 1
        
cdef inline unsigned long c_hash_equal(HashTableKey value):
    return 1


cythonPT.pyx:

cimport cythonHash
from cythonHash cimport HashTable

class MY_Phrase_Table(object):

    def __init__(self):
    
        pp = HashTable          #error here
      print type(pp), pp        

This yields in the following error: 'HashTable' is not a constant, variable
or function identifier.

I don't really get how I should reference to Hashtable. I thought it was
already declared from .pxd and the original .c file.




-----Original Message-----
From: Stefan Behnel [mailto:[email protected]] 
Sent: vrijdag 4 september 2009 16:03
To: [email protected]
Cc: [email protected]
Subject: Re: [Cython] FW: cython and hash tables / dictionary


Sanne Korzec wrote:
> In the documentation
>
http://c-algorithms.sourceforge.net/doc/hash-table_8h.html#e361c4c0256ec6c74
> 1ecfeabef33d891 , I can find:
> 
> HashTable* hash_table_new     (       HashTableHashFunc       hash_func,
>               HashTableEqualFunc      equal_func       
>       )                       
> 
> To create a new hash table. But I can't find were the HashTableHashFunc
and
> HashTableEqualFunc are declared. The only thing I can find is in the
header
> file which state:
> 
> typedef unsigned long(* HashTableHashFunc)(HashTableKey value)
> typedef unsigned long(* HashTableHashFunc)(HashTableKey value)
> 
> Does this mean I have to write these functions myself?

Yes.

> In c?

You can write them in Cython:

    cdef unsigned long c_hash(HashTableKey value):
        return huge_calculation_on(value)

> And how then do I call them from cython?

You don't. Instead, you pass the function names (i.e. pointers) into
hash_table_new().


> My guess: 
> 
> hashtable.pyx

"hashtable.pxd", I assume?


> cdef extern from "hash_table.h":
> 
>       object HashTable hash_table_new(object hash_func, object equal_func)

That won't work. You can't use Python functions as their signature won't
match the required signatures. Instead, define HashTable as a struct and
the functions as a ctypedef.

Stefan


_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to