Permalink
Browse files

Add string method to perfect hashing vtable

  • Loading branch information...
1 parent 7bc2663 commit 9e83a792345ce53c99a627e6a20ba22bae23218e @markflorisson committed Apr 3, 2013
Showing with 26 additions and 3 deletions.
  1. +26 −3 extensibletype/methodtable.pyx
@@ -64,10 +64,14 @@ cdef class PerfectHashMethodTable(object):
cdef uint16_t *displacements
cdef Hasher hasher
+ cdef object id_to_signature, signatures
+
def __init__(self, hasher):
self.hasher = hasher
+ # For debugging
+ self.id_to_signature = {}
- def generate_table(self, n, ids, flags, funcs):
+ def generate_table(self, n, ids, flags, funcs, method_names=None):
cdef Py_ssize_t i
cdef cnp.ndarray[uint64_t] hashes
@@ -81,10 +85,13 @@ cdef class PerfectHashMethodTable(object):
# Initialize hash table entries, build hash ids
for i, (signature, flag, func) in enumerate(zip(ids, flags, funcs)):
- self.table.entries[i].id = self.hasher.hash_signature(signature)
+ id = self.hasher.hash_signature(signature)
+
+ self.table.entries[i].id = id
self.table.entries[i].ptr = <void *> <uintptr_t> func
- hashes[i] = self.hasher.hash_signature(signature)
+ hashes[i] = id
+ self.id_to_signature[id] = signature
hashes[n:self.table.n] = extensibletype.draw_hashes(np.random,
self.table.n - n)
@@ -95,6 +102,9 @@ cdef class PerfectHashMethodTable(object):
for signature in ids:
assert self.find_method(signature)
+ # For debugging
+ self.signatures = ids
+
def find_method(self, signature):
"""
Find method of the given signature. Use from non-performance
@@ -113,6 +123,19 @@ cdef class PerfectHashMethodTable(object):
return (<uintptr_t> self.table.entries[idx].ptr,
self.table.entries[idx].id & 0xFF)
+ def __str__(self):
+ buf = ["PerfectHashMethodTable("]
+ for i in range(self.table.n):
+ id = self.table.entries[i].id
+ ptr = <uintptr_t> self.table.entries[i].ptr
+ sig = self.id_to_signature.get(id, "<empty>")
+ s = " id: %20d funcptr: %20d signature: %s" % (id, ptr, sig)
+ buf.append(s)
+
+ buf.append(")")
+
+ return "\n".join(buf)
+
def __dealloc__(self):
# stdlib.free(self.table)
# self.table = NULL

0 comments on commit 9e83a79

Please sign in to comment.