Ejemplo n.º 1
0
/*
 * call-seq:
 *   
 * 
 * From: https://code.ros.org/trac/opencv/browser/trunk/opencv/samples/c/find_obj.cpp?rev=2065
 */
VALUE
rb_flann(VALUE klass, VALUE objectDesc, VALUE imageDesc)
{
  const cv::Mat m_object(CVMAT(objectDesc));
  const cv::Mat m_image(CVMAT(imageDesc));
  
  cv::Mat m_indices(m_object.rows, 2, CV_32S);
  cv::Mat m_dists(m_object.rows, 2, CV_32F);
  
  cv::flann::Index flann_index(m_image, cv::flann::KDTreeIndexParams(4));  // using 4 randomized kdtrees
  flann_index.knnSearch(m_object, m_indices, m_dists, 2, cv::flann::SearchParams(64)); // maximum number of leafs checked
  
  VALUE ptpairs = rb_ary_new();
  
  int* indices_ptr = m_indices.ptr<int>(0);
  float* dists_ptr = m_dists.ptr<float>(0);
  for (int i = 0; i < m_indices.rows; ++i) {
      if (dists_ptr[2 * i] < 0.6 * dists_ptr[2 * i + 1]) {
          rb_ary_push(ptpairs, rb_int_new(i));
          rb_ary_push(ptpairs, rb_int_new(indices_ptr[2 * i]));
      }
  }
  
  return ptpairs;
}
Ejemplo n.º 2
0
/** Yields an entity's language, id, start, and end position to a required Ruby block */
void ragel_parse_yield_entity(const char *lang, const char *entity, int s, int e) {
  if (rb_block_given_p()) {
    VALUE ary;
    ary = rb_ary_new2(3);
    rb_ary_store(ary, 0, ID2SYM(rb_intern(lang)));
    rb_ary_store(ary, 1, ID2SYM(rb_intern(entity)));
    rb_ary_store(ary, 2, rb_int_new(s));
    rb_ary_store(ary, 3, rb_int_new(e));
    rb_yield(ary);
  }
}
Ejemplo n.º 3
0
/*
 * Get the settings used for the model.
 */
static VALUE wrap_get_model_settings(VALUE self) {
	if (!model_loaded)
		return Qnil;

	VALUE arr;
	
	arr = rb_ary_new();
	rb_ary_push(arr, rb_int_new(last_model->num_topics));
	rb_ary_push(arr, rb_int_new(last_model->num_terms));
	rb_ary_push(arr, rb_float_new(last_model->alpha));
	
	return arr;		//	[num_topics, num_terms, alpha]
}
Ejemplo n.º 4
0
/**
 * producer_tick
 *
 * This function is responsible for ticking the librdkafka reactor so we can
 * get feedback from the librdkafka threads back into the Ruby environment
 *
 *  @param  self	VALUE   the Ruby producer instance
 *  @param  message VALUE   A Ruby FixNum of how many ms we should wait on librdkafka
 */
static VALUE producer_tick(VALUE self, VALUE timeout) {
	hermann_conf_t *conf = NULL;
	long timeout_ms = 0;
	int events = 0;

	if (Qnil != timeout) {
		timeout_ms = rb_num2int(timeout);
	}
	else {
		rb_raise(rb_eArgError, "Cannot call `tick` with a nil timeout!\n");
	}

	Data_Get_Struct(self, hermann_conf_t, conf);

	/*
	 * if the producerConfig is not initialized then we never properly called
	 * producer_push_single, so why are we ticking?
	 */
	if (!conf->isInitialized) {
		rb_raise(rb_eRuntimeError, "Cannot call `tick` without having ever sent a message\n");
	}

	events = rd_kafka_poll(conf->rk, timeout_ms);

	if (conf->isErrored) {
		rb_raise(rb_eStandardError, conf->error);
	}

	return rb_int_new(events);
}
Ejemplo n.º 5
0
/*
 * call-seq:
 * multi = Curl::Multi.new
 * easy = Curl::Easy.new('url')
 *
 * multi.add(easy)
 *
 * Add an easy handle to the multi stack
 */
