Ejemplo n.º 1
0
/* Return the count of the number of times target occurs in the first 
 * n elements of array a.
 */   
int occurrences(int a[], int n, int target)
{
	if(n)
		return (a[0] == target) + occurrences(a+1, n-1, target); 
	else
		return 0;
}
Ejemplo n.º 2
0
char *str_replace(const char *str, const char *sub, const char *replace) {
    char *pos = (char *) str;
    int count = occurrences(sub, str);

    if (0 >= count) return strdup(str);

    int size = (
                   strlen(str)
                   - (strlen(sub) * count)
                   + strlen(replace) * count
               ) + 1;

    char *result = (char *) malloc(size);
    if (NULL == result) return NULL;
    char *current;
    while ((current = strstr(pos, sub))) {
        int len = current - pos;
        strncat(result, pos, len);
        strncat(result, replace, strlen(replace));
        pos = current + strlen(sub);
    }

    if (pos != (str + strlen(str))) {
        strncat(result, pos, (str - pos));
    }

    return result;
}
Ejemplo n.º 3
0
void evidence_huff_list_builder(void) {
    int* array = occurrences("senselessness");
    huff_list* list = huff_list_builder(array);
    huff_list_print(list);
    free(array);
    free(list);
    return;
}
Ejemplo n.º 4
0
void evidence_occurrences(void) {
    int* array = occurrences("senselessness");
    int i;
    for (i = 0; i < 27; i++) {
        printf("%d\n", array[i]);
    }
    free(array);
    return;
}
Ejemplo n.º 5
0
void evidence_insertion_sort(void) {
    int* array = occurrences("senselessness");
    huff_list* list = huff_list_builder(array);
    huff_list* sorted_list = insertion_sort(list);
    huff_list_print(sorted_list);
    free(array);
    free(sorted_list);
    return;
}
Ejemplo n.º 6
0
void huff_test(void) {
    huff_list* one_tree_list = \
        algorithm(insertion_sort(huff_list_builder(occurrences("senselessness"))));
    huff_show(one_tree_list->huff);
    printf("%u letters encoded\n", huff_height(one_tree_list->huff));
    printf("%s\n", code_generator('s', one_tree_list->huff));
    free(one_tree_list->huff);
    free(one_tree_list);
    return;
}
Ejemplo n.º 7
0
/* PUBLIC */
int occurrences(Term t, Term target)
{
  if (term_ident(t, target))
    return 1;
  else {
    int n = 0;
    int i;
    for (i = 0; i < ARITY(t); i++)
      n += occurrences(ARG(t,i), target);
    return n;
  }
}  /* occurrences */
Ejemplo n.º 8
0
/* PUBLIC */
BOOL variables_multisubset(Term a, Term b)
{
#if 1
  I2list a_vars = multiset_vars(a);
  I2list b_vars = multiset_vars(b);
  BOOL ok = i2list_multisubset(a_vars, b_vars);
  zap_i2list(a_vars);
  zap_i2list(b_vars);
  return ok;
#else  /* old version */
  Plist a_vars = set_of_variables(a);
  Plist p;
  BOOL ok = TRUE;

  for (p = a_vars; p && ok; p = p->next)
    ok = occurrences(b, p->v) >= occurrences(a, p->v);

  zap_plist(a_vars);
  return ok;
#endif
}  /* variables_multisubset */
Ejemplo n.º 9
0
char const *
password(void)
{
	static char const words[][6] = {
		"about", "after", "again", "below", "could", "every", "first",
		"found", "great", "house", "large", "learn", "never", "other",
		"place", "plant", "point", "right", "small", "sound", "spell",
		"still", "study", "their", "there", "these", "thing", "think",
		"three", "water", "where", "which", "world", "would", "write"
	};

	size_t const num_words = sizeof(words)/sizeof(*words);
	/* size_t const word_len  = 5; */

	char columns[5][7] = {{ 0 }};
	size_t const column_len = 6;
	size_t num_columns = 0;

	while (num_columns != 5) {
		printf("Enter column %lu: ", num_columns + 1);
		getl(columns[num_columns], 6);
		++num_columns;

		/* Stores indexes for words[]. */
		size_t answers[35] = {0};
		size_t num_answers = 0;

		for (int w = 0; w < num_words; ++w) {
			char const *word = words[w];
			for (int l = 0; l < num_columns; ++l) {
				char const  this_letter = word[l];
				char const *this_column = columns[l];
				size_t const last_column = num_columns - 1;

				if (!occurrences(this_column, column_len, this_letter))
					break;
				else if (l == last_column)
					answers[num_answers++] = w;
			}
		}

		if      (num_answers == 0) return "NONE FOUND";
		else if (num_answers == 1) return words[answers[0]];
		else continue;
	}

	return "NONE FOUND";
}
Ejemplo n.º 10
0
void evidence_hl_insert(void) {
    int* array = occurrences("senselessness");
    huff_list* list = huff_list_builder(array);
    huff_list* node1 = list;
    huff_list* node2 = list->next;
    huff_list* node3 = list->next->next;
    huff_list* node4 = list->next->next->next;
    huff_list* inserted_list = hl_insert(node1, NULL);
    inserted_list = hl_insert(node2, inserted_list);
    inserted_list = hl_insert(node3, inserted_list);
    inserted_list = hl_insert(node4, inserted_list);
    huff_list_print(inserted_list);
    free(array);
    free(inserted_list);
    return;
}
Ejemplo n.º 11
0
/* Return the count of the number of times target occurs in the first
 * n elements of array a.
 */
