19 #include <type_traits>
27 template <
typename T2>
29 test_comparable(decltype(std::declval<const T2 &>() == std::declval<const T2 &>()) *);
30 template <
typename T2>
32 template <
typename T2>
33 static std::true_type
test_value(
typename T2::value_type *);
34 template <
typename T2>
36 template <
typename T2>
37 static std::true_type
test_pair(
typename T2::first_type *,
typename T2::second_type *);
38 template <
typename T2>
42 = std::is_same<std::true_type, decltype(test_comparable<T>(
nullptr))>::
value;
43 static constexpr
const bool is_pair
44 = std::is_same<std::true_type, decltype(test_pair<T>(
nullptr,
nullptr))>::
value;
46 = std::is_same<std::true_type, decltype(test_value<T>(
nullptr))>::
value;
51 template <
typename T,
typename SFINAE =
void>
65 :
is_comparable<typename recursive_container_traits<T>::type_to_check_recursively> {};
78 template <
typename,
typename,
typename... Args>
80 template <
typename,
typename,
typename... Args>
82 template <
typename,
typename,
typename... Args>
84 template <
typename,
typename,
typename... Args>
87 template <
typename Vector,
typename Class_>
92 template <
typename Vector,
typename Class_>
94 using T =
typename Vector::value_type;
101 [](
const Vector &
v,
const T &
x) {
return std::count(
v.begin(),
v.end(),
x); },
103 "Return the number of times ``x`` appears in the list");
108 auto p = std::find(
v.begin(),
v.end(),
x);
116 "Remove the first item from the list whose value is x. "
117 "It is an error if there is no such item.");
121 [](
const Vector &
v,
const T &
x) {
return std::find(
v.begin(),
v.end(),
x) !=
v.end(); },
123 "Return true the container contains ``x``");
129 template <
typename Vector,
typename Class_>
132 using T =
typename Vector::value_type;
133 using SizeType =
typename Vector::size_type;
134 using DiffType =
typename Vector::difference_type;
136 auto wrap_i = [](DiffType
i, SizeType
n) {
140 if (
i < 0 || (SizeType)
i >=
n) {
150 "Add an item to the end of the list");
153 auto v = std::unique_ptr<Vector>(
new Vector());
156 v->push_back(
h.cast<
T>());
161 cl.def(
"clear", [](
Vector &
v) {
v.clear(); },
"Clear the contents");
165 [](
Vector &
v,
const Vector &src) {
v.insert(
v.end(), src.begin(), src.end()); },
167 "Extend the list by appending all the items in the given list");
172 const size_t old_size =
v.size();
176 v.push_back(
h.cast<
T>());
178 }
catch (
const cast_error &) {
179 v.erase(
v.begin() +
static_cast<typename Vector::difference_type
>(old_size),
183 }
catch (
const std::exception &) {
190 "Extend the list by appending all the items in the given list");
199 if (
i < 0 || (SizeType)
i >
v.size()) {
202 v.insert(
v.begin() +
i,
x);
206 "Insert an item at a given position.");
214 T t = std::move(
v.back());
218 "Remove and return the last item");
223 i = wrap_i(
i,
v.size());
224 T t = std::move(
v[(SizeType)
i]);
225 v.erase(std::next(
v.begin(),
i));
229 "Remove and return the item at index ``i``");
231 cl.def(
"__setitem__", [wrap_i](
Vector &
v, DiffType
i,
const T &
t) {
232 i = wrap_i(
i,
v.size());
240 size_t start = 0, stop = 0,
step = 0, slicelength = 0;
247 seq->reserve((
size_t) slicelength);
249 for (
size_t i = 0;
i < slicelength; ++
i) {
250 seq->push_back(
v[start]);
256 "Retrieve list elements using a slice object");
261 size_t start = 0, stop = 0,
step = 0, slicelength = 0;
266 if (slicelength !=
value.size()) {
267 throw std::runtime_error(
268 "Left and right hand size of slice assignment have different sizes!");
271 for (
size_t i = 0;
i < slicelength; ++
i) {
276 "Assign list elements using a slice object");
281 i = wrap_i(
i,
v.size());
282 v.erase(
v.begin() +
i);
284 "Delete the list elements at index ``i``");
289 size_t start = 0, stop = 0,
step = 0, slicelength = 0;
295 if (
step == 1 &&
false) {
296 v.erase(
v.begin() + (DiffType) start,
v.begin() + DiffType(start + slicelength));
298 for (
size_t i = 0;
i < slicelength; ++
i) {
299 v.erase(
v.begin() + DiffType(start));
304 "Delete list elements using a slice object");
309 template <
typename Vector>
312 typename Vector::value_type &>>;
315 template <
typename Vector,
typename Class_>
317 using T =
typename Vector::value_type;
318 using SizeType =
typename Vector::size_type;
319 using DiffType =
typename Vector::difference_type;
320 using ItType =
typename Vector::iterator;
322 auto wrap_i = [](DiffType
i, SizeType
n) {
326 if (
i < 0 || (SizeType)
i >=
n) {
335 i = wrap_i(
i,
v.size());
336 return v[(SizeType)
i];
344 return make_iterator<return_value_policy::reference_internal, ItType, ItType, T &>(
352 template <
typename Vector,
typename Class_>
354 using T =
typename Vector::value_type;
355 using SizeType =
typename Vector::size_type;
356 using DiffType =
typename Vector::difference_type;
357 using ItType =
typename Vector::iterator;
358 cl.def(
"__getitem__", [](
const Vector &
v, DiffType
i) ->
T {
365 auto i_st =
static_cast<SizeType
>(
i);
366 if (i_st >=
v.size()) {
375 return make_iterator<return_value_policy::copy, ItType, ItType, T>(
v.begin(),
v.end());
381 template <
typename Vector,
typename Class_>
383 -> decltype(std::declval<std::ostream &>() << std::declval<typename Vector::value_type>(),
385 using size_type =
typename Vector::size_type;
390 std::ostringstream
s;
392 for (size_type
i = 0;
i <
v.size(); ++
i) {
394 if (
i !=
v.size() - 1) {
401 "Return the canonical string representation of this list.");
407 template <
typename Vector,
typename =
void>
409 template <
typename Vector>
412 enable_if_t<
std::is_same<decltype(format_descriptor<typename Vector::value_type>::format(),
413 std::declval<Vector>().data()),
414 typename Vector::value_type *>
::value>> : std::true_type {};
419 template <
typename... Args>
421 return detail::any_of<std::is_same<Args, buffer_protocol>...>
::value;
428 template <
typename Vector,
typename Class_,
typename... Args>
430 using T =
typename Vector::value_type;
433 "There is not an appropriate format descriptor for this vector");
451 throw type_error(
"Only valid 1D buffers can be copied to a vector");
455 throw type_error(
"Format mismatch (Python: " + info.format
456 +
" C++: " + format_descriptor<T>::format() +
")");
459 T *
p =
static_cast<T *
>(
info.ptr);
466 vec.reserve((
size_t)
info.shape[0]);
476 template <
typename Vector,
typename Class_,
typename... Args>
479 template <
typename Vector,
typename Class_,
typename... Args>
482 cl, detail::any_of<std::is_same<Args, buffer_protocol>...>{});
490 template <
typename Vector,
typename holder_type = std::unique_ptr<Vector>,
typename... Args>
496 using vtype =
typename Vector::value_type;
498 bool local = !vtype_info || vtype_info->module_local;
508 detail::vector_if_copy_constructible<Vector, Class_>(
cl);
511 detail::vector_if_equal_operator<Vector, Class_>(
cl);
514 detail::vector_if_insertion_operator<Vector, Class_>(
cl,
name);
517 detail::vector_modifiers<Vector, Class_>(
cl);
520 detail::vector_accessor<Vector, Class_>(
cl);
524 [](
const Vector &
v) ->
bool {
return !
v.empty(); },
525 "Check whether the list is nonempty");
527 cl.def(
"__len__", [](
const Vector &vec) {
return vec.size(); });
535 "changes the number of elements stored");
541 v.erase(
v.begin() +
i);
542 },
"erases element at index ``i``");
544 cl.def(
"empty", &Vector::empty,
"checks whether the container is empty");
546 cl.def(
"push_back", (
void (
Vector::*)(
const T&)) &Vector::push_back,
"adds an element to the end");
547 cl.def(
"pop_back", &Vector::pop_back,
"removes the last element");
549 cl.def(
"max_size", &Vector::max_size,
"returns the maximum possible number of elements");
550 cl.def(
"reserve", &Vector::reserve,
"reserves storage");
551 cl.def(
"capacity", &Vector::capacity,
"returns the number of elements that can be held in currently allocated storage");
552 cl.def(
"shrink_to_fit", &Vector::shrink_to_fit,
"reduces memory usage by freeing unused memory");
554 cl.def(
"clear", &Vector::clear,
"clears the contents");
558 if (
v.size())
return v.front();
559 else throw index_error();
560 },
"access the first element");
563 if (
v.size())
return v.back();
564 else throw index_error();
565 },
"access the last element ");
579 template <
typename,
typename,
typename... Args>
581 template <
typename,
typename,
typename... Args>
585 template <
typename Map,
typename Class_>
588 using KeyType =
typename Map::key_type;
589 using MappedType =
typename Map::mapped_type;
591 cl.def(
"__setitem__", [](Map &
m,
const KeyType &k,
const MappedType &
v) {
603 template <
typename Map,
typename Class_>
607 using KeyType =
typename Map::key_type;
608 using MappedType =
typename Map::mapped_type;
610 cl.def(
"__setitem__", [](Map &
m,
const KeyType &k,
const MappedType &
v) {
612 auto r =
m.emplace(k,
v);
622 template <
typename Map,
typename Class_>
624 -> decltype(std::declval<std::ostream &>() << std::declval<typename Map::key_type>()
625 << std::declval<typename Map::mapped_type>(),
631 std::ostringstream
s;
634 for (
auto const &kv :
m) {
638 s << kv.first <<
": " << kv.second;
644 "Return the canonical string representation of this map.");
648 virtual size_t len() = 0;
655 virtual size_t len() = 0;
661 virtual size_t len() = 0;
666 template <
typename Map>
669 size_t len()
override {
return map.size(); }
673 return map.find(k.template cast<typename Map::key_type>()) !=
map.end();
674 }
catch (
const cast_error &) {
681 template <
typename Map>
684 size_t len()
override {
return map.size(); }
689 template <
typename Map>
692 size_t len()
override {
return map.size(); }
699 template <
typename Map,
typename holder_type = std::unique_ptr<Map>,
typename... Args>
701 using KeyType =
typename Map::key_type;
702 using MappedType =
typename Map::mapped_type;
703 using KeysView = detail::keys_view;
704 using ValuesView = detail::values_view;
705 using ItemsView = detail::items_view;
712 bool local = !tinfo || tinfo->module_local;
715 local = !tinfo || tinfo->module_local;
728 keys_view.def(
"__contains__", &KeysView::contains);
752 detail::map_if_insertion_operator<Map, Class_>(
cl,
name);
756 [](
const Map &
m) ->
bool {
return !
m.empty(); },
757 "Check whether the map is nonempty");
767 [](Map &
m) {
return std::unique_ptr<KeysView>(
new detail::KeysViewImpl<Map>(
m)); },
773 [](Map &
m) {
return std::unique_ptr<ValuesView>(
new detail::ValuesViewImpl<Map>(
m)); },
779 [](Map &
m) {
return std::unique_ptr<ItemsView>(
new detail::ItemsViewImpl<Map>(
m)); },
785 [](Map &
m,
const KeyType &k) -> MappedType & {
795 cl.def(
"__contains__", [](Map &
m,
const KeyType &k) ->
bool {
803 cl.def(
"__contains__", [](Map &,
const object &) ->
bool {
return false; });
806 detail::map_assignment<Map, Class_>(
cl);
808 cl.def(
"__delitem__", [](Map &
m,
const KeyType &k) {
817 cl.def(
"__len__", [](
const Map &
m) {
return m.size(); });