From 1d481414a2fa8505a2591c88e2b7b8f86a682ca2 Mon Sep 17 00:00:00 2001 From: Victor Chahuneau Date: Fri, 27 Jul 2012 22:25:15 -0400 Subject: [python] conversion from cdec.sa.Rule to cdec.TRule + remove configobj dependency + re-structure packages (no more top-level library) + "const" stuff + use __new__ instead of constructor for some objects --- python/src/vectors.pxi | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'python/src/vectors.pxi') diff --git a/python/src/vectors.pxi b/python/src/vectors.pxi index cd1c2598..989a6a7c 100644 --- a/python/src/vectors.pxi +++ b/python/src/vectors.pxi @@ -1,7 +1,16 @@ from cython.operator cimport preincrement as pinc cdef class DenseVector: - cdef vector[weight_t]* vector # Not owned by DenseVector + cdef vector[weight_t]* vector + cdef bint owned # if True, do not manage memory + + def __init__(self): + self.vector = new vector[weight_t]() + self.owned = False + + def __dealloc__(self): + if not self.owned: + del self.vector def __len__(self): return self.vector.size() @@ -28,7 +37,7 @@ cdef class DenseVector: return other.dot(self) def tosparse(self): - cdef SparseVector sparse = SparseVector() + cdef SparseVector sparse = SparseVector.__new__(SparseVector) sparse.vector = new FastSparseVector[weight_t]() InitSparseVector(self.vector[0], sparse.vector) return sparse @@ -36,6 +45,9 @@ cdef class DenseVector: cdef class SparseVector: cdef FastSparseVector[weight_t]* vector + def __init__(self): + self.vector = new FastSparseVector[weight_t]() + def __dealloc__(self): del self.vector @@ -83,7 +95,7 @@ cdef class SparseVector: return self.vector.nonzero(FDConvert(fname)) def __neg__(self): - cdef SparseVector result = SparseVector() + cdef SparseVector result = SparseVector.__new__(SparseVector) result.vector = new FastSparseVector[weight_t](self.vector[0]) result.vector[0] *= -1.0 return result @@ -105,12 +117,12 @@ cdef class SparseVector: return self def __add__(SparseVector x, SparseVector y): - cdef SparseVector result = SparseVector() + cdef SparseVector result = SparseVector.__new__(SparseVector) result.vector = new FastSparseVector[weight_t](x.vector[0] + y.vector[0]) return result def __sub__(SparseVector x, SparseVector y): - cdef SparseVector result = SparseVector() + cdef SparseVector result = SparseVector.__new__(SparseVector) result.vector = new FastSparseVector[weight_t](x.vector[0] - y.vector[0]) return result @@ -119,7 +131,7 @@ cdef class SparseVector: cdef float scalar if isinstance(x, SparseVector): vector, scalar = x, y else: vector, scalar = y, x - cdef SparseVector result = SparseVector() + cdef SparseVector result = SparseVector.__new__(SparseVector) result.vector = new FastSparseVector[weight_t](vector.vector[0] * scalar) return result @@ -128,6 +140,6 @@ cdef class SparseVector: cdef float scalar if isinstance(x, SparseVector): vector, scalar = x, y else: vector, scalar = y, x - cdef SparseVector result = SparseVector() + cdef SparseVector result = SparseVector.__new__(SparseVector) result.vector = new FastSparseVector[weight_t](vector.vector[0] / scalar) return result -- cgit v1.2.3