提交 e25e5b42 authored 作者: Frederic Bastien's avatar Frederic Bastien

better implementation of scalar.Composite.__eq__ and .__hash__. started the…

better implementation of scalar.Composite.__eq__ and .__hash__. started the implementation of Composite pickling(disabled).
上级 7530ff9e
...@@ -1247,12 +1247,15 @@ class Composite(ScalarOp): ...@@ -1247,12 +1247,15 @@ class Composite(ScalarOp):
self.nin = len(inputs) self.nin = len(inputs)
self.nout = len(outputs) self.nout = len(outputs)
self.env = env self.env = env
self.inputs_type = tuple([input.type for input in self.env.inputs])
self.outputs_type = tuple([output.type for output in self.env.outputs])
self._rehash()
def output_types(self, input_types): def output_types(self, input_types):
if tuple(input_types) != tuple([input.type for input in self.env.inputs]): if tuple(input_types) != self.inputs_type:
raise TypeError("Wrong types for Composite. Expected %s, got %s." raise TypeError("Wrong types for Composite. Expected %s, got %s."
% (tuple([input.type for input in self.env.inputs]), tuple(input_types))) % (self.inputs_type, tuple(input_types)))
return [output.type for output in self.env.outputs] return self.outputs_type
def perform(self, node, inputs, output_storage): def perform(self, node, inputs, output_storage):
for storage, impl in zip(output_storage, self._impls): for storage, impl in zip(output_storage, self._impls):
...@@ -1281,7 +1284,28 @@ class Composite(ScalarOp): ...@@ -1281,7 +1284,28 @@ class Composite(ScalarOp):
return self._c_code % d return self._c_code % d
def __eq__(self, other): def __eq__(self, other):
return self is other if self is other: return True
if not isinstance(other, self.__class__): return False
if self.nin!=other.nin or self.nout != other.nout: return False
return self._hashval == other._hashval
return self._cmodule_key == other._cmodule_key
def _rehash(self):
#TODO: What no_recycling is used for? What I need to put their?
# no_recycling = []
self._cmodule_key = gof.CLinker.cmodule_key_(self.env, [])
self._hashval = hash(self._cmodule_key)
def __hash__(self): def __hash__(self):
return id(self) return self._hashval
# def __getstate__(self):
# d = copy(self.__dict__)
# d.pop('env')
# d.pop('_impls')
# #TODO: the self._impls must be restored to allow the perform to work.(c version continue to work.
# return d
# def __setstate__(self, d):
# self.__dict__.update(d)
# #TODO: how to restore the _impls?
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论