summaryrefslogtreecommitdiff
path: root/python/src/vectors.pxi
diff options
context:
space:
mode:
authorVictor Chahuneau <vchahune@cs.cmu.edu>2012-07-27 22:25:15 -0400
committerVictor Chahuneau <vchahune@cs.cmu.edu>2012-07-27 22:25:15 -0400
commit9961abf8f756279ac6d839e0b3de2b0d83431965 (patch)
tree2defad3f49df2921ff68a48a07a9ca0693a1f0f7 /python/src/vectors.pxi
parent733e1b1507d27d4f53055f740e8098f56215ab8f (diff)
[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
Diffstat (limited to 'python/src/vectors.pxi')
-rw-r--r--python/src/vectors.pxi26
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