use python hash+eq.
[sympyx.git] / csympy.pyx
blob74b79ce02f99ca8e230a4905d9902ef9de7a3c7e
1 cdef extern from "stdlib.h":
2 ctypedef int size_t
3 void *malloc (size_t size)
5 cdef extern from "Python.h":
6 ctypedef void PyObject
8 cdef extern from "stdint.h":
9 ctypedef long long int64_t
10 ctypedef int64_t sympyint
12 cdef extern from "glib.h":
13 ctypedef unsigned guint
14 ctypedef void *gconstpointer
15 ctypedef void *gpointer
16 ctypedef int gboolean
17 guint g_int_hash(gconstpointer v)
18 guint g_str_hash(gconstpointer v)
20 ctypedef struct GHashTable
21 ctypedef struct GHashTableIter:
22 pass
23 #guint (*GHashFunc) (gconstpointer key)
24 #gboolean (*GEqualFunc) (gconstpointer a, gconstpointer b)
25 ctypedef void * GHashFunc
26 ctypedef void * GEqualFunc
28 GHashTable *g_hash_table_new(GHashFunc hash_func, GEqualFunc key_equal_func)
29 void g_hash_table_insert(GHashTable *hash_table,
30 gpointer key, gpointer value)
31 void g_hash_table_destroy(GHashTable *hash_table)
32 void g_hash_table_iter_init(GHashTableIter *iter, GHashTable *hash_table)
33 gboolean g_hash_table_iter_next(GHashTableIter *iter,
34 gpointer *key, gpointer *value)
36 def int_hash(int v):
37 return g_int_hash(&v)
39 def str_hash(char *v):
40 return g_str_hash(v)
42 cdef class Table:
43 cdef GHashTable *thisptr
45 def __init__(self):
46 self.thisptr = g_hash_table_new(NULL, NULL)
48 def __dealloc__(self):
49 g_hash_table_destroy(self.thisptr)
51 def insert(self, key, coeff):
52 g_hash_table_insert(self.thisptr, <gpointer>key, <gpointer>coeff)
54 def list(self):
55 cdef GHashTableIter iter
57 g_hash_table_iter_init (&iter, self.thisptr);
58 cdef gpointer key, value
60 a = []
61 while g_hash_table_iter_next(&iter, &key, &value):
62 a.append((<object>key, <object>value))
63 return a
65 cdef guint my_hash(gconstpointer v):
66 o= <object>v
67 return hash(o)
69 cdef gboolean my_equal(gconstpointer a, gconstpointer b):
70 o1 = <object>a
71 o2 = <object>b
72 return o1 == o2
75 cdef class HashTable(Table):
77 def __init__(self):
78 self.thisptr = g_hash_table_new(&my_hash, &my_equal)