示例#1
0
uint HeapRegionManager::find_unavailable_from_idx(uint start_idx, uint* res_idx) const {
  guarantee(res_idx != NULL, "checking");
  guarantee(start_idx <= (max_length() + 1), "checking");

  uint num_regions = 0;

  uint cur = start_idx;
  while (cur < max_length() && is_available(cur)) {
    cur++;
  }
  if (cur == max_length()) {
    return num_regions;
  }
  *res_idx = cur;
  while (cur < max_length() && !is_available(cur)) {
    cur++;
  }
  num_regions = cur - *res_idx;
#ifdef ASSERT
  for (uint i = *res_idx; i < (*res_idx + num_regions); i++) {
    assert(!is_available(i), "just checking");
  }
  assert(cur == max_length() || num_regions == 0 || is_available(cur),
         "The region at the current position %u must be available or at the end of the heap.", cur);
#endif
  return num_regions;
}
示例#2
0
SEXP R_match_soundex(SEXP x, SEXP table, SEXP nomatch, SEXP matchNA) {

  int nx = length(x);
  int ntable = length(table);
  int no_match = INTEGER(nomatch)[0];
  int match_na = INTEGER(matchNA)[0];
  int bytes = IS_CHARACTER(x);

  // when a and b are character vectors; create unsigned int vectors in which
  // the elements of and b will be copied
  unsigned int *s = NULL, *t = NULL;
  if (bytes) {
    int ml_x = max_length(x);
    int ml_t = max_length(table);
    s = (unsigned int *) malloc((ml_x + ml_t) * sizeof(unsigned int));
    t = s + ml_x;
    if (s == NULL) {
       free(s);
       error("Unable to allocate enough memory");
    }
  }

  // output vector
  SEXP yy = allocVector(INTSXP, nx);
  PROTECT(yy);
  int* y = INTEGER(yy);

  int index, isna_s, isna_t, len_s, len_t;
  unsigned int nfail = 0;
  double d;
  for (int i=0; i<nx; ++i) {
    index = no_match;
    s = get_elem(x, i, bytes, &len_s, &isna_s, s);

    for (int j=0; j<ntable; ++j) {
      t = get_elem(table, j, bytes, &len_t, &isna_t, t);

      if (!isna_s && !isna_t) {        // both are char (usual case)
        d = soundex_dist(s, t, len_s, len_t, &nfail);
        if (d < 0.5) { // exact match as d can only take on values 0 and 1
          index = j + 1;
          break;
        } 
      } else if (isna_s && isna_t) {  // both are NA
        index = match_na ? j + 1 : no_match;
        break;
      }
    }
    y[i] = index;
  }   
  // cleanup and return
  check_fail(nfail);
  if (bytes) free(s); 
  UNPROTECT(1);
  return(yy);
}
示例#3
0
文件: G28.cpp 项目: teemuatlut/Marlin
  static void quick_home_xy() {

    // Pretend the current position is 0,0
    current_position[X_AXIS] = current_position[Y_AXIS] = 0.0;
    sync_plan_position();

    const int x_axis_home_dir =
      #if ENABLED(DUAL_X_CARRIAGE)
        x_home_dir(active_extruder)
      #else
        home_dir(X_AXIS)
      #endif
    ;

    const float mlx = max_length(X_AXIS),
                mly = max_length(Y_AXIS),
                mlratio = mlx > mly ? mly / mlx : mlx / mly,
                fr_mm_s = MIN(homing_feedrate(X_AXIS), homing_feedrate(Y_AXIS)) * SQRT(sq(mlratio) + 1.0);

    #if ENABLED(SENSORLESS_HOMING)
      sensorless_t stealth_states { false, false, false, false, false, false, false };
      stealth_states.x = tmc_enable_stallguard(stepperX);
      stealth_states.y = tmc_enable_stallguard(stepperY);
      #if AXIS_HAS_STALLGUARD(X2)
        stealth_states.x2 = tmc_enable_stallguard(stepperX2);
      #endif
      #if AXIS_HAS_STALLGUARD(Y2)
        stealth_states.y2 = tmc_enable_stallguard(stepperY2);
      #endif
    #endif

    do_blocking_move_to_xy(1.5 * mlx * x_axis_home_dir, 1.5 * mly * home_dir(Y_AXIS), fr_mm_s);

    endstops.validate_homing_move();

    current_position[X_AXIS] = current_position[Y_AXIS] = 0.0;

    #if ENABLED(SENSORLESS_HOMING)
      tmc_disable_stallguard(stepperX, stealth_states.x);
      tmc_disable_stallguard(stepperY, stealth_states.y);
      #if AXIS_HAS_STALLGUARD(X2)
        tmc_disable_stallguard(stepperX2, stealth_states.x2);
      #endif
      #if AXIS_HAS_STALLGUARD(Y2)
        tmc_disable_stallguard(stepperY2, stealth_states.y2);
      #endif
    #endif
  }
