1 // SWIG file IndicesCollection.i
2
3 %{
4 #include "openturns/IndicesCollection.hxx"
5 %}
6
7 %include IndicesCollection_doc.i
8
9 OTTypedInterfaceObjectHelper(IndicesCollection)
10
11 %typemap(in) const IndicesCollection & ($1_basetype temp) {
12 if (! SWIG_IsOK(SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, SWIG_POINTER_NO_NULL))) {
13 try {
14 temp = OT::convert<OT::_PySequence_, OT::IndicesCollection>($input);
15 $1 = &temp;
catch(OT::InvalidArgumentException &)16 } catch (OT::InvalidArgumentException &) {
17 SWIG_exception(SWIG_TypeError, "Object passed as argument is not convertible to an IndicesCollection");
18 }
19 }
20 }
21
22 %typemap(typecheck,precedence=5) const IndicesCollection & {
23 $1 = SWIG_IsOK(SWIG_ConvertPtr($input, NULL, $1_descriptor, SWIG_POINTER_NO_NULL)) ||
24 OT::isAPythonBufferOf<OT::UnsignedInteger, 2>($input) || OT::isAPythonSequenceOf<OT::_PySequence_>($input);
25 }
26
27 %apply const IndicesCollection & { const OT::IndicesCollection & };
28
29 %include openturns/IndicesCollection.hxx
30
31 namespace OT {
32 %extend IndicesCollection {
33
__getitem__(SignedInteger index)34 Indices __getitem__(SignedInteger index) const {
35 OT::UnsignedInteger size = self->getSize();
36 if (size == 0) throw OT::OutOfBoundException(HERE) << "collection is empty.";
37 if (index < 0) {
38 index += self->getSize();
39 }
40 if (index < 0 || index >= static_cast<OT::SignedInteger>(size)) {
41 throw OT::OutOfBoundException(HERE) << "index should be in [-" << size << ", " << size - 1 << "]." ;
42 }
43 return OT::Indices(self->cbegin_at(index), self->cend_at(index));
44 }
45
__setitem__(SignedInteger index,const Indices & val)46 void __setitem__ (SignedInteger index,
47 const Indices & val) {
48 OT::UnsignedInteger size = self->getSize();
49 if (size == 0) throw OT::OutOfBoundException(HERE) << "collection is empty.";
50 if (index < 0) {
51 index += self->getSize();
52 }
53 if (index < 0 || index >= static_cast<OT::SignedInteger>(size)) {
54 throw OT::OutOfBoundException(HERE) << "index should be in [-" << size << ", " << size - 1 << "]." ;
55 }
56 OT::UnsignedInteger thisSize = self->cend_at(index) - self->cbegin_at(index);
57 if (val.getSize() != thisSize)
58 throw OT::InvalidArgumentException(HERE) << "element at position " << index << " is of size " << thisSize << " whereas value size is " << val.getSize();
59 std::copy(val.begin(), val.end(), self->begin_at(index));
60 }
61
__len__()62 UnsignedInteger __len__() const
63 {
64 return self->getSize();
65 }
66
IndicesCollection(const IndicesCollection & other)67 IndicesCollection(const IndicesCollection & other)
68 {
69 return new OT::IndicesCollection(other);
70 }
71
IndicesCollection(PyObject * pyObj)72 IndicesCollection(PyObject * pyObj)
73 {
74 return new OT::IndicesCollection( OT::convert< OT::_PySequence_, OT::IndicesCollection >(pyObj) );
75 }
76
77 }
78 }
79