1 cdef extern from "stdlib.h":
3 void *malloc
(size_t size
)
5 cdef extern from "Python.h":
7 void Py_INCREF
(PyObject
*x
)
8 void Py_DECREF
(PyObject
*x
)
10 cdef extern from "stdint.h":
11 ctypedef long long int64_t
12 ctypedef int64_t sympyint
14 cdef extern from "glib.h":
15 ctypedef unsigned guint
16 ctypedef void *gconstpointer
17 ctypedef void *gpointer
19 guint g_int_hash
(gconstpointer v
)
20 guint g_str_hash
(gconstpointer v
)
22 ctypedef struct GHashTable
23 ctypedef struct GHashTableIter
:
25 #guint (*GHashFunc) (gconstpointer key)
26 #gboolean (*GEqualFunc) (gconstpointer a, gconstpointer b)
27 ctypedef void * GHashFunc
28 ctypedef void * GEqualFunc
30 GHashTable
*g_hash_table_new
(GHashFunc hash_func
, GEqualFunc key_equal_func
)
31 void g_hash_table_insert
(GHashTable
*hash_table
,
32 gpointer key
, gpointer value
)
33 gpointer g_hash_table_lookup
(GHashTable
*hash_table
, gconstpointer key
)
34 guint g_hash_table_size
(GHashTable
*hash_table
)
35 void g_hash_table_destroy
(GHashTable
*hash_table
)
36 void g_hash_table_iter_init
(GHashTableIter
*iter
, GHashTable
*hash_table
)
37 gboolean g_hash_table_iter_next
(GHashTableIter
*iter
,
38 gpointer
*key
, gpointer
*value
)
43 def str_hash
(char *v
):
47 cdef GHashTable
*thisptr
50 self.thisptr
= g_hash_table_new
(NULL
, NULL
)
52 def __dealloc__
(self):
53 g_hash_table_destroy
(self.thisptr
)
55 def __setitem__
(self, x
, y
):
58 def __getitem__
(self, x
):
61 def __contains__
(self, x
):
69 return g_hash_table_size
(self.thisptr
)
71 def insert
(self, key
, coeff
):
72 g_hash_table_insert
(self.thisptr
, <gpointer
>key
, <gpointer
>coeff
)
73 # Without the following it sometimes segfaults, but maybe the INCREF
74 # should be put at some other place:
75 Py_INCREF
(<PyObject
*>key
)
76 Py_INCREF
(<PyObject
*>coeff
)
80 value
= g_hash_table_lookup
(self.thisptr
, <gconstpointer
>key
)
86 cdef GHashTableIter iter
88 g_hash_table_iter_init
(&iter
, self.thisptr
);
89 cdef gpointer key
, value
92 while g_hash_table_iter_next
(&iter
, &key
, &value
):
93 a
.append
((<object>key
, <object>value
))
96 cdef guint my_hash
(gconstpointer v
):
100 cdef gboolean my_equal
(gconstpointer a
, gconstpointer b
):
106 cdef class HashTable
(Table
):
109 self.thisptr
= g_hash_table_new
(&my_hash
, &my_equal
)