%module index %include cstring.i %include exception.i %apply (char *STRING, int LENGTH) { (char *str, int length) }; %cstring_output_allocate_size(char **s, int *slen, free(*$1)); %{ #include "except.h" #include "index.h" /* This function returns the offset table as raw data. This is then used by the python code to directly work with the raw data. This is a bit of a hack, but it allows passing vast numbers of offsets from the c layer to the python layer with only a single transition call. */ void get_offset_table(struct indexing_trie *trie,char **s, int *slen) { *slen=trie->list->last * sizeof(struct offset); *s = (char *)malloc(*slen); memcpy(*s,trie->list->offsets,*slen); // printf("Allocating %u bytes for %u elements\n",*slen,trie->list->last); }; %} %pythoncode %{ import struct class offsets: """ An iterator to iterate over all the offsets """ ## The format of the offset struct fmt = "@ii" def __init__(self,data): """ Data is the raw data as passed by get_offset_table """ self.data = data ## This is the size of each offset struct self.size = struct.calcsize(self.fmt) self.element=0 def __iter__(self): return self def next(self): # print "Getting %s element (%s)" % (self.element,len(self.data)) try: s=self.data[self.element:self.element+self.size] if len(s)