示例#4
0
static void
print_errs (FILE *out, state *s)
{
  errs *errp = s->errs;
  size_t width = 0;
  int i;

  /* Compute the width of the look-ahead token column.  */
  for (i = 0; i < errp->num; ++i)
    if (errp->symbols[i])
      max_length (&width, errp->symbols[i]->tag);

  /* Nothing to report. */
  if (!width)
    return;

  fputc ('\n', out);
  width += 2;

  /* Report look-ahead tokens and errors.  */
  for (i = 0; i < errp->num; ++i)
    if (errp->symbols[i])
      {
	const char *tag = errp->symbols[i]->tag;
	int j;
	fprintf (out, "    %s", tag);
	for (j = width - strlen (tag); j > 0; --j)
	  fputc (' ', out);
	fputs (_("error (nonassociative)\n"), out);
      }
}
示例#5
0
uint HeapRegionManager::find_contiguous(size_t num, bool empty_only) {
  uint found = 0;
  size_t length_found = 0;
  uint cur = 0;

  while (length_found < num && cur < max_length()) {
    HeapRegion* hr = _regions.get_by_index(cur);
    if ((!empty_only && !is_available(cur)) || (is_available(cur) && hr != NULL && hr->is_empty())) {
      // This region is a potential candidate for allocation into.
      length_found++;
    } else {
      // This region is not a candidate. The next region is the next possible one.
      found = cur + 1;
      length_found = 0;
    }
    cur++;
  }

  if (length_found == num) {
    for (uint i = found; i < (found + num); i++) {
      HeapRegion* hr = _regions.get_by_index(i);
      // sanity check
      guarantee((!empty_only && !is_available(i)) || (is_available(i) && hr != NULL && hr->is_empty()),
                "Found region sequence starting at " UINT32_FORMAT ", length " SIZE_FORMAT
                " that is not empty at " UINT32_FORMAT ". Hr is " PTR_FORMAT, found, num, i, p2i(hr));
    }
    return found;
  } else {
    return G1_NO_HRM_INDEX;
  }
}
示例#6
0
uint HeapRegionManager::find_highest_free(bool* expanded) {
  // Loop downwards from the highest region index, looking for an
  // entry which is either free or not yet committed.  If not yet
  // committed, expand_at that index.
  uint curr = max_length() - 1;
  while (true) {
    HeapRegion *hr = _regions.get_by_index(curr);
    if (hr == NULL) {
      uint res = expand_at(curr, 1);
      if (res == 1) {
        *expanded = true;
        return curr;
      }
    } else {
      if (hr->is_free()) {
        *expanded = false;
        return curr;
      }
    }
    if (curr == 0) {
      return G1_NO_HRM_INDEX;
    }
    curr--;
  }
}
typeArrayOop TypeArrayKlass::allocate(int length, HeapColor color, TRAPS) {
    assert(log2_element_size() >= 0, "bad scale");
    if (length >= 0) {
        if (length <= max_length()) {
            size_t size = typeArrayOopDesc::object_size(layout_helper(), length);
            KlassHandle h_k(THREAD, this);
            typeArrayOop t;
            CollectedHeap* ch = Universe::heap();
            /*
            if (size < ch->large_typearray_limit()) {
            */
            t = (typeArrayOop)CollectedHeap::array_allocate(h_k, (int)size, length,
                    color, CHECK_NULL);
            /*
            } else {
              t = (typeArrayOop)CollectedHeap::large_typearray_allocate(h_k, (int)size,
                                               length, color, CHECK_NULL);
            }
            */
            //assert(t->is_parsable(), "Don't publish unless parsable");
            return t;
        } else {
            report_java_out_of_memory("Requested array size exceeds VM limit");
            THROW_OOP_0(Universe::out_of_memory_error_array_size());
        }
    } else {
        THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
    }
}
示例#8
0
文件: elastic.cpp 项目: bradc6/libdar
    elastic::elastic(U_32 size)
    {
	if(size == 0)
	    throw Erange("elastic::elastic", gettext("Zero is not a valid size for an elastic buffer"));
	if(size >  max_length())
	    throw Erange("elastic::elastic", gettext("Size too large for an elastic buffer"));

	taille = size;
    }
示例#9
0
SEXP R_soundex_dist(SEXP a, SEXP b) {
  int na = length(a);
  int nb = length(b);
  int nt = MAX(na,nb);
  int bytes = IS_CHARACTER(a);

  // when a and b are character vectors; create unsigned int vectors in which
  // the elements of and b will be copied
  unsigned int *s = NULL, *t = NULL;
  if (bytes) {
    int ml_a = max_length(a);
    int ml_b = max_length(b);
    s = (unsigned int *) malloc((ml_a + ml_b) * sizeof(unsigned int));
    t = s + ml_a;
    if (s == NULL) {
       free(s);
       error("Unable to allocate enough memory");
    }
  }

  // create output variable
  SEXP yy = allocVector(REALSXP, nt);
  PROTECT(yy);
  double *y = REAL(yy);

  // compute distances, skipping NA's
  unsigned int nfail = 0;
  int len_s, len_t, isna_s, isna_t;
  for (int k=0; k < nt; ++k, ++y) {
    s = get_elem(a, k % na, bytes, &len_s, &isna_s, s);
    t = get_elem(b, k % nb, bytes, &len_t, &isna_t, t);
    if (isna_s || isna_t) {
      (*y) = NA_REAL;
    } else { 
      (*y) = soundex_dist(s, t, len_s, len_t, &nfail);
    } 
  }
  // cleanup and return
  check_fail(nfail);
  if (bytes) free(s);
  UNPROTECT(1);
  return yy;
}
示例#10
0
		virtual bool take_some(const_pointer& aFirst, const_pointer aLast)
		{
			if (aFirst == aLast)
				return false;
			if (has_max_length() && length() + (aLast - aFirst) > max_length())
				throw typename base_type::packet_too_big();
			iContents.insert(iContents.end(), aFirst, aLast);
			aFirst = aLast;
			return true;
		}
示例#11
0
void HeapRegionSeq::verify_optional() {
  guarantee(length() <= _allocated_length,
            err_msg("invariant: _length: %u _allocated_length: %u",
                    length(), _allocated_length));
  guarantee(_allocated_length <= max_length(),
            err_msg("invariant: _allocated_length: %u _max_length: %u",
                    _allocated_length, max_length()));
  guarantee(_next_search_index <= length(),
            err_msg("invariant: _next_search_index: %u _length: %u",
                    _next_search_index, length()));

  HeapWord* prev_end = heap_bottom();
  for (uint i = 0; i < _allocated_length; i += 1) {
    HeapRegion* hr = _regions.get_by_index(i);
    guarantee(hr != NULL, err_msg("invariant: i: %u", i));
    guarantee(hr->bottom() == prev_end,
              err_msg("invariant i: %u "HR_FORMAT" prev_end: "PTR_FORMAT,
                      i, HR_FORMAT_PARAMS(hr), p2i(prev_end)));
    guarantee(hr->hrs_index() == i,
              err_msg("invariant: i: %u hrs_index(): %u", i, hr->hrs_index()));
    if (i < length()) {
      // Asserts will fire if i is >= _length
      HeapWord* addr = hr->bottom();
      guarantee(addr_to_region(addr) == hr, "sanity");
      guarantee(addr_to_region_unsafe(addr) == hr, "sanity");
    } else {
      guarantee(hr->is_empty(), "sanity");
      guarantee(!hr->isHumongous(), "sanity");
      // using assert instead of guarantee here since containing_set()
      // is only available in non-product builds.
      assert(hr->containing_set() == NULL, "sanity");
    }
    if (hr->startsHumongous()) {
      prev_end = hr->orig_end();
    } else {
      prev_end = hr->end();
    }
  }
  for (uint i = _allocated_length; i < max_length(); i += 1) {
    guarantee(_regions.get_by_index(i) == NULL, err_msg("invariant i: %u", i));
  }
}
示例#12
0
void HeapRegionManager::verify() {
  guarantee(length() <= _allocated_heapregions_length,
            "invariant: _length: %u _allocated_length: %u",
            length(), _allocated_heapregions_length);
  guarantee(_allocated_heapregions_length <= max_length(),
            "invariant: _allocated_length: %u _max_length: %u",
            _allocated_heapregions_length, max_length());

  bool prev_committed = true;
  uint num_committed = 0;
  HeapWord* prev_end = heap_bottom();
  for (uint i = 0; i < _allocated_heapregions_length; i++) {
    if (!is_available(i)) {
      prev_committed = false;
      continue;
    }
    num_committed++;
    HeapRegion* hr = _regions.get_by_index(i);
    guarantee(hr != NULL, "invariant: i: %u", i);
    guarantee(!prev_committed || hr->bottom() == prev_end,
              "invariant i: %u " HR_FORMAT " prev_end: " PTR_FORMAT,
              i, HR_FORMAT_PARAMS(hr), p2i(prev_end));
    guarantee(hr->hrm_index() == i,
              "invariant: i: %u hrm_index(): %u", i, hr->hrm_index());
    // Asserts will fire if i is >= _length
    HeapWord* addr = hr->bottom();
    guarantee(addr_to_region(addr) == hr, "sanity");
    // We cannot check whether the region is part of a particular set: at the time
    // this method may be called, we have only completed allocation of the regions,
    // but not put into a region set.
    prev_committed = true;
    prev_end = hr->end();
  }
  for (uint i = _allocated_heapregions_length; i < max_length(); i++) {
    guarantee(_regions.get_by_index(i) == NULL, "invariant i: %u", i);
  }

  guarantee(num_committed == _num_committed, "Found %u committed regions, but should be %u", num_committed, _num_committed);
  _free_list.verify();
}
示例#13
0
void HeapRegionManager::iterate(HeapRegionClosure* blk) const {
  uint len = max_length();

  for (uint i = 0; i < len; i++) {
    if (!is_available(i)) {
      continue;
    }
    guarantee(at(i) != NULL, "Tried to access region %u that has a NULL HeapRegion*", i);
    bool res = blk->doHeapRegion(at(i));
    if (res) {
      blk->incomplete();
      return;
    }
  }
}
示例#14
0
void HeapRegionManager::commit_regions(uint index, size_t num_regions) {
  guarantee(num_regions > 0, "Must commit more than zero regions");
  guarantee(_num_committed + num_regions <= max_length(), "Cannot commit more than the maximum amount of regions");

  _num_committed += (uint)num_regions;

  _heap_mapper->commit_regions(index, num_regions);

  // Also commit auxiliary data
  _prev_bitmap_mapper->commit_regions(index, num_regions);
  _next_bitmap_mapper->commit_regions(index, num_regions);

  _bot_mapper->commit_regions(index, num_regions);
  _cardtable_mapper->commit_regions(index, num_regions);

  _card_counts_mapper->commit_regions(index, num_regions);
}
示例#15
0
MemRegion HeapRegionSeq::expand_by(HeapWord* old_end,
                                   HeapWord* new_end,
                                   FreeRegionList* list) {
  assert(old_end < new_end, "don't call it otherwise");
  G1CollectedHeap* g1h = G1CollectedHeap::heap();

  HeapWord* next_bottom = old_end;
  assert(heap_bottom() <= next_bottom, "invariant");
  while (next_bottom < new_end) {
    assert(next_bottom < heap_end(), "invariant");
    uint index = length();

    assert(index < max_length(), "otherwise we cannot expand further");
    if (index == 0) {
      // We have not allocated any regions so far
      assert(next_bottom == heap_bottom(), "invariant");
    } else {
      // next_bottom should match the end of the last/previous region
      assert(next_bottom == at(index - 1)->end(), "invariant");
    }

    if (index == _allocated_length) {
      // We have to allocate a new HeapRegion.
      HeapRegion* new_hr = g1h->new_heap_region(index, next_bottom);
      if (new_hr == NULL) {
        // allocation failed, we bail out and return what we have done so far
        return MemRegion(old_end, next_bottom);
      }
      assert(_regions.get_by_index(index) == NULL, "invariant");
      _regions.set_by_index(index, new_hr);
      increment_allocated_length();
    }
    // Have to increment the length first, otherwise we will get an
    // assert failure at(index) below.
    increment_length();
    HeapRegion* hr = at(index);
    list->add_as_tail(hr);

    next_bottom = hr->end();
  }
  assert(next_bottom == new_end, "post-condition");
  return MemRegion(old_end, next_bottom);
}
示例#16
0
		virtual bool take_some(const_pointer& aFirst, const_pointer aLast)
		{
			if (aFirst == aLast)
				return false;
			while (aFirst != aLast && is_delimiter(*aFirst))
				++aFirst;
			const_pointer start = aFirst;
			while (aFirst != aLast && !is_delimiter(*aFirst))
				++aFirst;
			const_pointer end = aFirst;
			if (has_max_length() && length() + (end - start) > max_length())
				throw typename base_type::packet_too_big();
			iContents.insert(iContents.end(), start, end);
			while (aFirst != aLast && !is_terminating_delimiter(*aFirst))
				++aFirst;
			if (aFirst != aLast)
				++aFirst;
			return end != aLast;
		}
