我有一个C库的python包装器,它执行二进制数据的编码/解码。其中一个包装器函数将python列表作为参数并返回已处理的python列表。
目前我这样做:
list pycf_cobs_decode(list data) {
list decoded;
uint32_t data_len = 0, worst_data_len = 0;
uint8_t *cdata = NULL;
uint8_t *cdata_dec = NULL;
data_len = len( data );
worst_data_len = COBS_WORST_LEN( data_len );
// Clone the python list
cdata = new uint8_t[ data_len ];
for (int i=0; i<data_len; i++)
cdata[ i ] = extract< uint8_t >( data[ i ] );
// Decode it
cdata_dec = new uint8_t[ worst_data_len ];
cobs_decode( cdata, data_len, cdata_dec );
for (int i=0; i<worst_data_len; i++)
decoded.append( cdata_dec[ i ] );
delete[] cdata_dec;
delete[] cdata;
return decoded;
}
但是,通过创建一个空列表,然后逐个附加所有字节远远没有效率(导致大量的realloc调用)。
我一直试图从这些页面中找到有用的东西,但我真的不知道该寻找什么:
http://www.boost.org/doc/libs/1_57_0/libs/python/doc/v2/reference.html
http://www.boost.org/doc/libs/1_57_0/libs/python/doc/v2/list.html
谢谢
编辑:实现std :: string也支持二进制数组,boost :: python自动将其转换为python字符串。使用std :: string,代码现在更短,并且希望产生更好的性能。但是,这并没有回答这个问题,因为它只适用于一组字符。
std::string pycf_cobs_decode(const std::string &data) {
uint32_t worst_len = COBS_WORST_LEN( data.size() );
char *cdec = new char[ worst_len ];
// Decode it
cobs_decode( ( const uint8_t * ) data.c_str(), data.size(),
( uint8_t * ) cdec );
std::string decoded( cdec, worst_len );
delete[] cdec;
return decoded;
}
答案 0 :(得分:3)
在一次附加项目时,可能没有人们期望的内部分配数量。对于list.append()
,[None] * n
的平均和摊销的最差情况复杂度是恒定的。{/ p>
Boost.Python在允许用C ++编写Python-ish代码方面做得相当不错。因此,可以使用Python惯用法/// @brief Construct list with `n` elements. each element is a copy
/// of `value`.
/// @param n Iniitail container size.
/// @param item Item with which to fill the container.
boost::python::list make_list(
const std::size_t n,
boost::python::object item = boost::python::object() /* none */)
{
namespace python = boost::python;
// >>> [None] * n;
python::list result;
result.append(item);
result *= n;
return result;
}
将列表分配给给定的大小:
len
我找不到明确说明此类操作的实现行为的文档,但这个习惯用法经常执行而不是追加。尽管如此,虽然Boost.Python没有公开所有Python / C API功能,但可以通过使用documented创建列表来保证单个分配,然后通过Boost.Python管理和操作它。如文档中所述,当PyList_SetItem()
大于零时,在将列表对象暴露给Python代码之前,必须通过boost::python::list
将所有项目设置为实际对象,或者在这种情况下,{{1}对象:
如果
len
大于零,则返回的列表对象的项目将设置为NULL
。因此,在使用PySequence_SetItem()
将所有项目设置为实际对象之前,不能使用PyList_SetItem()
等抽象API函数或将对象公开给Python代码。
辅助功能可能有助于隐藏这些细节。例如,可以使用具有类似填充行为的工厂函数:
/// @brief Construct list with `n` elements. each element is a copy
/// of `value`.
/// @param n Iniitail container size.
/// @param item Item with which to fill the container.
boost::python::list make_list(
const std::size_t n,
boost::python::object item = boost::python::object() /* none */)
{
namespace python = boost::python;
python::handle<> list_handle(PyList_New(n));
for (std::size_t i=0; i < n; ++i)
{
// PyList_SetItem will steal the item reference. As Boost.Python is
// managing the item, explicitly increment the item's reference count
// so that the stolen reference remains alive when this Boost.Python
// object's scope ends.
Py_XINCREF(item.ptr());
PyList_SetItem(list_handle.get(), i, item.ptr());
}
return python::list{list_handle};
}
或适用于范围的工厂功能:
/// @brief Construct a list from a given range of [first,last). The
/// copied range includes all elements between `first` to `last`,
/// including `first`.
/// @param first Input iterator to the initial item to copy.
/// @param last Input iterator to one after the final item to be copied.
template <typename Iterator>
boost::python::list make_list(Iterator first, Iterator last)
{
namespace python = boost::python;
const auto size = std::distance(first, last);
python::handle<> list_handle{PyList_New(size)};
for (auto i=0; i < size; ++i, ++first)
{
python::object item{*first};
// PyList_SetItem will steal the item reference. As Boost.Python is
// managing the item, explicitly increment the item's reference count
// so that the stolen reference remains alive when this Boost.Python
// object's scope ends.
Py_XINCREF(item.ptr());
PyList_SetItem(list_handle.get(), i, item.ptr());
}
return boost::python::list{list_handle};
}
以下是一个完整的示例PyList_New(len)
,其中包含模拟的COBS_WORST_LEN()
和cobs_decode()
函数,这些函数通过累积对进行解码。由于在构造返回列表时已知解码值,我选择使用范围工厂函数来防止必须遍历列表并将值设置两次:
#include <boost/python.hpp>
#include <iostream>
#include <vector>
#include <boost/python/stl_iterator.hpp>
/// Mockup that halves the list, rounding up.
std::size_t COBS_WORST_LEN(const std::size_t len)
{
return (len / 2) + (len % 2);
}
/// Mockup that just adds adjacent elements together.
void cobs_decode(
const boost::uint8_t* input,
const std::size_t size,
boost::uint8_t* output)
{
for (std::size_t i=0; i < size; ++i, ++input)
{
if (i % 2 == 0)
{
*output = *input;
}
else
{
*output += *input;
++output;
}
}
}
/// @brief Construct a list from a given range of [first,last). The
/// copied range includes all elements between `first` to `last`,
/// including `first`.
/// @param first Input iterator to the initial value to copy.
/// @param last Input iterator to one after the final element to be copied.
template <typename Iterator>
boost::python::list make_list(Iterator first, Iterator last)
{
namespace python = boost::python;
const auto size = std::distance(first, last);
python::handle<> list_handle{PyList_New(size)};
for (auto i=0; i < size; ++i, ++first)
{
python::object item{*first};
// PyList_SetItem will steal the item reference. As Boost.Python is
// managing the item, explicitly increment the item's reference count
// so that the stolen reference remains alive when this Boost.Python
// object's scope ends.
Py_XINCREF(item.ptr());
PyList_SetItem(list_handle.get(), i, item.ptr());
}
return boost::python::list{list_handle};
}
/// @brief Decode a list, returning the aggregation of paired items.
boost::python::list decode(boost::python::list py_data)
{
namespace python = boost::python;
// Clone the list.
std::vector<boost::uint8_t> data(len(py_data));
std::copy(
python::stl_input_iterator<boost::uint8_t>{py_data},
python::stl_input_iterator<boost::uint8_t>{},
begin(data));
// Decode the list.
std::vector<boost::uint8_t> decoded(COBS_WORST_LEN(data.size()));
cobs_decode(&data[0], data.size(), &decoded[0]);
return make_list(begin(decoded), end(decoded));
}
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::def("decode", &decode);
}
交互式使用:
>>> import example
>>> assert(example.decode([1,2,3,4]) == [3,7])
此外,由于Boost.Python可以抛出异常,因此可能值得考虑使用内存管理的容器,例如std::vector
或智能指针,而不是原始动态数组。