static VALUE ruby_curl_multi_add(VALUE self, VALUE easy) {
  CURLMcode mcode;
  ruby_curl_easy *rbce;
  ruby_curl_multi *rbcm;

  Data_Get_Struct(self, ruby_curl_multi, rbcm);
  Data_Get_Struct(easy, ruby_curl_easy, rbce);

  mcode = curl_multi_add_handle(rbcm->handle, rbce->curl);
  if (mcode != CURLM_CALL_MULTI_PERFORM && mcode != CURLM_OK) {
    raise_curl_multi_error_exception(mcode);
  }

  /* save a pointer to self */
  rbce->self = easy;

  /* setup the easy handle */
  ruby_curl_easy_setup( rbce, &(rbce->bodybuf), &(rbce->headerbuf), &(rbce->curl_headers) );

  rbcm->active++;
  if (mcode == CURLM_CALL_MULTI_PERFORM) {
    curl_multi_perform(rbcm->handle, &(rbcm->running));
  }

  rb_hash_aset( rbcm->requests, rb_int_new((long)rbce->curl), easy );
  // active should equal INT2FIX(RHASH(rbcm->requests)->tbl->num_entries)

  if (rbcm->active > rbcm->running) {
    rb_curl_multi_read_info(self, rbcm->handle);
  }

  return self;
}
Ejemplo n.º 6
0
/*
 * call-seq: call(in_start, in_end)
 *
 * Call technical function with input data from in_start..in_end.
 */