示例#17
0
static void
print_transitions (state *s, FILE *out, bool display_transitions_p)
{
  transitions *trans = s->transitions;
  size_t width = 0;
  int i;

  /* Compute the width of the look-ahead token column.  */
  for (i = 0; i < trans->num; i++)
    if (!TRANSITION_IS_DISABLED (trans, i)
	&& TRANSITION_IS_SHIFT (trans, i) == display_transitions_p)
      {
	symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)];
	max_length (&width, sym->tag);
      }

  /* Nothing to report. */
  if (!width)
    return;

  fputc ('\n', out);
  width += 2;

  /* Report look-ahead tokens and shifts.  */
  for (i = 0; i < trans->num; i++)
    if (!TRANSITION_IS_DISABLED (trans, i)
	&& TRANSITION_IS_SHIFT (trans, i) == display_transitions_p)
      {
	symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)];
	const char *tag = sym->tag;
	state *s1 = trans->states[i];
	int j;

	fprintf (out, "    %s", tag);
	for (j = width - strlen (tag); j > 0; --j)
	  fputc (' ', out);
	if (display_transitions_p)
	  fprintf (out, _("shift, and go to state %d\n"), s1->number);
	else
	  fprintf (out, _("go to state %d\n"), s1->number);
      }
}
示例#18
0
文件: sql.c 项目: engeenity/bareos
/*
 * List dashes as part of header for listing SQL results in a table
 */
void list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx)
{
   SQL_FIELD *field;
   int i, j;
   int len;
   int num_fields;

   sql_field_seek(mdb, 0);
   send(ctx, "+");
   num_fields = sql_num_fields(mdb);
   for (i = 0; i < num_fields; i++) {
      field = sql_fetch_field(mdb);
      if (!field) {
         break;
      }
      len = max_length(field->max_length + 2);
      for (j = 0; j < len; j++) {
         send(ctx, "-");
      }
      send(ctx, "+");
   }
   send(ctx, "\n");
}
示例#19
0
typeArrayOop TypeArrayKlass::allocate_common(int length, bool do_zero, TRAPS) {
    assert(log2_element_size() >= 0, "bad scale");
    if (length >= 0) {
        if (length <= max_length()) {
            size_t size = typeArrayOopDesc::object_size(layout_helper(), length);
            KlassHandle h_k(THREAD, this);
            typeArrayOop t;
            CollectedHeap* ch = Universe::heap();
            if (do_zero) {
                t = (typeArrayOop)CollectedHeap::array_allocate(h_k, (int)size, length, CHECK_NULL);
            } else {
                t = (typeArrayOop)CollectedHeap::array_allocate_nozero(h_k, (int)size, length, CHECK_NULL);
            }
            return t;
        } else {
            report_java_out_of_memory("Requested array size exceeds VM limit");
            JvmtiExport::post_array_size_exhausted();
            THROW_OOP_0(Universe::out_of_memory_error_array_size());
        }
    } else {
        THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
    }
}
示例#20
0
文件: sql.c 项目: debfx/bareos
/*
 * List dashes as part of header for listing SQL results in a table
 */
static void list_dashes(B_DB *mdb, OUTPUT_FORMATTER *send)
{
   SQL_FIELD *field;
   int i, j;
   int len;
   int num_fields;

   sql_field_seek(mdb, 0);
   send->decoration("+");
   num_fields = sql_num_fields(mdb);
   for (i = 0; i < num_fields; i++) {
      field = sql_fetch_field(mdb);
      if (!field) {
         break;
      }
      len = max_length(field->max_length + 2);
      for (j = 0; j < len; j++) {
         send->decoration("-");
      }
      send->decoration("+");
   }
   send->decoration("\n");
}
示例#21
0
/* R interface to qgram distance */
SEXP R_qgram_tree(SEXP a, SEXP b, SEXP qq, SEXP distance){
  PROTECT(a);
  PROTECT(b);
  PROTECT(qq);
  PROTECT(distance);
  // choose distance function

  int dist = INTEGER(distance)[0]
    , q = INTEGER(qq)[0]
    , na = length(a)
    , nb = length(b)
    , ml_a = max_length(a)
    , ml_b = max_length(b)
    , bytes = IS_CHARACTER(a);

  // set up a qtree; 
  qtree *Q = new_qtree(q, 2L);
  unsigned int *s = NULL, *t = NULL;
  if ( bytes ){
    s = (unsigned int *) malloc( (ml_a + ml_b) * sizeof(int) );
    if ( s == NULL ){ 
      UNPROTECT(4);
      error("Unable to allocate enough memory");
    }
    t = s + ml_a;
  }

  // output
  int nt = (na > nb) ? na : nb;
  SEXP yy; 
  PROTECT(yy = allocVector(REALSXP, nt));
  double *y = REAL(yy);

  
 
  int i=0, j=0, len_s, len_t, isna_s, isna_t;
  for ( int k=0; k < nt; ++k 
      , i = RECYCLE(i+1,na)
      , j = RECYCLE(j+1,nb) ){

    s = get_elem(a, i, bytes, &len_s, &isna_s, s);
    t = get_elem(b, j, bytes, &len_t, &isna_t, t);

    if ( isna_s || isna_t ){
      y[k] = NA_REAL;
      continue;
    }
    y[k] = qgram_tree(s, t, len_s, len_t, q, Q, dist);
    if (y[k] == -2.0){
      UNPROTECT(5);
      error("Unable to allocate enough memory");
    }
    if (y[k] == -1.0){
      y[k] = R_PosInf;
    }
  }


  free_qtree();
  if ( bytes ) free(s);
  UNPROTECT(5);
  return yy;
}
示例#22
0
文件: sql.c 项目: engeenity/bareos
int list_result(void *vctx, int nb_col, char **row)
{
   SQL_FIELD *field;
   int i, col_len, max_len = 0;
   int num_fields;
   char buf[2000], ewc[30];

   LIST_CTX *pctx = (LIST_CTX *)vctx;
   DB_LIST_HANDLER *send = pctx->send;
   e_list_type type = pctx->type;
   B_DB *mdb = pctx->mdb;
   void *ctx = pctx->ctx;
   JCR *jcr = pctx->jcr;

   num_fields = sql_num_fields(mdb);
   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      /*
       * No need to calculate things like maximum field lenght for
       * unformated or raw output.
       */
      break;
   case HORZ_LIST:
   case VERT_LIST:
      if (!pctx->once) {
         pctx->once = true;

         Dmsg1(800, "list_result starts looking at %d fields\n", num_fields);
         /*
          * Determine column display widths
          */
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            Dmsg1(800, "list_result processing field %d\n", i);
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            col_len = cstrlen(field->name);
            if (type == VERT_LIST) {
               if (col_len > max_len) {
                  max_len = col_len;
               }
            } else {
               if (sql_field_is_numeric(mdb, field->type) && (int)field->max_length > 0) { /* fixup for commas */
                  field->max_length += (field->max_length - 1) / 3;
               }
               if (col_len < (int)field->max_length) {
                  col_len = field->max_length;
               }
               if (col_len < 4 && !sql_field_is_not_null(mdb, field->flags)) {
                  col_len = 4;                 /* 4 = length of the word "NULL" */
               }
               field->max_length = col_len;    /* reset column info */
            }
         }

         pctx->num_rows++;

         Dmsg0(800, "list_result finished first loop\n");
         if (type == VERT_LIST) {
            break;
         }

         Dmsg1(800, "list_result starts second loop looking at %d fields\n", num_fields);

         /*
          * Keep the result to display the same line at the end of the table
          */
         list_dashes(mdb, last_line_handler, pctx);
         send(ctx, pctx->line);

         send(ctx, "|");
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            Dmsg1(800, "list_result looking at field %d\n", i);
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            max_len = max_length(field->max_length);
            bsnprintf(buf, sizeof(buf), " %-*s |", max_len, field->name);
            send(ctx, buf);
         }
         send(ctx, "\n");
         list_dashes(mdb, send, ctx);
      }
      break;
   default:
      break;
   }

   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      Dmsg1(800, "list_result starts third loop looking at %d fields\n", num_fields);
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         if (row[i] == NULL) {
            bsnprintf(buf, sizeof(buf), " %s", "NULL");
         } else {
            bsnprintf(buf, sizeof(buf), " %s", row[i]);
         }
         send(ctx, buf);
      }
      if (type != RAW_LIST) {
         send(ctx, "\n");
      }
      break;
   case HORZ_LIST:
      Dmsg1(800, "list_result starts third loop looking at %d fields\n", num_fields);
      sql_field_seek(mdb, 0);
      send(ctx, "|");
      for (i = 0; i < num_fields; i++) {
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         max_len = max_length(field->max_length);
         if (row[i] == NULL) {
            bsnprintf(buf, sizeof(buf), " %-*s |", max_len, "NULL");
         } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
            bsnprintf(buf, sizeof(buf), " %*s |", max_len,
                      add_commas(row[i], ewc));
         } else {
            bsnprintf(buf, sizeof(buf), " %-*s |", max_len, row[i]);
         }
         send(ctx, buf);
      }
      send(ctx, "\n");
      break;
   case VERT_LIST:
      Dmsg1(800, "list_result starts vertical list at %d fields\n", num_fields);
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         if (row[i] == NULL) {
            bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, "NULL");
         } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
            bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name,
                      add_commas(row[i], ewc));
         } else {
            bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, row[i]);
         }
         send(ctx, buf);
      }
      send(ctx, "\n");
      break;
   default:
      break;
   }
   return 0;
}
 digit_t get_digit(std::size_t i) const { return (i < max_length() ?
     digits()[i] : 0); }
