From e9af853510a3c527f34f950733a1dd4b428787ac Mon Sep 17 00:00:00 2001 From: Ondrej Certik Date: Mon, 11 Aug 2008 20:44:03 +0200 Subject: [PATCH] use python hash+eq. --- csympy.pyx | 19 +++++++++++++++++-- t.py | 8 ++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/csympy.pyx b/csympy.pyx index ca7837b..74b79ce 100644 --- a/csympy.pyx +++ b/csympy.pyx @@ -39,11 +39,11 @@ def int_hash(int v): def str_hash(char *v): return g_str_hash(v) -cdef class HashTable: +cdef class Table: cdef GHashTable *thisptr def __init__(self): - self.thisptr = g_hash_table_new(&g_int_hash, NULL) + self.thisptr = g_hash_table_new(NULL, NULL) def __dealloc__(self): g_hash_table_destroy(self.thisptr) @@ -61,3 +61,18 @@ cdef class HashTable: while g_hash_table_iter_next(&iter, &key, &value): a.append((key, value)) return a + +cdef guint my_hash(gconstpointer v): + o= v + return hash(o) + +cdef gboolean my_equal(gconstpointer a, gconstpointer b): + o1 = a + o2 = b + return o1 == o2 + + +cdef class HashTable(Table): + + def __init__(self): + self.thisptr = g_hash_table_new(&my_hash, &my_equal) diff --git a/t.py b/t.py index 003b698..2ecc628 100755 --- a/t.py +++ b/t.py @@ -1,6 +1,6 @@ #! /usr/bin/env python -from csympy import int_hash, str_hash, HashTable +from csympy import int_hash, str_hash, HashTable, Table print int_hash(5) print int_hash(6) @@ -18,12 +18,12 @@ print "hash" h = HashTable() h.insert(1, 2) h.insert(3, 5) -h.insert(2, 6) +h.insert(2, "ano") print h.list() print "hash" h = HashTable() -h.insert(1, 2) h.insert(3, 5) -h.insert(2, 6) +h.insert(2, "ano") +h.insert(1, 2) print h.list() -- 2.11.4.GIT