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

auc at codespeak.net auc at codespeak.net
Thu Sep 28 13:01:59 CEST 2006


Author: auc
Date: Thu Sep 28 13:01:55 2006
New Revision: 32690

Added:
   pypy/dist/pypy/objspace/cclp/constraint/btree.py   (contents, props changed)
   pypy/dist/pypy/objspace/cclp/constraint/test/test_btree.py   (contents, props changed)
Modified:
   pypy/dist/pypy/objspace/cclp/constraint/constraint.py
   pypy/dist/pypy/objspace/cclp/constraint/distributor.py
   pypy/dist/pypy/objspace/cclp/scheduler.py
Log:
transl. fixes (we seem to complete the annotation phase now)


Added: pypy/dist/pypy/objspace/cclp/constraint/btree.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/objspace/cclp/constraint/btree.py	Thu Sep 28 13:01:55 2006
@@ -0,0 +1,75 @@
+from pypy.interpreter import baseobjspace
+
+class Found(Exception): pass
+
+class BTree(object):
+    """binary tree"""
+
+    def __init__(self):
+        self.leaf = True
+
+    def _populate(self, key, val):
+        self.leaf = False
+        self.key = key
+        self.val = val
+        self.left = BTree()
+        self.right = BTree()
+
+    def add(self, key, val):
+        assert isinstance(key, int)
+        assert isinstance(val, baseobjspace.W_Root)
+        if self.leaf:
+            self._populate(key, val)
+        else:
+            if key > self.key:
+                self.right.add(key, val)
+            else:
+                self.left.add(key, val)
+
+    def _in(self, key):
+        if self.leaf:
+            return False
+        if self.key == key: raise Found
+        self.left._in(key)
+        self.right._in(key)
+
+    def __contains__(self, key):
+        if self.leaf: return False
+        if self.key == key: return True
+        try:
+            self._in(key)
+            self._in(key)
+            return False
+        except Found:
+            return True
+
+    def _infixly(self, output, ret=0):
+        """depending on the value of ret:
+           0 : keys
+           1 : values
+           2 : both (items)
+        """
+        if self.leaf: return
+        self.left._infixly(output, ret)
+        if ret == 0:
+            output.append(self.key)
+        elif ret == 1:
+            output.append(self.val)
+        else:
+            output.append((self.key, self.val))
+        self.right._infixly(output, ret)
+
+    def keys(self):
+        out = []
+        self._infixly(out, ret=0)
+        return out
+
+    def values(self):
+        out = []
+        self._infixly(out, ret=1)
+        return out
+
+    def items(self):
+        out = []
+        self._infixly(out, ret=2)
+        return out

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 Sep 28 13:01:55 2006
@@ -15,7 +15,7 @@
 
 from pypy.objspace.std.model import StdObjSpaceMultiMethod
 
-#from pypy.objspace.constraint.btree import BTree
+from pypy.objspace.cclp.constraint.btree import BTree
 #from pypy.objspace.constraint.util import sort
 
 all_mms = {}
@@ -74,7 +74,7 @@
 make_filter_mm.register(make_filter__List_String, W_ListObject, W_StringObject)
 all_mms['make_filter'] = make_filter_mm
 
-class Quadruple(object):
+class Quadruple(W_Root):
     def __init__(self, zero, one, two, three):
         self.zero = zero
         self.one = one
@@ -101,8 +101,8 @@
         return result_cache
 
     def _assign_values(self):
-        variables = []
         kwargs = self._space.newdict()
+        variables = BTree()
         for variable in self._variables:
             assert isinstance(variable, W_Variable)
             domain = variable.w_dom
@@ -110,18 +110,18 @@
             values = domain.get_values()
             assert isinstance(values, list)
             ds = domain.size()
+            assert isinstance(ds, int)
 	    w_name = variable.w_name()
             lval = len(values)
-            vstruct = (ds, Quadruple(w_name, values, 0, lval))
-            variables.append(vstruct)
+            variables.add(ds, Quadruple(w_name, values, 0, lval))
             # was meant to be:
             #variables.append((domain.size(),
             #                  [w_name, values, 0, len(values)]))
             first_value = values[0]
             assert isinstance(first_value, W_Root)
             kwargs.content[variable.w_name()] = first_value
-        # sort variables to instanciate those with fewer possible values first
-        variables.sort()
+        # get sorted variables to instanciate those with fewer possible values first
+        variables = variables.values()
         self._assign_values_state = variables
         return kwargs 
         
@@ -130,13 +130,18 @@
         # try to instanciate the next variable
         variables = self._assign_values_state
 
-        for _, curr in variables:
+        for curr in variables:
+            assert isinstance(curr, Quadruple)
             w_name = curr.zero
             dom_values = curr.one
             dom_index = curr.two
             dom_len = curr.three
