vector.go 646 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package R
  2. /*
  3. #cgo LDFLAGS: -lm -lR
  4. #cgo CFLAGS: -I /usr/share/R/include/
  5. #include <stdlib.h>
  6. #include <R.h>
  7. #include <Rinternals.h>
  8. #include <Rdefines.h>
  9. #include <R_ext/Parse.h>
  10. #include <Rembedded.h>
  11. SEXP GenericVectorElt(SEXP vec, int i) {
  12. return VECTOR_ELT(vec, i);
  13. }
  14. */
  15. import "C"
  16. type Vector struct {
  17. expression
  18. }
  19. func NewVector(length int) *Vector {
  20. v := Vector{}
  21. v.expr = C.allocVector(C.REALSXP, C.R_xlen_t(length))
  22. v.length = length
  23. return &v
  24. }
  25. func (this *Vector) Get(i int) *Result {
  26. this.boundsCheck(i)
  27. C.Rf_protect(this.expr)
  28. defer C.Rf_unprotect(1)
  29. return NewResult(C.GenericVectorElt(this.expr, C.int(i)))
  30. }