int occurrences(int a[], int n, int target)
{
   if (n < 0){ return 0; }
   return occurrences(a + 1, n - 1, target) + ((a[0] == target) ? 1:0);
}
Ejemplo n.º 12
0
  int
  process(const ecto::tendrils& inputs, const ecto::tendrils& outputs)
  {
    // Get the origin of the plane
    cv::Point origin;

    cv::Matx33f K, R;
    cv::Vec3f T;
    if (*do_center_)
      origin = cv::Point(masks_->cols/2, masks_->rows/2);
    else {
      if (!K_ || K_->empty() || !R_in_ || R_in_->empty() || !T_in_ || T_in_->empty()) {
        *found_ = false;
        return ecto::OK;
      }
      K = *K_;
      R = *R_in_;
      T = *T_in_;
      cv::Vec3f T = K*T;
      origin = cv::Point(T(0)/T(2), T(1)/T(2));
    }

    // Go over the plane masks and simply count the occurrence of each mask
    std::vector<int> occurrences(256, 0);
    for(int y = std::max(0, origin.y - *window_size_); y < std::min(masks_->rows, origin.y + *window_size_); ++y) {
      uchar *mask = masks_->ptr<uchar>(y) + std::max(0, origin.x - *window_size_);
      uchar *mask_end = masks_->ptr<uchar>(y) + std::min(masks_->cols, origin.x + *window_size_);
      for(; mask != mask_end; ++mask)
        if (*mask != 255)
          ++occurrences[*mask];
    }

    // Find the most common plane
    int best_index = -1;
    int best_count = 0;
    for(size_t i = 0; i < 255; ++i) {
      if (occurrences[i] > best_count) {
        best_index = i;
        best_count = occurrences[i];
      }
    }

    // Convert the plane coefficients to R,t
    cv::Matx33f rotation;
    cv::Vec3f translation;
    if (best_index >= 0) {
      *coeffs_ = (*planes_)[best_index];
      float a = (*coeffs_)[0], b = (*coeffs_)[1], c = (*coeffs_)[2], d = (*coeffs_)[3];

      // Deal with translation
      if (*do_center_) {
        getPlaneTransform(*coeffs_, rotation, translation);
        // Make sure T_ points to the center of the image
        translation = cv::Vec3f(0,0,-d/c);
      } else {
        // Have T_ point to the origin. Find alpha such that alpha*Kinv*origin is on the plane
        cv::Matx33f K_inv = K.inv();
        cv::Vec3f origin_inv = cv::Mat(K_inv * cv::Vec3f(origin.x, origin.y, 1));
        float alpha = -d/(a*origin_inv(0) + b*origin_inv(1) + c*origin_inv(2));
        translation = alpha*origin_inv;
        if (translation(2) < 0)
          translation = -translation;

        // Make the rotation fit to the plane (but as close as possible to the current estimate
        // Get the Z axis
        cv::Vec3f N(a, b, c);
        N = N/cv::norm(N);
        // Get the X, Y axes
        cv::Vec3f vecX(R(0,0), R(1,0), R(2,0));
        cv::Vec3f vecY(R(0,1), R(1,1), R(2,1));
        // Project them onto the plane
        vecX = vecX - vecX.dot(N)*N;
        vecY = vecY - vecY.dot(N)*N;
        vecX = vecX/cv::norm(vecX);
        vecY = vecY/cv::norm(vecY);
        // Get the median
        cv::Vec3f median = vecX + vecY;
        median = median/cv::norm(median);
        // Get a new basis
        cv::Vec3f vecYtmp = vecY - median.dot(vecY)*median;
        cv::Vec3f vecXtmp = vecX - median.dot(vecX)*median;
        vecYtmp = vecYtmp/cv::norm(vecYtmp);
        vecXtmp = vecXtmp/cv::norm(vecXtmp);
        // Get the rectified X/Y axes
        cv::Vec3f vecXnew = median + vecXtmp;
        cv::Vec3f vecYnew = median + vecYtmp;
        vecXnew = vecXnew/cv::norm(vecXnew);
        vecYnew = vecYnew/cv::norm(vecYnew);
        // Fill in the matrix
        rotation = cv::Matx33f(vecXnew(0), vecYnew(0), N(0), vecXnew(1), vecYnew(1), N(1), vecXnew(2), vecYnew(2), N(2));
      }

      *R_out_ = cv::Mat(rotation);
      *T_out_ = cv::Mat(translation);
      *found_ = true;
    } else
      *found_ = false;

    return ecto::OK;
  }
Ejemplo n.º 13
0
OPimBackendOccurrence::List ODateBookAccessBackend::occurrences( const QDateTime& dt )const
{
    OPimBackendOccurrence::List day = occurrences( dt.date(), dt.date() );

    return filterOccurrences( day, dt );
}