static VALUE ta_func_call(VALUE self, VALUE in_start, VALUE in_end)
{
  TA_RetCode ret_code;
  ParamHolder *param_holder;
  TA_Integer out_start, out_num;
  VALUE ary, sub_ary;
  int i,j;

  Data_Get_Struct(self, ParamHolder, param_holder);
  ret_code = TA_CallFunc( param_holder->p, FIX2INT(in_start), FIX2INT(in_end), &out_start, &out_num);
  if ( ret_code != TA_SUCCESS )
    rb_raise(rb_eRuntimeError, "unsuccess return code TA_CallFunc");
  ary = rb_iv_get(self, "@result");
  for (i = 0; i<RARRAY_LEN(ary); i++)
    if (TYPE(rb_ary_entry(ary, i)) == T_ARRAY)
    {
      sub_ary = rb_ary_entry(ary, i);
      for (j=0; j<out_num; j++)
      {
        if(param_holder->out_type[i] == TA_Output_Real) {
          double el = ((double*)param_holder->out[i])[j];
          rb_ary_store(sub_ary, j+out_start, rb_float_new(el));
        }
        else if(param_holder->out_type[i] == TA_Output_Integer) {
          int el = ((int*)param_holder->out[i])[j];
          rb_ary_store(sub_ary, j+out_start, rb_int_new(el));
        }
      }
    }
  return rb_ary_new3(2, INT2FIX(out_start), INT2FIX(out_num));
}
Ejemplo n.º 7
0
/*
    Convert a char *genome to ruby array of integers
*/
static VALUE genome_to_int_array(char *genome, int g_len) {
    int i;
    VALUE arr = rb_ary_new();
    for(i=0; i < g_len; ++i) {
        rb_ary_push(arr, rb_int_new((genome[i]) - '0'));
    }
    return arr;
}
Ejemplo n.º 8
0
/* initializes the Vector */
static VALUE vector_init(VALUE self, VALUE vec_vals) {
  Vector *p;
  register int i;
  Data_Get_Struct(self, Vector, p);
  /* gets the size of the ruby array that contains the new values */
  if(NIL_P(vec_vals) != FALSE) {
    p->size = RARRAY(vec_vals)->len;
    copy_ary_to_vector(p, vec_vals); /* loads all the values in */
  } else {
    p->size = 3;
    VALUE zero_arr = rb_ary_new();
    rb_ary_push(zero_arr, rb_int_new(0));
    rb_ary_push(zero_arr, rb_int_new(0));
    rb_ary_push(zero_arr, rb_int_new(0));
    copy_ary_to_vector(p, zero_arr);
  }
  return self;
}
Ejemplo n.º 9
0
static
VALUE rhe_read_timeout(VALUE self, VALUE fileno, VALUE rbuf, VALUE len, VALUE offset, VALUE timeout) {
  char * d;
  ssize_t rv;
  d = malloc(len);
  rv = _read_timeout(NUM2INT(fileno), NUM2DBL(timeout), &d[NUM2LONG(offset)], NUM2LONG(len));
  if ( rv > 0 ) {
    rb_str_cat(rbuf, d, rv);
  }
  free(d);
  return rb_int_new(rv);
}
Ejemplo n.º 10
0
static
VALUE rhe_write_timeout(VALUE self, VALUE fileno, VALUE buf, VALUE len, VALUE offset, VALUE timeout) {
  char* d;
  ssize_t rv;

  d = StringValuePtr(buf);
  rv = _write_timeout(NUM2INT(fileno), NUM2DBL(timeout), &d[NUM2LONG(offset)], NUM2LONG(len));
  if ( rv < 0 ) {
    return Qnil;
  }
  return rb_int_new(rv);
}
Ejemplo n.º 11
0
//
// function to count the number of events for which amplitudes were generated.
//
//
VALUE count_amps(VALUE __self, VALUE __file_name){
  char *filename = STR2CSTR(__file_name);
  ifstream in_file;
  in_file.open(filename);
  int event = 0;
  complex<float> amp;

  while(!in_file.eof()){
    event++;
    in_file.read((char*)&amp,sizeof(amp));
  }
  return rb_int_new(event-1);
}
Ejemplo n.º 12
0
Archivo: carray.c Proyecto: m5/CArray
VALUE method_max_index(VALUE self, VALUE s){
    int len = RSTRING_LEN(s) / 4;
    int* arr = (int *) RSTRING_PTR(s);
    int max = 0;
    int max_idx = 0;
    int i = 0;
    for(i=0; i<len; i++){
      if(arr[i] > max){
        max = arr[i];
        max_idx = i;
      }
    }
    return rb_int_new(max_idx);
}
Ejemplo n.º 13
0
static void rb_curl_multi_remove(ruby_curl_multi *rbcm, VALUE easy) {
  CURLMcode result;
  ruby_curl_easy *rbce;
  Data_Get_Struct(easy, ruby_curl_easy, rbce);
 
  rbcm->active--;

  //printf( "calling rb_curl_multi_remove: 0x%X, active: %d\n", (long)easy, rbcm->active );

  result = curl_multi_remove_handle(rbcm->handle, rbce->curl);
  if (result != 0) {
    raise_curl_multi_error_exception(result);
  }
 
  ruby_curl_easy_cleanup( easy, rbce, rbce->bodybuf, rbce->headerbuf, rbce->curl_headers );
  rbce->headerbuf = Qnil;
  rbce->bodybuf = Qnil;
  rb_hash_delete( rbcm->requests, rb_int_new((long)rbce->curl) );
}
Ejemplo n.º 14
0
static
VALUE rhe_write_all(VALUE self, VALUE fileno, VALUE buf, VALUE offsetv, VALUE timeout) {
  char * d;
  ssize_t buf_len;
  ssize_t rv = 0;
  ssize_t written = 0;
  ssize_t offset = NUM2LONG(offsetv);

  d = StringValuePtr(buf);
  buf_len = RSTRING_LEN(buf);

  written = 0;
  while ( buf_len > written ) {
    rv = _write_timeout(NUM2INT(fileno), NUM2DBL(timeout), &d[written], buf_len - written);
    if ( rv <= 0 ) {
      break;
    }
    written += rv;
  }
  if (rv < 0) {
    return Qnil;
  }
  return rb_int_new(written);
}
Ejemplo n.º 15
0
VALUE call_tree_call_count(VALUE self)
{
    return rb_int_new(get_call_tree(self)->call_count);
}
Ejemplo n.º 16
0
/*
 * Get the estimate alpha value (fixed = 0).
 */
