/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: John Arbash Meinel
  • Date: 2006-04-25 15:05:42 UTC
  • mfrom: (1185.85.85 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060425150542-c7b518dca9928691
[merge] the old bzr-encoding changes, reparenting them on bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005 Canonical Ltd
2
 
#
 
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
16
 
19
17
# Author: Martin Pool <mbp@canonical.com>
20
18
 
62
60
    True
63
61
    >>> list(a)
64
62
    [10]
65
 
 
 
63
    
66
64
    """
67
65
    __slots__ = ['_val']
68
66
 
69
 
    def __init__(self, values=None, bitmask=0):
 
67
    def __init__(self, values=None, bitmask=0L):
70
68
        """Create a new intset.
71
69
 
72
70
        values
73
71
            If specified, an initial collection of values.
74
72
        """
75
73
        self._val = bitmask
76
 
        if values is not None:
 
74
        if values != None:
77
75
            self.update(values)
78
76
 
79
 
    def __bool__(self):
 
77
 
 
78
    def __nonzero__(self):
80
79
        """IntSets are false if empty, otherwise True.
81
80
 
82
81
        >>> bool(IntSet())
83
82
        False
84
 
 
 
83
        
85
84
        >>> bool(IntSet([0]))
86
85
        True
87
86
        """
88
87
        return bool(self._val)
89
88
 
90
 
    __nonzero__ = __bool__
91
89
 
92
90
    def __len__(self):
93
91
        """Number of elements in set.
103
101
            v = v >> 1
104
102
        return c
105
103
 
 
104
 
106
105
    def __and__(self, other):
107
106
        """Set intersection.
108
107
 
120
119
            raise NotImplementedError(type(other))
121
120
        return IntSet(bitmask=(self._val & other._val))
122
121
 
 
122
 
123
123
    def __or__(self, other):
124
124
        """Set union.
125
125
 
129
129
        """
130
130
        if not isinstance(other, IntSet):
131
131
            raise NotImplementedError(type(other))
132
 
        return IntSet(bitmask=(self._val | other._val))
 
132
        return IntSet(bitmask=(self._val | other._val))        
 
133
 
133
134
 
134
135
    def __eq__(self, other):
135
136
        """Comparison.
142
143
        else:
143
144
            return False
144
145
 
 
146
 
145
147
    def __ne__(self, other):
146
148
        return not self.__eq__(other)
147
149
 
 
150
 
148
151
    def __contains__(self, i):
149
 
        return self._val & (1 << i)
 
152
        assert i >= 0
 
153
        return self._val & (1L << i)
 
154
 
150
155
 
151
156
    def __iter__(self):
152
157
        """Return contents of set.
165
170
            v = v >> 1
166
171
            o = o + 1
167
172
 
 
173
        
168
174
    def update(self, to_add):
169
175
        """Add all the values from the sequence or intset to_add"""
170
176
        if isinstance(to_add, IntSet):
171
177
            self._val |= to_add._val
172
178
        else:
173
179
            for i in to_add:
174
 
                self._val |= (1 << i)
 
180
                assert i >= 0
 
181
                self._val |= (1L << i)
 
182
 
175
183
 
176
184
    def add(self, to_add):
177
 
        self._val |= (1 << to_add)
 
185
        assert 0 <= to_add
 
186
        self._val |= (1L << to_add)
 
187
 
178
188
 
179
189
    def remove(self, to_remove):
180
190
        """Remove one value from the set.
193
203
        >>> not a
194
204
        True
195
205
        """
196
 
        m = 1 << to_remove
 
206
        assert 0 <= to_remove
 
207
        m = 1L << to_remove
197
208
        if not self._val & m:
198
209
            raise KeyError(to_remove)
199
210
        self._val ^= m
215
226
            return
216
227
        intersect = self._val & to_remove._val
217
228
        self._val ^= intersect
 
229