[pypy-svn] r40889 - pypy/dist/pypy/rlib/cslib
auc at codespeak.net
auc at codespeak.net
Wed Mar 21 12:14:49 CET 2007
Author: auc
Date: Wed Mar 21 12:14:48 2007
New Revision: 40889
Modified:
pypy/dist/pypy/rlib/cslib/rdistributor.py
pypy/dist/pypy/rlib/cslib/rdomain.py
pypy/dist/pypy/rlib/cslib/rpropagation.py
Log:
cleanup & one helper for c.space distributors
Modified: pypy/dist/pypy/rlib/cslib/rdistributor.py
==============================================================================
--- pypy/dist/pypy/rlib/cslib/rdistributor.py (original)
+++ pypy/dist/pypy/rlib/cslib/rdistributor.py Wed Mar 21 12:14:48 2007
@@ -45,7 +45,7 @@
doms1 = make_new_domains(domains)
doms2 = make_new_domains(domains)
for modified_domain in self._distribute(doms1,doms2):
- modified_domain._changed = False
+ modified_domain._changed = False
return [doms1,doms2]
class AllOrNothingDistributor(AbstractDistributor):
@@ -53,25 +53,39 @@
The first new domain has a size of one,
and the second has all the other values"""
+ def _distribute_on_choice(self, dom, choice):
+ if choice == 1:
+ dom.remove_values(dom.get_values()[1:])
+ else:
+ dom.remove_value(dom.get_values()[0])
+
def _distribute(self, doms1, doms2):
"""See AbstractDistributor"""
variable = self.find_smallest_domain(doms1)
values = doms1[variable].get_values()
- doms1[variable].remove_values(values[1:])
- doms2[variable].remove_value(values[0])
+ self._distribute_on_choice(doms1[variable], 1)
+ self._distribute_on_choice(doms2[variable], 2)
return [doms1[variable], doms2[variable]]
class DichotomyDistributor(AbstractDistributor):
"""distributes domains by splitting the smallest domain in
two equal parts or as equal as possible."""
-
+
+ def _distribute_on_choice(self, dom, choice):
+ values = dom.get_values()
+ middle = len(values)/2
+ if choice == 1:
+ dom.remove_values(values[:middle])
+ else:
+ dom.remove_values(values[middle:])
+
def _distribute(self, doms1, doms2):
"""See AbstractDistributor"""
variable = self.find_smallest_domain(doms1)
values = doms1[variable].get_values()
middle = len(values)/2
- doms1[variable].remove_values(values[:middle])
- doms2[variable].remove_values(values[middle:])
+ self._distribute_on_choice(doms1[variable], 1)
+ self._distribute_on_choice(doms2[variable], 2)
return [doms1[variable], doms2[variable]]
DefaultDistributor = DichotomyDistributor
Modified: pypy/dist/pypy/rlib/cslib/rdomain.py
==============================================================================
--- pypy/dist/pypy/rlib/cslib/rdomain.py (original)
+++ pypy/dist/pypy/rlib/cslib/rdomain.py Wed Mar 21 12:14:48 2007
@@ -30,7 +30,6 @@
self._value_removed()
def remove_values(self, values):
- assert isinstance(values, list)
if len(values) > 0:
for val in values:
del self._values[val]
@@ -43,50 +42,7 @@
def get_values(self):
return self._values.keys()
-
def __repr__(self):
return "<Domain %s>" % self._values.keys()
-# XXX finish this
-class TodoBaseFiniteDomain:
- """
- Variable Domain with a finite set of int values
- """
-
- def __init__(self, values):
- """values is a list of values in the domain
- This class uses a dictionnary to make sure that there are
- no duplicate values"""
- assert isinstance(values, int)
- self._values = [True] * values
-
- def copy(self):
- dom = BaseFiniteDomain(len(self._values))
- for i, v in enumerate(self._values):
- dom._values[i] = v
-
- def _value_removed(self):
- "The implementation of remove_value should call this method"
- if self.size() == 0:
- raise ConsistencyError, "tried to make a domain empty"
-
- def remove_value(self, value):
- """Remove value of domain and check for consistency"""
- assert isinstance(value, int)
- del self._values[value]
- self._value_removed()
-
- def remove_values(self, values):
- assert isinstance(values, list)
- if len(values) > 0:
- for val in values:
- del self._values[val]
- self._value_removed()
-
- def size(self):
- """computes the size of a finite domain"""
- return len(self._values)
-
- def get_values(self):
- return self._values.keys()
Modified: pypy/dist/pypy/rlib/cslib/rpropagation.py
==============================================================================
--- pypy/dist/pypy/rlib/cslib/rpropagation.py (original)
+++ pypy/dist/pypy/rlib/cslib/rpropagation.py Wed Mar 21 12:14:48 2007
@@ -10,9 +10,9 @@
self._variables = domains.keys() # list of variable names
self._domains = domains # maps variable name to domain object
self._constraints = [] # list of constraint objects
- self._variableListeners = {}
+ self._varconst = {}
for var in self._variables:
- self._variableListeners[var] = []
+ self._varconst[var] = []
for constr in constraints:
self.add_constraint( constr )
@@ -30,17 +30,17 @@
else:
self._constraints.append(constraint)
for var in constraint._variables:
- self._variableListeners[var].append(constraint)
+ self._varconst[var].append(constraint)
def _remove_constraint(self, constraint):
self._constraints.remove(constraint)
for var in constraint._variables:
try:
- self._variableListeners[var].remove(constraint)
+ self._varconst[var].remove(constraint)
except ValueError:
raise ValueError('Error removing constraint from listener',
var,
- self._variableListeners[var],
+ self._varconst[var],
constraint)
def get_domains(self):
@@ -82,7 +82,7 @@
dom = self._domains[var]
if not dom._changed: # XXX
continue
- for constr in self._variableListeners[var]:
+ for constr in self._varconst[var]:
if constr is not constraint:
_affected_constraints[constr] = True
dom._changed = False
More information about the Pypy-commit
mailing list