1 // SWIG file Collection.i
2 
3 %{
4 #include "openturns/Collection.hxx"
5 %}
6 
7 %include Collection_doc.i
8 
9 %rename(__contains__) OT::Collection::contains;
10 %rename(__baseaddress__) OT::Collection::data;
11 %rename(__elementsize__) OT::Collection::elementSize;
12 %ignore OT::Collection::Collection(std::initializer_list<T> initList);
13 
14 %include openturns/Collection.hxx
15 %copyctor Collection;
16 
17 namespace OT {
18 
19 %extend Collection {
20 
Collection(PyObject * pyObj)21 Collection(PyObject * pyObj)
22 {
23   return OT::buildCollectionFromPySequence< T >(pyObj);
24 }
25 
Collection(const Collection<T> & other)26 template <class T> Collection(const Collection<T> & other)
27 {
28   return new OT::Collection<T>(other);
29 }
30 
31 
32 %define OT_COLLECTION_GETITEM(collectionType, elementType)
33 PyObject * __getitem__(PyObject * arg) const
34 {
35   if (PyInt_Check(arg))
36   {
37     long val2 = 0;
38     int ecode2 = 0;
39     ecode2 = SWIG_AsVal_long(arg, &val2);
40     if (!SWIG_IsOK(ecode2)) {
41       SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" #collectionType "___getitem__" "', argument " "2"" of type '" "OT::UnsignedInteger""'");
42     }
43     if (val2 < 0) {
44       val2 += self->getSize();
45     }
46     OT::UnsignedInteger arg2 = static_cast< OT::UnsignedInteger >(val2);
47     return OT::convert< elementType, OT::traitsPythonType<elementType>::Type>(self->at(arg2));
48   }
49   else if (PySlice_Check(arg))
50   {
51     Py_ssize_t start = 0;
52     Py_ssize_t stop = 0;
53     Py_ssize_t step = 0;
54     Py_ssize_t size = 0;
55     if (PySlice_GetIndicesEx(OT::SliceCast(arg), self->getSize(), &start, &stop, &step, &size ) < 0)
56       throw OT::InternalException(HERE) << "PySlice_GetIndicesEx failed";
57     collectionType result(size);
58     if (step == 1)
59       std::copy(self->begin() + start, self->begin() + start + size, result.begin());
60     else
61       for (Py_ssize_t i = 0; i < size; ++ i)
62         result.at(i) = self->at(start + i * step);
63     return SWIG_NewPointerObj((new collectionType(static_cast< const collectionType& >(result))), SWIG_TypeQuery(#collectionType " *"), SWIG_POINTER_OWN |  0);
64   }
65   else if (PySequence_Check(arg))
66   {
67     OT::ScopedPyObjectPointer newPyObj(PySequence_Fast(arg, ""));
68     const Py_ssize_t size = PySequence_Fast_GET_SIZE(newPyObj.get());
69     collectionType result(size);
70     for (Py_ssize_t i = 0; i < size; ++ i)
71     {
72       PyObject * elt = PySequence_Fast_GET_ITEM(newPyObj.get(), i);
73       long index = 0;
74       if (PyInt_Check(elt))
75         index = PyInt_AsLong(elt);
76       else if (PyObject_HasAttrString(elt, "__int__"))
77       {
78         OT::ScopedPyObjectPointer intValue(PyObject_CallMethod(elt, const_cast<char *>("__int__"), const_cast<char *>("()")));
79         if (intValue.isNull())
80           OT::handleException();
81         index = PyInt_AsLong(intValue.get());
82       }
83       else
84         throw OT::InvalidArgumentException(HERE) << "Indexing list expects int type";
85 
86       if (index < 0) {
87         index += self->getSize();
88       }
89       if (index < 0) {
90         throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getSize() << ", " << self->getSize() - 1 << "]." ;
91       }
92       result[i] = self->at(index);
93     }
94     return SWIG_NewPointerObj((new collectionType(static_cast< const collectionType& >(result))), SWIG_TypeQuery(#collectionType " *"), SWIG_POINTER_OWN |  0);
95   }
96   else if (PyObject_HasAttrString(arg, "__int__"))
97   {
98     OT::ScopedPyObjectPointer intValue(PyObject_CallMethod(arg, const_cast<char *>("__int__"), const_cast<char *>("()")));
99     if (intValue.isNull())
100       OT::handleException();
101     long index = PyInt_AsLong(intValue.get());
102     if (index < 0) {
103       index += self->getSize();
104     }
105     OT::UnsignedInteger arg2 = static_cast< OT::UnsignedInteger >(index);
106     return OT::convert< elementType, OT::traitsPythonType<elementType>::Type>(self->at(arg2));
107   }
108   else
109     SWIG_exception(SWIG_TypeError, "Collection.__getitem__ expects int, slice or sequence argument");
110 fail:
111   return NULL;
112 }
113 %enddef
114 
115 
116 %define OT_COLLECTION_SETITEM(collectionType, elementType)
117 PyObject * __setitem__(PyObject * arg, PyObject * valObj)
118 {
119   if (PyInt_Check(arg))
120   {
121     long val2 = 0;
122     int ecode2 = 0;
123     ecode2 = SWIG_AsVal_long(arg, &val2);
124     if (!SWIG_IsOK(ecode2)) {
125       SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" #collectionType "___setitem__" "', argument " "2"" of type '" "OT::UnsignedInteger""'");
126     }
127     if (val2 < 0) {
128       val2 += self->getSize();
129     }
130     OT::UnsignedInteger arg2 = static_cast< OT::UnsignedInteger >(val2);
131     elementType val = OT::checkAndConvert< OT::traitsPythonType<elementType>::Type, elementType >(valObj);
132     self->at(arg2) = val;
133   }
134   else if (PySlice_Check(arg))
135   {
136     Py_ssize_t start = 0;
137     Py_ssize_t stop = 0;
138     Py_ssize_t step = 0;
139     Py_ssize_t size = 0;
140     PySlice_GetIndicesEx(OT::SliceCast(arg), self->getSize(), &start, &stop, &step, &size);
141     collectionType temp2;
142     collectionType *val2 = 0;
143     if (! SWIG_IsOK(SWIG_ConvertPtr(valObj, (void **) &val2, SWIG_TypeQuery(#collectionType " *"), SWIG_POINTER_NO_NULL))) {
144       temp2 = OT::convert< OT::_PySequence_, collectionType >(valObj);
145       val2 = &temp2;
146     }
147     assert(val2);
148     if (step == 1)
149       std::copy(val2->begin(), val2->end(), self->begin() + start);
150     else
151       for (Py_ssize_t i = 0; i < size; ++ i)
152         self->at(start + i*step) = val2->at(i);
153   }
154   else if (PySequence_Check(arg))
155   {
156     OT::ScopedPyObjectPointer newPyObj(PySequence_Fast(arg, ""));
157     const Py_ssize_t size = PySequence_Fast_GET_SIZE(newPyObj.get());
158     OT::Indices indices(size);
159     for (Py_ssize_t i = 0; i < size; ++ i)
160     {
161       PyObject * elt = PySequence_Fast_GET_ITEM(newPyObj.get(), i);
162       long index = 0;
163       if (PyInt_Check(elt))
164         index = PyInt_AsLong(elt);
165       else if (PyObject_HasAttrString(elt, "__int__"))
166       {
167         OT::ScopedPyObjectPointer intValue(PyObject_CallMethod(elt, const_cast<char *>("__int__"), const_cast<char *>("()")));
168         if (intValue.isNull())
169           OT::handleException();
170         index = PyInt_AsLong(intValue.get());
171       }
172       else
173         throw OT::InvalidArgumentException(HERE) << "Indexing list expects int type";
174 
175       if (index < 0) {
176         index += self->getSize();
177       }
178       if (index < 0) {
179         throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getSize() << ", " << self->getSize() - 1 << "]." ;
180       }
181       indices[i] = index;
182     }
183     collectionType temp2;
184     collectionType *val2 = 0;
185     if (! SWIG_IsOK(SWIG_ConvertPtr(valObj, (void **) &val2, SWIG_TypeQuery(#collectionType " *"), SWIG_POINTER_NO_NULL))) {
186       temp2 = OT::convert< OT::_PySequence_, collectionType >(valObj);
187       val2 = &temp2;
188     }
189     assert(val2);
190     for (Py_ssize_t i = 0; i < size; ++ i)
191       self->at(indices[i]) = val2->at(i);
192   }
193   else if (PyObject_HasAttrString(arg, "__int__"))
194   {
195     OT::ScopedPyObjectPointer intValue(PyObject_CallMethod(arg, const_cast<char *>("__int__"), const_cast<char *>("()")));
196     if (intValue.isNull())
197       OT::handleException();
198     long index = PyInt_AsLong(intValue.get());
199     if (index < 0) {
200       index += self->getSize();
201     }
202     OT::UnsignedInteger arg2 = static_cast< OT::UnsignedInteger >(index);
203     elementType val = OT::checkAndConvert< OT::traitsPythonType<elementType>::Type, elementType >(valObj);
204     self->at(arg2) = val;
205   }
206   else
207     SWIG_exception(SWIG_TypeError, "Collection.__setitem__ expects int, slice or sequence argument");
208   return SWIG_Py_Void();
209 fail:
210   return NULL;
211 }
212 %enddef
213 
214 %define OTCollectionOperatorsHelper(collectionType, elementType)
215 OT_COLLECTION_SETITEM(collectionType, elementType)
216 OT_COLLECTION_GETITEM(collectionType, elementType)
217 Bool __eq__(const collectionType & other) { return (*self) == other; }
__ne__(const collectionType & other)218 Bool __ne__(const collectionType & other) { return (*self) != other; }
219 %enddef
220 
221 } // %extend
222 
223 }
224