hi there,
attached is a simple cy_stl.pyx file (together with its setup.py companion)
really just to get started :)
to test:
$ python -c 'import cy_stl as cc; cc.test()'
hth,
sebastien.
> 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#e361c4c0256ec6c7
> 4
>
> > 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
>
--
#########################################
# Dr. Sebastien Binet
# Laboratoire de l'Accelerateur Lineaire
# Universite Paris-Sud XI
# Batiment 200
# 91898 Orsay
#########################################
cdef extern from "libcalg/hash-pointer.h":
unsigned long c_pointer_hash "pointer_hash"(void* location)
cdef extern from "libcalg/compare-pointer.h":
int c_pointer_equal "pointer_equal" (void* loc1, void* loc2)
int c_pointer_compare "pointer_compare" (void* loc1, void* loc2)
cdef extern from "libcalg/hash-table.h":
ctypedef struct c_HashTable "HashTable"
ctypedef void *HashTableKey
ctypedef void *HashTableValue
ctypedef unsigned long (*HashTableHashFunc)(HashTableKey value)
ctypedef unsigned long (*HashTableEqualFunc)(HashTableKey val1,
HashTableKey val2)
c_HashTable *hash_table_new(HashTableHashFunc hash_func,
HashTableEqualFunc equal_func)
void hash_table_free(c_HashTable* ht)
int hash_table_insert(c_HashTable* self, HashTableKey k, HashTableValue v)
HashTableValue hash_table_lookup(c_HashTable* self, HashTableKey k)
int hash_table_remove(c_HashTable* self, HashTableKey k)
int hash_table_num_entries(c_HashTable* self)
cdef inline unsigned long c_ptr_hash_func(HashTableKey value):
# FIXME: use intptr_t
return <unsigned long>(value)
cdef inline unsigned long c_ptr_hash_equal(HashTableKey val1,
HashTableKey val2):
return (<void*>val1) == (<void*>val2)
cdef class HashTable:
cdef c_HashTable *_base
def __cinit__(self):
self._base = hash_table_new((<HashTableHashFunc>&c_ptr_hash_func),
(<HashTableEqualFunc>&c_ptr_hash_equal))
def __dealloc__(self):
hash_table_free(self._base)
cdef int insert(self, HashTableKey k, HashTableValue v):
return hash_table_insert(self._base, k, v)
cdef HashTableValue lookup(self, HashTableKey k):
return hash_table_lookup(self._base, k)
cdef int remove(self, HashTableKey k):
return hash_table_remove(self._base, k)
cdef int num_entries(self):
return hash_table_num_entries(self._base)
def test():
cdef HashTable ht = HashTable()
cdef char* k1 = "k"
cdef int* v1 = [666]
print "==> [%s]" % ht.num_entries()
ht.insert(<void*>k1, <void*>v1)
print "==> [%s]" % ht.num_entries()
cdef HashTableValue vv1 = ht.lookup(k1)
print "ht[%s]==[%s]" % (<char*>k1, (<int*>vv1)[0])
cdef char* k2 = "cy"
cdef int* v2 = [42]
print "==> [%s]" % ht.num_entries()
ht.insert(<void*>k2, <void*>v2)
print "==> [%s]" % ht.num_entries()
cdef HashTableValue vv2 = ht.lookup(k2)
print "ht[%s]==[%s]" % (<char*>k2, (<int*>vv2)[0])
#!/usr/bin/env python
# python setup.py build_ext --inplace
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext = Extension(
"cy_stl",
["cy_stl.pyx"],
language="c",
include_dirs=['/usr/include/libcalg-1.0'],
library_dirs=['/usr/lib'],
libraries=['calg'],
cmdclass={'build_ext': build_ext}
)
setup(
cmdclass={'build_ext': build_ext},
ext_modules=[ext]
)
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev