File: common.py

package info (click to toggle)
python-bitarray 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 1,288 kB
  • sloc: python: 11,456; ansic: 7,657; makefile: 73; sh: 6
file content (48 lines) | stat: -rw-r--r-- 1,124 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Common:

    def __repr__(self):
        return "SparseBitarray('%s')" % (''.join(str(v) for v in self))

    def pop(self, i = -1):
        if i < 0:
            i += len(self)
        res = self[i]
        del self[i]
        return res

    def remove(self, value):
        i = self.find(value)
        if i < 0:
            raise ValueError
        del self[i]

    def sort(self, reverse=False):
        if reverse:
            c1 = self.count(1)
            self[:c1:] = 1
            self[c1::] = 0
        else:
            c0 = self.count(0)
            self[:c0:] = 0
            self[c0::] = 1

    def _get_start_stop(self, key):
        if key.step not in (1, None):
            raise ValueError("only step = 1 allowed, got %r" % key)
        start = key.start
        if start is None:
            start = 0
        stop = key.stop
        if stop is None:
            stop = len(self)
        return start, stop

    def _adjust_index(self, i):
        n = len(self)
        if i < 0:
            i += n
            if i < 0:
                i = 0
        elif i > n:
            i = n
        return i