/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/intset.py

  • Committer: Jelmer Vernooij
  • Date: 2020-07-05 12:50:01 UTC
  • mfrom: (7490.40.46 work)
  • mto: (7490.40.48 work)
  • mto: This revision was merged to the branch mainline in revision 7519.
  • Revision ID: jelmer@jelmer.uk-20200705125001-7s3vo0p55szbbws7
Merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
from __future__ import absolute_import
 
18
 
 
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]
 
65
 
 
66
    """
 
67
    __slots__ = ['_val']
 
68
 
 
69
    def __init__(self, values=None, bitmask=0):
 
70
        """Create a new intset.
 
71
 
 
72
        values
 
73
            If specified, an initial collection of values.
 
74
        """
 
75
        self._val = bitmask
 
76
        if values is not None:
 
77
            self.update(values)
 
78
 
 
79
    def __bool__(self):
 
80
        """IntSets are false if empty, otherwise True.
 
81
 
 
82
        >>> bool(IntSet())
 
83
        False
 
84
 
 
85
        >>> bool(IntSet([0]))
 
86
        True
 
87
        """
 
88
        return bool(self._val)
 
89
 
 
90
    __nonzero__ = __bool__
 
91
 
 
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))
 
132
        return IntSet(bitmask=(self._val | other._val))
 
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):
 
149
        return self._val & (1 << i)
 
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:
 
174
                self._val |= (1 << i)
 
175
 
 
176
    def add(self, to_add):
 
177
        self._val |= (1 << to_add)
 
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
        """
 
196
        m = 1 << to_remove
 
197
        if not self._val & m:
 
198
            raise KeyError(to_remove)
 
199
        self._val ^= m
 
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