+            assert isinstance(w_name, W_StringObject)
+            assert isinstance(dom_values, list)
+            assert isinstance(dom_index, int)
+            assert isinstance(dom_len, int)
             if dom_index < dom_len:
-                kwargs.content[w_name] = dom_values[curr.two]
+                kwargs.content[w_name] = dom_values[dom_index]
                 curr.two = dom_index + 1
                 break
             else:
@@ -227,25 +232,27 @@
     def revise(self):
         _spc = self._space
 
-        #ord_vars = BTree()
-        variables = []
-        for variable in self._variables:
-            assert isinstance(variable, W_Variable)
-            dom = variable.w_dom
+        ord_vars = BTree()
+        for var in self._variables:
+            assert isinstance(var, W_Variable)
+            dom = var.w_dom
             assert isinstance(dom, W_AbstractDomain)
-            variables.append((dom.size(), variable))
-
-        variables.sort()
-        # variable = ord_vars.values()
+            sz = dom.size()
+            ord_vars.add(sz, var)
+        variables = ord_vars.values()
         
         # if a domain has a size of 1,
         # then the value must be removed from the other domains
-        for _, var in variables:
+        for var in variables:
+            assert isinstance(var, W_Variable)
             dom = var.w_dom
+            assert isinstance(dom, W_AbstractDomain)
             if dom.size() == 1:
                 #print "AllDistinct removes values"
-                for _, _var in variables:
+                for _var in variables:
+                    assert isinstance(_var, W_Variable)
                     _dom = _var.w_dom
+                    assert isinstance(_dom, W_AbstractDomain)
                     if not _var._same_as(var):
                         try:
                             _dom.remove_value(dom.get_values()[0])
@@ -256,8 +263,10 @@
 
         # if there are less values than variables, the constraint fails
         values = {}
-        for _, var in variables:
+        for var in variables:
+            assert isinstance(var, W_Variable)
             dom = var.w_dom
+            assert isinstance(dom, W_AbstractDomain)
             for val in dom.w_get_values().wrappeditems:
                 values[val] = 0
 
@@ -267,8 +276,10 @@
                                  _spc.wrap("ConsistencyFailure"))
 
         # the constraint is entailed if all domains have a size of 1
-        for _, var in variables:
+        for var in variables:
+            assert isinstance(var, W_Variable)
             dom = var.w_dom
+            assert isinstance(dom, W_AbstractDomain)
             if not dom.size() == 1:
                 return False
 

Modified: pypy/dist/pypy/objspace/cclp/constraint/distributor.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/constraint/distributor.py	(original)
+++ pypy/dist/pypy/objspace/cclp/constraint/distributor.py	Thu Sep 28 13:01:55 2006
@@ -134,7 +134,8 @@
 
     def _do_distribute(self, domain, choice):
         values = domain.get_values()
-        nb_elts = max(1, len(values)*1./self._subdomains())
+        subdoms = self._subdomains()
+        nb_elts = max(subdoms, len(values))  / float(subdoms)
         start, end = (int(math.floor(choice * nb_elts)),
                       int(math.floor((choice + 1) * nb_elts)))
         domain.remove_values(values[:start])

Added: pypy/dist/pypy/objspace/cclp/constraint/test/test_btree.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/objspace/cclp/constraint/test/test_btree.py	Thu Sep 28 13:01:55 2006
@@ -0,0 +1,17 @@
+class TestBTree(object):
+
+    def test_everything_at_once(self):
+        from pypy.objspace.constraint.btree import BTree
+        b = BTree()
+        l = [(-1, 'egg'), (-7, 'spam'), (3, 'bacon'),
+             (99, 'ham'), (77, 'cheese'), (7, 'tomato'),
+             (5, 'chicken'), (9, 'noodles')]
+        for k, v in l: b.add(k, v)
+        assert 77 in b
+        assert 66 not in b
+        assert b.values() == ['spam', 'egg', 'bacon', 'chicken',
+                               'tomato', 'noodles', 'cheese', 'ham']
+        assert b.keys() == [-7, -1, 3, 5, 7, 9, 77, 99]
+        assert b.items() == [(-7, 'spam'), (-1, 'egg'), (3, 'bacon'),
+                             (5, 'chicken'), (7, 'tomato'), (9, 'noodles'),
+                             (77, 'cheese'), (99, 'ham')]

Modified: pypy/dist/pypy/objspace/cclp/scheduler.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/scheduler.py	(original)
+++ pypy/dist/pypy/objspace/cclp/scheduler.py	Thu Sep 28 13:01:55 2006
@@ -44,10 +44,6 @@
         # for the reset case
         self._head._next = self._head._prev = self._head
 
-    def _set_head(self, thread):
-        assert isinstance(thread, ClonableCoroutine)
-        self._head = thread
-
     def _check_initial_conditions(self):
         try:
             assert self._head._next == self._head._prev == self._head



More information about the Pypy-commit mailing list