[pypy-svn] r25694 - in pypy/dist/pypy/objspace: . constraint constraint/test
auc at codespeak.net
auc at codespeak.net
Tue Apr 11 12:20:20 CEST 2006
Author: auc
Date: Tue Apr 11 12:20:17 2006
New Revision: 25694
Added:
pypy/dist/pypy/objspace/constraint/test/test_distributor.py
Modified:
pypy/dist/pypy/objspace/constraint/computationspace.py
pypy/dist/pypy/objspace/constraint/distributor.py
pypy/dist/pypy/objspace/logic.py
Log:
instantiate distributors
Modified: pypy/dist/pypy/objspace/constraint/computationspace.py
==============================================================================
--- pypy/dist/pypy/objspace/constraint/computationspace.py (original)
+++ pypy/dist/pypy/objspace/constraint/computationspace.py Tue Apr 11 12:20:17 2006
@@ -47,6 +47,18 @@
estimate_cost = interp2app(W_Constraint.w_estimate_cost),
revise = interp2app(W_Constraint.w_revise))
+
+#-- Distributors (standards) ------------
+
+class W_Distributor(Wrappable):
+
+ def __init__(self, object_space, fanout):
+ self._space = object_space
+ self.fanout = fanout
+
+W_Distributor.typedef = typedef.TypeDef("W_Distributor")
+
+
#-- Computation space -------------------
class W_ComputationSpace(Wrappable):
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 12:20:17 2006
@@ -1,5 +1,12 @@
import math
+from pypy.interpreter.error import OperationError
+from pypy.interpreter.baseobjspace import Wrappable
+from pypy.interpreter import baseobjspace, typedef, gateway
+from pypy.interpreter.gateway import interp2app
+
+from pypy.objspace.constraint.computationspace import W_Distributor
+
def arrange_domains(cs, variables):
"""build a data structure from var to dom
that satisfies distribute & friends"""
@@ -8,17 +15,17 @@
new_doms[var] = cs.dom(var).copy()
return new_doms
-class AbstractDistributor(object):
+
+class W_AbstractDistributor(W_Distributor):
"""_distribute is left unimplemented."""
- def __init__(self, c_space, nb_subspaces=2):
- self.nb_subspaces = nb_subspaces
- self.cs = c_space
- self.verbose = 0
-
- def set_space(self, space):
- self.cs = space
-
+ def w_fanout(self):
+ return self._space.newint(self.fanout)
+
+ def fanout(self):
+ """return number of possible splits"""
+ return self.fanout
+
def find_smallest_domain(self):
"""returns the variable having the smallest domain.
(or one of such varibles if there is a tie)
@@ -33,13 +40,11 @@
return best
- def nb_subdomains(self):
- """return number of possible splits"""
- return self.nb_subspaces
-
-
+ def w_distribute(self, w_cs, w_choice):
+ assert isintance(w_choice, W_IntegerObject)
+ self.distribute(w_cs, self._space.int_w(w_choice))
- def distribute(self, choice):
+ def distribute(self, w_cs, choice_w):
variable = self._find_distribution_variable()
self._do_distribute(self.cs.dom(variable), choice)
for const in self.cs.dependant_constraints(variable):
@@ -52,17 +57,20 @@
"""remove values from domain depending on choice"""
raise NotImplementedError
+W_Distributor.typedef = typedef.TypeDef("W_AbstractDistributor",
+ W_Distributor.typedef,
+ fanout = interp2app(W_AbstractDistributor.w_fanout),
+ distribute = interp2app(W_AbstractDistributor.w_distribute))
-
-
-class NaiveDistributor(AbstractDistributor):
+class W_NaiveDistributor(W_AbstractDistributor):
"""distributes domains by splitting the smallest domain in 2 new domains
The first new domain has a size of one,
and the second has all the other values"""
- def __init__(self, c_space):
- AbstractDistributor.__init__(self, c_space, 2)
+ def __init__(self, object_space, fanout_w):
+ # default fanout is 2, see make_naive_distributor
+ W_Distributor.__init__(self, object_space, fanout_w)
def _do_distribute(self, domain, choice):
values = domain.get_values()
@@ -70,17 +78,26 @@
domain.remove_values(values[1:])
else:
domain.remove_value(values[0])
-
-class SplitDistributor(AbstractDistributor):
+W_NaiveDistributor.typedef = typedef.TypeDef(
+ "W_NaiveDistributor",
+ W_AbstractDistributor.typedef)
+
+def make_naive_distributor(object_space, fanout=2):
+ return object_space.wrap(W_NaiveDistributor(object_space, fanout))
+app_make_naive_distributor = interp2app(make_naive_distributor,
+ unwrap_spec = [baseobjspace.ObjSpace, int])
+
+
+class W_SplitDistributor(W_AbstractDistributor):
"""distributes domains by splitting the smallest domain in
nb_subspaces equal parts or as equal as possible.
If nb_subspaces is 0, then the smallest domain is split in
domains of size 1"""
- def __init__(self, c_space, nb_subspaces=3):
- AbstractDistributor.__init__(self, c_space, nb_subspaces)
-
+ def __init__(self, object_space, fanout_w):
+ # 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()
@@ -100,11 +117,21 @@
domain.remove_values(values[:start])
domain.remove_values(values[end:])
+def make_split_distributor(object_space, fanout=3):
+ return object_space.wrap(W_SplitDistributor(object_space, fanout))
+app_make_split_distributor = interp2app(make_split_distributor,
+ unwrap_spec = [baseobjspace.ObjSpace, int])
+
-class DichotomyDistributor(SplitDistributor):
+class W_DichotomyDistributor(W_SplitDistributor):
"""distributes domains by splitting the smallest domain in
two equal parts or as equal as possible"""
- def __init__(self, c_space):
- SplitDistributor.__init__(self, c_space, 2)
+ def __init__(self, object_space, w_fanout):
+ W_SplitDistributor.__init__(self, object_space, w_fanout)
+
+def make_dichotomy_distributor(object_space):
+ return make_split_distributor(object_space, 2)
+app_make_dichotomy_distributor = interp2app(make_dichotomy_distributor)
+
-DefaultDistributor = NaiveDistributor
+#DefaultDistributor = W_NaiveDistributor
Added: pypy/dist/pypy/objspace/constraint/test/test_distributor.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/objspace/constraint/test/test_distributor.py Tue Apr 11 12:20:17 2006
@@ -0,0 +1,14 @@
+from pypy.conftest import gettestobjspace
+
+
+class AppTest_Distributor(object):
+
+ def setup_class(cls):
+ cls.space = gettestobjspace('logic')
+
+ def test_instantiate(self):
+ d1 = NaiveDistributor()
+ d2 = SplitDistributor(4)
+ d3 = DichotomyDistributor()
+
+
Modified: pypy/dist/pypy/objspace/logic.py
==============================================================================
--- pypy/dist/pypy/objspace/logic.py (original)
+++ pypy/dist/pypy/objspace/logic.py Tue Apr 11 12:20:17 2006
@@ -754,21 +754,25 @@
return proxy
-#------ domains -----------------
+#------ domains ------------------
from pypy.objspace.constraint import domain
all_mms.update(domain.all_mms)
W_FiniteDomain = domain.W_FiniteDomain
-#-------- computationspace --------------
+#-------- computationspace --------
from pypy.objspace.constraint import computationspace
all_mms.update(computationspace.all_mms)
W_ComputationSpace = computationspace.W_ComputationSpace
-# ---- constraints
+# ---- constraints ----------------
from pypy.objspace.constraint import constraint
+#----- distributors ---------------
+from pypy.objspace.constraint import distributor
+
+
#-- THE SPACE ---------------------------------------
#class UnificationError(w_RuntimeError):
@@ -836,6 +840,14 @@
space.wrap(constraint.app_make_filter))
space.setitem(space.builtin.w_dict, space.wrap('AllDistinct'),
space.wrap(constraint.app_make_alldistinct))
+ #-- distributor --
+ space.setitem(space.builtin.w_dict, space.wrap('NaiveDistributor'),
+ space.wrap(distributor.app_make_naive_distributor))
+ space.setitem(space.builtin.w_dict, space.wrap('SplitDistributor'),
+ space.wrap(distributor.app_make_split_distributor))
+ space.setitem(space.builtin.w_dict, space.wrap('DichotomyDistributor'),
+ space.wrap(distributor.app_make_dichotomy_distributor))
+
if USE_COROUTINES:
import os
More information about the Pypy-commit
mailing list