FiniteDomain: inherit from set builtins (closes #89865)

Most of FiniteDomain operation can now be handle by builtins set type.

The "copy on write" feature is dropped. I expect set copy to be fast enough

bumps version number for this massive changes

authorPierre-Yves David <pierre-yves.david@logilab.fr>
changeset9cc3a1b9600f
branchdefault
phasepublic
hiddenno
parent revision#1a324da0ffc8 psyco: drop psyco support (closes #90272)
child revision#fe68ce1b9248 don't suggests psyco anymore, support has been dropped
files modified by this revision
fd.py
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@logilab.fr>
# Date 1332407309 -3600
# Thu Mar 22 10:08:29 2012 +0100
# Node ID 9cc3a1b9600fc35e4e505d35a4a978ae7669cc50
# Parent 1a324da0ffc88352e22da3901e62473587854218
FiniteDomain: inherit from set builtins (closes #89865)

Most of FiniteDomain operation can now be handle by builtins set type.

The "copy on write" feature is dropped. I expect set copy to be fast enough

bumps version number for this massive changes

diff --git a/fd.py b/fd.py
@@ -34,11 +34,11 @@
1  from logilab.constraint.propagation import AbstractDomain, BasicConstraint, \
2                                             ConsistencyFailure, \
3                                             AbstractConstraint
4 
5 
6 -class FiniteDomain(AbstractDomain):
7 +class FiniteDomain(AbstractDomain, set):
8      """
9      Variable Domain with a finite set of possible values
10      """
11 
12      _copy_count = 0
@@ -47,66 +47,38 @@
13      def __init__(self, values):
14          """values is a list of values in the domain
15          This class uses a dictionnary to make sure that there are
16          no duplicate values"""
17          AbstractDomain.__init__(self)
18 -        if isinstance(values, FiniteDomain):
19 -            # do a copy on write
20 -            self._cow = True
21 -            values._cow = True
22 -            FiniteDomain._copy_count += 1
23 -            self._values = values._values
24 -        else:
25 -            assert len(values) > 0
26 -            self.setValues(values)
27 +        assert len(values) > 0
28 +        set.__init__(self, values)
29 
30 -        ##self.getValues = self._values.keys
31 -
32 -    def setValues(self, values):
33 -        self._cow = False
34 -        FiniteDomain._write_count += 1
35 -        self._values = {}
36 -        for val in values:
37 -            self._values[val] = 0
38 
39      def removeValue(self, value):
40          """Remove value of domain and check for consistency"""
41 -##         print "removing", value, "from", self._values.keys()
42 -        if self._cow:
43 -            self.setValues(self._values)
44 -        del self._values[value]
45 +        self.remove(value)
46          self._valueRemoved()
47 
48      def removeValues(self, values):
49          """Remove values of domain and check for consistency"""
50 -        if self._cow:
51 -            self.setValues(self._values)
52          if values:
53 -##             print "removing", values, "from", self._values.keys()
54 -            for val in values :
55 -                del self._values[val]
56 +            self.difference_update(values)
57              self._valueRemoved()
58      __delitem__ = removeValue
59 
60 -    def size(self):
61 -        """computes the size of a finite domain"""
62 -        return len(self._values)
63 -    __len__ = size
64 +    size = set.__len__
65 
66      def getValues(self):
67          """return all the values in the domain"""
68 -        return self._values.keys()
69 -
70 -    def __iter__(self):
71 -        return iter(self._values)
72 +        return list(self)
73 
74      def copy(self):
75          """clone the domain"""
76          return FiniteDomain(self)
77 
78      def __repr__(self):
79 -        return '<FiniteDomain %s>' % str(self.getValues())
80 +        return '<FiniteDomain %s>' % list(self)
81 
82  ##
83  ## Constraints
84  ##
85  class AllDistinct(AbstractConstraint):