Loading...
Searching...
No Matches
content.h
Go to the documentation of this file.
1// BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE
2
3#ifndef AWKWARDPY_CONTENT_H_
4#define AWKWARDPY_CONTENT_H_
5
6#include <pybind11/numpy.h>
7#include <pybind11/pybind11.h>
8#include <pybind11/stl.h>
9
11#include "awkward/util.h"
12
13namespace py = pybind11;
14namespace ak = awkward;
15
16
18py::class_<ak::ArrayBuilder>
19 make_ArrayBuilder(const py::handle& m, const std::string& name);
20
21namespace {
22 class NumpyBuffersContainer: public ak::BuffersContainer {
23 public:
24 py::dict container() {
25 return container_;
26 }
27
28 void*
29 empty_buffer(const std::string& name, int64_t num_bytes) override {
30 py::object pyarray = py::module::import("numpy").attr("empty")(num_bytes, "u1");
31 py::array_t<uint8_t> rawarray = pyarray.cast<py::array_t<uint8_t>>();
32 py::buffer_info rawinfo = rawarray.request();
33 container_[py::str(name)] = pyarray;
34 return rawinfo.ptr;
35 }
36
37 void
38 copy_buffer(const std::string& name, const void* source, int64_t num_bytes) override {
39 py::object pyarray = py::module::import("numpy").attr("empty")(num_bytes, "u1");
40 py::array_t<uint8_t> rawarray = pyarray.cast<py::array_t<uint8_t>>();
41 py::buffer_info rawinfo = rawarray.request();
42 std::memcpy(rawinfo.ptr, source, num_bytes);
43 container_[py::str(name)] = pyarray;
44 }
45
46 void
47 full_buffer(const std::string& name, int64_t length, int64_t value, const std::string& dtype) override {
48 py::object pyarray = py::module::import("numpy").attr("full")(py::int_(length), py::int_(value), py::str(dtype));
49 container_[py::str(name)] = pyarray;
50 }
51
52 private:
53 py::dict container_;
54 };
55
56 class EmptyBuffersContainer: public ak::BuffersContainer {
57 public:
58 void*
59 empty_buffer(const std::string& /* name */, int64_t /* num_bytes */) override {
60 return nullptr;
61 }
62
63 void
64 copy_buffer(const std::string& /* name */, const void* /* source */, int64_t /* num_bytes */) override { }
65
66 void
67 full_buffer(const std::string& /* name */, int64_t /* length */, int64_t /* value */, const std::string& /* dtype */) override { }
68 };
69}
70
71#endif // AWKWARDPY_CONTENT_H_
Abstract class to represent the output of ak.to_buffers. In Python, this would be a dict of NumPy arr...
Definition Builder.h:20
py::class_< ak::ArrayBuilder > make_ArrayBuilder(const py::handle &m, const std::string &name)
Makes an ArrayBuilder class in Python that mirrors the one in C++.
Definition ArrayBuilder.h:14