~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/intset.py

  • Committer: Patch Queue Manager
  • Date: 2016-02-01 19:13:13 UTC
  • mfrom: (6614.2.2 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20160201191313-wdfvmfff1djde6oq
(vila) Release 2.7.0 (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/python
2
 
 
3
1
# Copyright (C) 2005 Canonical Ltd
4
 
 
 
2
#
5
3
# This program is free software; you can redistribute it and/or modify
6
4
# it under the terms of the GNU General Public License as published by
7
5
# the Free Software Foundation; either version 2 of the License, or
8
6
# (at your option) any later version.
9
 
 
 
7
#
10
8
# This program is distributed in the hope that it will be useful,
11
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
11
# GNU General Public License for more details.
14
 
 
 
12
#
15
13
# You should have received a copy of the GNU General Public License
16
14
# along with this program; if not, write to the Free Software
17
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
from __future__ import absolute_import
18
18
 
19
19
# Author: Martin Pool <mbp@canonical.com>
20
20
 
62
62
    True
63
63
    >>> list(a)
64
64
    [10]
65
 
    
 
65
 
66
66
    """
67
67
    __slots__ = ['_val']
68
68
 
73
73
            If specified, an initial collection of values.
74
74
        """
75
75
        self._val = bitmask
76
 
        if values != None:
 
76
        if values is not None:
77
77
            self.update(values)
78
78
 
79
79
 
82
82
 
83
83
        >>> bool(IntSet())
84
84
        False
85
 
        
 
85
 
86
86
        >>> bool(IntSet([0]))
87
87
        True
88
88
        """
131
131
        """
132
132
        if not isinstance(other, IntSet):
133
133
            raise NotImplementedError(type(other))
134
 
        return IntSet(bitmask=(self._val | other._val))        
 
134
        return IntSet(bitmask=(self._val | other._val))
135
135
 
136
136
 
137
137
    def __eq__(self, other):
151
151
 
152
152
 
153
153
    def __contains__(self, i):
154
 
        assert i >= 0
155
154
        return self._val & (1L << i)
156
155
 
157
156
 
172
171
            v = v >> 1
173
172
            o = o + 1
174
173
 
175
 
        
 
174
 
176
175
    def update(self, to_add):
177
176
        """Add all the values from the sequence or intset to_add"""
178
177
        if isinstance(to_add, IntSet):
179
178
            self._val |= to_add._val
180
179
        else:
181
180
            for i in to_add:
182
 
                assert i >= 0
183
181
                self._val |= (1L << i)
184
182
 
185
183
 
186
184
    def add(self, to_add):
187
 
        assert 0 <= to_add
188
185
        self._val |= (1L << to_add)
189
186
 
190
187
 
205
202
        >>> not a
206
203
        True
207
204
        """
208
 
        assert 0 <= to_remove
209
205
        m = 1L << to_remove
210
206
        if not self._val & m:
211
207
            raise KeyError(to_remove)
212
208
        self._val ^= m
213
 
        
214
 
        
215
 
            
216
 
    
217
 
 
218
 
if __name__ == '__main__':
219
 
    import doctest
220
 
    doctest.testmod()
221
 
    
 
209
 
 
210
    def set_remove(self, to_remove):
 
211
        """Remove all values that exist in to_remove.
 
212
 
 
213
        >>> a = IntSet(range(10))
 
214
        >>> b = IntSet([2,3,4,7,12])
 
215
        >>> a.set_remove(b)
 
216
        >>> list(a)
 
217
        [0, 1, 5, 6, 8, 9]
 
218
        >>> a.set_remove([1,2,5])
 
219
        >>> list(a)
 
220
        [0, 6, 8, 9]
 
221
        """
 
222
        if not isinstance(to_remove, IntSet):
 
223
            self.set_remove(IntSet(to_remove))
 
224
            return
 
225
        intersect = self._val & to_remove._val
 
226
        self._val ^= intersect
 
227