[pypy-svn] r31230 - in pypy/dist/pypy/objspace/cclp/constraint: . test

auc at codespeak.net auc at codespeak.net
Thu Aug 10 15:00:47 CEST 2006


Author: auc
Date: Thu Aug 10 15:00:43 2006
New Revision: 31230

Modified:
   pypy/dist/pypy/objspace/cclp/constraint/constraint.py
   pypy/dist/pypy/objspace/cclp/constraint/domain.py
   pypy/dist/pypy/objspace/cclp/constraint/test/test_constraint.py
Log:
removed one yield


Modified: pypy/dist/pypy/objspace/cclp/constraint/constraint.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/constraint/constraint.py	(original)
+++ pypy/dist/pypy/objspace/cclp/constraint/constraint.py	Thu Aug 10 15:00:43 2006
@@ -16,7 +16,6 @@
 from pypy.objspace.constraint.btree import BTree
 from pypy.objspace.constraint.util import sort, reverse
 
-import operator
 
 all_mms = {}
 
@@ -62,11 +61,6 @@
         return self._space.newbool(self.revise())
             
     
-
-##     def __eq__(self, other): #FIXME and parent
-##         if not isinstance(other, self.__class__): return False
-##         return self._variables == other._variables
-    
 W_AbstractConstraint.typedef = typedef.TypeDef(
     "W_AbstractConstraint",
     W_Constraint.typedef,                                           
@@ -131,42 +125,48 @@
         for variable in self._variables:
             assert isinstance(variable, W_Variable)
             domain = variable.w_dom
-            values = domain.w_get_values()
-            variables.append((domain.size(),
-                              [variable, values, self._space.newint(0),
-                               self._space.len(values)]))
-            kwargs.content[variable.w_name()] = values.wrappeditems[0]
+            values = domain.get_values()
+            variables.append((domain.size(), [variable.w_name(), values, 0, len(values)]))
+            #kwargs.content[variable.w_name()] = values[0]
         # sort variables to instanciate those with fewer possible values first
         sort(variables)
-        res_kwargs = []
-        go_on = 1
-        while go_on:
-#            res_kwargs.append( kwargs)
-            yield kwargs
-            # try to instanciate the next variable
-            
-            for size, curr in variables:
-                assert isinstance(curr[0], W_Variable)
-                w_name = curr[0].w_name()
-                assert isinstance(w_name, W_StringObject)
-                if self._space.int_w(curr[2]) + 1 < self._space.int_w(curr[-1]):
-                    curr[2] = self._space.add(curr[2], self._space.newint(1))
-                    kwargs.content[w_name] = curr[1].wrappeditems[self._space.int_w(curr[2])]
-                    break
-                else:
-                    curr[2] = self._space.newint(0)
-                    kwargs.content[w_name] = curr[1].wrappeditems[0]
+        self._assign_values_state = variables
+        return kwargs 
+        
+    def _next_value(self, kwargs):
+
+        # try to instanciate the next variable
+        variables = self._assign_values_state
+
+        for _, curr in variables:
+            w_name = curr[0]
+            dom_values = curr[1] 
+            dom_index = curr[2]
+            dom_len = curr[3]
+            if dom_index < dom_len:
+                kwargs.content[w_name] = dom_values[curr[2]]
+                curr[2] = dom_index + 1
+                break
             else:
-                # it's over
-                go_on = 0
-#                return res_kwargs
+                curr[2] = 0
+                kwargs.content[w_name] = dom_values[0]
+        else:
+            # it's over
+            raise StopIteration
+        return kwargs
 
     def revise(self):
         """generic propagation algorithm for n-ary expressions"""
         maybe_entailed = True
         ffunc = self.filter_func
         result_cache = self._init_result_cache()
-        for kwargs in self._assign_values():
+
+        kwargs = self._assign_values()
+        while 1:
+            try:
+                kwargs = self._next_value(kwargs)
+            except StopIteration:
+                break
             if maybe_entailed:
                 for varname, val in kwargs.content.iteritems():
                     if val not in result_cache.content[varname].content:
@@ -186,7 +186,6 @@
                 domain.remove_values([val
                                       for val in domain._values.content.keys()
                                       if val not in keep.content])
-
         except ConsistencyFailure:
             raise ConsistencyFailure('Inconsistency while applying %s' % \
                                      repr(self))

Modified: pypy/dist/pypy/objspace/cclp/constraint/domain.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/constraint/domain.py	(original)
+++ pypy/dist/pypy/objspace/cclp/constraint/domain.py	Thu Aug 10 15:00:43 2006
@@ -1,6 +1,6 @@
 from pypy.interpreter.error import OperationError
 
-from pypy.interpreter import typedef, gateway
+from pypy.interpreter import typedef, gateway, baseobjspace
 from pypy.interpreter.gateway import interp2app
 
 from pypy.objspace.std.listobject import W_ListObject, W_TupleObject
@@ -27,6 +27,7 @@
         no duplicate values"""
         W_AbstractDomain.__init__(self, space)
         #XXX a pure dict used to work there (esp. in revise)
+        assert isinstance(w_values, W_ListObject)
         self._values = space.newdict([])
         self.set_values(w_values)
 
@@ -40,6 +41,7 @@
         
     def remove_value(self, w_value):
         """Remove value of domain and check for consistency"""
+        assert isinstance(w_value, baseobjspace.W_Root)
         del self._values.content[w_value]
         self._value_removed()
 

Modified: pypy/dist/pypy/objspace/cclp/constraint/test/test_constraint.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/constraint/test/test_constraint.py	(original)
+++ pypy/dist/pypy/objspace/cclp/constraint/test/test_constraint.py	Thu Aug 10 15:00:43 2006
@@ -54,7 +54,7 @@
         cstr = make_expression([v1], '2*v1==2')
         assert str(cstr).startswith('<W_Expression object at')        
 
-    def test_revise(self):
+    def test_revise2(self):
         v1 = domain([1, 2], 'v1')
         cstr = make_expression([v1], '2*v1==2')
         assert cstr.revise() == 0



More information about the Pypy-commit mailing list