/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
6619.3.20 by Jelmer Vernooij
Apply 2to3 nonzero fix.
79
    def __bool__(self):
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
80
        """IntSets are false if empty, otherwise True.
81
82
        >>> bool(IntSet())
83
        False
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
84
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
85
        >>> bool(IntSet([0]))
86
        True
87
        """
88
        return bool(self._val)
89
6619.3.23 by Jelmer Vernooij
Keep __nonzero__ around for Python2.
90
    __nonzero__ = __bool__
91
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
92
    def __len__(self):
93
        """Number of elements in set.
94
95
        >>> len(IntSet(xrange(20000)))
96
        20000
97
        """
98
        v = self._val
99
        c = 0
100
        while v:
101
            if v & 1:
102
                c += 1
103
            v = v >> 1
104
        return c
105
106
    def __and__(self, other):
107
        """Set intersection.
108
109
        >>> a = IntSet(range(10))
110
        >>> len(a)
111
        10
112
        >>> b = a & a
113
        >>> b == a
114
        True
115
        >>> a = a & IntSet([5, 7, 11, 13])
116
        >>> list(a)
117
        [5, 7]
118
        """
119
        if not isinstance(other, IntSet):
120
            raise NotImplementedError(type(other))
121
        return IntSet(bitmask=(self._val & other._val))
122
123
    def __or__(self, other):
124
        """Set union.
125
126
        >>> a = IntSet(range(10)) | IntSet([5, 15, 25])
127
        >>> len(a)
128
        12
129
        """
130
        if not isinstance(other, IntSet):
131
            raise NotImplementedError(type(other))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
132
        return IntSet(bitmask=(self._val | other._val))
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
133
134
    def __eq__(self, other):
135
        """Comparison.
136
137
        >>> IntSet(range(3)) == IntSet([2, 0, 1])
138
        True
139
        """
140
        if isinstance(other, IntSet):
141
            return self._val == other._val
142
        else:
143
            return False
144
145
    def __ne__(self, other):
146
        return not self.__eq__(other)
147
148
    def __contains__(self, i):
6619.3.17 by Jelmer Vernooij
Run 2to3 numliterals fixer.
149
        return self._val & (1 << i)
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
150
151
    def __iter__(self):
152
        """Return contents of set.
153
154
        >>> list(IntSet())
155
        []
156
        >>> list(IntSet([0, 1, 5, 7]))
157
        [0, 1, 5, 7]
158
        """
159
        v = self._val
160
        o = 0
161
        # XXX: This is a bit slow
162
        while v:
163
            if v & 1:
164
                yield o
165
            v = v >> 1
166
            o = o + 1
167
168
    def update(self, to_add):
169
        """Add all the values from the sequence or intset to_add"""
170
        if isinstance(to_add, IntSet):
171
            self._val |= to_add._val
172
        else:
173
            for i in to_add:
6619.3.17 by Jelmer Vernooij
Run 2to3 numliterals fixer.
174
                self._val |= (1 << i)
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
175
176
    def add(self, to_add):
6619.3.17 by Jelmer Vernooij
Run 2to3 numliterals fixer.
177
        self._val |= (1 << to_add)
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
178
179
    def remove(self, to_remove):
180
        """Remove one value from the set.
181
182
        Raises KeyError if the value is not present.
183
184
        >>> a = IntSet([10])
185
        >>> a.remove(9)
186
        Traceback (most recent call last):
187
          File "/usr/lib/python2.4/doctest.py", line 1243, in __run
188
            compileflags, 1) in test.globs
189
          File "<doctest __main__.IntSet.remove[1]>", line 1, in ?
190
            a.remove(9)
191
        KeyError: 9
192
        >>> a.remove(10)
193
        >>> not a
194
        True
195
        """
6619.3.17 by Jelmer Vernooij
Run 2to3 numliterals fixer.
196
        m = 1 << to_remove
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
197
        if not self._val & m:
198
            raise KeyError(to_remove)
199
        self._val ^= m
1185.50.24 by John Arbash Meinel
Added set_remove to get the set delete function to IntSet
200
201
    def set_remove(self, to_remove):
202
        """Remove all values that exist in to_remove.
203
204
        >>> a = IntSet(range(10))
205
        >>> b = IntSet([2,3,4,7,12])
206
        >>> a.set_remove(b)
207
        >>> list(a)
208
        [0, 1, 5, 6, 8, 9]
209
        >>> a.set_remove([1,2,5])
210
        >>> list(a)
211
        [0, 6, 8, 9]
212
        """
213
        if not isinstance(to_remove, IntSet):
214
            self.set_remove(IntSet(to_remove))
215
            return
216
        intersect = self._val & to_remove._val
217
        self._val ^= intersect