/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 bzrlib/intset.py

  • Committer: Martin Pool
  • Date: 2005-08-29 04:05:28 UTC
  • Revision ID: mbp@sourcefrog.net-20050829040528-3fb016af15c4e4c0
- smart_add routine now doesn't print filenames directly, but rather
  returns them to the caller for appropriate display.  the add command
  displays them in the same way as before.

Show diffs side-by-side

added added

removed removed

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