示例#24
0
文件: sql.c 项目: engeenity/bareos
/*
 * If full_list is set, we list vertically, otherwise, we
 *  list on one line horizontally.
 * Return number of rows
 */
int list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type type)
{
   SQL_FIELD *field;
   SQL_ROW row;
   int i, col_len, max_len = 0;
   int num_fields;
   char buf[2000], ewc[30];

   Dmsg0(800, "list_result starts\n");
   if (sql_num_rows(mdb) == 0) {
      send(ctx, _("No results to list.\n"));
      return sql_num_rows(mdb);
   }

   num_fields = sql_num_fields(mdb);
   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      /*
       * No need to calculate things like column widths for
       * unformated or raw output.
       */
      break;
   case HORZ_LIST:
   case VERT_LIST:
      Dmsg1(800, "list_result starts looking at %d fields\n", num_fields);
      /*
       * Determine column display widths
       */
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         Dmsg1(800, "list_result processing field %d\n", i);
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         col_len = cstrlen(field->name);
         if (type == VERT_LIST) {
            if (col_len > max_len) {
               max_len = col_len;
            }
         } else {
            if (sql_field_is_numeric(mdb, field->type) && (int)field->max_length > 0) { /* fixup for commas */
               field->max_length += (field->max_length - 1) / 3;
            }
            if (col_len < (int)field->max_length) {
               col_len = field->max_length;
            }
            if (col_len < 4 && !sql_field_is_not_null(mdb, field->flags)) {
               col_len = 4;                 /* 4 = length of the word "NULL" */
            }
            field->max_length = col_len;    /* reset column info */
         }
      }
      break;
   }

   Dmsg0(800, "list_result finished first loop\n");

   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      Dmsg1(800, "list_result starts second loop looking at %d fields\n", num_fields);
      while ((row = sql_fetch_row(mdb)) != NULL) {
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            if (row[i] == NULL) {
               bsnprintf(buf, sizeof(buf), " %s", "NULL");
            } else {
               bsnprintf(buf, sizeof(buf), " %s", row[i]);
            }
            send(ctx, buf);
         }
         if (type != RAW_LIST) {
            send(ctx, "\n");
         }
      }
      break;
   case HORZ_LIST:
      Dmsg1(800, "list_result starts second loop looking at %d fields\n", num_fields);
      list_dashes(mdb, send, ctx);
      send(ctx, "|");
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         Dmsg1(800, "list_result looking at field %d\n", i);
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         max_len = max_length(field->max_length);
         bsnprintf(buf, sizeof(buf), " %-*s |", max_len, field->name);
         send(ctx, buf);
      }
      send(ctx, "\n");
      list_dashes(mdb, send, ctx);

      Dmsg1(800, "list_result starts third loop looking at %d fields\n", num_fields);
      while ((row = sql_fetch_row(mdb)) != NULL) {
         sql_field_seek(mdb, 0);
         send(ctx, "|");
         for (i = 0; i < num_fields; i++) {
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            max_len = max_length(field->max_length);
            if (row[i] == NULL) {
               bsnprintf(buf, sizeof(buf), " %-*s |", max_len, "NULL");
            } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
               bsnprintf(buf, sizeof(buf), " %*s |", max_len,
                         add_commas(row[i], ewc));
            } else {
               bsnprintf(buf, sizeof(buf), " %-*s |", max_len, row[i]);
            }
            send(ctx, buf);
         }
         send(ctx, "\n");
      }
      list_dashes(mdb, send, ctx);
      break;
   case VERT_LIST:
      Dmsg1(800, "list_result starts vertical list at %d fields\n", num_fields);
      while ((row = sql_fetch_row(mdb)) != NULL) {
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            if (row[i] == NULL) {
               bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, "NULL");
            } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
               bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name,
                   add_commas(row[i], ewc));
            } else {
               bsnprintf(buf, sizeof(buf), " %*s: %s\n", max_len, field->name, row[i]);
            }
            send(ctx, buf);
         }
         send(ctx, "\n");
      }
      break;
   }
   return sql_num_rows(mdb);
}
示例#25
0
 // Return the number of available (uncommitted) regions.
 uint available() const { return max_length() - length(); }
