/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
1
# Copyright (C) 2005 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
16
6379.6.3 by Jelmer Vernooij
Use absolute_import.
17
from __future__ import absolute_import
18
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
19
# Author: Martin Pool <mbp@canonical.com>
20
21
22
# Somewhat surprisingly, it turns out that this is much slower than
23
# simply storing the ints in a set() type.  Python's performance model
24
# is very different to that of C.
25
26
27
class IntSet(Exception):
28
    """Faster set-like class storing only whole numbers.
29
30
    Despite the name this stores long integers happily, but negative
31
    values are not allowed.
32
33
    >>> a = IntSet([0, 2, 5])
34
    >>> bool(a)
35
    True
36
    >>> 2 in a
37
    True
38
    >>> 4 in a
39
    False
40
    >>> a.add(4)
41
    >>> 4 in a
42
    True
43
44
    >>> b = IntSet()
45
    >>> not b
46
    True
47
    >>> b.add(10)
48
    >>> 10 in a
49
    False
50
    >>> a.update(b)
51
    >>> 10 in a
52
    True
53
    >>> a.update(range(5))
54
    >>> 3 in a
55
    True
56
57
    Being a set, duplicates are ignored:
58
    >>> a = IntSet()
59
    >>> a.add(10)
60
    >>> a.add(10)
61
    >>> 10 in a
62
    True
63
    >>> list(a)
64
    [10]
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
65
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
66
    """
67
    __slots__ = ['_val']
68
6619.3.17 by Jelmer Vernooij
Run 2to3 numliterals fixer.
69
    def __init__(self, values=None, bitmask=0):
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
70
        """Create a new intset.
71
72
        values
73
            If specified, an initial collection of values.
74
        """
75
        self._val = bitmask
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
76
        if values is not None:
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
77
            self.update(values)
78
79
6619.3.20 by Jelmer Vernooij
Apply 2to3 nonzero fix.
80
    def __bool__(self):
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
81
        """IntSets are false if empty, otherwise True.
82
83
        >>> bool(IntSet())
84
        False
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
85
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
86
        >>> bool(IntSet([0]))
87
        True
88
        """
89
        return bool(self._val)
90
6619.3.23 by Jelmer Vernooij
Keep __nonzero__ around for Python2.
91
    __nonzero__ = __bool__
92
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
93
94
    def __len__(self):
95
        """Number of elements in set.
96
97
        >>> len(IntSet(xrange(20000)))
98
        20000
99
        """
100
        v = self._val
101
        c = 0
102
        while v:
103
            if v & 1:
104
                c += 1
105
            v = v >> 1
106
        return c
107
108
109
    def __and__(self, other):
110
        """Set intersection.
111
112
        >>> a = IntSet(range(10))
113
        >>> len(a)
114
        10
115
        >>> b = a & a
116
        >>> b == a
117
        True
118
        >>> a = a & IntSet([5, 7, 11, 13])
119
        >>> list(a)
120
        [5, 7]
121
        """
122
        if not isinstance(other, IntSet):
123
            raise NotImplementedError(type(other))
124
        return IntSet(bitmask=(self._val & other._val))
125
126
127
    def __or__(self, other):
128
        """Set union.
129
130
        >>> a = IntSet(range(10)) | IntSet([5, 15, 25])
131
        >>> len(a)
132
        12
133
        """
134
        if not isinstance(other, IntSet):
135
            raise NotImplementedError(type(other))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
136
        return IntSet(bitmask=(self._val | other._val))
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
137
138
139
    def __eq__(self, other):
140
        """Comparison.
141
142
        >>> IntSet(range(3)) == IntSet([2, 0, 1])
143
        True
144
        """
145
        if isinstance(other, IntSet):
146
            return self._val == other._val
147
        else:
148
            return False
149
150
151
    def __ne__(self, other):
152
        return not self.__eq__(other)
153
154
155
    def __contains__(self, i):
6619.3.17 by Jelmer Vernooij
Run 2to3 numliterals fixer.
156
        return self._val & (1 << i)
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
157
158
159
    def __iter__(self):
160
        """Return contents of set.
161
162
        >>> list(IntSet())
163
        []
164
        >>> list(IntSet([0, 1, 5, 7]))
165
        [0, 1, 5, 7]
166
        """
167
        v = self._val
168
        o = 0
169
        # XXX: This is a bit slow
170
        while v:
171
            if v & 1:
172
                yield o
173
            v = v >> 1
174
            o = o + 1
175
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
176
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
177
    def update(self, to_add):
178
        """Add all the values from the sequence or intset to_add"""
179
        if isinstance(to_add, IntSet):
180
            self._val |= to_add._val
181
        else:
182
            for i in to_add:
6619.3.17 by Jelmer Vernooij
Run 2to3 numliterals fixer.
183
                self._val |= (1 << i)
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
184
185
186
    def add(self, to_add):
6619.3.17 by Jelmer Vernooij
Run 2to3 numliterals fixer.
187
        self._val |= (1 << to_add)
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
188
189
190
    def remove(self, to_remove):
191
        """Remove one value from the set.
192
193
        Raises KeyError if the value is not present.
194
195
        >>> a = IntSet([10])
196
        >>> a.remove(9)
197
        Traceback (most recent call last):
198
          File "/usr/lib/python2.4/doctest.py", line 1243, in __run
199
            compileflags, 1) in test.globs
200
          File "<doctest __main__.IntSet.remove[1]>", line 1, in ?
201
            a.remove(9)
202
        KeyError: 9
203
        >>> a.remove(10)
204
        >>> not a
205
        True
206
        """
6619.3.17 by Jelmer Vernooij
Run 2to3 numliterals fixer.
207
        m = 1 << to_remove
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
208
        if not self._val & m:
209
            raise KeyError(to_remove)
210
        self._val ^= m
1185.50.24 by John Arbash Meinel
Added set_remove to get the set delete function to IntSet
211
212
    def set_remove(self, to_remove):
213
        """Remove all values that exist in to_remove.
214
215
        >>> a = IntSet(range(10))
216
        >>> b = IntSet([2,3,4,7,12])
217
        >>> a.set_remove(b)
218
        >>> list(a)
219
        [0, 1, 5, 6, 8, 9]
220
        >>> a.set_remove([1,2,5])
221
        >>> list(a)
222
        [0, 6, 8, 9]
223
        """
224
        if not isinstance(to_remove, IntSet):
225
            self.set_remove(IntSet(to_remove))
226
            return
227
        intersect = self._val & to_remove._val
228
        self._val ^= intersect
229