bitarray 3.5.2__tar.gz → 3.6.1__tar.gz
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.
- {bitarray-3.5.2 → bitarray-3.6.1}/PKG-INFO +30 -16
- {bitarray-3.5.2 → bitarray-3.6.1}/README.rst +29 -15
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray/_bitarray.c +5 -48
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray/_util.c +107 -19
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray/bitarray.h +30 -1
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray/test_bitarray.py +0 -214
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray/test_util.py +304 -259
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray/util.py +123 -26
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray/util.pyi +4 -0
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray.egg-info/PKG-INFO +30 -16
- {bitarray-3.5.2 → bitarray-3.6.1}/LICENSE +0 -0
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray/__init__.py +0 -0
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray/__init__.pyi +0 -0
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray/py.typed +0 -0
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray/pythoncapi_compat.h +0 -0
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray/test_281.pickle +0 -0
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray.egg-info/SOURCES.txt +0 -0
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray.egg-info/dependency_links.txt +0 -0
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray.egg-info/not-zip-safe +0 -0
- {bitarray-3.5.2 → bitarray-3.6.1}/bitarray.egg-info/top_level.txt +0 -0
- {bitarray-3.5.2 → bitarray-3.6.1}/pyproject.toml +0 -0
- {bitarray-3.5.2 → bitarray-3.6.1}/setup.cfg +0 -0
- {bitarray-3.5.2 → bitarray-3.6.1}/setup.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: bitarray
|
|
3
|
-
Version: 3.
|
|
3
|
+
Version: 3.6.1
|
|
4
4
|
Summary: efficient arrays of booleans -- C extension
|
|
5
5
|
Home-page: https://github.com/ilanschnell/bitarray
|
|
6
6
|
Author: Ilan Schnell
|
|
@@ -83,20 +83,13 @@ versions. Which means you can simply:
|
|
|
83
83
|
|
|
84
84
|
$ pip install bitarray
|
|
85
85
|
|
|
86
|
-
In addition, conda packages are available (both the default Anaconda
|
|
87
|
-
repository as well as conda-forge support bitarray):
|
|
88
|
-
|
|
89
|
-
.. code-block:: shell-session
|
|
90
|
-
|
|
91
|
-
$ conda install bitarray
|
|
92
|
-
|
|
93
86
|
Once you have installed the package, you may want to test it:
|
|
94
87
|
|
|
95
88
|
.. code-block:: shell-session
|
|
96
89
|
|
|
97
90
|
$ python -c 'import bitarray; bitarray.test()'
|
|
98
91
|
bitarray is installed in: /Users/ilan/bitarray/bitarray
|
|
99
|
-
bitarray version: 3.
|
|
92
|
+
bitarray version: 3.6.1
|
|
100
93
|
sys.version: 3.13.5 (main, Jun 16 2025) [Clang 18.1.8]
|
|
101
94
|
sys.prefix: /Users/ilan/miniforge
|
|
102
95
|
pointer size: 64 bit
|
|
@@ -110,7 +103,7 @@ Once you have installed the package, you may want to test it:
|
|
|
110
103
|
.........................................................................
|
|
111
104
|
................................................................
|
|
112
105
|
----------------------------------------------------------------------
|
|
113
|
-
Ran
|
|
106
|
+
Ran 591 tests in 0.163s
|
|
114
107
|
|
|
115
108
|
OK
|
|
116
109
|
|
|
@@ -427,7 +420,7 @@ and can therefore be used as a dictionary key:
|
|
|
427
420
|
Reference
|
|
428
421
|
=========
|
|
429
422
|
|
|
430
|
-
bitarray version: 3.
|
|
423
|
+
bitarray version: 3.6.1 -- `change log <https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst>`__
|
|
431
424
|
|
|
432
425
|
In the following, ``item`` and ``value`` are usually a single bit -
|
|
433
426
|
an integer 0 or 1.
|
|
@@ -928,11 +921,25 @@ This sub-module was added in version 1.2.
|
|
|
928
921
|
New in version 1.8
|
|
929
922
|
|
|
930
923
|
|
|
924
|
+
``random_k(n, /, k, endian=None)`` -> bitarray
|
|
925
|
+
Return (pseudo-) random bitarray of length ``n`` with ``k`` elements
|
|
926
|
+
set to one. Mathematically equivalent to setting (in a bitarray of
|
|
927
|
+
length ``n``) all bits at indices ``random.sample(range(n), k)`` to one.
|
|
928
|
+
The random bitarrays are reproducible when giving Python's ``random.seed()``
|
|
929
|
+
with a specific seed value.
|
|
930
|
+
|
|
931
|
+
This function requires Python 3.9 or higher, as it depends on the standard
|
|
932
|
+
library function ``random.randbytes()``. Raises ``NotImplementedError``
|
|
933
|
+
when Python version is too low.
|
|
934
|
+
|
|
935
|
+
New in version 3.6
|
|
936
|
+
|
|
937
|
+
|
|
931
938
|
``random_p(n, /, p=0.5, endian=None)`` -> bitarray
|
|
932
|
-
Return (pseudo-) random bitarray of length ``n
|
|
933
|
-
of being one (independent of any other bits). Mathematically
|
|
934
|
-
to ``bitarray((random() < p for _ in range(n)), endian)``, but much
|
|
935
|
-
for large ``n``. The random bitarrays are reproducible when giving
|
|
939
|
+
Return (pseudo-) random bitarray of length ``n``, where each bit has
|
|
940
|
+
probability ``p`` of being one (independent of any other bits). Mathematically
|
|
941
|
+
equivalent to ``bitarray((random() < p for _ in range(n)), endian)``, but much
|
|
942
|
+
faster for large ``n``. The random bitarrays are reproducible when giving
|
|
936
943
|
Python's ``random.seed()`` with a specific seed value.
|
|
937
944
|
|
|
938
945
|
This function requires Python 3.12 or higher, as it depends on the standard
|
|
@@ -987,6 +994,13 @@ This sub-module was added in version 1.2.
|
|
|
987
994
|
iteration is stopped as soon as one mismatch is found.
|
|
988
995
|
|
|
989
996
|
|
|
997
|
+
``sum_indices(a, /)`` -> int
|
|
998
|
+
Return sum of indices of all active bits in bitarray ``a``.
|
|
999
|
+
Equivalent to ``sum(i for i, v in enumerate(a) if v)``.
|
|
1000
|
+
|
|
1001
|
+
New in version 3.6
|
|
1002
|
+
|
|
1003
|
+
|
|
990
1004
|
``urandom(n, /, endian=None)`` -> bitarray
|
|
991
1005
|
Return random bitarray of length ``n`` (uses ``os.urandom()``).
|
|
992
1006
|
|
|
@@ -1016,7 +1030,7 @@ This sub-module was added in version 1.2.
|
|
|
1016
1030
|
``xor_indices(a, /)`` -> int
|
|
1017
1031
|
Return xor reduced indices of all active bits in bitarray ``a``.
|
|
1018
1032
|
This is essentially equivalent to
|
|
1019
|
-
``reduce(operator.xor,
|
|
1033
|
+
``reduce(operator.xor, (i for i, v in enumerate(a) if v))``.
|
|
1020
1034
|
|
|
1021
1035
|
New in version 3.2
|
|
1022
1036
|
|
|
@@ -51,20 +51,13 @@ versions. Which means you can simply:
|
|
|
51
51
|
|
|
52
52
|
$ pip install bitarray
|
|
53
53
|
|
|
54
|
-
In addition, conda packages are available (both the default Anaconda
|
|
55
|
-
repository as well as conda-forge support bitarray):
|
|
56
|
-
|
|
57
|
-
.. code-block:: shell-session
|
|
58
|
-
|
|
59
|
-
$ conda install bitarray
|
|
60
|
-
|
|
61
54
|
Once you have installed the package, you may want to test it:
|
|
62
55
|
|
|
63
56
|
.. code-block:: shell-session
|
|
64
57
|
|
|
65
58
|
$ python -c 'import bitarray; bitarray.test()'
|
|
66
59
|
bitarray is installed in: /Users/ilan/bitarray/bitarray
|
|
67
|
-
bitarray version: 3.
|
|
60
|
+
bitarray version: 3.6.1
|
|
68
61
|
sys.version: 3.13.5 (main, Jun 16 2025) [Clang 18.1.8]
|
|
69
62
|
sys.prefix: /Users/ilan/miniforge
|
|
70
63
|
pointer size: 64 bit
|
|
@@ -78,7 +71,7 @@ Once you have installed the package, you may want to test it:
|
|
|
78
71
|
.........................................................................
|
|
79
72
|
................................................................
|
|
80
73
|
----------------------------------------------------------------------
|
|
81
|
-
Ran
|
|
74
|
+
Ran 591 tests in 0.163s
|
|
82
75
|
|
|
83
76
|
OK
|
|
84
77
|
|
|
@@ -395,7 +388,7 @@ and can therefore be used as a dictionary key:
|
|
|
395
388
|
Reference
|
|
396
389
|
=========
|
|
397
390
|
|
|
398
|
-
bitarray version: 3.
|
|
391
|
+
bitarray version: 3.6.1 -- `change log <https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst>`__
|
|
399
392
|
|
|
400
393
|
In the following, ``item`` and ``value`` are usually a single bit -
|
|
401
394
|
an integer 0 or 1.
|
|
@@ -896,11 +889,25 @@ This sub-module was added in version 1.2.
|
|
|
896
889
|
New in version 1.8
|
|
897
890
|
|
|
898
891
|
|
|
892
|
+
``random_k(n, /, k, endian=None)`` -> bitarray
|
|
893
|
+
Return (pseudo-) random bitarray of length ``n`` with ``k`` elements
|
|
894
|
+
set to one. Mathematically equivalent to setting (in a bitarray of
|
|
895
|
+
length ``n``) all bits at indices ``random.sample(range(n), k)`` to one.
|
|
896
|
+
The random bitarrays are reproducible when giving Python's ``random.seed()``
|
|
897
|
+
with a specific seed value.
|
|
898
|
+
|
|
899
|
+
This function requires Python 3.9 or higher, as it depends on the standard
|
|
900
|
+
library function ``random.randbytes()``. Raises ``NotImplementedError``
|
|
901
|
+
when Python version is too low.
|
|
902
|
+
|
|
903
|
+
New in version 3.6
|
|
904
|
+
|
|
905
|
+
|
|
899
906
|
``random_p(n, /, p=0.5, endian=None)`` -> bitarray
|
|
900
|
-
Return (pseudo-) random bitarray of length ``n
|
|
901
|
-
of being one (independent of any other bits). Mathematically
|
|
902
|
-
to ``bitarray((random() < p for _ in range(n)), endian)``, but much
|
|
903
|
-
for large ``n``. The random bitarrays are reproducible when giving
|
|
907
|
+
Return (pseudo-) random bitarray of length ``n``, where each bit has
|
|
908
|
+
probability ``p`` of being one (independent of any other bits). Mathematically
|
|
909
|
+
equivalent to ``bitarray((random() < p for _ in range(n)), endian)``, but much
|
|
910
|
+
faster for large ``n``. The random bitarrays are reproducible when giving
|
|
904
911
|
Python's ``random.seed()`` with a specific seed value.
|
|
905
912
|
|
|
906
913
|
This function requires Python 3.12 or higher, as it depends on the standard
|
|
@@ -955,6 +962,13 @@ This sub-module was added in version 1.2.
|
|
|
955
962
|
iteration is stopped as soon as one mismatch is found.
|
|
956
963
|
|
|
957
964
|
|
|
965
|
+
``sum_indices(a, /)`` -> int
|
|
966
|
+
Return sum of indices of all active bits in bitarray ``a``.
|
|
967
|
+
Equivalent to ``sum(i for i, v in enumerate(a) if v)``.
|
|
968
|
+
|
|
969
|
+
New in version 3.6
|
|
970
|
+
|
|
971
|
+
|
|
958
972
|
``urandom(n, /, endian=None)`` -> bitarray
|
|
959
973
|
Return random bitarray of length ``n`` (uses ``os.urandom()``).
|
|
960
974
|
|
|
@@ -984,7 +998,7 @@ This sub-module was added in version 1.2.
|
|
|
984
998
|
``xor_indices(a, /)`` -> int
|
|
985
999
|
Return xor reduced indices of all active bits in bitarray ``a``.
|
|
986
1000
|
This is essentially equivalent to
|
|
987
|
-
``reduce(operator.xor,
|
|
1001
|
+
``reduce(operator.xor, (i for i, v in enumerate(a) if v))``.
|
|
988
1002
|
|
|
989
1003
|
New in version 3.2
|
|
990
1004
|
|
|
@@ -189,21 +189,6 @@ buffers_overlap(bitarrayobject *self, bitarrayobject *other)
|
|
|
189
189
|
#undef PIB
|
|
190
190
|
}
|
|
191
191
|
|
|
192
|
-
/* setup translation table, which maps each byte to its reversed:
|
|
193
|
-
reverse_trans = {0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, ..., 0xff} */
|
|
194
|
-
static void
|
|
195
|
-
setup_reverse_trans(void)
|
|
196
|
-
{
|
|
197
|
-
int j, k;
|
|
198
|
-
|
|
199
|
-
for (k = 0; k < 256; k++) {
|
|
200
|
-
reverse_trans[k] = 0x00;
|
|
201
|
-
for (j = 0; j < 8; j++)
|
|
202
|
-
if (k & 128 >> j)
|
|
203
|
-
reverse_trans[k] |= 1 << j;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
192
|
/* reverse bits in first n characters of p */
|
|
208
193
|
static void
|
|
209
194
|
bytereverse(char *p, Py_ssize_t n)
|
|
@@ -221,7 +206,7 @@ bytereverse(char *p, Py_ssize_t n)
|
|
|
221
206
|
They operate on little-endian and bit-endian bitarrays respectively.
|
|
222
207
|
As we shift right, we need to start with the highest address and loop
|
|
223
208
|
downwards such that lower bytes are still unaltered.
|
|
224
|
-
See also
|
|
209
|
+
See also devel/shift_r8.c
|
|
225
210
|
*/
|
|
226
211
|
static void
|
|
227
212
|
shift_r8le(unsigned char *buff, Py_ssize_t n, int k)
|
|
@@ -321,7 +306,7 @@ shift_r8(bitarrayobject *self, Py_ssize_t a, Py_ssize_t b, int k)
|
|
|
321
306
|
}
|
|
322
307
|
|
|
323
308
|
/* Copy n bits from other (starting at b) onto self (starting at a).
|
|
324
|
-
Please see
|
|
309
|
+
Please see devel/copy_n.py for more details.
|
|
325
310
|
|
|
326
311
|
Notes:
|
|
327
312
|
- self and other may have opposite bit-endianness
|
|
@@ -4210,30 +4195,6 @@ Return tuple containing:\n\
|
|
|
4210
4195
|
7. PY_LITTLE_ENDIAN\n\
|
|
4211
4196
|
8. PY_BIG_ENDIAN");
|
|
4212
4197
|
|
|
4213
|
-
/* ---------- module functions exposed in debug mode for testing ------- */
|
|
4214
|
-
|
|
4215
|
-
#ifndef NDEBUG
|
|
4216
|
-
|
|
4217
|
-
/* Return zlw(a) as a new bitarray, rather than an int object.
|
|
4218
|
-
This makes testing easier, because the int result would depend
|
|
4219
|
-
on the machine byteorder. */
|
|
4220
|
-
static PyObject *
|
|
4221
|
-
module_zlw(PyObject *module, PyObject *obj)
|
|
4222
|
-
{
|
|
4223
|
-
bitarrayobject *a, *res;
|
|
4224
|
-
uint64_t w;
|
|
4225
|
-
|
|
4226
|
-
assert(bitarray_Check(obj));
|
|
4227
|
-
a = (bitarrayobject *) obj;
|
|
4228
|
-
w = zlw(a);
|
|
4229
|
-
if ((res = newbitarrayobject(&Bitarray_Type, 64, a->endian)) == NULL)
|
|
4230
|
-
return NULL;
|
|
4231
|
-
memcpy(res->ob_item, &w, 8);
|
|
4232
|
-
return (PyObject *) res;
|
|
4233
|
-
}
|
|
4234
|
-
|
|
4235
|
-
#endif /* NDEBUG */
|
|
4236
|
-
|
|
4237
4198
|
|
|
4238
4199
|
static PyMethodDef module_functions[] = {
|
|
4239
4200
|
{"bits2bytes", (PyCFunction) bits2bytes, METH_O,
|
|
@@ -4247,12 +4208,6 @@ static PyMethodDef module_functions[] = {
|
|
|
4247
4208
|
set_default_endian_doc},
|
|
4248
4209
|
{"_sysinfo", (PyCFunction) sysinfo, METH_NOARGS,
|
|
4249
4210
|
sysinfo_doc},
|
|
4250
|
-
|
|
4251
|
-
#ifndef NDEBUG
|
|
4252
|
-
/* functions exposed in debug mode for testing */
|
|
4253
|
-
{"_zlw", (PyCFunction) module_zlw, METH_O, 0},
|
|
4254
|
-
#endif
|
|
4255
|
-
|
|
4256
4211
|
{NULL, NULL} /* sentinel */
|
|
4257
4212
|
};
|
|
4258
4213
|
|
|
@@ -4291,7 +4246,9 @@ PyInit__bitarray(void)
|
|
|
4291
4246
|
{
|
|
4292
4247
|
PyObject *m;
|
|
4293
4248
|
|
|
4294
|
-
|
|
4249
|
+
/* setup translation table, which maps each byte to its reversed:
|
|
4250
|
+
reverse_trans = {0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, ..., 0xff} */
|
|
4251
|
+
setup_table(reverse_trans, 'r');
|
|
4295
4252
|
|
|
4296
4253
|
if ((m = PyModule_Create(&moduledef)) == NULL)
|
|
4297
4254
|
return NULL;
|
|
@@ -265,20 +265,79 @@ Return parity of bitarray `a`.\n\
|
|
|
265
265
|
`parity(a)` is equivalent to `a.count() % 2` but more efficient.");
|
|
266
266
|
|
|
267
267
|
|
|
268
|
+
/* Internal functions, like sum_indices(), but bitarrays are limited in
|
|
269
|
+
size. For details see: devel/test_sum_indices.py
|
|
270
|
+
*/
|
|
271
|
+
static PyObject *
|
|
272
|
+
ssqi(PyObject *module, PyObject *args)
|
|
273
|
+
{
|
|
274
|
+
static char count_table[256], sum_table[256], sum_sqr_table[256];
|
|
275
|
+
static int setup = -1; /* endianness of tables */
|
|
276
|
+
bitarrayobject *a;
|
|
277
|
+
uint64_t nbytes, i;
|
|
278
|
+
uint64_t sm = 0; /* accumulated sum */
|
|
279
|
+
int mode = 1;
|
|
280
|
+
|
|
281
|
+
if (!PyArg_ParseTuple(args, "O!|i", bitarray_type,
|
|
282
|
+
(PyObject *) &a, &mode))
|
|
283
|
+
return NULL;
|
|
284
|
+
if (mode < 1 || mode > 2)
|
|
285
|
+
return PyErr_Format(PyExc_ValueError, "unexpected mode %d", mode);
|
|
286
|
+
if (((uint64_t) a->nbits) > (mode == 1 ? 6074001000LLU : 3810778LLU))
|
|
287
|
+
return PyErr_Format(PyExc_OverflowError, "ssqi %zd", a->nbits);
|
|
288
|
+
|
|
289
|
+
if (setup != a->endian) {
|
|
290
|
+
setup_table(count_table, 'c');
|
|
291
|
+
setup_table(sum_table, IS_LE(a) ? 'a' : 'A');
|
|
292
|
+
setup_table(sum_sqr_table, IS_LE(a) ? 's' : 'S');
|
|
293
|
+
setup = a->endian;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
nbytes = Py_SIZE(a);
|
|
297
|
+
set_padbits(a);
|
|
298
|
+
for (i = 0; i < nbytes; i++) {
|
|
299
|
+
unsigned char c = a->ob_item[i];
|
|
300
|
+
if (c) {
|
|
301
|
+
uint64_t k = count_table[c], z1 = sum_table[c];
|
|
302
|
+
if (mode == 1) {
|
|
303
|
+
sm += k * 8LLU * i + z1;
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
uint64_t z2 = (unsigned char) sum_sqr_table[c];
|
|
307
|
+
sm += (k * 64LLU * i + 16LLU * z1) * i + z2;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return PyLong_FromUnsignedLongLong(sm);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
|
|
268
315
|
static PyObject *
|
|
269
316
|
xor_indices(PyObject *module, PyObject *obj)
|
|
270
317
|
{
|
|
318
|
+
static char parity_table[256], xor_table[256];
|
|
319
|
+
static int setup = -1; /* endianness of xor_table */
|
|
271
320
|
bitarrayobject *a;
|
|
272
|
-
Py_ssize_t res = 0,
|
|
321
|
+
Py_ssize_t res = 0, nbytes, i;
|
|
273
322
|
|
|
274
323
|
if (ensure_bitarray(obj) < 0)
|
|
275
324
|
return NULL;
|
|
276
325
|
|
|
277
326
|
a = (bitarrayobject *) obj;
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
327
|
+
nbytes = Py_SIZE(a);
|
|
328
|
+
set_padbits(a);
|
|
329
|
+
|
|
330
|
+
if (setup != a->endian) {
|
|
331
|
+
setup_table(xor_table, IS_LE(a) ? 'x' : 'X');
|
|
332
|
+
setup_table(parity_table, 'p');
|
|
333
|
+
setup = a->endian;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
for (i = 0; i < nbytes; i++) {
|
|
337
|
+
unsigned char c = a->ob_item[i];
|
|
338
|
+
if (parity_table[c])
|
|
339
|
+
res ^= i << 3;
|
|
340
|
+
res ^= xor_table[c];
|
|
282
341
|
}
|
|
283
342
|
return PyLong_FromSsize_t(res);
|
|
284
343
|
}
|
|
@@ -288,7 +347,7 @@ PyDoc_STRVAR(xor_indices_doc,
|
|
|
288
347
|
\n\
|
|
289
348
|
Return xor reduced indices of all active bits in bitarray `a`.\n\
|
|
290
349
|
This is essentially equivalent to\n\
|
|
291
|
-
`reduce(operator.xor,
|
|
350
|
+
`reduce(operator.xor, (i for i, v in enumerate(a) if v))`.");
|
|
292
351
|
|
|
293
352
|
/* --------------------------- binary functions ------------------------ */
|
|
294
353
|
|
|
@@ -786,6 +845,7 @@ digit_to_int(int m, char c)
|
|
|
786
845
|
static int setup = 0;
|
|
787
846
|
int i;
|
|
788
847
|
|
|
848
|
+
assert(1 <= m && m <= 6);
|
|
789
849
|
if (m < 5) { /* base 2, 4, 8, 16 */
|
|
790
850
|
i = hex_to_int(c);
|
|
791
851
|
return i >> m ? -1 : i;
|
|
@@ -2031,14 +2091,43 @@ static PyTypeObject CHDI_Type = {
|
|
|
2031
2091
|
|
|
2032
2092
|
#ifndef NDEBUG
|
|
2033
2093
|
|
|
2094
|
+
static PyObject *
|
|
2095
|
+
module_setup_table(PyObject *module, PyObject *obj)
|
|
2096
|
+
{
|
|
2097
|
+
char table[256];
|
|
2098
|
+
|
|
2099
|
+
assert(PyUnicode_Check(obj));
|
|
2100
|
+
assert(PyUnicode_GET_LENGTH(obj) == 1);
|
|
2101
|
+
setup_table(table, PyUnicode_READ_CHAR(obj, 0));
|
|
2102
|
+
return PyBytes_FromStringAndSize(table, 256);
|
|
2103
|
+
}
|
|
2104
|
+
|
|
2105
|
+
/* Return zlw(a) as a new bitarray, rather than an int object.
|
|
2106
|
+
This makes testing easier, because the int result would depend
|
|
2107
|
+
on the machine byteorder. */
|
|
2108
|
+
static PyObject *
|
|
2109
|
+
module_zlw(PyObject *module, PyObject *obj)
|
|
2110
|
+
{
|
|
2111
|
+
bitarrayobject *a, *res;
|
|
2112
|
+
uint64_t w;
|
|
2113
|
+
|
|
2114
|
+
assert(bitarray_Check(obj));
|
|
2115
|
+
a = (bitarrayobject *) obj;
|
|
2116
|
+
w = zlw(a);
|
|
2117
|
+
if ((res = new_bitarray(64, Py_None, -1)) == NULL)
|
|
2118
|
+
return NULL;
|
|
2119
|
+
res->endian = a->endian;
|
|
2120
|
+
memcpy(res->ob_item, &w, 8);
|
|
2121
|
+
return (PyObject *) res;
|
|
2122
|
+
}
|
|
2123
|
+
|
|
2034
2124
|
static PyObject *
|
|
2035
2125
|
module_cfw(PyObject *module, PyObject *args) /* count_from_word() */
|
|
2036
2126
|
{
|
|
2037
2127
|
bitarrayobject *a;
|
|
2038
2128
|
Py_ssize_t i;
|
|
2039
2129
|
|
|
2040
|
-
if (!PyArg_ParseTuple(args, "O!n",
|
|
2041
|
-
bitarray_type, (PyObject *) &a, &i))
|
|
2130
|
+
if (!PyArg_ParseTuple(args, "O!n", bitarray_type, (PyObject *) &a, &i))
|
|
2042
2131
|
return NULL;
|
|
2043
2132
|
return PyLong_FromSsize_t(count_from_word(a, i));
|
|
2044
2133
|
}
|
|
@@ -2084,9 +2173,8 @@ static PyMethodDef module_functions[] = {
|
|
|
2084
2173
|
METH_VARARGS, ones_doc},
|
|
2085
2174
|
{"count_n", (PyCFunction) count_n, METH_VARARGS, count_n_doc},
|
|
2086
2175
|
{"parity", (PyCFunction) parity, METH_O, parity_doc},
|
|
2087
|
-
{"
|
|
2088
|
-
|
|
2089
|
-
METH_O, xor_indices_doc},
|
|
2176
|
+
{"_ssqi", (PyCFunction) ssqi, METH_VARARGS, 0},
|
|
2177
|
+
{"xor_indices", (PyCFunction) xor_indices, METH_O, xor_indices_doc},
|
|
2090
2178
|
{"count_and", (PyCFunction) count_and, METH_VARARGS, count_and_doc},
|
|
2091
2179
|
{"count_or", (PyCFunction) count_or, METH_VARARGS, count_or_doc},
|
|
2092
2180
|
{"count_xor", (PyCFunction) count_xor, METH_VARARGS, count_xor_doc},
|
|
@@ -2096,10 +2184,8 @@ static PyMethodDef module_functions[] = {
|
|
|
2096
2184
|
(PyCFunction) correspond_all,
|
|
2097
2185
|
METH_VARARGS, correspond_all_doc},
|
|
2098
2186
|
{"byteswap", (PyCFunction) byteswap, METH_VARARGS, byteswap_doc},
|
|
2099
|
-
{"serialize",
|
|
2100
|
-
{"deserialize",
|
|
2101
|
-
(PyCFunction) deserialize,
|
|
2102
|
-
METH_O, deserialize_doc},
|
|
2187
|
+
{"serialize", (PyCFunction) serialize, METH_O, serialize_doc},
|
|
2188
|
+
{"deserialize", (PyCFunction) deserialize, METH_O, deserialize_doc},
|
|
2103
2189
|
{"ba2hex", (PyCFunction) ba2hex, METH_KEYWORDS |
|
|
2104
2190
|
METH_VARARGS, ba2hex_doc},
|
|
2105
2191
|
{"hex2ba", (PyCFunction) hex2ba, METH_KEYWORDS |
|
|
@@ -2118,10 +2204,12 @@ static PyMethodDef module_functions[] = {
|
|
|
2118
2204
|
|
|
2119
2205
|
#ifndef NDEBUG
|
|
2120
2206
|
/* functions exposed in debug mode for testing */
|
|
2121
|
-
{"
|
|
2122
|
-
{"
|
|
2123
|
-
{"
|
|
2124
|
-
{"
|
|
2207
|
+
{"_setup_table", (PyCFunction) module_setup_table, METH_O, 0},
|
|
2208
|
+
{"_zlw", (PyCFunction) module_zlw, METH_O, 0},
|
|
2209
|
+
{"_cfw", (PyCFunction) module_cfw, METH_VARARGS, 0},
|
|
2210
|
+
{"_read_n", (PyCFunction) module_read_n, METH_VARARGS, 0},
|
|
2211
|
+
{"_write_n", (PyCFunction) module_write_n, METH_VARARGS, 0},
|
|
2212
|
+
{"_sc_rts", (PyCFunction) module_sc_rts, METH_O, 0},
|
|
2125
2213
|
#endif
|
|
2126
2214
|
|
|
2127
2215
|
{NULL, NULL} /* sentinel */
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
Author: Ilan Schnell
|
|
6
6
|
*/
|
|
7
|
-
#define BITARRAY_VERSION "3.
|
|
7
|
+
#define BITARRAY_VERSION "3.6.1"
|
|
8
8
|
|
|
9
9
|
#ifdef STDC_HEADERS
|
|
10
10
|
# include <stddef.h>
|
|
@@ -225,6 +225,35 @@ swap_bytes(char *p, Py_ssize_t n)
|
|
|
225
225
|
}
|
|
226
226
|
}
|
|
227
227
|
|
|
228
|
+
/* write 256 characters into table for given kernel operation */
|
|
229
|
+
static inline void
|
|
230
|
+
setup_table(char *table, char kop)
|
|
231
|
+
{
|
|
232
|
+
int k;
|
|
233
|
+
for (k = 0; k < 256; k++) {
|
|
234
|
+
char t = 0, j;
|
|
235
|
+
for (j = 0; j < 8; j++) {
|
|
236
|
+
if (k & 1 << j)
|
|
237
|
+
/* j are the indices of active bits in k (little endian) */
|
|
238
|
+
switch (kop) {
|
|
239
|
+
case 'a': t += j; break; /* add active indices */
|
|
240
|
+
case 'A': t += 7 - j; break; /* 'a' for big endian */
|
|
241
|
+
case 's': t += j * j; /* add squares of active indices */
|
|
242
|
+
break;
|
|
243
|
+
case 'S': t += (7-j) * (7-j); /* 's' for big endian */
|
|
244
|
+
break;
|
|
245
|
+
case 'x': t ^= j; break; /* xor active indices */
|
|
246
|
+
case 'X': t ^= 7 - j; break; /* 'x' for big endian */
|
|
247
|
+
case 'c': t++; break; /* bit count */
|
|
248
|
+
case 'p': t ^= 1; break; /* parity */
|
|
249
|
+
case 'r': t |= 128 >> j; break; /* reverse bits */
|
|
250
|
+
default: Py_UNREACHABLE();
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
table[k] = t;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
228
257
|
/* Return distance [0..3] to next aligned pointer.
|
|
229
258
|
While on modern compilers uint64_t pointers may be misaligned, it may
|
|
230
259
|
cause problems on older ones. Moreover, it may lead to slowdown (even
|