示例#26
0
文件: sql.c 项目: debfx/bareos
int list_result(void *vctx, int nb_col, char **row)
{
   SQL_FIELD *field;
   int i, col_len, max_len = 0;
   int num_fields;
   char ewc[30];
   POOL_MEM key;
   POOL_MEM value;

   LIST_CTX *pctx = (LIST_CTX *)vctx;
   OUTPUT_FORMATTER *send = pctx->send;
   e_list_type type = pctx->type;
   B_DB *mdb = pctx->mdb;
   JCR *jcr = pctx->jcr;

   send->object_start("row");

   num_fields = sql_num_fields(mdb);
   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      /*
       * No need to calculate things like maximum field lenght for
       * unformated or raw output.
       */
      break;
   case HORZ_LIST:
   case VERT_LIST:
      if (!pctx->once) {
         pctx->once = true;

         Dmsg1(800, "list_result starts looking at %d fields\n", num_fields);
         /*
          * Determine column display widths
          */
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            Dmsg1(800, "list_result processing field %d\n", i);
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            col_len = cstrlen(field->name);
            if (type == VERT_LIST) {
               if (col_len > max_len) {
                  max_len = col_len;
               }
            } else {
               if (sql_field_is_numeric(mdb, field->type) && (int)field->max_length > 0) { /* fixup for commas */
                  field->max_length += (field->max_length - 1) / 3;
               }
               if (col_len < (int)field->max_length) {
                  col_len = field->max_length;
               }
               if (col_len < 4 && !sql_field_is_not_null(mdb, field->flags)) {
                  col_len = 4;                 /* 4 = length of the word "NULL" */
               }
               field->max_length = col_len;    /* reset column info */
            }
         }

         pctx->num_rows++;

         Dmsg0(800, "list_result finished first loop\n");
         if (type == VERT_LIST) {
            break;
         }

         Dmsg1(800, "list_result starts second loop looking at %d fields\n", num_fields);

         /*
          * Keep the result to display the same line at the end of the table
          */
         list_dashes(mdb, send);

         send->decoration("|");
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            Dmsg1(800, "list_result looking at field %d\n", i);
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            max_len = max_length(field->max_length);
            send->decoration(" %-*s |", max_len, field->name);
         }
         send->decoration("\n");
         list_dashes(mdb, send);
      }
      break;
   default:
      break;
   }

   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      Dmsg1(800, "list_result starts third loop looking at %d fields\n", num_fields);
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         if (row[i] == NULL) {
            value.bsprintf(" %s", "NULL");
         } else {
            value.bsprintf(" %s", row[i]);
         }
         send->object_key_value(field->name, value.c_str(), "%s");
      }
      if (type != RAW_LIST) {
         send->decoration("\n");
      }
      break;
   case HORZ_LIST:
      Dmsg1(800, "list_result starts third loop looking at %d fields\n", num_fields);
      sql_field_seek(mdb, 0);
      send->decoration("|");
      for (i = 0; i < num_fields; i++) {
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }

         max_len = max_length(field->max_length);
         if (row[i] == NULL) {
            value.bsprintf(" %-*s |", max_len, "NULL");
         } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
            value.bsprintf(" %*s |", max_len, add_commas(row[i], ewc));
         } else {
            value.bsprintf(" %-*s |", max_len, row[i]);
         }

         /*
          * Use value format string to send preformated value.
          */
         send->object_key_value(field->name, row[i], value.c_str());
      }
      send->decoration("\n");
      break;
   case VERT_LIST:
      Dmsg1(800, "list_result starts vertical list at %d fields\n", num_fields);
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         if (row[i] == NULL) {
            key.bsprintf(" %*s: ", max_len, field->name);
            value.bsprintf("%s\n", "NULL");
         } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
            key.bsprintf(" %*s: ", max_len, field->name);
            value.bsprintf("%s\n", add_commas(row[i], ewc));
         } else {
            key.bsprintf(" %*s: ", max_len, field->name);
            value.bsprintf("%s\n", row[i]);
         }

         /*
          * Use value format string to send preformated value.
          */
         send->object_key_value(field->name, key.c_str(), row[i], value.c_str());
      }
      send->decoration("\n");
      break;
   default:
      break;
   }
   send->object_end("row");
   return 0;
}
示例#27
0
SEXP R_dl(SEXP a, SEXP b, SEXP weight){
  PROTECT(a);
  PROTECT(b);
  PROTECT(weight);
   
  int na = length(a)
    , nb = length(b)
    , nt = (na > nb) ? na : nb
    , bytes = IS_CHARACTER(a)
    , ml_a = max_length(a)
    , ml_b = max_length(b);
  
  double *w = REAL(weight);

  /* claim space for workhorse */
  unsigned int *s=NULL, *t=NULL;
  dictionary *dict = new_dictionary( ml_a + ml_b + 1 );

  double *scores = (double *) malloc( (ml_a + 3) * (ml_b + 2) * sizeof(double) );

  int slen = (ml_a + ml_b + 2) * sizeof(int);
  s = (unsigned int *) malloc(slen);

  if ( (scores == NULL) | ( s == NULL ) ){
    UNPROTECT(3); free(scores); free(s);
    error("Unable to allocate enough memory");
  } 

  t = s + ml_a + 1;
  memset(s, 0, slen);


  // output
  SEXP yy; 
  PROTECT(yy = allocVector(REALSXP, nt));
  double *y = REAL(yy);

  int i=0, j=0, len_s, len_t, isna_s, isna_t;
  unsigned int *s1, *t1;
  for ( int k=0; k < nt; ++k ){
    if (bytes){
      s = get_elem(a, i, bytes, &len_s, &isna_s, s);
      t = get_elem(b, j, bytes, &len_t, &isna_t, t);
    } else { // make sure there's an extra 0 at the end of the string.
      s1 = get_elem(a, i, bytes, &len_s, &isna_s, s);
      t1 = get_elem(b, j, bytes, &len_t, &isna_t, t);
      memcpy(s,s1,len_s*sizeof(int));
      memcpy(t,t1,len_t*sizeof(int));
    }
    if ( isna_s || isna_t ){
      y[k] = NA_REAL;
      continue;
    }

    y[k] = distance(
     s, t, len_s, len_t,
     w, dict, scores
    );
    if (y[k] < 0 ) y[k] = R_PosInf;
    i = RECYCLE(i+1,na);
    j = RECYCLE(j+1,nb);
    memset(s, 0, slen);
  }
  
  free_dictionary(dict);
  free(scores);
  free(s);
  UNPROTECT(4);
  return yy;
} 
示例#28
0
SEXP R_match_dl(SEXP x, SEXP table, SEXP nomatch, SEXP matchNA, SEXP weight, SEXP maxDistance){
  PROTECT(x);
  PROTECT(table);
  PROTECT(nomatch);
  PROTECT(matchNA);
  PROTECT(weight);
  PROTECT(maxDistance);

  int nx = length(x)
    , ntable = length(table)
    , no_match = INTEGER(nomatch)[0]
    , match_na = INTEGER(matchNA)[0]
    , bytes = IS_CHARACTER(x)
    , ml_x = max_length(x)
    , ml_t = max_length(table);

  double *w = REAL(weight);
  double maxDist = REAL(maxDistance)[0];
  
  /* claim space for workhorse */
  dictionary *dict = new_dictionary( ml_x + ml_t + 1 );
  double *scores = (double *) malloc( (ml_x + 3) * (ml_t + 2) * sizeof(double) );

  unsigned int *X = NULL, *T = NULL;

  X = (unsigned int *) malloc( (ml_x + ml_t + 2) * sizeof(int) );

  if ( (scores == NULL) ||  (X == NULL) ){
    UNPROTECT(6); free(X); free(scores); 
    error("Unable to allocate enough memory");
  }

  T = X + ml_x + 1;
  memset(X, 0, (ml_x + ml_t + 2)*sizeof(int));


  // output vector
  SEXP yy;
  PROTECT(yy = allocVector(INTSXP, nx));
  int *y = INTEGER(yy);

  double d = R_PosInf, d1 = R_PosInf;
  int index, len_X, len_T, isna_X, isna_T;
  unsigned int *X1, *T1;
  for ( int i=0; i<nx; i++){
    index = no_match;
    if ( bytes ){
      X = get_elem(x, i , bytes, &len_X, &isna_X, X);
    } else {
      X1 = get_elem(x, i , bytes, &len_X, &isna_X, X);
      memcpy(X, X1, len_X*sizeof(int));
    }
    d1 = R_PosInf;

    for ( int j=0; j<ntable; j++){
      if ( bytes ){
        T = get_elem(table, j, bytes, &len_T, &isna_T, T);
      } else {
        T1 = get_elem(table, j, bytes, &len_T, &isna_T, T);
        memcpy(T, T1, len_T * sizeof(int));
      }
      if ( !isna_X && !isna_T ){        // both are char (usual case)
        d = distance(
          X, T, len_X, len_T, w, dict, scores
        );
        memset(T,0, (ml_t+1)*sizeof(int));
        if ( d <= maxDist && d < d1){ 
          index = j + 1;
          if ( abs(d) < 1e-14 ) break;
          d1 = d;
        }
      } else if ( isna_X && isna_T ) {  // both are NA
        index = match_na ? j + 1 : no_match;
        break;
      }
    }
    
    y[i] = index;
    memset(X,0,(ml_x + 1)*sizeof(int));
  }  
  UNPROTECT(7);
  free(X);
  free_dictionary(dict);
  free(scores);
  return(yy);
}
示例#29
0
文件: sql.c 项目: debfx/bareos
/*
 * If full_list is set, we list vertically, otherwise, we
 *  list on one line horizontally.
 * Return number of rows
 */
