bitarray 3.7.2__cp313-cp313-win_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,991 @@
1
+ Metadata-Version: 2.4
2
+ Name: bitarray
3
+ Version: 3.7.2
4
+ Summary: efficient arrays of booleans -- C extension
5
+ Home-page: https://github.com/ilanschnell/bitarray
6
+ Author: Ilan Schnell
7
+ Author-email: ilanschnell@gmail.com
8
+ License: PSF-2.0
9
+ Classifier: Development Status :: 6 - Mature
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: C
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.6
15
+ Classifier: Programming Language :: Python :: 3.7
16
+ Classifier: Programming Language :: Python :: 3.8
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Programming Language :: Python :: 3.14
23
+ Classifier: Topic :: Utilities
24
+ License-File: LICENSE
25
+ Dynamic: author
26
+ Dynamic: author-email
27
+ Dynamic: classifier
28
+ Dynamic: description
29
+ Dynamic: home-page
30
+ Dynamic: license
31
+ Dynamic: license-file
32
+ Dynamic: summary
33
+
34
+ bitarray: efficient arrays of booleans
35
+ ======================================
36
+
37
+ This library provides an object type which efficiently represents an array
38
+ of booleans. Bitarrays are sequence types and behave very much like usual
39
+ lists. Eight bits are represented by one byte in a contiguous block of
40
+ memory. The user can select between two representations: little-endian
41
+ and big-endian. All functionality is implemented in C.
42
+ Methods for accessing the machine representation are provided, including the
43
+ ability to import and export buffers. This allows creating bitarrays that
44
+ are mapped to other objects, including memory-mapped files.
45
+
46
+
47
+ Key features
48
+ ------------
49
+
50
+ * The bit-endianness can be specified for each bitarray object, see below.
51
+ * Sequence methods: slicing (including slice assignment and deletion),
52
+ operations ``+``, ``*``, ``+=``, ``*=``, the ``in`` operator, ``len()``
53
+ * Bitwise operations: ``~``, ``&``, ``|``, ``^``, ``<<``, ``>>`` (as well as
54
+ their in-place versions ``&=``, ``|=``, ``^=``, ``<<=``, ``>>=``).
55
+ * Fast methods for encoding and decoding variable bit length prefix codes.
56
+ * Bitarray objects support the buffer protocol (both importing and
57
+ exporting buffers).
58
+ * Packing and unpacking to other binary data formats, e.g. ``numpy.ndarray``.
59
+ * Pickling and unpickling of bitarray objects.
60
+ * Immutable ``frozenbitarray`` objects which are hashable
61
+ * Sequential search
62
+ * Type hinting
63
+ * Extensive test suite with about 600 unittests
64
+ * Utility module ``bitarray.util``:
65
+
66
+ * conversion to and from hexadecimal strings
67
+ * generating random bitarrays
68
+ * pretty printing
69
+ * conversion to and from integers
70
+ * creating Huffman codes
71
+ * compression of sparse bitarrays
72
+ * (de-) serialization
73
+ * various count functions
74
+ * other helpful functions
75
+
76
+
77
+ Installation
78
+ ------------
79
+
80
+ Python wheels are are available on PyPI for all major platforms and Python
81
+ versions. Which means you can simply:
82
+
83
+ .. code-block:: shell-session
84
+
85
+ $ pip install bitarray
86
+
87
+ Once you have installed the package, you may want to test it:
88
+
89
+ .. code-block:: shell-session
90
+
91
+ $ python -c 'import bitarray; bitarray.test()'
92
+ bitarray is installed in: /Users/ilan/bitarray/bitarray
93
+ bitarray version: 3.7.2
94
+ sys.version: 3.13.5 (main, Jun 16 2025) [Clang 18.1.8]
95
+ sys.prefix: /Users/ilan/miniforge
96
+ pointer size: 64 bit
97
+ sizeof(size_t): 8
98
+ sizeof(bitarrayobject): 80
99
+ HAVE_BUILTIN_BSWAP64: 1
100
+ default bit-endianness: big
101
+ machine byte-order: little
102
+ Py_DEBUG: 0
103
+ DEBUG: 0
104
+ .........................................................................
105
+ .........................................................................
106
+ ................................................................
107
+ ----------------------------------------------------------------------
108
+ Ran 597 tests in 0.165s
109
+
110
+ OK
111
+
112
+ The ``test()`` function is part of the API. It will return
113
+ a ``unittest.runner.TextTestResult`` object, such that one can verify that
114
+ all tests ran successfully by:
115
+
116
+ .. code-block:: python
117
+
118
+ import bitarray
119
+ assert bitarray.test().wasSuccessful()
120
+
121
+
122
+ Usage
123
+ -----
124
+
125
+ As mentioned above, bitarray objects behave very much like lists, so
126
+ there is not too much to learn. The biggest difference from list
127
+ objects (except that bitarray are obviously homogeneous) is the ability
128
+ to access the machine representation of the object.
129
+ When doing so, the bit-endianness is of importance; this issue is
130
+ explained in detail in the section below. Here, we demonstrate the
131
+ basic usage of bitarray objects:
132
+
133
+ .. code-block:: python
134
+
135
+ >>> from bitarray import bitarray
136
+ >>> a = bitarray() # create empty bitarray
137
+ >>> a.append(1)
138
+ >>> a.extend([1, 0])
139
+ >>> a
140
+ bitarray('110')
141
+ >>> x = bitarray(2 ** 20) # bitarray of length 1048576 (initialized to 0)
142
+ >>> len(x)
143
+ 1048576
144
+ >>> bitarray('1001 011') # initialize from string (whitespace is ignored)
145
+ bitarray('1001011')
146
+ >>> lst = [1, 0, False, True, True]
147
+ >>> a = bitarray(lst) # initialize from iterable
148
+ >>> a
149
+ bitarray('10011')
150
+ >>> a[2] # indexing a single item will always return an integer
151
+ 0
152
+ >>> a[2:4] # whereas indexing a slice will always return a bitarray
153
+ bitarray('01')
154
+ >>> a[2:3] # even when the slice length is just one
155
+ bitarray('0')
156
+ >>> a.count(1)
157
+ 3
158
+ >>> a.remove(0) # removes first occurrence of 0
159
+ >>> a
160
+ bitarray('1011')
161
+
162
+ Like lists, bitarray objects support slice assignment and deletion:
163
+
164
+ .. code-block:: python
165
+
166
+ >>> a = bitarray(50)
167
+ >>> a.setall(0) # set all elements in a to 0
168
+ >>> a[11:37:3] = 9 * bitarray('1')
169
+ >>> a
170
+ bitarray('00000000000100100100100100100100100100000000000000')
171
+ >>> del a[12::3]
172
+ >>> a
173
+ bitarray('0000000000010101010101010101000000000')
174
+ >>> a[-6:] = bitarray('10011')
175
+ >>> a
176
+ bitarray('000000000001010101010101010100010011')
177
+ >>> a += bitarray('000111')
178
+ >>> a[9:]
179
+ bitarray('001010101010101010100010011000111')
180
+
181
+ In addition, slices can be assigned to booleans, which is easier (and
182
+ faster) than assigning to a bitarray in which all values are the same:
183
+
184
+ .. code-block:: python
185
+
186
+ >>> a = 20 * bitarray('0')
187
+ >>> a[1:15:3] = True
188
+ >>> a
189
+ bitarray('01001001001001000000')
190
+
191
+ This is easier and faster than:
192
+
193
+ .. code-block:: python
194
+
195
+ >>> a = 20 * bitarray('0')
196
+ >>> a[1:15:3] = 5 * bitarray('1')
197
+ >>> a
198
+ bitarray('01001001001001000000')
199
+
200
+ Note that in the latter we have to create a temporary bitarray whose length
201
+ must be known or calculated. Another example of assigning slices to Booleans,
202
+ is setting ranges:
203
+
204
+ .. code-block:: python
205
+
206
+ >>> a = bitarray(30)
207
+ >>> a[:] = 0 # set all elements to 0 - equivalent to a.setall(0)
208
+ >>> a[10:25] = 1 # set elements in range(10, 25) to 1
209
+ >>> a
210
+ bitarray('000000000011111111111111100000')
211
+
212
+ As of bitarray version 2.8, indices may also be lists of arbitrary
213
+ indices (like in NumPy), or bitarrays that are treated as masks,
214
+ see `Bitarray indexing <https://github.com/ilanschnell/bitarray/blob/master/doc/indexing.rst>`__.
215
+
216
+
217
+ Bitwise operators
218
+ -----------------
219
+
220
+ Bitarray objects support the bitwise operators ``~``, ``&``, ``|``, ``^``,
221
+ ``<<``, ``>>`` (as well as their in-place versions ``&=``, ``|=``, ``^=``,
222
+ ``<<=``, ``>>=``). The behavior is very much what one would expect:
223
+
224
+ .. code-block:: python
225
+
226
+ >>> a = bitarray('101110001')
227
+ >>> ~a # invert
228
+ bitarray('010001110')
229
+ >>> b = bitarray('111001011')
230
+ >>> a ^ b # bitwise XOR
231
+ bitarray('010111010')
232
+ >>> a &= b # inplace AND
233
+ >>> a
234
+ bitarray('101000001')
235
+ >>> a <<= 2 # in-place left-shift by 2
236
+ >>> a
237
+ bitarray('100000100')
238
+ >>> b >> 1 # return b right-shifted by 1
239
+ bitarray('011100101')
240
+
241
+ The C language does not specify the behavior of negative shifts and
242
+ of left shifts larger or equal than the width of the promoted left operand.
243
+ The exact behavior is compiler/machine specific.
244
+ This Python bitarray library specifies the behavior as follows:
245
+
246
+ * the length of the bitarray is never changed by any shift operation
247
+ * blanks are filled by 0
248
+ * negative shifts raise ``ValueError``
249
+ * shifts larger or equal to the length of the bitarray result in
250
+ bitarrays with all values 0
251
+
252
+ It is worth noting that (regardless of bit-endianness) the bitarray left
253
+ shift (``<<``) always shifts towards lower indices, and the right
254
+ shift (``>>``) always shifts towards higher indices.
255
+
256
+
257
+ Bit-endianness
258
+ --------------
259
+
260
+ For many purposes the bit-endianness is not of any relevance to the end user
261
+ and can be regarded as an implementation detail of bitarray objects.
262
+ However, there are use cases when the bit-endianness becomes important.
263
+ These use cases involve explicitly reading and writing the bitarray buffer
264
+ using ``.tobytes()``, ``.frombytes()``, ``.tofile()`` or ``.fromfile()``,
265
+ importing and exporting buffers. Also, a number of utility functions
266
+ in ``bitarray.util`` will return different results depending on
267
+ bit-endianness, such as ``ba2hex()`` or ``ba2int``.
268
+ To better understand this topic, please read `bit-endianness <https://github.com/ilanschnell/bitarray/blob/master/doc/endianness.rst>`__.
269
+
270
+
271
+ Buffer protocol
272
+ ---------------
273
+
274
+ Bitarray objects support the buffer protocol. They can both export their
275
+ own buffer, as well as import another object's buffer. To learn more about
276
+ this topic, please read `buffer protocol <https://github.com/ilanschnell/bitarray/blob/master/doc/buffer.rst>`__. There is also an example that shows how
277
+ to memory-map a file to a bitarray: `mmapped-file.py <https://github.com/ilanschnell/bitarray/blob/master/examples/mmapped-file.py>`__
278
+
279
+
280
+ Variable bit length prefix codes
281
+ --------------------------------
282
+
283
+ The ``.encode()`` method takes a dictionary mapping symbols to bitarrays
284
+ and an iterable, and extends the bitarray object with the encoded symbols
285
+ found while iterating. For example:
286
+
287
+ .. code-block:: python
288
+
289
+ >>> d = {'H':bitarray('111'), 'e':bitarray('0'),
290
+ ... 'l':bitarray('110'), 'o':bitarray('10')}
291
+ ...
292
+ >>> a = bitarray()
293
+ >>> a.encode(d, 'Hello')
294
+ >>> a
295
+ bitarray('111011011010')
296
+
297
+ Note that the string ``'Hello'`` is an iterable, but the symbols are not
298
+ limited to characters, in fact any immutable Python object can be a symbol.
299
+ Taking the same dictionary, we can apply the ``.decode()`` method which will
300
+ return an iterable of the symbols:
301
+
302
+ .. code-block:: python
303
+
304
+ >>> list(a.decode(d))
305
+ ['H', 'e', 'l', 'l', 'o']
306
+ >>> ''.join(a.decode(d))
307
+ 'Hello'
308
+
309
+ Symbols are not limited to being characters.
310
+ The above dictionary ``d`` can be efficiently constructed using the function
311
+ ``bitarray.util.huffman_code()``. I also wrote `Huffman coding in Python
312
+ using bitarray <http://ilan.schnell-web.net/prog/huffman/>`__ for more
313
+ background information.
314
+
315
+ When the codes are large, and you have many decode calls, most time will
316
+ be spent creating the (same) internal decode tree objects. In this case,
317
+ it will be much faster to create a ``decodetree`` object, which can be
318
+ passed to bitarray's ``.decode()`` method, instead of passing the prefix
319
+ code dictionary to those methods itself:
320
+
321
+ .. code-block:: python
322
+
323
+ >>> from bitarray import bitarray, decodetree
324
+ >>> t = decodetree({'a': bitarray('0'), 'b': bitarray('1')})
325
+ >>> a = bitarray('0110')
326
+ >>> list(a.decode(t))
327
+ ['a', 'b', 'b', 'a']
328
+
329
+ The sole purpose of the immutable ``decodetree`` object is to be passed
330
+ to bitarray's ``.decode()`` method.
331
+
332
+
333
+ Frozenbitarrays
334
+ ---------------
335
+
336
+ A ``frozenbitarray`` object is very similar to the bitarray object.
337
+ The difference is that this a ``frozenbitarray`` is immutable, and hashable,
338
+ and can therefore be used as a dictionary key:
339
+
340
+ .. code-block:: python
341
+
342
+ >>> from bitarray import frozenbitarray
343
+ >>> key = frozenbitarray('1100011')
344
+ >>> {key: 'some value'}
345
+ {frozenbitarray('1100011'): 'some value'}
346
+ >>> key[3] = 1
347
+ Traceback (most recent call last):
348
+ ...
349
+ TypeError: frozenbitarray is immutable
350
+
351
+
352
+ Reference
353
+ =========
354
+
355
+ bitarray version: 3.7.2 -- `change log <https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst>`__
356
+
357
+ In the following, ``item`` and ``value`` are usually a single bit -
358
+ an integer 0 or 1.
359
+
360
+ Also, ``sub_bitarray`` refers to either a bitarray, or an ``item``.
361
+
362
+
363
+ The bitarray object:
364
+ --------------------
365
+
366
+ ``bitarray(initializer=0, /, endian='big', buffer=None)`` -> bitarray
367
+ Return a new bitarray object whose items are bits initialized from
368
+ the optional initializer, and bit-endianness.
369
+ The initializer may be one of the following types:
370
+ a.) ``int`` bitarray, initialized to zeros, of given length
371
+ b.) ``bytes`` or ``bytearray`` to initialize buffer directly
372
+ c.) ``str`` of 0s and 1s, ignoring whitespace and "_"
373
+ d.) iterable of integers 0 or 1.
374
+
375
+ Optional keyword arguments:
376
+
377
+ ``endian``: Specifies the bit-endianness of the created bitarray object.
378
+ Allowed values are ``big`` and ``little`` (the default is ``big``).
379
+ The bit-endianness effects the buffer representation of the bitarray.
380
+
381
+ ``buffer``: Any object which exposes a buffer. When provided, ``initializer``
382
+ cannot be present (or has to be ``None``). The imported buffer may be
383
+ read-only or writable, depending on the object type.
384
+
385
+ New in version 2.3: optional ``buffer`` argument
386
+
387
+ New in version 3.4: allow initializer ``bytes`` or ``bytearray`` to set buffer directly
388
+
389
+
390
+ bitarray methods:
391
+ -----------------
392
+
393
+ ``all()`` -> bool
394
+ Return ``True`` when all bits in bitarray are 1.
395
+ ``a.all()`` is a faster version of ``all(a)``.
396
+
397
+
398
+ ``any()`` -> bool
399
+ Return ``True`` when any bit in bitarray is 1.
400
+ ``a.any()`` is a faster version of ``any(a)``.
401
+
402
+
403
+ ``append(item, /)``
404
+ Append ``item`` to the end of the bitarray.
405
+
406
+
407
+ ``buffer_info()`` -> BufferInfo
408
+ Return named tuple with following fields:
409
+
410
+ 0. ``address``: memory address of buffer
411
+ 1. ``nbytes``: buffer size (in bytes)
412
+ 2. ``endian``: bit-endianness as a string
413
+ 3. ``padbits``: number of pad bits
414
+ 4. ``alloc``: allocated memory for buffer (in bytes)
415
+ 5. ``readonly``: memory is read-only (bool)
416
+ 6. ``imported``: buffer is imported (bool)
417
+ 7. ``exports``: number of buffer exports
418
+
419
+ New in version 3.7: return named tuple
420
+
421
+
422
+ ``bytereverse(start=0, stop=<end of buffer>, /)``
423
+ For each byte in byte-range(``start``, ``stop``) reverse bits in-place.
424
+ The start and stop indices are given in terms of bytes (not bits).
425
+ Also note that this method only changes the buffer; it does not change the
426
+ bit-endianness of the bitarray object. Pad bits are left unchanged such
427
+ that two consecutive calls will always leave the bitarray unchanged.
428
+
429
+ New in version 2.2.5: optional start and stop arguments
430
+
431
+
432
+ ``clear()``
433
+ Remove all items from bitarray.
434
+
435
+ New in version 1.4
436
+
437
+
438
+ ``copy()`` -> bitarray
439
+ Return copy of bitarray (with same bit-endianness).
440
+
441
+
442
+ ``count(value=1, start=0, stop=<end>, step=1, /)`` -> int
443
+ Number of occurrences of ``value`` bitarray within ``[start:stop:step]``.
444
+ Optional arguments ``start``, ``stop`` and ``step`` are interpreted in
445
+ slice notation, meaning ``a.count(value, start, stop, step)`` equals
446
+ ``a[start:stop:step].count(value)``.
447
+ The ``value`` may also be a sub-bitarray. In this case non-overlapping
448
+ occurrences are counted within ``[start:stop]`` (``step`` must be 1).
449
+
450
+ New in version 1.1.0: optional start and stop arguments
451
+
452
+ New in version 2.3.7: optional step argument
453
+
454
+ New in version 2.9: add non-overlapping sub-bitarray count
455
+
456
+
457
+ ``decode(code, /)`` -> iterator
458
+ Given a prefix code (a dict mapping symbols to bitarrays, or ``decodetree``
459
+ object), decode content of bitarray and return an iterator over
460
+ corresponding symbols.
461
+
462
+ See also: `Bitarray 3 transition <https://github.com/ilanschnell/bitarray/blob/master/doc/bitarray3.rst>`__
463
+
464
+ New in version 3.0: returns iterator (equivalent to past ``.iterdecode()``)
465
+
466
+
467
+ ``encode(code, iterable, /)``
468
+ Given a prefix code (a dict mapping symbols to bitarrays),
469
+ iterate over the iterable object with symbols, and extend bitarray
470
+ with corresponding bitarray for each symbol.
471
+
472
+
473
+ ``extend(iterable, /)``
474
+ Append items from to the end of the bitarray.
475
+ If ``iterable`` is a (Unicode) string, each ``0`` and ``1`` are appended as
476
+ bits (ignoring whitespace and underscore).
477
+
478
+ New in version 3.4: allow ``bytes`` object
479
+
480
+
481
+ ``fill()`` -> int
482
+ Add zeros to the end of the bitarray, such that the length will be
483
+ a multiple of 8, and return the number of bits added [0..7].
484
+
485
+
486
+ ``find(sub_bitarray, start=0, stop=<end>, /, right=False)`` -> int
487
+ Return lowest (or rightmost when ``right=True``) index where sub_bitarray
488
+ is found, such that sub_bitarray is contained within ``[start:stop]``.
489
+ Return -1 when sub_bitarray is not found.
490
+
491
+ New in version 2.1
492
+
493
+ New in version 2.9: add optional keyword argument ``right``
494
+
495
+
496
+ ``frombytes(bytes, /)``
497
+ Extend bitarray with raw bytes from a bytes-like object.
498
+ Each added byte will add eight bits to the bitarray.
499
+
500
+ New in version 2.5.0: allow bytes-like argument
501
+
502
+
503
+ ``fromfile(f, n=-1, /)``
504
+ Extend bitarray with up to ``n`` bytes read from file object ``f`` (or any
505
+ other binary stream what supports a ``.read()`` method, e.g. ``io.BytesIO``).
506
+ Each read byte will add eight bits to the bitarray. When ``n`` is omitted
507
+ or negative, reads and extends all data until EOF.
508
+ When ``n`` is non-negative but exceeds the available data, ``EOFError`` is
509
+ raised. However, the available data is still read and extended.
510
+
511
+
512
+ ``index(sub_bitarray, start=0, stop=<end>, /, right=False)`` -> int
513
+ Return lowest (or rightmost when ``right=True``) index where sub_bitarray
514
+ is found, such that sub_bitarray is contained within ``[start:stop]``.
515
+ Raises ``ValueError`` when sub_bitarray is not present.
516
+
517
+ New in version 2.9: add optional keyword argument ``right``
518
+
519
+
520
+ ``insert(index, value, /)``
521
+ Insert ``value`` into bitarray before ``index``.
522
+
523
+
524
+ ``invert(index=<all bits>, /)``
525
+ Invert all bits in bitarray (in-place).
526
+ When the optional ``index`` is given, only invert the single bit at ``index``.
527
+
528
+ New in version 1.5.3: optional index argument
529
+
530
+
531
+ ``pack(bytes, /)``
532
+ Extend bitarray from a bytes-like object, where each byte corresponds
533
+ to a single bit. The byte ``b'\x00'`` maps to bit 0 and all other bytes
534
+ map to bit 1.
535
+
536
+ This method, as well as the ``.unpack()`` method, are meant for efficient
537
+ transfer of data between bitarray objects to other Python objects (for
538
+ example NumPy's ndarray object) which have a different memory view.
539
+
540
+ New in version 2.5.0: allow bytes-like argument
541
+
542
+
543
+ ``pop(index=-1, /)`` -> item
544
+ Remove and return item at ``index`` (default last).
545
+ Raises ``IndexError`` if index is out of range.
546
+
547
+
548
+ ``remove(value, /)``
549
+ Remove the first occurrence of ``value``.
550
+ Raises ``ValueError`` if value is not present.
551
+
552
+
553
+ ``reverse()``
554
+ Reverse all bits in bitarray (in-place).
555
+
556
+
557
+ ``search(sub_bitarray, start=0, stop=<end>, /, right=False)`` -> iterator
558
+ Return iterator over indices where sub_bitarray is found, such that
559
+ sub_bitarray is contained within ``[start:stop]``.
560
+ The indices are iterated in ascending order (from lowest to highest),
561
+ unless ``right=True``, which will iterate in descending order (starting with
562
+ rightmost match).
563
+
564
+ See also: `Bitarray 3 transition <https://github.com/ilanschnell/bitarray/blob/master/doc/bitarray3.rst>`__
565
+
566
+ New in version 2.9: optional start and stop arguments - add optional keyword argument ``right``
567
+
568
+ New in version 3.0: returns iterator (equivalent to past ``.itersearch()``)
569
+
570
+
571
+ ``setall(value, /)``
572
+ Set all elements in bitarray to ``value``.
573
+ Note that ``a.setall(value)`` is equivalent to ``a[:] = value``.
574
+
575
+
576
+ ``sort(reverse=False)``
577
+ Sort all bits in bitarray (in-place).
578
+
579
+
580
+ ``to01(group=0, sep=' ')`` -> str
581
+ Return bitarray as (Unicode) string of ``0``s and ``1``s.
582
+ The bits are grouped into ``group`` bits (default is no grouping).
583
+ When grouped, the string ``sep`` is inserted between groups
584
+ of ``group`` characters, default is a space.
585
+
586
+ New in version 3.3: optional ``group`` and ``sep`` arguments
587
+
588
+
589
+ ``tobytes()`` -> bytes
590
+ Return the bitarray buffer (pad bits are set to zero).
591
+
592
+
593
+ ``tofile(f, /)``
594
+ Write bitarray buffer to file object ``f``.
595
+
596
+
597
+ ``tolist()`` -> list
598
+ Return bitarray as list of integers.
599
+ ``a.tolist()`` equals ``list(a)``.
600
+
601
+ Note that the list object being created will require 32 or 64 times more
602
+ memory (depending on the machine architecture) than the bitarray object,
603
+ which may cause a memory error if the bitarray is very large.
604
+
605
+
606
+ ``unpack(zero=b'\x00', one=b'\x01')`` -> bytes
607
+ Return bytes that contain one byte for each bit in the bitarray,
608
+ using specified mapping.
609
+
610
+
611
+ bitarray data descriptors:
612
+ --------------------------
613
+
614
+ Data descriptors were added in version 2.6.
615
+
616
+ ``endian`` -> str
617
+ bit-endianness as Unicode string
618
+
619
+ New in version 3.4: replaces former ``.endian()`` method
620
+
621
+
622
+ ``nbytes`` -> int
623
+ buffer size in bytes
624
+
625
+
626
+ ``padbits`` -> int
627
+ number of pad bits
628
+
629
+
630
+ ``readonly`` -> bool
631
+ bool indicating whether buffer is read-only
632
+
633
+
634
+ Other objects:
635
+ --------------
636
+
637
+ ``frozenbitarray(initializer=0, /, endian='big', buffer=None)`` -> frozenbitarray
638
+ Return a ``frozenbitarray`` object. Initialized the same way a ``bitarray``
639
+ object is initialized. A ``frozenbitarray`` is immutable and hashable,
640
+ and may therefore be used as a dictionary key.
641
+
642
+ New in version 1.1
643
+
644
+
645
+ ``decodetree(code, /)`` -> decodetree
646
+ Given a prefix code (a dict mapping symbols to bitarrays),
647
+ create a binary tree object to be passed to ``.decode()``.
648
+
649
+ New in version 1.6
650
+
651
+
652
+ Functions defined in the `bitarray` module:
653
+ -------------------------------------------
654
+
655
+ ``bits2bytes(n, /)`` -> int
656
+ Return the number of bytes necessary to store n bits.
657
+
658
+
659
+ ``get_default_endian()`` -> str
660
+ Return the default bit-endianness for new bitarray objects being created.
661
+ Unless ``_set_default_endian('little')`` was called, the default
662
+ bit-endianness is ``big``.
663
+
664
+ New in version 1.3
665
+
666
+
667
+ ``test(verbosity=1)`` -> TextTestResult
668
+ Run self-test, and return ``unittest.runner.TextTestResult`` object.
669
+
670
+
671
+ Functions defined in `bitarray.util` module:
672
+ --------------------------------------------
673
+
674
+ This sub-module was added in version 1.2.
675
+
676
+ ``any_and(a, b, /)`` -> bool
677
+ Efficient implementation of ``any(a & b)``.
678
+
679
+ New in version 2.7
680
+
681
+
682
+ ``ba2base(n, bitarray, /, group=0, sep=' ')`` -> str
683
+ Return a string containing the base ``n`` ASCII representation of
684
+ the bitarray. Allowed values for ``n`` are 2, 4, 8, 16, 32 and 64.
685
+ The bitarray has to be multiple of length 1, 2, 3, 4, 5 or 6 respectively.
686
+ For ``n=32`` the RFC 4648 Base32 alphabet is used, and for ``n=64`` the
687
+ standard base 64 alphabet is used.
688
+ When grouped, the string ``sep`` is inserted between groups
689
+ of ``group`` characters, default is a space.
690
+
691
+ See also: `Bitarray representations <https://github.com/ilanschnell/bitarray/blob/master/doc/represent.rst>`__
692
+
693
+ New in version 1.9
694
+
695
+ New in version 3.3: optional ``group`` and ``sep`` arguments
696
+
697
+
698
+ ``ba2hex(bitarray, /, group=0, sep=' ')`` -> hexstr
699
+ Return a string containing the hexadecimal representation of
700
+ the bitarray (which has to be multiple of 4 in length).
701
+ When grouped, the string ``sep`` is inserted between groups
702
+ of ``group`` characters, default is a space.
703
+
704
+ New in version 3.3: optional ``group`` and ``sep`` arguments
705
+
706
+
707
+ ``ba2int(bitarray, /, signed=False)`` -> int
708
+ Convert the given bitarray to an integer.
709
+ The bit-endianness of the bitarray is respected.
710
+ ``signed`` indicates whether two's complement is used to represent the integer.
711
+
712
+
713
+ ``base2ba(n, asciistr, /, endian=None)`` -> bitarray
714
+ Bitarray of base ``n`` ASCII representation.
715
+ Allowed values for ``n`` are 2, 4, 8, 16, 32 and 64.
716
+ For ``n=32`` the RFC 4648 Base32 alphabet is used, and for ``n=64`` the
717
+ standard base 64 alphabet is used. Whitespace is ignored.
718
+
719
+ See also: `Bitarray representations <https://github.com/ilanschnell/bitarray/blob/master/doc/represent.rst>`__
720
+
721
+ New in version 1.9
722
+
723
+ New in version 3.3: ignore whitespace
724
+
725
+
726
+ ``byteswap(a, n=<buffer size>, /)``
727
+ Reverse every ``n`` consecutive bytes of ``a`` in-place.
728
+ By default, all bytes are reversed. Note that ``n`` is not limited to 2, 4
729
+ or 8, but can be any positive integer.
730
+ Also, ``a`` may be any object that exposes a writable buffer.
731
+ Nothing about this function is specific to bitarray objects.
732
+
733
+ We should mention that Python's ``array.array`` object has a
734
+ method ``.byteswap()`` with similar functionality. However, unlike
735
+ bitarray's ``util.byteswap()`` function, this method is limited to
736
+ swapping 2, 4, or 8 consecutive bytes.
737
+
738
+ New in version 3.4
739
+
740
+
741
+ ``canonical_decode(bitarray, count, symbol, /)`` -> iterator
742
+ Decode bitarray using canonical Huffman decoding tables
743
+ where ``count`` is a sequence containing the number of symbols of each length
744
+ and ``symbol`` is a sequence of symbols in canonical order.
745
+
746
+ See also: `Canonical Huffman Coding <https://github.com/ilanschnell/bitarray/blob/master/doc/canonical.rst>`__
747
+
748
+ New in version 2.5
749
+
750
+
751
+ ``canonical_huffman(dict, /)`` -> tuple
752
+ Given a frequency map, a dictionary mapping symbols to their frequency,
753
+ calculate the canonical Huffman code. Returns a tuple containing:
754
+
755
+ 0. the canonical Huffman code as a dict mapping symbols to bitarrays
756
+ 1. a list containing the number of symbols of each code length
757
+ 2. a list of symbols in canonical order
758
+
759
+ Note: the two lists may be used as input for ``canonical_decode()``.
760
+
761
+ See also: `Canonical Huffman Coding <https://github.com/ilanschnell/bitarray/blob/master/doc/canonical.rst>`__
762
+
763
+ New in version 2.5
764
+
765
+
766
+ ``correspond_all(a, b, /)`` -> tuple
767
+ Return tuple with counts of: ~a & ~b, ~a & b, a & ~b, a & b
768
+
769
+ New in version 3.4
770
+
771
+
772
+ ``count_and(a, b, /)`` -> int
773
+ Return ``(a & b).count()`` in a memory efficient manner,
774
+ as no intermediate bitarray object gets created.
775
+
776
+
777
+ ``count_n(a, n, value=1, /)`` -> int
778
+ Return lowest index ``i`` for which ``a[:i].count(value) == n``.
779
+ Raises ``ValueError`` when ``n`` exceeds total count (``a.count(value)``).
780
+
781
+ New in version 2.3.6: optional value argument
782
+
783
+
784
+ ``count_or(a, b, /)`` -> int
785
+ Return ``(a | b).count()`` in a memory efficient manner,
786
+ as no intermediate bitarray object gets created.
787
+
788
+
789
+ ``count_xor(a, b, /)`` -> int
790
+ Return ``(a ^ b).count()`` in a memory efficient manner,
791
+ as no intermediate bitarray object gets created.
792
+
793
+ This is also known as the Hamming distance.
794
+
795
+
796
+ ``deserialize(bytes, /)`` -> bitarray
797
+ Return a bitarray given a bytes-like representation such as returned
798
+ by ``serialize()``.
799
+
800
+ See also: `Bitarray representations <https://github.com/ilanschnell/bitarray/blob/master/doc/represent.rst>`__
801
+
802
+ New in version 1.8
803
+
804
+ New in version 2.5.0: allow bytes-like argument
805
+
806
+
807
+ ``gen_primes(n, /, endian=None, odd=False)`` -> bitarray
808
+ Generate a bitarray of length ``n`` in which active indices are prime numbers.
809
+ By default (``odd=False``), active indices correspond to prime numbers directly.
810
+ When ``odd=True``, only odd prime numbers are represented in the resulting
811
+ bitarray ``a``, and ``a[i]`` corresponds to ``2*i+1`` being prime or not.
812
+
813
+ Apart from working with prime numbers, this function is useful for
814
+ testing, as it provides a simple way to create a well-defined bitarray
815
+ of any length.
816
+
817
+ New in version 3.7
818
+
819
+
820
+ ``hex2ba(hexstr, /, endian=None)`` -> bitarray
821
+ Bitarray of hexadecimal representation. hexstr may contain any number
822
+ (including odd numbers) of hex digits (upper or lower case).
823
+ Whitespace is ignored.
824
+
825
+ New in version 3.3: ignore whitespace
826
+
827
+
828
+ ``huffman_code(dict, /, endian=None)`` -> dict
829
+ Given a frequency map, a dictionary mapping symbols to their frequency,
830
+ calculate the Huffman code, i.e. a dict mapping those symbols to
831
+ bitarrays (with given bit-endianness). Note that the symbols are not limited
832
+ to being strings. Symbols may be any hashable object.
833
+
834
+
835
+ ``int2ba(int, /, length=None, endian=None, signed=False)`` -> bitarray
836
+ Convert the given integer to a bitarray (with given bit-endianness,
837
+ and no leading (big-endian) / trailing (little-endian) zeros), unless
838
+ the ``length`` of the bitarray is provided. An ``OverflowError`` is raised
839
+ if the integer is not representable with the given number of bits.
840
+ ``signed`` determines whether two's complement is used to represent the integer,
841
+ and requires ``length`` to be provided.
842
+
843
+
844
+ ``intervals(bitarray, /)`` -> iterator
845
+ Compute all uninterrupted intervals of 1s and 0s, and return an
846
+ iterator over tuples ``(value, start, stop)``. The intervals are guaranteed
847
+ to be in order, and their size is always non-zero (``stop - start > 0``).
848
+
849
+ New in version 2.7
850
+
851
+
852
+ ``ones(n, /, endian=None)`` -> bitarray
853
+ Create a bitarray of length ``n``, with all values ``1``, and optional
854
+ bit-endianness (``little`` or ``big``).
855
+
856
+ New in version 2.9
857
+
858
+
859
+ ``parity(a, /)`` -> int
860
+ Return parity of bitarray ``a``.
861
+ ``parity(a)`` is equivalent to ``a.count() % 2`` but more efficient.
862
+
863
+ New in version 1.9
864
+
865
+
866
+ ``pprint(bitarray, /, stream=None, group=8, indent=4, width=80)``
867
+ Pretty-print bitarray object to ``stream``, defaults is ``sys.stdout``.
868
+ By default, bits are grouped in bytes (8 bits), and 64 bits per line.
869
+ Non-bitarray objects are printed using ``pprint.pprint()``.
870
+
871
+ New in version 1.8
872
+
873
+
874
+ ``random_k(n, /, k, endian=None)`` -> bitarray
875
+ Return (pseudo-) random bitarray of length ``n`` with ``k`` elements
876
+ set to one. Mathematically equivalent to setting (in a bitarray of
877
+ length ``n``) all bits at indices ``random.sample(range(n), k)`` to one.
878
+ The random bitarrays are reproducible when giving Python's ``random.seed()``
879
+ a specific seed value.
880
+
881
+ New in version 3.6
882
+
883
+
884
+ ``random_p(n, /, p=0.5, endian=None)`` -> bitarray
885
+ Return (pseudo-) random bitarray of length ``n``, where each bit has
886
+ probability ``p`` of being one (independent of any other bits). Mathematically
887
+ equivalent to ``bitarray((random() < p for _ in range(n)), endian)``, but much
888
+ faster for large ``n``. The random bitarrays are reproducible when giving
889
+ Python's ``random.seed()`` with a specific seed value.
890
+
891
+ This function requires Python 3.12 or higher, as it depends on the standard
892
+ library function ``random.binomialvariate()``. Raises ``NotImplementedError``
893
+ when Python version is too low.
894
+
895
+ See also: `Random Bitarrays <https://github.com/ilanschnell/bitarray/blob/master/doc/random_p.rst>`__
896
+
897
+ New in version 3.5
898
+
899
+
900
+ ``sc_decode(stream, /)`` -> bitarray
901
+ Decompress binary stream (an integer iterator, or bytes-like object) of a
902
+ sparse compressed (``sc``) bitarray, and return the decoded bitarray.
903
+ This function consumes only one bitarray and leaves the remaining stream
904
+ untouched. Use ``sc_encode()`` for compressing (encoding).
905
+
906
+ See also: `Compression of sparse bitarrays <https://github.com/ilanschnell/bitarray/blob/master/doc/sparse_compression.rst>`__
907
+
908
+ New in version 2.7
909
+
910
+
911
+ ``sc_encode(bitarray, /)`` -> bytes
912
+ Compress a sparse bitarray and return its binary representation.
913
+ This representation is useful for efficiently storing sparse bitarrays.
914
+ Use ``sc_decode()`` for decompressing (decoding).
915
+
916
+ See also: `Compression of sparse bitarrays <https://github.com/ilanschnell/bitarray/blob/master/doc/sparse_compression.rst>`__
917
+
918
+ New in version 2.7
919
+
920
+
921
+ ``serialize(bitarray, /)`` -> bytes
922
+ Return a serialized representation of the bitarray, which may be passed to
923
+ ``deserialize()``. It efficiently represents the bitarray object (including
924
+ its bit-endianness) and is guaranteed not to change in future releases.
925
+
926
+ See also: `Bitarray representations <https://github.com/ilanschnell/bitarray/blob/master/doc/represent.rst>`__
927
+
928
+ New in version 1.8
929
+
930
+
931
+ ``strip(bitarray, /, mode='right')`` -> bitarray
932
+ Return a new bitarray with zeros stripped from left, right or both ends.
933
+ Allowed values for mode are the strings: ``left``, ``right``, ``both``
934
+
935
+
936
+ ``subset(a, b, /)`` -> bool
937
+ Return ``True`` if bitarray ``a`` is a subset of bitarray ``b``.
938
+ ``subset(a, b)`` is equivalent to ``a | b == b`` (and equally ``a & b == a``) but
939
+ more efficient as no intermediate bitarray object is created and the buffer
940
+ iteration is stopped as soon as one mismatch is found.
941
+
942
+
943
+ ``sum_indices(a, /, mode=1)`` -> int
944
+ Return sum of indices of all active bits in bitarray ``a``.
945
+ Equivalent to ``sum(i for i, v in enumerate(a) if v)``.
946
+ ``mode=2`` sums square of indices.
947
+
948
+ New in version 3.6
949
+
950
+ New in version 3.7: add optional mode argument
951
+
952
+
953
+ ``urandom(n, /, endian=None)`` -> bitarray
954
+ Return random bitarray of length ``n`` (uses ``os.urandom()``).
955
+
956
+ New in version 1.7
957
+
958
+
959
+ ``vl_decode(stream, /, endian=None)`` -> bitarray
960
+ Decode binary stream (an integer iterator, or bytes-like object), and
961
+ return the decoded bitarray. This function consumes only one bitarray and
962
+ leaves the remaining stream untouched. Use ``vl_encode()`` for encoding.
963
+
964
+ See also: `Variable length bitarray format <https://github.com/ilanschnell/bitarray/blob/master/doc/variable_length.rst>`__
965
+
966
+ New in version 2.2
967
+
968
+
969
+ ``vl_encode(bitarray, /)`` -> bytes
970
+ Return variable length binary representation of bitarray.
971
+ This representation is useful for efficiently storing small bitarray
972
+ in a binary stream. Use ``vl_decode()`` for decoding.
973
+
974
+ See also: `Variable length bitarray format <https://github.com/ilanschnell/bitarray/blob/master/doc/variable_length.rst>`__
975
+
976
+ New in version 2.2
977
+
978
+
979
+ ``xor_indices(a, /)`` -> int
980
+ Return xor reduced indices of all active bits in bitarray ``a``.
981
+ This is essentially equivalent to
982
+ ``reduce(operator.xor, (i for i, v in enumerate(a) if v))``.
983
+
984
+ New in version 3.2
985
+
986
+
987
+ ``zeros(n, /, endian=None)`` -> bitarray
988
+ Create a bitarray of length ``n``, with all values ``0``, and optional
989
+ bit-endianness (``little`` or ``big``).
990
+
991
+