Exemplo n.º 1
0
VALUE
rb_seq_push(VALUE self, VALUE args, int flag)
{
  CvSeq *seq = CVSEQ(self);
  VALUE klass = seqblock_class(seq), object;
  void *buffer = NULL;
  for (int i = 0; i < RARRAY_LEN(args); i++) {
    object = RARRAY_PTR(args)[i];
    if (CLASS_OF(object) == klass) {
      if (flag == CV_FRONT)
	cvSeqPushFront(seq, DATA_PTR(object));
      else
	cvSeqPush(seq, DATA_PTR(object));
    }
    else if (rb_obj_is_kind_of(object, rb_klass) && CLASS_OF(rb_first(object)) == klass) { // object is CvSeq
      buffer = cvCvtSeqToArray(CVSEQ(object), cvAlloc(CVSEQ(object)->total * CVSEQ(object)->elem_size));
      cvSeqPushMulti(seq, buffer, CVSEQ(object)->total, flag);
      cvFree(&buffer);
    }
    else {
      rb_raise(rb_eTypeError, "arguments should be %s or %s which includes %s.",
	       rb_class2name(klass), rb_class2name(rb_klass), rb_class2name(klass));
    }
  }
  return self;
}
Exemplo n.º 2
0
/*
 * call-seq:
 *   [<i>index</i>] -> obj or nil
 *
 * Return sequence-block at <i>index</i>.
 */
VALUE
rb_aref(VALUE self, VALUE index)
{
  CvSeq *seq = CVSEQ(self);
  int idx = NUM2INT(index);
  if (seq->total == 0)
    return Qnil;
  if (idx >= seq->total)
    rb_raise(rb_eIndexError, "index %d out of sequence", idx);

  VALUE result = Qnil;
  try {
    if (seqblock_class(seq) == rb_cFixnum)
      result = INT2NUM(*CV_GET_SEQ_ELEM(int, seq, idx));
    else
      result = REFER_OBJECT(seqblock_class(seq), cvGetSeqElem(seq, idx), self);
  }
Exemplo n.º 3
0
/*
 * call-seq:
 *   last -> obj or nil
 *
 * Return last sequence-block.
 */
VALUE
rb_last(VALUE self)
{
  CvSeq *seq = CVSEQ(self);
  if(!(seq->total > 0))
    return Qnil;
  return REFER_OBJECT(seqblock_class(seq), cvGetSeqElem(seq, -1), self);
}
Exemplo n.º 4
0
/*
 * call-seq:
 *   v_next -> seq or nil
 *
 * Return the sequence vertically located in next.
 * Return <tt>nil</tt> if not existing.
 */
VALUE
rb_v_next(VALUE self)
{
  CvSeq *seq = CVSEQ(self);
  if (seq->v_next)
    return new_sequence(CLASS_OF(self), seq->v_next, seqblock_class(seq), lookup_root_object(seq));
  else
    return Qnil;
}
Exemplo n.º 5
0
/*
 * call-seq:
 *   insert(<i>index,obj</i>) -> self
 *
 * Inserts the given values before element with the given index (which may be negative).
 */
VALUE
rb_insert(VALUE self, VALUE index, VALUE object)
{
  Check_Type(index, T_FIXNUM);
  CvSeq *seq = CVSEQ(self);
  VALUE klass = seqblock_class(seq);
  if(CLASS_OF(object) != klass)
    rb_raise(rb_eTypeError, "arguments should be %s.", rb_class2name(klass));
  cvSeqInsert(seq, FIX2INT(index), DATA_PTR(object));
  return self;
}
Exemplo n.º 6
0
/*
 * call-seq:
 *   shift -> obj or nil
 *
 * Returns the first element of <i>self</i> and removes it (shifting all other elements down by one). Returns <tt>nil</tt> if the array is empty.
 */
VALUE
rb_shift(VALUE self)
{
  CvSeq *seq = CVSEQ(self);
  if(!(seq->total > 0)){
    return Qnil;
  }
  VALUE object = GENERIC_OBJECT(seqblock_class(seq), malloc(seq->elem_size));
  cvSeqPopFront(seq, DATA_PTR(object));
  return object;
}
Exemplo n.º 7
0
/*
 * call-seq:
 *   [<i>index</i>] -> obj or nil
 *
 * Return sequence-block at <i>index</i>.
 */
VALUE
rb_aref(VALUE self, VALUE index)
{
  CvSeq *seq = CVSEQ(self);
  int idx = NUM2INT(index);
  if(!(seq->total > 0))
    return Qnil;
  if (idx >= seq->total)
    rb_raise(rb_eIndexError, "index %d out of sequence", idx);
  return REFER_OBJECT(seqblock_class(seq), cvGetSeqElem(seq, idx), self);
}
Exemplo n.º 8
0
/*
 * call-seq:
 *   each{|obj| ... } -> self
 *
 * Calls block once for each sequence-block in <i>self</i>,
 * passing that sequence-block as a parameter.
 *   seq = CvSeq.new(CvIndex)
 *   seq.push(5, 6, 7)
 *   seq.each{|x| print x, " -- " }
 * produces:
 *   5 -- 6 -- 7 --
 */
VALUE
rb_each(VALUE self)
{
  CvSeq *seq = CVSEQ(self);
  if(seq->total > 0){
    VALUE klass = seqblock_class(seq);
    for(int i = 0; i < seq->total; i++){
      rb_yield(REFER_OBJECT(klass, cvGetSeqElem(seq, i), self));
    }
  }
  return self;
}
Exemplo n.º 9
0
/*
 * call-seq:
 *   [<i>index</i>] -> obj or nil
 *
 * Return sequence-block at <i>index</i>.
 */
VALUE
rb_aref(VALUE self, VALUE index)
{
  CvSeq *seq = CVSEQ(self);
  int idx = NUM2INT(index);
  if (seq->total == 0) {
    return Qnil;
  }
  if (idx >= seq->total) {
    rb_raise(rb_eIndexError, "index %d out of sequence", idx);
  }

  VALUE result = Qnil;
  try {
    VALUE klass = seqblock_class(seq);
    if (RTEST(rb_class_inherited_p(klass, rb_cInteger))) {
      result = INT2NUM(*CV_GET_SEQ_ELEM(int, seq, idx));
    }
    else {
      result = REFER_OBJECT(klass, cvGetSeqElem(seq, idx), self);
    }
  }