static VALUE wrap_get_estimate_alpha(VALUE self) {
	return rb_int_new(ESTIMATE_ALPHA);
}
Ejemplo n.º 17
0
/*
 * Get the number of topics being clustered.
 */
static VALUE wrap_get_num_topics(VALUE self) {
	return rb_int_new(NTOPICS);
}
Ejemplo n.º 18
0
/*
 * Get the max iterations for the EM algorithm.
 */
static VALUE wrap_get_em_max_iter(VALUE self) {
	return rb_int_new(EM_MAX_ITER);
}
Ejemplo n.º 19
0
/* call-seq: calc_coherent_sums(cuts,ic,a,file) -> self
 *
 *
 *
 */
VALUE calc_coherent_sums(VALUE __self, VALUE __num_files, VALUE __cuts_on,
			 VALUE __cuts_ary, VALUE __num_evts){

  VectorFlt2D *cross_term_ints 
    = get_cpp_ptr(rb_iv_get(__self,"@cross_term_ints"),__VectorFlt2D__);
  int cuts_on = NUM2INT(__cuts_on);
  int num_amps = NUM2INT(__num_files);
  int num_evts = NUM2INT(__num_evts);
  double cut_check;
  int good_events = 0;
  ifstream in_file[num_amps];
  char *amp_names[num_amps];

  // array to hold the sums
  complex<float> cross_terms[num_amps][num_amps];
  // Naming and opening files...
  for(int iter = 0;iter<num_amps;iter++){
    amp_names[iter] = STR2CSTR(rb_ary_entry(rb_iv_get(__self,
						      "@coh_amps"),iter));
    in_file[iter].open(amp_names[iter]);
  }
  bool take_event;
  complex<float> amp[num_amps];
  int event = 0;
  int read_tag = 0;
  while(event<num_evts && read_tag<1){
    for(int j = 1;j<num_amps;j++){
      if(in_file[j].eof())  read_tag++;
    }
    if(cuts_on){
      cut_check = NUM2DBL(rb_ary_entry(__cuts_ary,event));
      if(cut_check >= 0.0){
	take_event = true;
      }
      else take_event = false;
    }
    else if(!cuts_on){
      take_event = true;
    }
    // read the amp vals...
    for(int amp_num = 0;amp_num<num_amps;amp_num++){
      in_file[amp_num].read((char*)&amp[amp_num],sizeof(amp[amp_num]));
    }

    if(take_event){
      good_events++;
      for(int row = 0;row<num_amps;row++){
	for(int col = row;col<num_amps;col++){
	  cross_terms[row][col] = cross_terms[row][col]
	    + amp[row] * conj(amp[col]);
	}
      }
    }
    event++;
  }
  cout << "Events used: " << good_events << "\n";
  // impose symmetry...
  for(int row = 0;row<num_amps;row++){
    for(int col = 0;col<=row;col++){
      cross_terms[row][col] = conj(cross_terms[col][row]);
    }
  }
  // assign values to VectorFlt2D...
  for(int i = 0;i<num_amps;i++){
    for(int j = 0;j<num_amps;j++){
      (*cross_term_ints)[i][j] = cross_terms[i][j];
    }
  }
  return rb_int_new(event);
}
Ejemplo n.º 20
0
/* call-seq:
   line_no -> int

   returns the line number of the method */
