1 // R specific swig components
2 /*
3   Vectors
4 */
5 
6 %fragment("StdVectorTraits","header",fragment="StdSequenceTraits")
7 %{
8   namespace swig {
9     // vectors of doubles
10     template <>
11       struct traits_from_ptr<std::vector<double> > {
12       static SEXP from (std::vector<double > *val, int owner = 0) {
13         SEXP result;
14         PROTECT(result = Rf_allocVector(REALSXP, val->size()));
15         for (unsigned pos = 0; pos < val->size(); pos++)
16           {
17             NUMERIC_POINTER(result)[pos] = ((*val)[pos]);
18           }
19         UNPROTECT(1);
20         return(result);
21       }
22     };
23     // vectors of floats
24     template <>
25       struct traits_from_ptr<std::vector<float> > {
26       static SEXP from (std::vector<float > *val, int owner = 0) {
27         SEXP result;
28         PROTECT(result = Rf_allocVector(REALSXP, val->size()));
29         for (unsigned pos = 0; pos < val->size(); pos++)
30           {
31             NUMERIC_POINTER(result)[pos] = ((*val)[pos]);
32           }
33         UNPROTECT(1);
34         return(result);
35       }
36     };
37     // vectors of unsigned 8bit int
38     template <>
39       struct traits_from_ptr<std::vector<unsigned char> > {
40       static SEXP from (std::vector<unsigned char > *val, int owner = 0) {
41         SEXP result;
42         PROTECT(result = Rf_allocVector(INTSXP, val->size()));
43         for (unsigned pos = 0; pos < val->size(); pos++)
44           {
45             INTEGER_POINTER(result)[pos] = ((*val)[pos]);
46           }
47         UNPROTECT(1);
48         return(result);
49       }
50     };
51     // vectors of 8bit int
52     template <>
53       struct traits_from_ptr<std::vector<signed char> > {
54       static SEXP from (std::vector<signed char > *val, int owner = 0) {
55         SEXP result;
56         PROTECT(result = Rf_allocVector(INTSXP, val->size()));
57         for (unsigned pos = 0; pos < val->size(); pos++)
58           {
59             INTEGER_POINTER(result)[pos] = ((*val)[pos]);
60           }
61         UNPROTECT(1);
62         return(result);
63       }
64     };
65 
66     // vectors of unsigned 16bit int
67     template <>
68       struct traits_from_ptr<std::vector<unsigned short int> > {
69       static SEXP from (std::vector<unsigned short int > *val, int owner = 0) {
70         SEXP result;
71         PROTECT(result = Rf_allocVector(INTSXP, val->size()));
72         for (unsigned pos = 0; pos < val->size(); pos++)
73           {
74             INTEGER_POINTER(result)[pos] = ((*val)[pos]);
75           }
76         UNPROTECT(1);
77         return(result);
78       }
79     };
80     // vectors of 16bit int
81     template <>
82       struct traits_from_ptr<std::vector<short int> > {
83       static SEXP from (std::vector<short int > *val, int owner = 0) {
84         SEXP result;
85         PROTECT(result = Rf_allocVector(INTSXP, val->size()));
86         for (unsigned pos = 0; pos < val->size(); pos++)
87           {
88             INTEGER_POINTER(result)[pos] = ((*val)[pos]);
89           }
90         UNPROTECT(1);
91         return(result);
92       }
93     };
94 
95    // vectors of 32 bit unsigned int
96     template <>
97       struct traits_from_ptr<std::vector<unsigned int> > {
98       static SEXP from (std::vector<unsigned int> *val, int owner = 0) {
99         SEXP result;
100         PROTECT(result = Rf_allocVector(INTSXP, val->size()));
101         for (unsigned pos = 0; pos < val->size(); pos++)
102           {
103             INTEGER_POINTER(result)[pos] = ((*val)[pos]);
104           }
105         UNPROTECT(1);
106         return(result);
107       }
108     };
109 
110     // vectors of 32bit int
111     template <>
112       struct traits_from_ptr<std::vector<int> > {
113       static SEXP from (std::vector<int > *val, int owner = 0) {
114         SEXP result;
115         PROTECT(result = Rf_allocVector(INTSXP, val->size()));
116         for (unsigned pos = 0; pos < val->size(); pos++)
117           {
118             INTEGER_POINTER(result)[pos] = ((*val)[pos]);
119           }
120         UNPROTECT(1);
121         return(result);
122       }
123     };
124 
125    // vectors of 64 bit unsigned int
126 #if defined(SWIGWORDSIZE64)
127     template <>
128       struct traits_from_ptr<std::vector<unsigned long int> > {
129       static SEXP from (std::vector<unsigned long int> *val, int owner = 0) {
130         SEXP result;
131         PROTECT(result = Rf_allocVector(INTSXP, val->size()));
132         for (unsigned pos = 0; pos < val->size(); pos++)
133           {
134             INTEGER_POINTER(result)[pos] = ((*val)[pos]);
135           }
136         UNPROTECT(1);
137         return(result);
138       }
139     };
140      // vectors of 64 bit int
141     template <>
142       struct traits_from_ptr<std::vector<long int> > {
143       static SEXP from (std::vector<long int> *val, int owner = 0) {
144         SEXP result;
145         PROTECT(result = Rf_allocVector(INTSXP, val->size()));
146         for (unsigned pos = 0; pos < val->size(); pos++)
147           {
148             INTEGER_POINTER(result)[pos] = ((*val)[pos]);
149           }
150         UNPROTECT(1);
151         return(result);
152       }
153     };
154 #else
155     template <>
156       struct traits_from_ptr<std::vector<unsigned long long int> > {
157       static SEXP from (std::vector<unsigned long long int> *val, int owner = 0) {
158         SEXP result;
159         PROTECT(result = Rf_allocVector(INTSXP, val->size()));
160         for (unsigned pos = 0; pos < val->size(); pos++)
161           {
162             INTEGER_POINTER(result)[pos] = ((*val)[pos]);
163           }
164         UNPROTECT(1);
165         return(result);
166       }
167     };
168      // vectors of 64 bit int
169     template <>
170       struct traits_from_ptr<std::vector<long long int> > {
171       static SEXP from (std::vector<long long int> *val, int owner = 0) {
172         SEXP result;
173         PROTECT(result = Rf_allocVector(INTSXP, val->size()));
174         for (unsigned pos = 0; pos < val->size(); pos++)
175           {
176             INTEGER_POINTER(result)[pos] = ((*val)[pos]);
177           }
178         UNPROTECT(1);
179         return(result);
180       }
181     };
182 #endif
183     // vectors of bool
184     template <>
185       struct traits_from_ptr<std::vector<bool> > {
186       static SEXP from (std::vector<bool> *val, int owner = 0) {
187         SEXP result;
188         PROTECT(result = Rf_allocVector(LGLSXP, val->size()));
189         for (unsigned pos = 0; pos < val->size(); pos++)
190           {
191             LOGICAL_POINTER(result)[pos] = ((*val)[pos]);
192           }
193         UNPROTECT(1);
194         return(result);
195       }
196     };
197 
198     // vectors of strings
199     template <>
200       struct traits_from_ptr<std::vector<std::basic_string<char> > > {
201       static SEXP from (std::vector<std::basic_string<char> > *val, int owner = 0) {
202         SEXP result;
203          PROTECT(result = Rf_allocVector(STRSXP, val->size()));
204          for (unsigned pos = 0; pos < val->size(); pos++)
205            {
206              CHARACTER_POINTER(result)[pos] = Rf_mkChar(((*val)[pos]).c_str());
207            }
208         UNPROTECT(1);
209         return(result);
210       }
211     };
212 
213     // catch all that does everything with vectors
214     template <typename T>
215       struct traits_from_ptr< std::vector< T > > {
216       static SEXP from (std::vector< T > *val, int owner = 0) {
217         return SWIG_R_NewPointerObj(val, type_info< std::vector< T >  >(), owner);
218       }
219     };
220     /////////////////////////////////////////////////
221     template <>
222   struct traits_asptr < std::vector<double> > {
223     static int asptr(SEXP obj, std::vector<double> **val) {
224       std::vector<double> *p;
225       // not sure how to check the size of the SEXP obj is correct
226       unsigned int sexpsz = Rf_length(obj);
227       p = new std::vector<double>(sexpsz);
228       double *S = NUMERIC_POINTER(obj);
229       for (unsigned pos = 0; pos < p->size(); pos++)
230         {
231           (*p)[pos] = static_cast<double>(S[pos]);
232         }
233       int res = SWIG_OK;
234       if (SWIG_IsOK(res)) {
235         if (val) *val = p;
236       }
237       return res;
238     }
239   };
240 
241     template <>
242   struct traits_asptr < std::vector<float> > {
243     static int asptr(SEXP obj, std::vector<float> **val) {
244       std::vector<float> *p;
245       // not sure how to check the size of the SEXP obj is correct
246       unsigned int sexpsz = Rf_length(obj);
247       p = new std::vector<float>(sexpsz);
248       double *S = NUMERIC_POINTER(obj);
249       for (unsigned pos = 0; pos < p->size(); pos++)
250         {
251           (*p)[pos] = static_cast<double>(S[pos]);
252         }
253       int res = SWIG_OK;
254       if (SWIG_IsOK(res)) {
255         if (val) *val = p;
256       }
257       return res;
258     }
259   };
260 
261     // 8 bit integer types
262     template <>
263   struct traits_asptr < std::vector<unsigned char> > {
264     static int asptr(SEXP obj, std::vector<unsigned char> **val) {
265       std::vector<unsigned char> *p;
266       unsigned int sexpsz = Rf_length(obj);
267       p = new std::vector<unsigned char>(sexpsz);
268       SEXP coerced;
269       PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
270       int *S = INTEGER_POINTER(coerced);
271       for (unsigned pos = 0; pos < p->size(); pos++)
272         {
273           (*p)[pos] = static_cast<unsigned char>(S[pos]);
274         }
275       int res = SWIG_OK;
276       if (SWIG_IsOK(res)) {
277         if (val) *val = p;
278       }
279       UNPROTECT(1);
280       return res;
281     }
282   };
283 
284     template <>
285   struct traits_asptr < std::vector<signed char> > {
286     static int asptr(SEXP obj, std::vector<signed char> **val) {
287       std::vector<signed char> *p;
288       // not sure how to check the size of the SEXP obj is correct
289       int sexpsz = Rf_length(obj);
290       p = new std::vector<signed char>(sexpsz);
291       SEXP coerced;
292       PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
293       int *S = INTEGER_POINTER(coerced);
294       for (unsigned pos = 0; pos < p->size(); pos++)
295         {
296           (*p)[pos] = static_cast<signed char>(S[pos]);
297         }
298       int res = SWIG_OK;
299       if (SWIG_IsOK(res)) {
300         if (val) *val = p;
301       }
302       UNPROTECT(1);
303       return res;
304     }
305   };
306 
307    // 16 bit integer types
308     template <>
309   struct traits_asptr < std::vector<unsigned short int> > {
310     static int asptr(SEXP obj, std::vector<unsigned short int> **val) {
311       std::vector<unsigned short int> *p;
312       unsigned int sexpsz = Rf_length(obj);
313       p = new std::vector<unsigned short int>(sexpsz);
314       SEXP coerced;
315       PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
316       int *S = INTEGER_POINTER(coerced);
317       for (unsigned pos = 0; pos < p->size(); pos++)
318         {
319           (*p)[pos] = static_cast<unsigned short int>(S[pos]);
320         }
321       int res = SWIG_OK;
322       if (SWIG_IsOK(res)) {
323         if (val) *val = p;
324       }
325       UNPROTECT(1);
326       return res;
327     }
328   };
329 
330     template <>
331   struct traits_asptr < std::vector<short int> > {
332     static int asptr(SEXP obj, std::vector<short int> **val) {
333       std::vector<short int> *p;
334       // not sure how to check the size of the SEXP obj is correct
335       int sexpsz = Rf_length(obj);
336       p = new std::vector<short int>(sexpsz);
337       SEXP coerced;
338       PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
339       int *S = INTEGER_POINTER(coerced);
340       for (unsigned pos = 0; pos < p->size(); pos++)
341         {
342           (*p)[pos] = static_cast<short int>(S[pos]);
343         }
344       int res = SWIG_OK;
345       if (SWIG_IsOK(res)) {
346         if (val) *val = p;
347       }
348       UNPROTECT(1);
349       return res;
350     }
351   };
352     // 32 bit integer types
353     template <>
354   struct traits_asptr < std::vector<unsigned int> > {
355     static int asptr(SEXP obj, std::vector<unsigned int> **val) {
356       std::vector<unsigned int> *p;
357       unsigned int sexpsz = Rf_length(obj);
358       p = new std::vector<unsigned int>(sexpsz);
359       SEXP coerced;
360       PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
361       int *S = INTEGER_POINTER(coerced);
362       for (unsigned pos = 0; pos < p->size(); pos++)
363         {
364           (*p)[pos] = static_cast<unsigned int>(S[pos]);
365         }
366       int res = SWIG_OK;
367       if (SWIG_IsOK(res)) {
368         if (val) *val = p;
369       }
370       UNPROTECT(1);
371       return res;
372     }
373   };
374 
375     template <>
376   struct traits_asptr < std::vector<int> > {
377     static int asptr(SEXP obj, std::vector<int> **val) {
378       std::vector<int> *p;
379       // not sure how to check the size of the SEXP obj is correct
380       int sexpsz = Rf_length(obj);
381       p = new std::vector<int>(sexpsz);
382       SEXP coerced;
383       PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
384       int *S = INTEGER_POINTER(coerced);
385       for (unsigned pos = 0; pos < p->size(); pos++)
386         {
387           (*p)[pos] = static_cast<int>(S[pos]);
388         }
389       int res = SWIG_OK;
390       if (SWIG_IsOK(res)) {
391         if (val) *val = p;
392       }
393       UNPROTECT(1);
394       return res;
395     }
396   };
397 
398 #if defined(SWIGWORDSIZE64)
399     // 64 bit integer types
400     template <>
401   struct traits_asptr < std::vector<unsigned long int> > {
402     static int asptr(SEXP obj, std::vector<unsigned long int> **val) {
403       std::vector<unsigned long int> *p;
404       unsigned int sexpsz = Rf_length(obj);
405       p = new std::vector<unsigned long int>(sexpsz);
406       SEXP coerced;
407       PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
408       int *S = INTEGER_POINTER(coerced);
409       for (unsigned pos = 0; pos < p->size(); pos++)
410         {
411           (*p)[pos] = static_cast<unsigned long int>(S[pos]);
412         }
413       int res = SWIG_OK;
414       if (SWIG_IsOK(res)) {
415         if (val) *val = p;
416       }
417       UNPROTECT(1);
418       return res;
419     }
420   };
421 
422     template <>
423   struct traits_asptr < std::vector<long int> > {
424     static int asptr(SEXP obj, std::vector<long int> **val) {
425       std::vector<long int> *p;
426       // not sure how to check the size of the SEXP obj is correct
427       int sexpsz = Rf_length(obj);
428       p = new std::vector<long int>(sexpsz);
429       SEXP coerced;
430       PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
431       int *S = INTEGER_POINTER(coerced);
432       for (unsigned pos = 0; pos < p->size(); pos++)
433         {
434           (*p)[pos] = static_cast<long int>(S[pos]);
435         }
436       int res = SWIG_OK;
437       if (SWIG_IsOK(res)) {
438         if (val) *val = p;
439       }
440       UNPROTECT(1);
441       return res;
442     }
443   };
444 
445 #else
446     // 64 bit integer types
447     template <>
448   struct traits_asptr < std::vector<unsigned long long int> > {
449     static int asptr(SEXP obj, std::vector<unsigned long long int> **val) {
450       std::vector<unsigned long long int> *p;
451       unsigned int sexpsz = Rf_length(obj);
452       p = new std::vector<unsigned long long int>(sexpsz);
453       SEXP coerced;
454       PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
455       int *S = INTEGER_POINTER(coerced);
456       for (unsigned pos = 0; pos < p->size(); pos++)
457         {
458           (*p)[pos] = static_cast<unsigned long long int>(S[pos]);
459         }
460       int res = SWIG_OK;
461       if (SWIG_IsOK(res)) {
462         if (val) *val = p;
463       }
464       UNPROTECT(1);
465       return res;
466     }
467   };
468 
469     template <>
470   struct traits_asptr < std::vector<long long int> > {
471     static int asptr(SEXP obj, std::vector<long long int> **val) {
472       std::vector<long long int> *p;
473       // not sure how to check the size of the SEXP obj is correct
474       int sexpsz = Rf_length(obj);
475       p = new std::vector<long long int>(sexpsz);
476       SEXP coerced;
477       PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
478       int *S = INTEGER_POINTER(coerced);
479       for (unsigned pos = 0; pos < p->size(); pos++)
480         {
481           (*p)[pos] = static_cast<long long int>(S[pos]);
482         }
483       int res = SWIG_OK;
484       if (SWIG_IsOK(res)) {
485         if (val) *val = p;
486       }
487       UNPROTECT(1);
488       return res;
489     }
490   };
491 
492 #endif
493 
494     template <>
495   struct traits_asptr < std::vector<bool> > {
496     static int asptr(SEXP obj, std::vector<bool> **val) {
497       std::vector<bool> *p;
498       // not sure how to check the size of the SEXP obj is correct
499       int sexpsz = Rf_length(obj);
500       p = new std::vector<bool>(sexpsz);
501       SEXP coerced;
502       PROTECT(coerced = Rf_coerceVector(obj, LGLSXP));
503       int *S = LOGICAL_POINTER(coerced);
504       for (unsigned pos = 0; pos < p->size(); pos++)
505         {
506           (*p)[pos] = static_cast<bool>(S[pos]);
507         }
508       int res = SWIG_OK;
509       if (SWIG_IsOK(res)) {
510         if (val) *val = p;
511       }
512       UNPROTECT(1);
513       return res;
514     }
515   };
516 
517     template <>
518       struct traits_asptr < std::vector<std::basic_string<char> > > {
519       static int asptr(SEXP obj, std::vector<std::basic_string<char> > **val) {
520 	std::vector<std::basic_string<char> > *p;
521       // R character vectors are STRSXP containing CHARSXP
522       // access a CHARSXP using STRING_ELT
523       int sexpsz = Rf_length(obj);
524       p = new std::vector<std::basic_string<char> >(sexpsz);
525       SEXP coerced;
526       PROTECT(coerced = Rf_coerceVector(obj, STRSXP));
527       //SEXP *S = CHARACTER_POINTER(coerced);
528       for (unsigned pos = 0; pos < p->size(); pos++)
529         {
530 	  const char * thecstring = CHAR(STRING_ELT(coerced, pos));
531           (*p)[pos] = std::basic_string<char>(thecstring);
532         }
533       int res = SWIG_OK;
534       if (SWIG_IsOK(res)) {
535         if (val) *val = p;
536       }
537       UNPROTECT(1);
538       return res;
539     }
540   };
541 
542     // catchall for R to vector conversion
543   template <typename T>
544   struct traits_asptr < std::vector<T> > {
545     static int asptr(SEXP obj, std::vector<T> **val) {
546       std::vector<T> *p;
547       int res = SWIG_R_ConvertPtr(obj, (void**)&p, type_info< std::vector<T> >(), 0);
548       if (SWIG_IsOK(res)) {
549         if (val) *val = p;
550       }
551       return res;
552     }
553   };
554 
555   // now for vectors of vectors. These will be represented as lists of vectors on the
556   // catch all that does everything with vectors
557   template <>
558     struct traits_from_ptr<std::vector<std::vector<unsigned int> > > {
559       static SEXP from (std::vector< std::vector<unsigned int> > *val, int owner = 0) {
560         SEXP result;
561         // allocate the R list
562         PROTECT(result = Rf_allocVector(VECSXP, val->size()));
563         for (unsigned pos = 0; pos < val->size(); pos++)
564           {
565             // allocate the R vector
566             SET_VECTOR_ELT(result, pos, Rf_allocVector(INTSXP, val->at(pos).size()));
567             // Fill the R vector
568             for (unsigned vpos = 0; vpos < val->at(pos).size(); ++vpos)
569               {
570                 INTEGER_POINTER(VECTOR_ELT(result, pos))[vpos] = static_cast<int>(val->at(pos).at(vpos));
571               }
572           }
573         UNPROTECT(1);
574         return(result);
575       }
576     };
577 
578 
579   template <>
580     struct traits_from_ptr<std::vector<std::vector<int> > > {
581       static SEXP from (std::vector< std::vector<int > > *val, int owner = 0) {
582         SEXP result;
583         // allocate the R list
584         PROTECT(result = Rf_allocVector(VECSXP, val->size()));
585         for (unsigned pos = 0; pos < val->size(); pos++)
586           {
587             // allocate the R vector
588             SET_VECTOR_ELT(result, pos, Rf_allocVector(INTSXP, val->at(pos).size()));
589             // Fill the R vector
590             for (unsigned vpos = 0; vpos < val->at(pos).size(); ++vpos)
591               {
592                 INTEGER_POINTER(VECTOR_ELT(result, pos))[vpos] = static_cast<int>(val->at(pos).at(vpos));
593               }
594           }
595         UNPROTECT(1);
596         return(result);
597       }
598     };
599 
600   template <>
601     struct traits_from_ptr<std::vector<std::vector<float> > > {
602       static SEXP from (std::vector< std::vector<float > > *val, int owner = 0) {
603         SEXP result;
604         // allocate the R list
605         PROTECT(result = Rf_allocVector(VECSXP, val->size()));
606         for (unsigned pos = 0; pos < val->size(); pos++)
607           {
608             // allocate the R vector
609             SET_VECTOR_ELT(result, pos, Rf_allocVector(REALSXP, val->at(pos).size()));
610             // Fill the R vector
611             for (unsigned vpos = 0; vpos < val->at(pos).size(); ++vpos)
612               {
613                 NUMERIC_POINTER(VECTOR_ELT(result, pos))[vpos] = static_cast<double>(val->at(pos).at(vpos));
614               }
615           }
616         UNPROTECT(1);
617         return(result);
618       }
619     };
620 
621   template <>
622     struct traits_from_ptr<std::vector<std::vector<double> > > {
623       static SEXP from (std::vector< std::vector<double > > *val, int owner = 0) {
624         SEXP result;
625         // allocate the R list
626         PROTECT(result = Rf_allocVector(VECSXP, val->size()));
627         for (unsigned pos = 0; pos < val->size(); pos++)
628           {
629             // allocate the R vector
630             SET_VECTOR_ELT(result, pos, Rf_allocVector(REALSXP, val->at(pos).size()));
631             // Fill the R vector
632             for (unsigned vpos = 0; vpos < val->at(pos).size(); ++vpos)
633               {
634                 NUMERIC_POINTER(VECTOR_ELT(result, pos))[vpos] = static_cast<double>(val->at(pos).at(vpos));
635               }
636           }
637         UNPROTECT(1);
638         return(result);
639       }
640     };
641 
642   template <>
643     struct traits_from_ptr<std::vector<std::vector<bool> > > {
644       static SEXP from (std::vector< std::vector<bool> > *val, int owner = 0) {
645         SEXP result;
646         // allocate the R list
647         PROTECT(result = Rf_allocVector(VECSXP, val->size()));
648         for (unsigned pos = 0; pos < val->size(); pos++)
649           {
650             // allocate the R vector
651             SET_VECTOR_ELT(result, pos, Rf_allocVector(LGLSXP, val->at(pos).size()));
652             // Fill the R vector
653             for (unsigned vpos = 0; vpos < val->at(pos).size(); ++vpos)
654               {
655                 LOGICAL_POINTER(VECTOR_ELT(result, pos))[vpos] = (val->at(pos).at(vpos));
656               }
657           }
658         UNPROTECT(1);
659         return(result);
660       }
661     };
662 
663   template <typename T>
664     struct traits_from_ptr< std::vector < std::vector< T > > > {
665     static SEXP from (std::vector < std::vector< T > > *val, int owner = 0) {
666       return SWIG_R_NewPointerObj(val, type_info< std::vector < std::vector< T > > >(), owner);
667     }
668   };
669 
670   /////////////////////////////////////////////////////////////////
671 
672   // R side
673   template <>
674     struct traits_asptr < std::vector< std::vector<unsigned int> > > {
675     static int asptr(SEXP obj, std::vector< std::vector<unsigned int> > **val) {
676       std::vector <std::vector<unsigned int> > *p;
677       // this is the length of the list
678       unsigned int sexpsz = Rf_length(obj);
679       p = new std::vector< std::vector<unsigned int> > (sexpsz);
680 
681       for (unsigned listpos = 0; listpos < sexpsz; ++listpos)
682         {
683           unsigned vecsize = Rf_length(VECTOR_ELT(obj, listpos));
684           for (unsigned vpos = 0; vpos < vecsize; ++vpos)
685             {
686               (*p)[listpos].push_back(static_cast<int>(INTEGER_POINTER(VECTOR_ELT(obj, listpos))[vpos]));
687             }
688         }
689 
690       int res = SWIG_OK;
691 
692       if (SWIG_IsOK(res)) {
693         if (val) *val = p;
694       }
695       return res;
696     }
697   };
698 
699   template <>
700     struct traits_asptr < std::vector< std::vector< int> > > {
701     static int asptr(SEXP obj, std::vector< std::vector< int> > **val) {
702       std::vector <std::vector< int> > *p;
703       // this is the length of the list
704       unsigned int sexpsz = Rf_length(obj);
705       p = new std::vector< std::vector< int> > (sexpsz);
706 
707       for (unsigned listpos = 0; listpos < sexpsz; ++listpos)
708         {
709           unsigned vecsize = Rf_length(VECTOR_ELT(obj, listpos));
710           for (unsigned vpos = 0; vpos < vecsize; ++vpos)
711             {
712               (*p)[listpos].push_back(static_cast<int>(INTEGER_POINTER(VECTOR_ELT(obj, listpos))[vpos]));
713             }
714         }
715 
716       int res = SWIG_OK;
717 
718       if (SWIG_IsOK(res)) {
719         if (val) *val = p;
720       }
721       return res;
722     }
723   };
724 
725   template <>
726     struct traits_asptr < std::vector< std::vector< float> > > {
727     static int asptr(SEXP obj, std::vector< std::vector< float> > **val) {
728       std::vector <std::vector< float> > *p;
729       // this is the length of the list
730       unsigned int sexpsz = Rf_length(obj);
731       p = new std::vector< std::vector< float> > (sexpsz);
732 
733       for (unsigned listpos = 0; listpos < sexpsz; ++listpos)
734         {
735           unsigned vecsize = Rf_length(VECTOR_ELT(obj, listpos));
736           for (unsigned vpos = 0; vpos < vecsize; ++vpos)
737             {
738               (*p)[listpos].push_back(static_cast<float>(NUMERIC_POINTER(VECTOR_ELT(obj, listpos))[vpos]));
739             }
740         }
741 
742       int res = SWIG_OK;
743 
744       if (SWIG_IsOK(res)) {
745         if (val) *val = p;
746       }
747       return res;
748     }
749   };
750 
751   template <>
752     struct traits_asptr < std::vector< std::vector< double> > > {
753     static int asptr(SEXP obj, std::vector< std::vector< double> > **val) {
754       std::vector <std::vector< double> > *p;
755       // this is the length of the list
756       unsigned int sexpsz = Rf_length(obj);
757       p = new std::vector< std::vector< double> > (sexpsz);
758 
759       for (unsigned listpos = 0; listpos < sexpsz; ++listpos)
760         {
761           unsigned vecsize = Rf_length(VECTOR_ELT(obj, listpos));
762           for (unsigned vpos = 0; vpos < vecsize; ++vpos)
763             {
764               (*p)[listpos].push_back(static_cast<double>(NUMERIC_POINTER(VECTOR_ELT(obj, listpos))[vpos]));
765             }
766         }
767 
768       int res = SWIG_OK;
769 
770       if (SWIG_IsOK(res)) {
771         if (val) *val = p;
772       }
773       return res;
774     }
775   };
776 
777   template <>
778     struct traits_asptr < std::vector< std::vector< bool > > > {
779     static int asptr(SEXP obj, std::vector< std::vector< bool> > **val) {
780       std::vector <std::vector< bool > > *p;
781       // this is the length of the list
782       unsigned int sexpsz = Rf_length(obj);
783       p = new std::vector< std::vector< bool > > (sexpsz);
784 
785       for (unsigned listpos = 0; listpos < sexpsz; ++listpos)
786         {
787           unsigned vecsize = Rf_length(VECTOR_ELT(obj, listpos));
788           for (unsigned vpos = 0; vpos < vecsize; ++vpos)
789             {
790               (*p)[listpos].push_back(static_cast<bool>(LOGICAL_POINTER(VECTOR_ELT(obj, listpos))[vpos]));
791             }
792         }
793 
794       int res = SWIG_OK;
795 
796       if (SWIG_IsOK(res)) {
797         if (val) *val = p;
798       }
799       return res;
800     }
801   };
802 
803   //  catchall
804   template <typename T>
805     struct traits_asptr < std::vector< std::vector<T> > > {
806     static int asptr(SEXP obj, std::vector< std::vector<T> > **val) {
807       std::vector< std::vector<T> > *p;
808       Rprintf("vector of vectors - unsupported content\n");
809       int res = SWIG_R_ConvertPtr(obj, (void**)&p, type_info< std::vector< std::vector<T> > > (), 0);
810       if (SWIG_IsOK(res)) {
811         if (val) *val = p;
812       }
813       return res;
814     }
815   };
816 
817   }
818 %}
819 
820 #define %swig_vector_methods(Type...) %swig_sequence_methods(Type)
821 #define %swig_vector_methods_val(Type...) %swig_sequence_methods_val(Type);
822 
823 %define %traits_type_name(Type...)
824 %fragment(SWIG_Traits_frag(Type), "header",
825           fragment="StdTraits",fragment="StdVectorTraits") {
826   namespace swig {
827     template <>  struct traits< Type > {
828       typedef pointer_category category;
829       static const char* type_name() {
830         return #Type;
831       }
832     };
833   }
834  }
835 %enddef
836 
837 %include <std/std_vector.i>
838 
839 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<double>)
840 %traits_type_name(std::vector<double>)
841 %typemap("rtypecheck") std::vector<double>, std::vector<double> *, std::vector<double> &
842     %{ is.numeric($arg) %}
843 %typemap("rtype") std::vector<double> "numeric"
844 %typemap("scoercein") std::vector<double>, std::vector<double> *, std::vector<double> & "$input = as.numeric($input);";
845 
846 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<float>)
847 %traits_type_name(std::vector<float>)
848 
849 // reuse these for float
850 %typemap("rtype") std::vector<float>, std::vector<float> *, std::vector<float> & = std::vector<double>;
851 %typemap("rtypecheck") std::vector<float>, std::vector<float> *, std::vector<float> & = std::vector<double>;
852 %typemap("scoercein") std::vector<float>, std::vector<float> *, std::vector<float> & = std::vector<double>;
853 
854 
855 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<bool>);
856 %traits_type_name(std::vector<bool>);
857 %typemap("rtypecheck") std::vector<bool>, std::vector<bool> *, std::vector<bool> &
858    %{ is.logical($arg) %}
859 %typemap("rtype") std::vector<bool> "logical"
860 %typemap("scoercein") std::vector<bool> , std::vector<bool> & "$input = as.logical($input);";
861 
862 
863 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<int>);
864 %traits_type_name(std::vector<int>);
865 %typemap("rtypecheck") std::vector<int>, std::vector<int>  *, std::vector<int>  &
866    %{ is.integer($arg) || is.numeric($arg) %}
867 
868 %typemap("rtype") std::vector<int> "integer"
869 %typemap("scoercein") std::vector<int> , std::vector<int> *, std::vector<int> & "$input = as.integer($input);";
870 
871 // strings
872 %typemap("rtype") std::vector< std::basic_string<char> >,
873 std::vector< std::basic_string<char> > *,
874    std::vector< std::basic_string<char> > & "character"
875 
876 %typemap("rtypecheck") std::vector< std::basic_string<char> >,
877 std::vector< std::basic_string<char> > *,
878    std::vector< std::basic_string<char> > &
879    %{ is.character($arg) %}
880 
881 %typemap("scoercein") std::vector< std::basic_string<char> >,
882 std::vector< std::basic_string<char> > *,
883    std::vector< std::basic_string<char> > & "$input = as.character($input);";
884 
885 %typemap("scoerceout") std::vector< std::basic_string<char> >,
886 std::vector< std::basic_string<char> > *,
887    std::vector< std::basic_string<char> > &
888 %{    %}
889 
890 %apply std::vector< std::basic_string<char> > { std::vector< std::string> };
891 
892 // all the related integer vectors
893 // signed
894 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<signed char>);
895 %traits_type_name(std::vector<signed char>);
896 
897 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<signed short>);
898 %traits_type_name(std::vector<signed short>);
899 
900 #if defined(SWIGWORDSIZE64)
901 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<long int>);
902 %traits_type_name(std::vector<long int>);
903 #else
904 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<long long int>);
905 %traits_type_name(std::vector<long long int>);
906 #endif
907 
908 // unsigned
909 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<unsigned char>);
910 %traits_type_name(std::vector<unsigned char>);
911 
912 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<unsigned short>);
913 %traits_type_name(std::vector<unsigned short>);
914 
915 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<unsigned int>);
916 %traits_type_name(std::vector<unsigned int>);
917 
918 #if defined(SWIGWORDSIZE64)
919 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<unsigned long int>);
920 %traits_type_name(std::vector<unsigned long int>);
921 #else
922 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<unsigned long long int>);
923 %traits_type_name(std::vector<unsigned long long int>);
924 #endif
925 
926 // These R side typemaps are common for integer types
927 // but we can't use %apply as it will copy the C side ones too
928 // Also note that we don't seem to be able to use types like
929 // int_least8_t here.
930 %typemap("rtype") std::vector<signed char>, std::vector<signed char> *, std::vector<signed char> & = std::vector<int>;
931 %typemap("rtype") std::vector<signed short>, std::vector<signed short> *, std::vector<signed short> & = std::vector<int>;
932 %typemap("rtype") std::vector<unsigned char>, std::vector<unsigned char> *, std::vector<unsigned char> & = std::vector<int>;
933 %typemap("rtype") std::vector<unsigned int>, std::vector<unsigned int> *, std::vector<unsigned int> & = std::vector<int>;
934 
935 #if defined(SWIGWORDSIZE64)
936 %typemap("rtype") std::vector<long int>, std::vector<long int> *, std::vector<long int> & = std::vector<int>;
937 %typemap("rtype") std::vector<unsigned long int>, std::vector<unsigned long int> *, std::vector<unsigned long int> & = std::vector<int>;
938 #else
939 %typemap("rtype") std::vector<long long int>, std::vector<long long int> *, std::vector<long long int> & = std::vector<int>;
940 %typemap("rtype") std::vector<unsigned long long int>, std::vector<unsigned long long int> *, std::vector<unsigned long long int> & = std::vector<int>;
941 #endif
942 
943 
944 %typemap("scoercein") std::vector<signed char>, std::vector<signed char> *, std::vector<signed char> & = std::vector<int>;
945 %typemap("scoercein") std::vector<signed short>, std::vector<signed short> *, std::vector<signed short> & = std::vector<int>;
946 %typemap("scoercein") std::vector<unsigned char>, std::vector<unsigned char> *, std::vector<unsigned char> & = std::vector<int>;
947 %typemap("scoercein") std::vector<unsigned int>, std::vector<unsigned int> *, std::vector<unsigned int> & = std::vector<int>;
948 
949 #if defined(SWIGWORDSIZE64)
950 %typemap("scoercein") std::vector<long int>, std::vector<long int> *, std::vector<long int> & = std::vector<int>;
951 %typemap("scoercein") std::vector<unsigned long int>, std::vector<unsigned long int> *, std::vector<unsigned long int> & = std::vector<int>;
952 #else
953 %typemap("scoercein") std::vector<long long int>, std::vector<long long int> *, std::vector<long long int> & = std::vector<int>;
954 %typemap("scoercein") std::vector<unsigned long long int>, std::vector<unsigned long long int> *, std::vector<unsigned long long int> & = std::vector<int>;
955 #endif
956 
957 %typemap("rtypecheck") std::vector<signed char>, std::vector<signed char> *, std::vector<signed char> & = std::vector<int>;
958 %typemap("rtypecheck") std::vector<signed short>, std::vector<signed short> *, std::vector<signed short> & = std::vector<int>;
959 %typemap("rtypecheck") std::vector<unsigned char>, std::vector<unsigned char> *, std::vector<unsigned char> & = std::vector<int>;
960 %typemap("rtypecheck") std::vector<unsigned int>, std::vector<unsigned int> *, std::vector<unsigned int> & = std::vector<int>;
961 
962 #if defined(SWIGWORDSIZE64)
963 %typemap("rtypecheck") std::vector<long int>, std::vector<long int> *, std::vector<long int> &  = std::vector<int>;
964 %typemap("rtypecheck") std::vector<unsigned long int>, std::vector<unsigned long int> *, std::vector<unsigned long int> & = std::vector<int>;
965 #else
966 %typemap("rtypecheck") std::vector<long long int>, std::vector<long long int> *, std::vector<long long int> & = std::vector<int>;
967 %typemap("rtypecheck") std::vector<unsigned long long int>, std::vector<unsigned long long int> *, std::vector<unsigned long long int> & = std::vector<int>;
968 #endif
969 
970 ///////////////////////////////////////////////////////////////
971 
972 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<std::vector<int> >);
973 %traits_type_name(std::vector< std::vector<int> >);
974 %typemap("rtypecheck") std::vector<std::vector<int> >, std::vector<std::vector<int> > *, std::vector<std::vector<int> > &
975    %{ is.list($arg) && all(sapply($arg , is.integer) || sapply($arg, is.numeric)) %}
976 %typemap("rtype") std::vector<std::vector<int> >, std::vector<std::vector<int> > *, std::vector<std::vector<int> > & "list"
977 %typemap("scoercein") std::vector< std::vector<int> >, std::vector<std::vector<int> > *, std::vector<std::vector<int> > & "$input = lapply($input, as.integer);";
978 
979 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<std::vector<unsigned int> >);
980 %traits_type_name(std::vector< std::vector<unsigned int> >);
981 %typemap("rtypecheck") std::vector<std::vector<unsigned int> >, std::vector<std::vector<unsigned int> > *, std::vector<std::vector<unsigned int> > &
982    %{ is.list($arg) && all(sapply($arg , is.integer) || sapply($arg, is.numeric)) %}
983 %typemap("rtype") std::vector<std::vector<unsigned int> >, std::vector<std::vector<unsigned int> > *, std::vector<std::vector<unsigned int> > & "list"
984 %typemap("scoercein") std::vector< std::vector<unsigned int> >, std::vector<std::vector<int> > *, std::vector<std::vector<unsigned int> > & "$input = lapply($input, as.integer);";
985 
986 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<std::vector<float> >);
987 %traits_type_name(std::vector< std::vector<float> >);
988 %typemap("rtypecheck") std::vector<std::vector<float> >, std::vector<std::vector<float> > *, std::vector<std::vector<float> > &
989    %{ is.list($arg) && all(sapply($arg , is.integer) || sapply($arg, is.numeric)) %}
990 %typemap("rtype") std::vector<std::vector<float> >, std::vector<std::vector<float> > *, std::vector<std::vector<float> > "list"
991 %typemap("scoercein") std::vector< std::vector<float> >, std::vector<std::vector<float> > *, std::vector<std::vector<float> > & "$input = lapply($input, as.numeric);";
992 
993 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<std::vector<double> >);
994 %traits_type_name(std::vector< std::vector<double> >);
995 %typemap("rtypecheck") std::vector<std::vector<double> >, std::vector<std::vector<double> > *, std::vector<std::vector<double> > &
996    %{ is.list($arg) && all(sapply($arg , is.integer) || sapply($arg, is.numeric)) %}
997 %typemap("rtype") std::vector<std::vector<double> >, std::vector<std::vector<double> > *, std::vector<std::vector<double> > & "list"
998 %typemap("scoercein") std::vector< std::vector<double> >, std::vector<std::vector<double> > *, std::vector<std::vector<double> > &
999  "$input = lapply($input, as.numeric);";
1000 
1001 %typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<std::vector<bool> >);
1002 %traits_type_name(std::vector< std::vector<bool> >);
1003 %typemap("rtypecheck") std::vector<std::vector<bool> >, std::vector<std::vector<bool> > *, std::vector<std::vector<bool> > &
1004    %{ is.list($arg) && all(sapply($arg , is.integer) || sapply($arg, is.numeric)) %}
1005 %typemap("rtype") std::vector<std::vector<bool> >, std::vector<std::vector<bool> > *, std::vector<std::vector<bool> > & "list"
1006 %typemap("scoercein") std::vector< std::vector<bool> >, std::vector<std::vector<bool> > *, std::vector<std::vector<bool> > & "$input = lapply($input, as.logical);";
1007 
1008 // we don't want these to be given R classes as they
1009 // have already been turned into R vectors.
1010 %typemap(scoerceout) std::vector<double>,
1011    std::vector<double>*,
1012    std::vector<double>&,
1013    std::vector<float> ,
1014    std::vector<float>*,
1015    std::vector<float> ,
1016    std::vector<signed char>,
1017    std::vector<signed char>*,
1018    std::vector<signed char>&,
1019    std::vector<signed short>,
1020    std::vector<signed short>*,
1021    std::vector<signed short>&,
1022    std::vector<int>,
1023    std::vector<int>*,
1024    std::vector<int>&,
1025    std::vector<unsigned char>,
1026    std::vector<unsigned char>*,
1027    std::vector<unsigned char>&,
1028    std::vector<unsigned short>,
1029    std::vector<unsigned short>*,
1030    std::vector<unsigned short>&,
1031    std::vector<unsigned int>,
1032    std::vector<unsigned int>*,
1033    std::vector<unsigned int>&,
1034    std::vector<bool>,
1035    std::vector<bool>*,
1036    std::vector<bool>&,
1037  // vectors of vectors
1038    std::vector< std::vector<unsigned int> >,
1039    std::vector< std::vector<unsigned int> >*,
1040    std::vector< std::vector<unsigned int> >&,
1041    std::vector< std::vector<int> >,
1042    std::vector< std::vector<int> >*,
1043    std::vector< std::vector<int> >&,
1044    std::vector< std::vector<float> >,
1045    std::vector< std::vector<float> >*,
1046    std::vector< std::vector<float> >&,
1047    std::vector< std::vector<double> >,
1048    std::vector< std::vector<double> >*,
1049    std::vector< std::vector<double> >&,
1050    std::vector< std::vector<bool> >,
1051    std::vector< std::vector<bool> >*,
1052    std::vector< std::vector<bool> >&
1053  %{    %}
1054 
1055 #if defined(SWIGWORDSIZE64)
1056 %typemap(scoerceout) std::vector<long int>,
1057    std::vector<long int>*,
1058    std::vector<long int>&,
1059    std::vector<unsigned long int>,
1060    std::vector<unsigned long int>*,
1061    std::vector<unsigned long int>&
1062  %{    %}
1063 #else
1064 
1065 %typemap(scoerceout) std::vector<long long int>,
1066    std::vector<long long int>*,
1067    std::vector<long long int>&,
1068    std::vector<unsigned long long int>,
1069    std::vector<unsigned long long int>*,
1070    std::vector<unsigned long long int>&
1071  %{    %}
1072 
1073 #endif
1074