12#include "detail/common.h"
24 template <
typename T2>
26 test_comparable(
decltype(std::declval<const T2 &>() == std::declval<const T2 &>()) *);
27 template <
typename T2>
29 template <
typename T2>
30 static std::true_type
test_value(
typename T2::value_type *);
31 template <
typename T2>
33 template <
typename T2>
34 static std::true_type
test_pair(
typename T2::first_type *,
typename T2::second_type *);
35 template <
typename T2>
39 = std::is_same<std::true_type, decltype(test_comparable<T>(
nullptr))>::value;
41 = std::is_same<std::true_type, decltype(test_pair<T>(
nullptr,
nullptr))>::value;
43 = std::is_same<std::true_type, decltype(test_value<T>(
nullptr))>::value;
48template <
typename T,
typename SFINAE =
void>
73template <
typename,
typename,
typename... Args>
75template <
typename,
typename,
typename... Args>
77template <
typename,
typename,
typename... Args>
79template <
typename,
typename,
typename... Args>
82template <
typename Vector,
typename Class_>
84 cl.def(init<const Vector &>(),
"Copy constructor");
87template <
typename Vector,
typename Class_>
89 using T =
typename Vector::value_type;
96 [](
const Vector &v,
const T &x) {
return std::count(v.begin(), v.end(), x); },
98 "Return the number of times ``x`` appears in the list");
102 [](Vector &v,
const T &x) {
103 auto p = std::find(v.begin(), v.end(), x);
111 "Remove the first item from the list whose value is x. "
112 "It is an error if there is no such item.");
116 [](
const Vector &v,
const T &x) {
return std::find(v.begin(), v.end(), x) != v.end(); },
118 "Return true the container contains ``x``");
124template <
typename Vector,
typename Class_>
127 using T =
typename Vector::value_type;
128 using SizeType =
typename Vector::size_type;
129 using DiffType =
typename Vector::difference_type;
131 auto wrap_i = [](DiffType i, SizeType n) {
135 if (i < 0 || (SizeType) i >= n) {
143 [](Vector &v,
const T &value) { v.push_back(value); },
145 "Add an item to the end of the list");
148 auto v = std::unique_ptr<Vector>(
new Vector());
151 v->push_back(h.cast<T>());
157 "clear", [](Vector &v) { v.clear(); },
"Clear the contents");
161 [](Vector &v,
const Vector &src) { v.insert(v.end(), src.begin(), src.end()); },
163 "Extend the list by appending all the items in the given list");
168 const size_t old_size = v.size();
172 v.push_back(h.cast<T>());
174 }
catch (
const cast_error &) {
175 v.erase(v.begin() +
static_cast<typename Vector::difference_type
>(old_size),
179 }
catch (
const std::exception &) {
186 "Extend the list by appending all the items in the given list");
190 [](Vector &v, DiffType i,
const T &x) {
195 if (i < 0 || (SizeType) i > v.size()) {
198 v.insert(v.begin() + i, x);
202 "Insert an item at a given position.");
210 T t = std::move(v.back());
214 "Remove and return the last item");
218 [wrap_i](Vector &v, DiffType i) {
219 i = wrap_i(i, v.size());
220 T t = std::move(v[(SizeType) i]);
221 v.erase(std::next(v.begin(), i));
225 "Remove and return the item at index ``i``");
227 cl.def(
"__setitem__", [wrap_i](Vector &v, DiffType i,
const T &t) {
228 i = wrap_i(i, v.size());
235 [](
const Vector &v,
slice slice) -> Vector * {
236 size_t start = 0, stop = 0, step = 0, slicelength = 0;
238 if (!
slice.
compute(v.size(), &start, &stop, &step, &slicelength)) {
242 auto *seq =
new Vector();
243 seq->reserve((
size_t) slicelength);
245 for (
size_t i = 0; i < slicelength; ++i) {
246 seq->push_back(v[start]);
252 "Retrieve list elements using a slice object");
256 [](Vector &v,
slice slice,
const Vector &value) {
257 size_t start = 0, stop = 0, step = 0, slicelength = 0;
258 if (!
slice.
compute(v.size(), &start, &stop, &step, &slicelength)) {
262 if (slicelength != value.size()) {
263 throw std::runtime_error(
264 "Left and right hand size of slice assignment have different sizes!");
267 for (
size_t i = 0; i < slicelength; ++i) {
272 "Assign list elements using a slice object");
276 [wrap_i](Vector &v, DiffType i) {
277 i = wrap_i(i, v.size());
278 v.erase(v.begin() + i);
280 "Delete the list elements at index ``i``");
285 size_t start = 0, stop = 0, step = 0, slicelength = 0;
287 if (!
slice.
compute(v.size(), &start, &stop, &step, &slicelength)) {
291 if (step == 1 &&
false) {
292 v.erase(v.begin() + (DiffType) start, v.begin() + DiffType(start + slicelength));
294 for (
size_t i = 0; i < slicelength; ++i) {
295 v.erase(v.begin() + DiffType(start));
300 "Delete list elements using a slice object");
305template <
typename Vector>
308 typename Vector::value_type &>>;
311template <
typename Vector,
typename Class_>
313 using T =
typename Vector::value_type;
314 using SizeType =
typename Vector::size_type;
315 using DiffType =
typename Vector::difference_type;
316 using ItType =
typename Vector::iterator;
318 auto wrap_i = [](DiffType i, SizeType n) {
322 if (i < 0 || (SizeType) i >= n) {
330 [wrap_i](Vector &v, DiffType i) -> T & {
331 i = wrap_i(i, v.size());
332 return v[(SizeType) i];
340 return make_iterator<return_value_policy::reference_internal, ItType, ItType, T &>(
348template <
typename Vector,
typename Class_>
350 using T =
typename Vector::value_type;
351 using SizeType =
typename Vector::size_type;
352 using DiffType =
typename Vector::difference_type;
353 using ItType =
typename Vector::iterator;
354 cl.def(
"__getitem__", [](
const Vector &v, DiffType i) -> T {
355 if (i < 0 && (i += v.size()) < 0) {
358 if ((SizeType) i >= v.size()) {
361 return v[(SizeType) i];
367 return make_iterator<return_value_policy::copy, ItType, ItType, T>(v.begin(), v.end());
373template <
typename Vector,
typename Class_>
375 ->
decltype(std::declval<std::ostream &>() << std::declval<typename Vector::value_type>(),
377 using size_type =
typename Vector::size_type;
382 std::ostringstream s;
384 for (size_type i = 0; i < v.size(); ++i) {
386 if (i != v.size() - 1) {
393 "Return the canonical string representation of this list.");
399template <
typename Vector,
typename =
void>
401template <
typename Vector>
404 enable_if_t<
std::is_same<decltype(format_descriptor<typename Vector::value_type>::format(),
405 std::declval<Vector>().data()),
406 typename Vector::value_type *>::value>> : std::true_type {};
411template <
typename... Args>
413 return detail::any_of<std::is_same<Args, buffer_protocol>...>::value;
420template <
typename Vector,
typename Class_,
typename... Args>
422 using T =
typename Vector::value_type;
425 "There is not an appropriate format descriptor for this vector");
433 static_cast<ssize_t>(
sizeof(T)),
442 if (info.ndim != 1 || info.strides[0] %
static_cast<ssize_t>(
sizeof(T))) {
443 throw type_error(
"Only valid 1D buffers can be copied to a vector");
445 if (!detail::compare_buffer_info<T>::compare(info)
446 || (
ssize_t)
sizeof(T) != info.itemsize) {
447 throw type_error(
"Format mismatch (Python: " + info.format
448 +
" C++: " + format_descriptor<T>::format() +
")");
451 T *p =
static_cast<T *
>(info.ptr);
453 T *end = p + info.shape[0] * step;
455 return Vector(p, end);
458 vec.reserve((
size_t) info.shape[0]);
459 for (; p != end; p += step) {
468template <
typename Vector,
typename Class_,
typename... Args>
471template <
typename Vector,
typename Class_,
typename... Args>
474 cl, detail::any_of<std::is_same<Args, buffer_protocol>...>{});
482template <
typename Vector,
typename holder_type = std::unique_ptr<Vector>,
typename... Args>
488 using vtype =
typename Vector::value_type;
489 auto *vtype_info = detail::get_type_info(
typeid(vtype));
490 bool local = !vtype_info || vtype_info->module_local;
492 Class_ cl(
scope,
name.c_str(), pybind11::module_local(local), std::forward<Args>(
args)...);
495 detail::vector_buffer<Vector, Class_, Args...>(cl);
500 detail::vector_if_copy_constructible<Vector, Class_>(cl);
503 detail::vector_if_equal_operator<Vector, Class_>(cl);
506 detail::vector_if_insertion_operator<Vector, Class_>(cl,
name);
509 detail::vector_modifiers<Vector, Class_>(cl);
512 detail::vector_accessor<Vector, Class_>(cl);
516 [](
const Vector &v) ->
bool {
return !v.empty(); },
517 "Check whether the list is nonempty");
519 cl.def(
"__len__", &Vector::size);
523 cl.def(init<size_type>());
526 (
void (Vector::*) (size_type count)) & Vector::resize,
527 "changes the number of elements stored");
530 [](Vector &v, SizeType i) {
533 v.erase(v.begin() + i);
534 },
"erases element at index ``i``");
536 cl.def(
"empty", &Vector::empty,
"checks whether the container is empty");
537 cl.def(
"size", &Vector::size,
"returns the number of elements");
538 cl.def(
"push_back", (
void (Vector::*)(
const T&)) &Vector::push_back,
"adds an element to the end");
539 cl.def(
"pop_back", &Vector::pop_back,
"removes the last element");
541 cl.def(
"max_size", &Vector::max_size,
"returns the maximum possible number of elements");
542 cl.def(
"reserve", &Vector::reserve,
"reserves storage");
543 cl.def(
"capacity", &Vector::capacity,
"returns the number of elements that can be held in currently allocated storage");
544 cl.def(
"shrink_to_fit", &Vector::shrink_to_fit,
"reduces memory usage by freeing unused memory");
546 cl.def(
"clear", &Vector::clear,
"clears the contents");
547 cl.def(
"swap", &Vector::swap,
"swaps the contents");
549 cl.def(
"front", [](Vector &v) {
550 if (v.size())
return v.front();
551 else throw index_error();
552 },
"access the first element");
554 cl.def(
"back", [](Vector &v) {
555 if (v.size())
return v.back();
556 else throw index_error();
557 },
"access the last element ");
571template <
typename,
typename,
typename... Args>
573template <
typename,
typename,
typename... Args>
577template <
typename Map,
typename Class_>
580 using KeyType =
typename Map::key_type;
581 using MappedType =
typename Map::mapped_type;
583 cl.def(
"__setitem__", [](Map &m,
const KeyType &k,
const MappedType &v) {
595template <
typename Map,
typename Class_>
599 using KeyType =
typename Map::key_type;
600 using MappedType =
typename Map::mapped_type;
602 cl.def(
"__setitem__", [](Map &m,
const KeyType &k,
const MappedType &v) {
604 auto r = m.emplace(k, v);
614template <
typename Map,
typename Class_>
616 ->
decltype(std::declval<std::ostream &>() << std::declval<typename Map::key_type>()
617 << std::declval<typename Map::mapped_type>(),
623 std::ostringstream s;
626 for (
auto const &kv : m) {
630 s << kv.first <<
": " << kv.second;
636 "Return the canonical string representation of this map.");
639template <
typename Map>
644template <
typename Map>
649template <
typename Map>
656template <
typename Map,
typename holder_type = std::unique_ptr<Map>,
typename... Args>
658 using KeyType =
typename Map::key_type;
659 using MappedType =
typename Map::mapped_type;
660 using KeysView = detail::keys_view<Map>;
661 using ValuesView = detail::values_view<Map>;
662 using ItemsView = detail::items_view<Map>;
668 auto *tinfo = detail::get_type_info(
typeid(MappedType));
669 bool local = !tinfo || tinfo->module_local;
671 tinfo = detail::get_type_info(
typeid(KeyType));
672 local = !tinfo || tinfo->module_local;
675 Class_ cl(
scope,
name.c_str(), pybind11::module_local(local), std::forward<Args>(
args)...);
677 scope, (
"KeysView[" +
name +
"]").
c_str(), pybind11::module_local(local));
679 scope, (
"ValuesView[" +
name +
"]").
c_str(), pybind11::module_local(local));
681 scope, (
"ItemsView[" +
name +
"]").
c_str(), pybind11::module_local(local));
686 detail::map_if_insertion_operator<Map, Class_>(cl,
name);
690 [](
const Map &m) ->
bool {
return !m.empty(); },
691 "Check whether the map is nonempty");
701 [](Map &m) {
return KeysView{m}; },
707 [](Map &m) {
return ValuesView{m}; },
713 [](Map &m) {
return ItemsView{m}; },
719 [](Map &m,
const KeyType &k) -> MappedType & {
729 cl.def(
"__contains__", [](Map &m,
const KeyType &k) ->
bool {
737 cl.def(
"__contains__", [](Map &,
const object &) ->
bool {
return false; });
740 detail::map_assignment<Map, Class_>(cl);
742 cl.def(
"__delitem__", [](Map &m,
const KeyType &k) {
750 cl.def(
"__len__", &Map::size);
752 keys_view.def(
"__len__", [](KeysView &view) {
return view.map.size(); });
755 [](KeysView &view) {
return make_key_iterator(view.map.begin(), view.map.end()); },
758 keys_view.def(
"__contains__", [](KeysView &view,
const KeyType &k) ->
bool {
759 auto it = view.map.find(k);
760 if (it == view.map.end()) {
766 keys_view.def(
"__contains__", [](KeysView &,
const object &) ->
bool {
return false; });
768 values_view.def(
"__len__", [](ValuesView &view) {
return view.map.size(); });
775 items_view.def(
"__len__", [](ItemsView &view) {
return view.map.size(); });
778 [](ItemsView &view) {
return make_iterator(view.map.begin(), view.map.end()); },
buffer_info request(bool writable=false) const
Fetch and hold an error which was already set in Python.
\rst Holds a reference to a Python object (no reference counting)
bool compute(size_t length, size_t *start, size_t *stop, size_t *step, size_t *slicelength) const
size_t len_hint(handle h)
Get the length hint of a Python object.
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)
@ reference_internal
This policy only applies to methods and properties.
const char * c_str(Args &&...args)
Constructs a std::string with the given arguments, stores it in internals, and returns its c_str().
iterator make_value_iterator(Iterator first, Sentinel last, Extra &&...extra)
Makes a python iterator over the values (.second) of a iterator over pairs from a first and past-the-...
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
iterator make_key_iterator(Iterator first, Sentinel last, Extra &&...extra)
Makes a python iterator over the keys (.first) of a iterator over pairs from a first and past-the-end...
iterator make_iterator(Iterator first, Sentinel last, Extra &&...extra)
Makes a python iterator from a first and past-the-end C++ InputIterator.
class_< Vector, holder_type > bind_vector(handle scope, std::string const &name, Args &&...args)
void vector_buffer_impl(Class_ &cl, std::true_type)
void vector_if_equal_operator(const Args &...)
void vector_accessor(enable_if_t<!vector_needs_copy< Vector >::value, Class_ > &cl)
constexpr bool args_any_are_buffer()
void vector_if_copy_constructible(const Args &...)
void vector_buffer(Class_ &cl)
void map_assignment(const Args &...)
class_< Map, holder_type > bind_map(handle scope, const std::string &name, Args &&...args)
void vector_modifiers(const Args &...)
void vector_if_insertion_operator(const Args &...)
void map_if_insertion_operator(const Args &...)
Annotation for arguments.
Information record describing a Python buffer object.
static std::true_type test_value(typename T2::value_type *)
static constexpr const bool is_pair
static std::true_type test_comparable(decltype(std::declval< const T2 & >()==std::declval< const T2 & >()) *)
static constexpr const bool is_element
static std::true_type test_pair(typename T2::first_type *, typename T2::second_type *)
static constexpr const bool is_vector
static std::false_type test_value(...)
static std::false_type test_comparable(...)
static std::false_type test_pair(...)
Keep patient alive while nurse lives.
Annotation for function names.
Annotation for parent scope.