Class: R2rb::RVector

Inherits:
Object show all
Defined in:
lib/R4rb/R2rb_eval.rb,
ext/R4rb/R4rb.c,
ext/R4rb/R4rb.4.1.c

Direct Known Subclasses

Rserve::RVector

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Object



353
354
355
356
357
358
359
# File 'ext/R4rb/R4rb.c', line 353

VALUE RVect_initialize(VALUE self, VALUE name)
{
  rb_iv_set(self,"@name",name);
  rb_iv_set(self,"@type",rb_str_new2("var"));
  rb_iv_set(self,"@arg",rb_str_new2(""));
  return self;
}

Instance Attribute Details

#nameObject

[]= iter !!!

#typeObject

Class Method Details

.assign(name, arr) ⇒ Object



535
536
537
538
539
540
541
542
543
544
545
546
# File 'ext/R4rb/R4rb.c', line 535

VALUE RVect_assign(VALUE obj, VALUE name,VALUE arr)
{
  SEXP ans;
  char *tmp;

  ans=util_VALUE2SEXP(arr);

  tmp = StringValuePtr(name);
  defineVar(install(tmp),ans,R_GlobalEnv);

  return Qnil; 
}

Instance Method Details

#<<(name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/R4rb/R2rb_eval.rb', line 28

def <<(name)
  if name.is_a? Symbol
    @name=name.to_s
    @type="var"
  else
    @name=name
    @type="expr"
  end
  ##p [:RVector, @name, @type]
  return self
end

#>(arr) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/R4rb/R2rb_eval.rb', line 51

def >(arr) 
  res=self.get

#puts "res";p @name;p res
  unless res.nil?
#puts "arr.class:";p arr.class
#puts "res.class";p res.class
    res=[res] unless res.is_a? Array
    arr.replace(res)
  else
    arr.clear
  end
  return self
end

#[](index) ⇒ Object

faster than self.to_a



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'ext/R4rb/R4rb.c', line 465

VALUE RVect_aref(VALUE self, VALUE index)
{
  SEXP ans;
  VALUE res;
  char *name;
  int n,i;
  Rcomplex cpl;
#ifdef cqls
  VALUE tmp;
#endif
  i = FIX2INT(index);
  
#ifdef cqls
  if(!RVect_isValid(self)) return Qnil;
  tmp=rb_iv_get(self,"@name");
  name = StringValuePtr(tmp);
  ans = findVar(install(name),R_GlobalEnv); //currently in  R_GlobalEnv!!!
#else
  ans = util_getVar(self);
#endif
  n=length(ans);
  //printf("i=%d and n=%d\n",i,n);
  if(i<n) {
    switch(TYPEOF(ans)) {
    case REALSXP:
      res=rb_float_new(REAL(ans)[i]);
      break;
    case INTSXP:
      res=INT2FIX(INTEGER(ans)[i]);
      break;
    case LGLSXP:
      res=(INTEGER(ans)[i] ? Qtrue : Qfalse);
      break;
    case STRSXP:
      res=rb_str_new2(CHAR(STRING_ELT(ans,i)));
      break;
    case CPLXSXP:
      rb_require("complex");
      cpl=COMPLEX(ans)[i];
      res = rb_eval_string("Complex.new(0,0)");
      rb_iv_set(res,"@real",rb_float_new(cpl.r));
      rb_iv_set(res,"@image",rb_float_new(cpl.i));
      break;
    }
  } else {
    res = Qnil;
  }
  return res;
}

#arg=(arg) ⇒ Object



40
41
42
# File 'lib/R4rb/R2rb_eval.rb', line 40

def arg=(arg)
  @arg=arg
end

#getObject Also known as: to_a, value



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'ext/R4rb/R4rb.c', line 407

VALUE RVect_get(VALUE self)
{
  SEXP ans;
  VALUE res;
  char *name;
  int n,i;
  Rcomplex cpl;
  VALUE res2; 

  //#define cqls
#ifdef cqls 
  VALUE tmp;
  if(!RVect_isValid(self)) return Qnil;
#else  
  ans = util_getVar(self);

  if(ans==R_NilValue) {
    //printf("Sortie de get avec nil\n");
    return Qnil;
  }
#endif
#ifdef cqls 
  tmp=rb_iv_get(self,"@name");
  name = StringValuePtr(tmp);
  ans = findVar(install(name),R_GlobalEnv); 
#endif

  res=util_SEXP2VALUE(ans);
  if(length(ans)==1) res=rb_ary_entry(res,0);
  return res; 
}

