diff options
author | Chris Dyer <cdyer@cs.cmu.edu> | 2012-07-28 12:11:44 -0400 |
---|---|---|
committer | Chris Dyer <cdyer@cs.cmu.edu> | 2012-07-28 12:11:44 -0400 |
commit | 306e0ba4754c6c4f460536cfe8c3f118dc1cc175 (patch) | |
tree | ad5ea3b0a5370ac613d1bad715fe0f5ab8c91c11 /python/src/vectors.pxi | |
parent | 934e55dc12c3f374684bc6a0797e6f85c7abb85a (diff) | |
parent | ee5e376e263d9aeabdeee6968b4457f53d3fc772 (diff) |
Merge branch 'master' of github.com:redpony/cdec
Diffstat (limited to 'python/src/vectors.pxi')
-rw-r--r-- | python/src/vectors.pxi | 26 |
1 files changed, 19 insertions, 7 deletions
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 |