[pypy-svn] r25698 - in pypy/dist/pypy/objspace/constraint: . test
auc at codespeak.net
auc at codespeak.net
Tue Apr 11 15:00:35 CEST 2006
Author: auc
Date: Tue Apr 11 15:00:33 2006
New Revision: 25698
Modified:
pypy/dist/pypy/objspace/constraint/distributor.py
pypy/dist/pypy/objspace/constraint/domain.py
pypy/dist/pypy/objspace/constraint/test/test_distributor.py
Log:
distribute with split distributor
Modified: pypy/dist/pypy/objspace/constraint/distributor.py
==============================================================================
--- pypy/dist/pypy/objspace/constraint/distributor.py (original)
+++ pypy/dist/pypy/objspace/constraint/distributor.py Tue Apr 11 15:00:33 2006
@@ -34,22 +34,20 @@
"""
vars_ = [var for var, dom in w_cs.var_dom.content.items()
if dom.size() > 1]
-
best = vars_[0]
for var in vars_:
if w_cs.var_dom.content[var].size() < w_cs.var_dom.content[best].size():
best = var
-
return best
def w_distribute(self, w_cs, w_choice):
assert isinstance(w_choice, W_IntObject)
- self.distribute(w_cs, self._space.int_w(w_choice))
+ self.distribute(w_cs, self._space.int_w(w_choice) -1)
def distribute(self, w_cs, choice_w):
variable = self.find_distribution_variable(w_cs)
- self._do_distribute(w_cs.var_dom.content[variable],
- choice_w)
+ domain = w_cs.w_dom(variable)
+ self._do_distribute(w_cs, domain, choice_w)
for const in w_cs.dependant_constraints(variable):
w_cs.to_check[const] = True
@@ -75,7 +73,7 @@
# default fanout is 2, see make_naive_distributor
W_Distributor.__init__(self, object_space, fanout_w)
- def _do_distribute(self, domain, choice):
+ def _do_distribute(self, w_cs, domain, choice):
values = domain.get_values()
if choice == 0:
domain.w_remove_values(values[1:])
@@ -102,19 +100,19 @@
# default fanout is 3, see make_split_distributor
W_Distributor.__init__(self, object_space, fanout_w)
- def nb_subdomains(self):
- to_split = self.find_smallest_domain()
- if self.nb_subspaces:
- return min(self.nb_subspaces,
- self.cs.dom(to_split).size())
+ def _subdomains(self, w_cs):
+ """returns the min number of partitions
+ for a domain to be distributed"""
+ to_split = self._find_smallest_domain(w_cs)
+ if self.fanout:
+ return min(self.fanout,
+ w_cs.w_dom(to_split).size())
else:
- return self.cs.dom(to_split).size()
+ return w_cs.w_dom(to_split).size()
-
- def _do_distribute(self, domain, choice):
- nb_subspaces = self.nb_subdomains()
+ def _do_distribute(self, w_cs, domain, choice):
values = domain.get_values()
- nb_elts = max(1, len(values)*1./nb_subspaces)
+ nb_elts = max(1, len(values)*1./self._subdomains(w_cs))
start, end = (int(math.floor(choice * nb_elts)),
int(math.floor((choice + 1) * nb_elts)))
domain.remove_values(values[:start])
Modified: pypy/dist/pypy/objspace/constraint/domain.py
==============================================================================
--- pypy/dist/pypy/objspace/constraint/domain.py (original)
+++ pypy/dist/pypy/objspace/constraint/domain.py Tue Apr 11 15:00:33 2006
@@ -78,9 +78,11 @@
def w_remove_values(self, w_values):
"""Remove values of domain and check for consistency"""
- if self._space.is_true(self._space.gt(self._space.len(w_values),
- self._space.newint(0))) :
- for w_val in w_values.wrappeditems :
+ self.remove_values(w_values.wrappeditems)
+
+ def remove_values(self, values):
+ if len(values) > 0:
+ for w_val in values:
del self._values[w_val]
self._value_removed()
Modified: pypy/dist/pypy/objspace/constraint/test/test_distributor.py
==============================================================================
--- pypy/dist/pypy/objspace/constraint/test/test_distributor.py (original)
+++ pypy/dist/pypy/objspace/constraint/test/test_distributor.py Tue Apr 11 15:00:33 2006
@@ -22,3 +22,17 @@
d = NaiveDistributor()
d.distribute(spc, 2)
assert spc.dom(y) == FiniteDomain([2])
+
+ def test_split_distributor(self):
+ spc = newspace()
+ x = spc.var('x', FiniteDomain([1]))
+ y = spc.var('y', FiniteDomain([1, 2]))
+ z = spc.var('z', FiniteDomain([1, 2, 3]))
+ w = spc.var('w', FiniteDomain([1, 2, 3, 4, 5, 6]))
+ d = SplitDistributor(3)
+ d.distribute(spc, 2)
+ assert spc.dom(y).size() == 1
+ d.distribute(spc, 1)
+ assert spc.dom(z).size() == 1
+ d.distribute(spc, 3)
+ assert spc.dom(w).size() == 2
More information about the Pypy-commit
mailing list