forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitArray_ext.cxx
More file actions
89 lines (81 loc) · 2.5 KB
/
bitArray_ext.cxx
File metadata and controls
89 lines (81 loc) · 2.5 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file bitArray_ext.cxx
* @author rdb
* @date 2020-03-21
*/
#include "bitArray_ext.h"
#ifdef HAVE_PYTHON
/**
* Creates a BitArray from a Python long object.
*/
void Extension<BitArray>::
__init__(PyObject *init_value) {
if (!PyLong_Check(init_value) || Py_SIZE(init_value) < 0) {
PyErr_SetString(PyExc_ValueError, "BitArray constructor requires a positive integer");
return;
}
int n = _PyLong_NumBits(init_value);
if (n > 0) {
size_t num_words = (n + BitArray::num_bits_per_word - 1) / BitArray::num_bits_per_word;
_this->_array.resize(num_words);
_PyLong_AsByteArray((PyLongObject *)init_value,
(unsigned char *)&_this->_array[0],
num_words * sizeof(BitArray::WordType),
1, 0);
}
}
/**
* Returns the value of the BitArray as a picklable Python object.
*
* We could just return a list of words. However, different builds of Panda3D
* may have different sizes for the WordType, so we'd also need to add code to
* convert between different WordTypes. Instead, we'll just encode the whole
* array as a Python long, with infinite arrays stored as inverted longs.
*/
PyObject *Extension<BitArray>::
__getstate__() const {
if (_this->_array.empty()) {
return PyLong_FromLong(-_this->_highest_bits);
}
if (_this->_highest_bits == 0) {
return _PyLong_FromByteArray(
(const unsigned char *)&_this->_array[0],
_this->_array.size() * sizeof(BitArray::WordType),
1, 0);
} else {
// This is an infinite array, so we invert it to make it a finite array and
// store it as an inverted long.
BitArray copy(*_this);
copy.invert_in_place();
PyObject *state = _PyLong_FromByteArray(
(const unsigned char *)©._array[0],
copy._array.size() * sizeof(BitArray::WordType),
1, 0);
PyObject *inverted = PyNumber_Invert(state);
Py_DECREF(state);
return inverted;
}
}
/**
* Takes the value returned by __getstate__ and uses it to freshly initialize
* this BitArray object.
*/
void Extension<BitArray>::
__setstate__(PyObject *state) {
if (Py_SIZE(state) >= 0) {
__init__(state);
} else {
PyObject *inverted = PyNumber_Invert(state);
__init__(inverted);
Py_DECREF(inverted);
_this->invert_in_place();
}
}
#endif