PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/rel-1-3-15/SWIG/Lib/python/std_vector.i

#
Swig | 726 lines | 681 code | 21 blank | 24 comment | 0 complexity | cb0d9e04703adb23123871a83a2f4266 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. //
  2. // SWIG typemaps for std::vector types
  3. // Luigi Ballabio
  4. // Apr 8, 2002
  5. //
  6. // Python implementation
  7. %include std_common.i
  8. %include exception.i
  9. // __getitem__ is required to raise an IndexError for for-loops to work
  10. // other methods which can raise are made to throw an IndexError as well
  11. %exception std::vector::__getitem__ {
  12. try {
  13. $action
  14. } catch (std::out_of_range& e) {
  15. SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
  16. }
  17. }
  18. %exception std::vector::__setitem__ {
  19. try {
  20. $action
  21. } catch (std::out_of_range& e) {
  22. SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
  23. }
  24. }
  25. %exception std::vector::__delitem__ {
  26. try {
  27. $action
  28. } catch (std::out_of_range& e) {
  29. SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
  30. }
  31. }
  32. %exception std::vector::pop {
  33. try {
  34. $action
  35. } catch (std::out_of_range& e) {
  36. SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
  37. }
  38. }
  39. // ------------------------------------------------------------------------
  40. // std::vector
  41. //
  42. // The aim of all that follows would be to integrate std::vector with
  43. // Python as much as possible, namely, to allow the user to pass and
  44. // be returned Python tuples or lists.
  45. // const declarations are used to guess the intent of the function being
  46. // exported; therefore, the following rationale is applied:
  47. //
  48. // -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*):
  49. // the parameter being read-only, either a Python sequence or a
  50. // previously wrapped std::vector<T> can be passed.
  51. // -- f(std::vector<T>&), f(std::vector<T>*):
  52. // the parameter must be modified; therefore, only a wrapped std::vector
  53. // can be passed.
  54. // -- std::vector<T> f():
  55. // the vector is returned by copy; therefore, a Python sequence of T:s
  56. // is returned which is most easily used in other Python functions
  57. // -- std::vector<T>& f(), std::vector<T>* f(), const std::vector<T>& f(),
  58. // const std::vector<T>* f():
  59. // the vector is returned by reference; therefore, a wrapped std::vector
  60. // is returned
  61. // ------------------------------------------------------------------------
  62. %{
  63. #include <vector>
  64. #include <algorithm>
  65. #include <stdexcept>
  66. %}
  67. // exported class
  68. namespace std {
  69. template<class T> class vector {
  70. %typemap(in) vector<T> (std::vector<T>* v) {
  71. if (PyTuple_Check($input) || PyList_Check($input)) {
  72. unsigned int size = (PyTuple_Check($input) ?
  73. PyTuple_Size($input) :
  74. PyList_Size($input));
  75. $1 = std::vector<T >(size);
  76. for (unsigned int i=0; i<size; i++) {
  77. T* x;
  78. PyObject* o = PySequence_GetItem($input,i);
  79. if ((SWIG_ConvertPtr(o,(void **) &x,
  80. $descriptor(T *),0)) != -1) {
  81. (($1_type &)$1)[i] = *x;
  82. Py_DECREF(o);
  83. } else {
  84. Py_DECREF(o);
  85. PyErr_SetString(PyExc_TypeError,
  86. "vector<" #T "> expected");
  87. SWIG_fail;
  88. }
  89. }
  90. } else if (SWIG_ConvertPtr($input,(void **) &v,
  91. $&1_descriptor,1) != -1){
  92. $1 = *v;
  93. } else {
  94. PyErr_SetString(PyExc_TypeError,"vector<" #T "> expected");
  95. SWIG_fail;
  96. }
  97. }
  98. %typemap(in) const vector<T>& (std::vector<T> temp,
  99. std::vector<T>* v),
  100. const vector<T>* (std::vector<T> temp,
  101. std::vector<T>* v) {
  102. if (PyTuple_Check($input) || PyList_Check($input)) {
  103. unsigned int size = (PyTuple_Check($input) ?
  104. PyTuple_Size($input) :
  105. PyList_Size($input));
  106. temp = std::vector<T >(size);
  107. $1 = &temp;
  108. for (unsigned int i=0; i<size; i++) {
  109. T* x;
  110. PyObject* o = PySequence_GetItem($input,i);
  111. if ((SWIG_ConvertPtr(o,(void **) &x,
  112. $descriptor(T *),0)) != -1) {
  113. temp[i] = *x;
  114. Py_DECREF(o);
  115. } else {
  116. Py_DECREF(o);
  117. PyErr_SetString(PyExc_TypeError,
  118. "vector<" #T "> expected");
  119. SWIG_fail;
  120. }
  121. }
  122. } else if (SWIG_ConvertPtr($input,(void **) &v,
  123. $1_descriptor,1) != -1){
  124. $1 = v;
  125. } else {
  126. PyErr_SetString(PyExc_TypeError,"vector<" #T "> expected");
  127. SWIG_fail;
  128. }
  129. }
  130. %typemap(out) vector<T> {
  131. $result = PyTuple_New($1.size());
  132. for (unsigned int i=0; i<$1.size(); i++) {
  133. T* ptr = new T((($1_type &)$1)[i]);
  134. PyTuple_SetItem($result,i,
  135. SWIG_NewPointerObj((void *) ptr,
  136. $descriptor(T *), 1));
  137. }
  138. }
  139. %typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
  140. /* native sequence? */
  141. if (PyTuple_Check($input) || PyList_Check($input)) {
  142. unsigned int size = (PyTuple_Check($input) ?
  143. PyTuple_Size($input) :
  144. PyList_Size($input));
  145. if (size == 0) {
  146. /* an empty sequence can be of any type */
  147. $1 = 1;
  148. } else {
  149. /* check the first element only */
  150. T* x;
  151. PyObject* o = PySequence_GetItem($input,0);
  152. if ((SWIG_ConvertPtr(o,(void **) &x,
  153. $descriptor(T *),0)) != -1)
  154. $1 = 1;
  155. else
  156. $1 = 0;
  157. }
  158. } else {
  159. /* wrapped vector? */
  160. std::vector<T >* v;
  161. if (SWIG_ConvertPtr($input,(void **) &v,
  162. $&1_descriptor,0) != -1)
  163. $1 = 1;
  164. else
  165. $1 = 0;
  166. }
  167. }
  168. %typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
  169. const vector<T>* {
  170. /* native sequence? */
  171. if (PyTuple_Check($input) || PyList_Check($input)) {
  172. unsigned int size = (PyTuple_Check($input) ?
  173. PyTuple_Size($input) :
  174. PyList_Size($input));
  175. if (size == 0) {
  176. /* an empty sequence can be of any type */
  177. $1 = 1;
  178. } else {
  179. /* check the first element only */
  180. T* x;
  181. PyObject* o = PySequence_GetItem($input,0);
  182. if ((SWIG_ConvertPtr(o,(void **) &x,
  183. $descriptor(T *),0)) != -1)
  184. $1 = 1;
  185. else
  186. $1 = 0;
  187. }
  188. } else {
  189. /* wrapped vector? */
  190. std::vector<T >* v;
  191. if (SWIG_ConvertPtr($input,(void **) &v,
  192. $1_descriptor,0) != -1)
  193. $1 = 1;
  194. else
  195. $1 = 0;
  196. }
  197. }
  198. public:
  199. vector();
  200. vector(unsigned int size, const T& value=T());
  201. vector(const vector<T> &);
  202. %rename(__len__) size;
  203. unsigned int size() const;
  204. void clear();
  205. %rename(append) push_back;
  206. void push_back(const T& x);
  207. %extend {
  208. bool __nonzero__() {
  209. return !(self->empty());
  210. }
  211. T pop() {
  212. if (self->size() == 0)
  213. throw std::out_of_range("pop from empty vector");
  214. T x = self->back();
  215. self->pop_back();
  216. return x;
  217. }
  218. T& __getitem__(int i) {
  219. int size = int(self->size());
  220. if (i<0) i += size;
  221. if (i>=0 && i<size)
  222. return (*self)[i];
  223. else
  224. throw std::out_of_range("vector index out of range");
  225. }
  226. std::vector<T> __getslice__(int i, int j) {
  227. int size = int(self->size());
  228. if (i<0) i = size+i;
  229. if (j<0) j = size+j;
  230. if (i<0) i = 0;
  231. if (j>size) j = size;
  232. std::vector<T > tmp(j-i);
  233. std::copy(self->begin()+i,self->begin()+j,tmp.begin());
  234. return tmp;
  235. }
  236. void __setitem__(int i, const T& x) {
  237. int size = int(self->size());
  238. if (i<0) i+= size;
  239. if (i>=0 && i<size)
  240. (*self)[i] = x;
  241. else
  242. throw std::out_of_range("vector index out of range");
  243. }
  244. void __setslice__(int i, int j, const std::vector<T>& v) {
  245. int size = int(self->size());
  246. if (i<0) i = size+i;
  247. if (j<0) j = size+j;
  248. if (i<0) i = 0;
  249. if (j>size) j = size;
  250. if (int(v.size()) == j-i) {
  251. std::copy(v.begin(),v.end(),self->begin()+i);
  252. } else {
  253. self->erase(self->begin()+i,self->begin()+j);
  254. if (i+1 <= self->size()) {
  255. self->insert(self->begin()+i,v.begin(),v.end());
  256. } else {
  257. self->insert(self->end(),v.begin(),v.end());
  258. }
  259. }
  260. }
  261. void __delitem__(int i) {
  262. int size = int(self->size());
  263. if (i<0) i+= size;
  264. if (i>=0 && i<size)
  265. self->erase(self->begin()+i);
  266. else
  267. throw std::out_of_range("vector index out of range");
  268. }
  269. void __delslice__(int i, int j) {
  270. int size = int(self->size());
  271. if (i<0) i = size+i;
  272. if (j<0) j = size+j;
  273. if (i<0) i = 0;
  274. if (j>size) j = size;
  275. self->erase(self->begin()+i,self->begin()+j);
  276. }
  277. }
  278. };
  279. // Partial specialization for vectors of pointers. [ beazley ]
  280. template<class T> class vector<T*> {
  281. %typemap(in) vector<T> (std::vector<T>* v) {
  282. if (PyTuple_Check($input) || PyList_Check($input)) {
  283. unsigned int size = (PyTuple_Check($input) ?
  284. PyTuple_Size($input) :
  285. PyList_Size($input));
  286. $1 = std::vector<T >(size);
  287. for (unsigned int i=0; i<size; i++) {
  288. T x;
  289. PyObject* o = PySequence_GetItem($input,i);
  290. if ((SWIG_ConvertPtr(o,(void **) &x,
  291. $descriptor(T),0)) != -1) {
  292. (($1_type &)$1)[i] = x;
  293. Py_DECREF(o);
  294. } else {
  295. Py_DECREF(o);
  296. PyErr_SetString(PyExc_TypeError,
  297. "vector<" #T "> expected");
  298. SWIG_fail;
  299. }
  300. }
  301. } else if (SWIG_ConvertPtr($input,(void **) &v,
  302. $&1_descriptor,1) != -1){
  303. $1 = *v;
  304. } else {
  305. PyErr_SetString(PyExc_TypeError,"vector<" #T "> expected");
  306. SWIG_fail;
  307. }
  308. }
  309. %typemap(in) const vector<T>& (std::vector<T> temp,
  310. std::vector<T>* v),
  311. const vector<T>* (std::vector<T> temp,
  312. std::vector<T>* v) {
  313. if (PyTuple_Check($input) || PyList_Check($input)) {
  314. unsigned int size = (PyTuple_Check($input) ?
  315. PyTuple_Size($input) :
  316. PyList_Size($input));
  317. temp = std::vector<T >(size);
  318. $1 = &temp;
  319. for (unsigned int i=0; i<size; i++) {
  320. T x;
  321. PyObject* o = PySequence_GetItem($input,i);
  322. if ((SWIG_ConvertPtr(o,(void **) &x,
  323. $descriptor(T),0)) != -1) {
  324. temp[i] = x;
  325. Py_DECREF(o);
  326. } else {
  327. Py_DECREF(o);
  328. PyErr_SetString(PyExc_TypeError,
  329. "vector<" #T "> expected");
  330. SWIG_fail;
  331. }
  332. }
  333. } else if (SWIG_ConvertPtr($input,(void **) &v,
  334. $1_descriptor,1) != -1){
  335. $1 = v;
  336. } else {
  337. PyErr_SetString(PyExc_TypeError,"vector<" #T "> expected");
  338. SWIG_fail;
  339. }
  340. }
  341. %typemap(out) vector<T> {
  342. $result = PyTuple_New($1.size());
  343. for (unsigned int i=0; i<$1.size(); i++) {
  344. T ptr = (($1_type &)$1)[i];
  345. PyTuple_SetItem($result,i,
  346. SWIG_NewPointerObj((void *) ptr,
  347. $descriptor(T), 0));
  348. }
  349. }
  350. %typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
  351. /* native sequence? */
  352. if (PyTuple_Check($input) || PyList_Check($input)) {
  353. unsigned int size = (PyTuple_Check($input) ?
  354. PyTuple_Size($input) :
  355. PyList_Size($input));
  356. if (size == 0) {
  357. /* an empty sequence can be of any type */
  358. $1 = 1;
  359. } else {
  360. /* check the first element only */
  361. T x;
  362. PyObject* o = PySequence_GetItem($input,0);
  363. if ((SWIG_ConvertPtr(o,(void **) &x,
  364. $descriptor(T),0)) != -1)
  365. $1 = 1;
  366. else
  367. $1 = 0;
  368. }
  369. } else {
  370. /* wrapped vector? */
  371. std::vector<T >* v;
  372. if (SWIG_ConvertPtr($input,(void **) &v,
  373. $&1_descriptor,0) != -1)
  374. $1 = 1;
  375. else
  376. $1 = 0;
  377. }
  378. }
  379. %typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
  380. const vector<T>* {
  381. /* native sequence? */
  382. if (PyTuple_Check($input) || PyList_Check($input)) {
  383. unsigned int size = (PyTuple_Check($input) ?
  384. PyTuple_Size($input) :
  385. PyList_Size($input));
  386. if (size == 0) {
  387. /* an empty sequence can be of any type */
  388. $1 = 1;
  389. } else {
  390. /* check the first element only */
  391. T x;
  392. PyObject* o = PySequence_GetItem($input,0);
  393. if ((SWIG_ConvertPtr(o,(void **) &x,
  394. $descriptor(T),0)) != -1)
  395. $1 = 1;
  396. else
  397. $1 = 0;
  398. }
  399. } else {
  400. /* wrapped vector? */
  401. std::vector<T >* v;
  402. if (SWIG_ConvertPtr($input,(void **) &v,
  403. $1_descriptor,0) != -1)
  404. $1 = 1;
  405. else
  406. $1 = 0;
  407. }
  408. }
  409. public:
  410. vector();
  411. vector(unsigned int size, const T& value=T());
  412. vector(const vector<T> &);
  413. %rename(__len__) size;
  414. unsigned int size() const;
  415. void clear();
  416. %rename(append) push_back;
  417. void push_back(T x);
  418. %extend {
  419. bool __nonzero__() {
  420. return !(self->empty());
  421. }
  422. T pop() {
  423. if (self->size() == 0)
  424. throw std::out_of_range("pop from empty vector");
  425. T x = self->back();
  426. self->pop_back();
  427. return x;
  428. }
  429. T __getitem__(int i) {
  430. int size = int(self->size());
  431. if (i<0) i += size;
  432. if (i>=0 && i<size)
  433. return (*self)[i];
  434. else
  435. throw std::out_of_range("vector index out of range");
  436. }
  437. std::vector<T> __getslice__(int i, int j) {
  438. int size = int(self->size());
  439. if (i<0) i = size+i;
  440. if (j<0) j = size+j;
  441. if (i<0) i = 0;
  442. if (j>size) j = size;
  443. std::vector<T > tmp(j-i);
  444. std::copy(self->begin()+i,self->begin()+j,tmp.begin());
  445. return tmp;
  446. }
  447. void __setitem__(int i, const T& x) {
  448. int size = int(self->size());
  449. if (i<0) i+= size;
  450. if (i>=0 && i<size)
  451. (*self)[i] = x;
  452. else
  453. throw std::out_of_range("vector index out of range");
  454. }
  455. void __setslice__(int i, int j, const std::vector<T>& v) {
  456. int size = int(self->size());
  457. if (i<0) i = size+i;
  458. if (j<0) j = size+j;
  459. if (i<0) i = 0;
  460. if (j>size) j = size;
  461. if (int(v.size()) == j-i) {
  462. std::copy(v.begin(),v.end(),self->begin()+i);
  463. } else {
  464. self->erase(self->begin()+i,self->begin()+j);
  465. if (i+1 <= self->size())
  466. self->insert(self->begin()+i,v.begin(),v.end());
  467. else
  468. self->insert(self->end(),v.begin(),v.end());
  469. }
  470. }
  471. void __delitem__(int i) {
  472. int size = int(self->size());
  473. if (i<0) i+= size;
  474. if (i>=0 && i<size)
  475. self->erase(self->begin()+i);
  476. else
  477. throw std::out_of_range("vector index out of range");
  478. }
  479. void __delslice__(int i, int j) {
  480. int size = int(self->size());
  481. if (i<0) i = size+i;
  482. if (j<0) j = size+j;
  483. if (i<0) i = 0;
  484. if (j>size) j = size;
  485. self->erase(self->begin()+i,self->begin()+j);
  486. }
  487. }
  488. };
  489. // specializations for built-ins
  490. %define specialize_std_vector(T,CHECK,CONVERT_FROM,CONVERT_TO)
  491. template<> class vector<T> {
  492. %typemap(in) vector<T> (std::vector<T>* v) {
  493. if (PyTuple_Check($input) || PyList_Check($input)) {
  494. unsigned int size = (PyTuple_Check($input) ?
  495. PyTuple_Size($input) :
  496. PyList_Size($input));
  497. $1 = std::vector<T >(size);
  498. for (unsigned int i=0; i<size; i++) {
  499. PyObject* o = PySequence_GetItem($input,i);
  500. if (CHECK(o)) {
  501. (($1_type &)$1)[i] = (T)(CONVERT_FROM(o));
  502. Py_DECREF(o);
  503. } else {
  504. Py_DECREF(o);
  505. PyErr_SetString(PyExc_TypeError,
  506. "vector<" #T "> expected");
  507. SWIG_fail;
  508. }
  509. }
  510. } else if (SWIG_ConvertPtr($input,(void **) &v,
  511. $&1_descriptor,1) != -1){
  512. $1 = *v;
  513. } else {
  514. PyErr_SetString(PyExc_TypeError,"vector<" #T "> expected");
  515. SWIG_fail;
  516. }
  517. }
  518. %typemap(in) const vector<T>& (std::vector<T> temp,
  519. std::vector<T>* v),
  520. const vector<T>* (std::vector<T> temp,
  521. std::vector<T>* v) {
  522. if (PyTuple_Check($input) || PyList_Check($input)) {
  523. unsigned int size = (PyTuple_Check($input) ?
  524. PyTuple_Size($input) :
  525. PyList_Size($input));
  526. temp = std::vector<T >(size);
  527. $1 = &temp;
  528. for (unsigned int i=0; i<size; i++) {
  529. PyObject* o = PySequence_GetItem($input,i);
  530. if (CHECK(o)) {
  531. temp[i] = (T)(CONVERT_FROM(o));
  532. Py_DECREF(o);
  533. } else {
  534. Py_DECREF(o);
  535. PyErr_SetString(PyExc_TypeError,
  536. "vector<" #T "> expected");
  537. SWIG_fail;
  538. }
  539. }
  540. } else if (SWIG_ConvertPtr($input,(void **) &v,
  541. $1_descriptor,1) != -1){
  542. $1 = v;
  543. } else {
  544. PyErr_SetString(PyExc_TypeError,"vector<" #T "> expected");
  545. SWIG_fail;
  546. }
  547. }
  548. %typemap(out) vector<T> {
  549. $result = PyTuple_New($1.size());
  550. for (unsigned int i=0; i<$1.size(); i++)
  551. PyTuple_SetItem($result,i,
  552. CONVERT_TO((($1_type &)$1)[i]));
  553. }
  554. %typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
  555. /* native sequence? */
  556. if (PyTuple_Check($input) || PyList_Check($input)) {
  557. unsigned int size = (PyTuple_Check($input) ?
  558. PyTuple_Size($input) :
  559. PyList_Size($input));
  560. if (size == 0) {
  561. /* an empty sequence can be of any type */
  562. $1 = 1;
  563. } else {
  564. /* check the first element only */
  565. PyObject* o = PySequence_GetItem($input,0);
  566. if (CHECK(o))
  567. $1 = 1;
  568. else
  569. $1 = 0;
  570. }
  571. } else {
  572. /* wrapped vector? */
  573. std::vector<T >* v;
  574. if (SWIG_ConvertPtr($input,(void **) &v,
  575. $&1_descriptor,0) != -1)
  576. $1 = 1;
  577. else
  578. $1 = 0;
  579. }
  580. }
  581. %typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
  582. const vector<T>* {
  583. /* native sequence? */
  584. if (PyTuple_Check($input) || PyList_Check($input)) {
  585. unsigned int size = (PyTuple_Check($input) ?
  586. PyTuple_Size($input) :
  587. PyList_Size($input));
  588. if (size == 0) {
  589. /* an empty sequence can be of any type */
  590. $1 = 1;
  591. } else {
  592. /* check the first element only */
  593. PyObject* o = PySequence_GetItem($input,0);
  594. if (CHECK(o))
  595. $1 = 1;
  596. else
  597. $1 = 0;
  598. }
  599. } else {
  600. /* wrapped vector? */
  601. std::vector<T >* v;
  602. if (SWIG_ConvertPtr($input,(void **) &v,
  603. $1_descriptor,0) != -1)
  604. $1 = 1;
  605. else
  606. $1 = 0;
  607. }
  608. }
  609. public:
  610. vector();
  611. vector(unsigned int size, const T& value=T());
  612. vector(const vector<T> &);
  613. %rename(__len__) size;
  614. unsigned int size() const;
  615. %rename(__nonzero__) empty;
  616. bool empty() const;
  617. void clear();
  618. %rename(append) push_back;
  619. void push_back(T x);
  620. %extend {
  621. T pop() {
  622. if (self->size() == 0)
  623. throw std::out_of_range("pop from empty vector");
  624. T x = self->back();
  625. self->pop_back();
  626. return x;
  627. }
  628. T __getitem__(int i) {
  629. int size = int(self->size());
  630. if (i<0) i += size;
  631. if (i>=0 && i<size)
  632. return (*self)[i];
  633. else
  634. throw std::out_of_range("vector index out of range");
  635. }
  636. std::vector<T> __getslice__(int i, int j) {
  637. int size = int(self->size());
  638. if (i<0) i = size+i;
  639. if (j<0) j = size+j;
  640. if (i<0) i = 0;
  641. if (j>size) j = size;
  642. std::vector<T > tmp(j-i);
  643. std::copy(self->begin()+i,self->begin()+j,tmp.begin());
  644. return tmp;
  645. }
  646. void __setitem__(int i, T x) {
  647. int size = int(self->size());
  648. if (i<0) i+= size;
  649. if (i>=0 && i<size)
  650. (*self)[i] = x;
  651. else
  652. throw std::out_of_range("vector index out of range");
  653. }
  654. void __setslice__(int i, int j, const std::vector<T>& v) {
  655. int size = int(self->size());
  656. if (i<0) i = size+i;
  657. if (j<0) j = size+j;
  658. if (i<0) i = 0;
  659. if (j>size) j = size;
  660. if (int(v.size()) == j-i) {
  661. std::copy(v.begin(),v.end(),self->begin()+i);
  662. } else {
  663. self->erase(self->begin()+i,self->begin()+j);
  664. if (i+1 <= self->size())
  665. self->insert(self->begin()+i,v.begin(),v.end());
  666. else
  667. self->insert(self->end(),v.begin(),v.end());
  668. }
  669. }
  670. void __delitem__(int i) {
  671. int size = int(self->size());
  672. if (i<0) i+= size;
  673. if (i>=0 && i<size)
  674. self->erase(self->begin()+i);
  675. else
  676. throw std::out_of_range("vector index out of range");
  677. }
  678. void __delslice__(int i, int j) {
  679. int size = int(self->size());
  680. if (i<0) i = size+i;
  681. if (j<0) j = size+j;
  682. if (i<0) i = 0;
  683. if (j>size) j = size;
  684. self->erase(self->begin()+i,self->begin()+j);
  685. }
  686. }
  687. };
  688. %enddef
  689. specialize_std_vector(bool,PyInt_Check,PyInt_AsLong,SwigInt_FromBool);
  690. specialize_std_vector(int,PyInt_Check,PyInt_AsLong,PyInt_FromLong);
  691. specialize_std_vector(short,PyInt_Check,PyInt_AsLong,PyInt_FromLong);
  692. specialize_std_vector(long,PyInt_Check,PyInt_AsLong,PyInt_FromLong);
  693. specialize_std_vector(unsigned int,PyInt_Check,\
  694. PyInt_AsLong,PyInt_FromLong);
  695. specialize_std_vector(unsigned short,PyInt_Check,\
  696. PyInt_AsLong,PyInt_FromLong);
  697. specialize_std_vector(unsigned long,PyInt_Check,\
  698. PyInt_AsLong,PyInt_FromLong);
  699. specialize_std_vector(double,SwigNumber_Check,\
  700. SwigNumber_AsDouble,PyFloat_FromDouble);
  701. specialize_std_vector(float,SwigNumber_Check,\
  702. SwigNumber_AsDouble,PyFloat_FromDouble);
  703. specialize_std_vector(std::string,PyString_Check,\
  704. SwigString_AsString,SwigString_FromString);
  705. }