#get_with_argObject Also known as: value_with_arg

method “arg=” defined in eval.rb!! @arg initialized in method “initialize”



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'ext/R4rb/R4rb.c', line 439

VALUE RVect_get_with_arg(VALUE self)
{
  SEXP ans;
  VALUE res;
  char *name;
  int n,i;
  Rcomplex cpl;
  VALUE res2; 

  ans = util_getExpr_with_arg(self);
 
  if(ans==R_NilValue) {
    //printf("Sortie de get avec nil\n");
    return Qnil;
  }
  res=util_SEXP2VALUE(ans);
 
//printf("RVect_get_with_arg: length(ans)=%d\n",length(ans));
 if (length(ans)==1) res=rb_ary_entry(res,0);

  return res;
}

#lengthObject



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'ext/R4rb/R4rb.c', line 386

VALUE RVect_length(VALUE self)
{
  SEXP ans;
  char *name;
#ifdef cqls
  VALUE tmp;
  tmp=rb_iv_get(self,"@name");
  if(!RVect_isValid(self)) return Qnil;
  name = StringValuePtr(tmp);
  ans = findVar(install(name),R_GlobalEnv); //currently in  R_GlobalEnv!!!
#else
  ans = util_getVar(self);

  if(ans==R_NilValue) {
    //printf("Sortie de length avec nil\n");
    return Qnil;
  }
#endif
  return INT2NUM(length(ans));
}

#set(arr) ⇒ Object Also known as: <, value=



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'ext/R4rb/R4rb.c', line 515

VALUE RVect_set(VALUE self,VALUE arr)
{
  SEXP ans;
  char *name;
  VALUE tmp;

  ans=util_VALUE2SEXP(arr);
  
  tmp=rb_iv_get(self,"@name");
  name = StringValuePtr(tmp);
  if(util_isVariable(self)) {
    defineVar(install(name),ans,R_GlobalEnv); //currently in R_GlobalEnv!!!
  } else {
    defineVar(install(".rubyExport"),ans,R_GlobalEnv);
    util_eval1string(rb_str_cat2(rb_str_dup(rb_iv_get(self,"@name")),"<-.rubyExport"));
  }

  return self; 
}

#set_arg(arg) ⇒ Object

this method is the same as the previous one but return self! Let us notice that even by adding return self in the previous one I could not manage to execute (rvect.arg=“”).value_with_arg but fortunately rvect.set_arg(“”).value_with_arg is working!



46
47
48
49
# File 'lib/R4rb/R2rb_eval.rb', line 46

def set_arg(arg)
  @arg=arg
  return self
end

#set_with_arg(arr) ⇒ Object Also known as: value_with_arg=



548
549
550
551
552
553
554
555
# File 'ext/R4rb/R4rb.c', line 548

VALUE RVect_set_with_arg(VALUE self,VALUE arr)
{
  VALUE tmp;
  defineVar(install(".rubyExport"),util_VALUE2SEXP(arr),R_GlobalEnv);
  tmp=rb_iv_get(self,"@arg"); 
  util_eval1string(rb_str_cat2(rb_str_cat2(rb_str_dup(rb_iv_get(self,"@name")),StringValuePtr(tmp)),"<-.rubyExport"));
  return self;
}

#valid?Boolean

Returns:

  • (Boolean)


361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'ext/R4rb/R4rb.c', line 361

VALUE RVect_isValid(VALUE self)
{
  SEXP ans;
  char *name;

#ifdef cqls
  VALUE tmp;
  tmp=rb_iv_get(self,"@name");
  name = StringValuePtr(tmp);
  ans = findVar(install(name),R_GlobalEnv); //currently in  R_GlobalEnv!!!
#else
  ans = util_getVar(self);
#endif
  if(!util_isVector(ans)) {
#ifndef cqls
    VALUE tmp;
    tmp=rb_iv_get(self,"@name");
    name = StringValuePtr(tmp);
#endif
    rb_warn("%s is not a R vector !!!",name); //TODO name not defined
    return Qfalse;
  }
  return Qtrue;
}