static VALUE
prof_call_info_line(VALUE self)
{
  prof_call_info_t *result = prof_get_call_info_result(self);
  return rb_int_new(result->line);
}
Ejemplo n.º 21
0
static VALUE print_size(VALUE self) {
  Vector *p;
  Data_Get_Struct(self, Vector, p);
  return rb_int_new(p->size);
}
Ejemplo n.º 22
0
static
VALUE rhe_write_response(VALUE self, VALUE filenov, VALUE timeoutv, VALUE status_codev, VALUE headers, VALUE body) {
  ssize_t hlen;
  ssize_t blen;

  ssize_t len;
  ssize_t rv = 0;
  ssize_t iovcnt;
  ssize_t vec_offset;
  ssize_t written;
  int count;
  int i;
  char status_line[512];
  char date_line[512];
  int date_pushed = 0;
  VALUE arr;
  VALUE key_obj;
  VALUE val_obj;
  char * key;

  int fileno = NUM2INT(filenov);
  double timeout = NUM2DBL(timeoutv);
  int status_code = NUM2INT(status_codev);

  arr = rb_ary_new();
  rb_hash_foreach(headers, my_hash_keys, arr);
  hlen = RARRAY_LEN(arr);
  blen = RARRAY_LEN(body);
  iovcnt = 10 + (hlen * 2) + blen;

  {
    struct iovec v[iovcnt]; // Needs C99 compiler
    /* status line */
    iovcnt = 0;
    i=0;
    status_line[i++] = 'H';
    status_line[i++] = 'T';
    status_line[i++] = 'T';
    status_line[i++] = 'P';
    status_line[i++] = '/';
    status_line[i++] = '1';
    status_line[i++] = '.';
    status_line[i++] = '0';
    status_line[i++] = ' ';
    str_i(status_line,&i,status_code,3);
    status_line[i++] = ' ';
    const char * message = status_message(status_code);
    str_s(status_line, &i, message, strlen(message));
    status_line[i++] = 13;
    status_line[i++] = 10;
    v[iovcnt].iov_base = status_line;
    v[iovcnt].iov_len = i;
    iovcnt++;

    v[iovcnt].iov_base = "Connection: close\r\nServer: Rhebok\r\n";
    v[iovcnt].iov_len = sizeof("Connection: close\r\nServer: Rhebok\r\n")-1;
    iovcnt++;

    date_pushed = 0;
    for ( i = 0; i < hlen; i++ ) {
      key_obj = rb_ary_entry(arr, i);
      key = StringValuePtr(key_obj);
      len = RSTRING_LEN(key_obj);
      if ( strncasecmp(key,"Connection",len) == 0 ) {
        continue;
      }
      if ( strncasecmp(key,"Server",len) == 0 ) {
        v[1].iov_len -= (sizeof("Server: Rhebok\r\n") - 1);
      }
      if ( strncasecmp(key,"Date",len) == 0 ) {
        date_pushed = 1;
      }
      v[iovcnt].iov_base = key;
      v[iovcnt].iov_len = len;
      iovcnt++;
      v[iovcnt].iov_base = ": ";
      v[iovcnt].iov_len = sizeof(": ") - 1;
      iovcnt++;
      /* value */
      val_obj = rb_hash_aref(headers, key_obj);
      v[iovcnt].iov_base = StringValuePtr(val_obj);
      v[iovcnt].iov_len = RSTRING_LEN(val_obj);
      iovcnt++;
      v[iovcnt].iov_base = "\r\n";
      v[iovcnt].iov_len = sizeof("\r\n") - 1;
      iovcnt++;
    }

    if ( date_pushed == 0 ) {
      v[iovcnt].iov_len = _date_line(date_line);
      v[iovcnt].iov_base = date_line;
      iovcnt++;
    }

    v[iovcnt].iov_base = "\r\n";
    v[iovcnt].iov_len = sizeof("\r\n") - 1;
    iovcnt++;

    for (i=0; i < blen; i++ ) {
      val_obj = rb_ary_entry(body, i);
      v[iovcnt].iov_base = StringValuePtr(val_obj);
      v[iovcnt].iov_len = RSTRING_LEN(val_obj);
      iovcnt++;
    }

    vec_offset = 0;
    written = 0;
    while ( iovcnt - vec_offset > 0 ) {
      count = (iovcnt > IOV_MAX) ? IOV_MAX : iovcnt;
      rv = _writev_timeout(fileno, timeout,  &v[vec_offset], count - vec_offset, (vec_offset == 0) ? 0 : 1);
      if ( rv <= 0 ) {
        // error or disconnected
        break;
      }
      written += rv;
      while ( rv > 0 ) {
        if ( (int)rv >= v[vec_offset].iov_len ) {
        rv -= v[vec_offset].iov_len;
          vec_offset++;
        }
        else {
          v[vec_offset].iov_base = (char*)v[vec_offset].iov_base + rv;
          v[vec_offset].iov_len -= rv;
          rv = 0;
        }
      }
    }
  }
  if ( rv < 0 ) {
    return Qnil;
  }
  return rb_int_new(written);
}
Ejemplo n.º 23
0
/* call-seq:
   depth -> int

   returns the depth of this call info in the call graph */