int list_result(JCR *jcr, B_DB *mdb, OUTPUT_FORMATTER *send, e_list_type type)
{
   SQL_FIELD *field;
   SQL_ROW row;
   int i, col_len, max_len = 0;
   int num_fields;
   char ewc[30];
   POOL_MEM key;
   POOL_MEM value;

   Dmsg0(800, "list_result starts\n");
   if (sql_num_rows(mdb) == 0) {
      send->decoration(_("No results to list.\n"));
      send->object_end("table");
      return sql_num_rows(mdb);
   }

   num_fields = sql_num_fields(mdb);
   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      /*
       * No need to calculate things like column widths for
       * unformated or raw output.
       */
      break;
   case HORZ_LIST:
   case VERT_LIST:
      Dmsg1(800, "list_result starts looking at %d fields\n", num_fields);
      /*
       * Determine column display widths
       */
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         Dmsg1(800, "list_result processing field %d\n", i);
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         col_len = cstrlen(field->name);
         if (type == VERT_LIST) {
            if (col_len > max_len) {
               max_len = col_len;
            }
         } else {
            if (sql_field_is_numeric(mdb, field->type) && (int)field->max_length > 0) { /* fixup for commas */
               field->max_length += (field->max_length - 1) / 3;
            }
            if (col_len < (int)field->max_length) {
               col_len = field->max_length;
            }
            if (col_len < 4 && !sql_field_is_not_null(mdb, field->flags)) {
               col_len = 4;                 /* 4 = length of the word "NULL" */
            }
            field->max_length = col_len;    /* reset column info */
         }
      }
      break;
   }

   Dmsg0(800, "list_result finished first loop\n");

   switch (type) {
   case NF_LIST:
   case RAW_LIST:
      Dmsg1(800, "list_result starts second loop looking at %d fields\n", num_fields);
      while ((row = sql_fetch_row(mdb)) != NULL) {
         send->object_start(row[0]);
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            if (row[i] == NULL) {
               value.bsprintf(" %s", "NULL");
            } else {
               value.bsprintf(" %s", row[i]);
            }
            send->object_key_value(field->name, value.c_str(), "%s");
         }
         if (type != RAW_LIST) {
            send->decoration("\n");
         }
         send->object_end(row[0]);
      }
      break;
   case HORZ_LIST:
      Dmsg1(800, "list_result starts second loop looking at %d fields\n", num_fields);
      list_dashes(mdb, send);
      send->decoration("|");
      sql_field_seek(mdb, 0);
      for (i = 0; i < num_fields; i++) {
         Dmsg1(800, "list_result looking at field %d\n", i);
         field = sql_fetch_field(mdb);
         if (!field) {
            break;
         }
         max_len = max_length(field->max_length);
         send->decoration(" %-*s |", max_len, field->name);
      }
      send->decoration("\n");
      list_dashes(mdb, send);

      Dmsg1(800, "list_result starts third loop looking at %d fields\n", num_fields);
      while ((row = sql_fetch_row(mdb)) != NULL) {
         send->object_start(row[0]);
         sql_field_seek(mdb, 0);
         send->decoration("|");
         for (i = 0; i < num_fields; i++) {
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            max_len = max_length(field->max_length);
            if (row[i] == NULL) {
               value.bsprintf(" %-*s |", max_len, "NULL");
            } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
               value.bsprintf(" %*s |", max_len, add_commas(row[i], ewc));
            } else {
               value.bsprintf(" %-*s |", max_len, row[i]);
            }
            if (i == num_fields-1) {
               value.strcat("\n");
            }
            /* use value format string to send preformated value */
            send->object_key_value(field->name, row[i], value.c_str());
         }
         send->object_end(row[0]);
      }
      list_dashes(mdb, send);
      break;
   case VERT_LIST:
      Dmsg1(800, "list_result starts vertical list at %d fields\n", num_fields);
      while ((row = sql_fetch_row(mdb)) != NULL) {
         send->object_start(row[0]);
         sql_field_seek(mdb, 0);
         for (i = 0; i < num_fields; i++) {
            field = sql_fetch_field(mdb);
            if (!field) {
               break;
            }
            if (row[i] == NULL) {
               key.bsprintf(" %*s: ", max_len, field->name);
               value.bsprintf("%s\n", "NULL");
            } else if (sql_field_is_numeric(mdb, field->type) && !jcr->gui && is_an_integer(row[i])) {
               key.bsprintf(" %*s: ", max_len, field->name);
               value.bsprintf("%s\n", add_commas(row[i], ewc));
            } else {
               key.bsprintf(" %*s: ", max_len, field->name);
               value.bsprintf("%s\n", row[i]);
            }
            /* use value format string to send preformated value */
            send->object_key_value(field->name, key.c_str(), row[i], value.c_str());
         }
         send->decoration("\n");
         send->object_end(row[0]);
      }
      break;
   }
   return sql_num_rows(mdb);
}
示例#30
0
SEXP R_match_qgram_tree(SEXP x, SEXP table, SEXP nomatch, SEXP matchNA, SEXP qq, SEXP maxDist, SEXP distance){
  PROTECT(x);
  PROTECT(table);
  PROTECT(nomatch);
  PROTECT(matchNA);
  PROTECT(qq);
  PROTECT(maxDist);
  PROTECT(distance);

  double max_dist = REAL(maxDist)[0] == 0.0 ? R_PosInf : REAL(maxDist)[0];
  
  
  int dist = INTEGER(distance)[0] // choose distance function
    , q = INTEGER(qq)[0]
    , nx = length(x)
    , ntable = length(table)
    , no_match = INTEGER(nomatch)[0]
    , match_na = INTEGER(matchNA)[0]
    , bytes = IS_CHARACTER(x)
    , ml_x = max_length(x)
    , ml_t = max_length(table);
  
  // set up a qtree;
  qtree *Q = new_qtree(q, 2);

  unsigned int *X = NULL, *T = NULL;
  if (bytes){
    X = (unsigned int *) malloc( (ml_x + ml_t) * sizeof(int));
    if ( X == NULL){
      UNPROTECT(7);
      error("Unable to allocate enough memory");
    }
    T = X + ml_x;
  }

  // output vector
  SEXP yy;
  PROTECT(yy = allocVector(INTSXP, nx));
  int *y = INTEGER(yy);


  double d = R_PosInf, d1 = R_PosInf;
  int index, isna_X, isna_T, len_X, len_T;

  for ( int i=0; i<nx; i++){
    index = no_match;
    X = get_elem(x, i, bytes, &len_X, &isna_X, X);
    d1 = R_PosInf;
    for ( int j=0; j<ntable; j++){

      T = get_elem(table, j, bytes, &len_T, &isna_T,T);

      if ( !isna_X && !isna_T ){        // both are char (usual case)
        d = qgram_tree(
          X, T, len_X, len_T, q, Q, dist
        );
        if ( d == -2.0 ){
          UNPROTECT(7);
          error("Unable to allocate enough memory for qgram storage");
        }
        if ( d > max_dist ){
          continue;
        } else if ( d > -1 && d < d1){ 
          index = j + 1;
          if ( abs(d) < 1e-14 ) break; 
          d1 = d;
        }
      } else if ( isna_X && isna_T ) {  // both are NA
        index = match_na ? j + 1 : no_match;
        break;
      }
    }
    
    y[i] = index;
  } 
  if ( bytes ) free(X);
  free_qtree();
  UNPROTECT(8);
  return(yy);
}