summaryrefslogtreecommitdiff
path: root/python/src/sa/float_list.pxi
diff options
context:
space:
mode:
authorVictor Chahuneau <vchahune@cs.cmu.edu>2012-07-27 01:16:03 -0400
committerVictor Chahuneau <vchahune@cs.cmu.edu>2012-07-27 01:16:03 -0400
commit8fdc3681fb7551e7faeff9f720102cdd417ba077 (patch)
tree1129d2b79a3255c249e181141814cb92b52b4d4d /python/src/sa/float_list.pxi
parent0aac9fd78f1c8b9ba3d91d702f592288075cbbde (diff)
[python] Fork of the suffix-array extractor with surface improvements
Available as the cdec.sa module, with commande-line helpers: python -m cdec.sa.compile -f ... -e ... -a ... -o sa-out/ -c extract.ini python -m cdec.sa.extract -c extract.ini -g grammars-out/ < input.txt > input.sgml + renamed cdec.scfg -> cdec.sa + Python README
Diffstat (limited to 'python/src/sa/float_list.pxi')
-rw-r--r--python/src/sa/float_list.pxi77
1 files changed, 77 insertions, 0 deletions
diff --git a/python/src/sa/float_list.pxi b/python/src/sa/float_list.pxi
new file mode 100644
index 00000000..88ffd5fc
--- /dev/null
+++ b/python/src/sa/float_list.pxi
@@ -0,0 +1,77 @@
+# defines int arrays in C, with some convenience methods
+# for reading arrays directly as globs directly from disk.
+# Adam Lopez <alopez@cs.umd.edu>
+
+from libc.stdio cimport FILE, fopen, fread, fwrite, fclose
+from libc.stdlib cimport malloc, realloc, free
+from libc.string cimport memset, strcpy, strlen
+
+cdef class FloatList:
+ cdef int size
+ cdef int increment
+ cdef int len
+ cdef float* arr
+
+ def __cinit__(self, int size=0, int increment=1, int initial_len=0):
+ if initial_len > size:
+ size = initial_len
+ self.size = size
+ self.increment = increment
+ self.len = initial_len
+ self.arr = <float*> malloc(size*sizeof(float))
+ memset(self.arr, 0, initial_len*sizeof(float))
+
+ def __dealloc__(self):
+ free(self.arr)
+
+ def __getitem__(self, i):
+ j = i
+ if i<0:
+ j = self.len + i
+ if j<0 or j>=self.len:
+ raise IndexError("Requested index %d of %d-length FloatList" % (i, self.len))
+ return self.arr[j]
+
+ cdef void set(self, int i, float v):
+ j = i
+ if i<0:
+ j = self.len + i
+ if j<0 or j>=self.len:
+ raise IndexError("Requested index %d of %d-length FloatList" % (i, self.len))
+ self.arr[j] = v
+
+ def __setitem__(self, i, val):
+ self.set(i, val)
+
+ def __len__(self):
+ return self.len
+
+ def append(self, float val):
+ if self.len == self.size:
+ self.size = self.size + self.increment
+ self.arr = <float*> realloc(self.arr, self.size*sizeof(float))
+ self.arr[self.len] = val
+ self.len = self.len + 1
+
+ cdef void write_handle(self, FILE* f):
+ fwrite(&(self.len), sizeof(float), 1, f)
+ fwrite(self.arr, sizeof(float), self.len, f)
+
+ def write(self, char* filename):
+ cdef FILE* f
+ f = fopen(filename, "w")
+ self.write_handle(f)
+ fclose(f)
+
+ cdef void read_handle(self, FILE* f):
+ free(self.arr)
+ fread(&(self.len), sizeof(float), 1, f)
+ self.arr = <float*> malloc(self.len * sizeof(float))
+ self.size = self.len
+ fread(self.arr, sizeof(float), self.len, f)
+
+ def read(self, char* filename):
+ cdef FILE* f
+ f = fopen(filename, "r")
+ self.read_handle(f)
+ fclose(f)