static VALUE
prof_call_info_depth(VALUE self)
{
  prof_call_info_t *result = prof_get_call_info(self);
  return rb_int_new(result->depth);
}
Ejemplo n.º 24
0
static
VALUE rhe_accept(VALUE self, VALUE fileno, VALUE timeoutv, VALUE tcp, VALUE env) {
  struct sockaddr_in cliaddr;
  unsigned int len;
  char read_buf[MAX_HEADER_SIZE];
  VALUE req;
  int flag = 1;
  ssize_t rv = 0;
  ssize_t buf_len;
  ssize_t reqlen;
  int fd;
  double timeout = NUM2DBL(timeoutv);

  len = sizeof(cliaddr);
  fd = _accept(NUM2INT(fileno), (struct sockaddr *)&cliaddr, len);

  /* endif */
  if (fd < 0) {
    goto badexit;
  }

  rv = _read_timeout(fd, timeout, &read_buf[0], MAX_HEADER_SIZE);
  if ( rv <= 0 ) {
    close(fd);
    goto badexit;
  }

  if ( IMMEDIATE_P(tcp) ) {
    setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int));
    rb_hash_aset(env, remote_addr_key, rb_str_new2(inet_ntoa(cliaddr.sin_addr)));
    rb_hash_aset(env, remote_port_key, rb_String(rb_int_new(ntohs(cliaddr.sin_port))));
  }
  else {
    rb_hash_aset(env, remote_addr_key, rb_str_new("",0));
    rb_hash_aset(env, remote_port_key, rb_String(rb_int_new(0)));
  }

  buf_len = rv;
  while (1) {
    reqlen = _parse_http_request(&read_buf[0],buf_len,env);
    if ( reqlen >= 0 ) {
      break;
    }
    else if ( reqlen == -1 ) {
      /* error */
      close(fd);
      goto badexit;
    }
    if ( MAX_HEADER_SIZE - buf_len == 0 ) {
      /* too large header  */
     char* badreq;
     badreq = BAD_REQUEST;
     rv = _write_timeout(fd, timeout, badreq, sizeof(BAD_REQUEST) - 1);
     close(fd);
     goto badexit;
    }
    /* request is incomplete */
    rv = _read_timeout(fd, timeout, &read_buf[buf_len], MAX_HEADER_SIZE - buf_len);
    if ( rv <= 0 ) {
      close(fd);
      goto badexit;
    }
    buf_len += rv;
  }

  req = rb_ary_new2(3);
  rb_ary_push(req, rb_int_new(fd));
  rb_ary_push(req, rb_str_new(&read_buf[reqlen],buf_len - reqlen));
  return req;
 badexit:
  return Qnil;
}
Ejemplo n.º 25
0
Archivo: carray.c Proyecto: m5/CArray
VALUE method_get_index(VALUE self, VALUE s, VALUE idx){
    int* arr = (int *) RSTRING_PTR(s);
    return rb_int_new(arr[NUM2INT(idx)]);
}