示例#1
0
文件: manager.c 项目: rigma/polymorph
void entry_remove(entry_t **list, char *name)
{
	entry_t *previous = NULL, *iterator = NULL;

	if (name == NULL)
		return;

	iterator = *list;
	while (iterator != NULL)
	{
		if (iterator->type == POLYNOMIAL && !strcmp(name, ((polynomial_t*) iterator->polynomial)->name))
			break;
		else if (iterator->type == POLYMORPH && !strcmp(name, ((polymorph_t*) iterator->polynomial)->name))
			break;

		previous = iterator;
		iterator = iterator->next;
	}

	if (previous == NULL)
		*list = iterator->next;
	else
		previous->next = iterator->next;

	if (iterator->type == POLYNOMIAL)
		polynomial_free((polynomial_t*) iterator->polynomial);
	else if (iterator->type == POLYMORPH)
		polymorph_free((polymorph_t*) iterator->polynomial);
	else
		free(iterator->polynomial);

	free(iterator);
}
示例#2
0
文件: manager.c 项目: rigma/polymorph
void entry_add(entry_t **list, void *polynomial, unsigned char type)
{
	entry_t *iterator = NULL, *previous = NULL;

	if (polynomial == NULL)
		return;

	if (*list == NULL)
	{
		*list = (entry_t*) malloc(sizeof(entry_t));
		if (*list == NULL)
			return;

		(*list)->polynomial = polynomial;
		(*list)->type = type;
		(*list)->next = NULL;
	}
	else
	{
		previous = NULL;
		iterator = *list;
		while (iterator != NULL)
		{
			if (type == POLYNOMIAL && !strcmp(((polynomial_t*) iterator->polynomial)->name, ((polynomial_t*) polynomial)->name))
				break;
			else if (type == POLYMORPH && !strcmp(((polymorph_t*) iterator->polynomial)->name, ((polymorph_t*) polynomial)->name))
				break;

			previous = iterator;
			iterator = iterator->next;
		}
		
		if (iterator != NULL)
		{
			if (iterator->type == POLYNOMIAL)
				polynomial_free((polynomial_t*) iterator->polynomial);
			else if (iterator->type == POLYMORPH)
				polymorph_free((polymorph_t*) iterator->polynomial);

			iterator->polynomial = polynomial;
		}
		else
		{
			previous->next = (entry_t*) malloc(sizeof(entry_t));
			if (previous->next == NULL)
				return;

			previous->next->polynomial = polynomial;
			previous->next->type = type;
			previous->next->next = NULL;
		}
	}
}
示例#3
0
polynomial_t *polynomial_extract(polynomial_t *p, unsigned long degree, unsigned long end)
{
	polynomial_t *q = NULL;
	complex_t *z = NULL;
	monomial_t *iterator = NULL;
	unsigned long i = 0;

	if (p == NULL)
		return NULL;

	q = polynomial_init(NULL);
	if (q == NULL)
		return NULL;

	for (i = p->degree, iterator = p->first; i >= end && iterator != NULL; i--, iterator = iterator->next)
	{
		if (degree >= iterator->degree && iterator->degree >= end)
		{
			z = complex_init(iterator->coef->re, iterator->coef->im);
			if (z == NULL)
			{
				polynomial_free(q);

				return NULL;
			}

			polynomial_append(q, z, iterator->degree);
		}
	}

	if (q->size == 0)
	{
		polynomial_free(q);
		q = NULL;
	}

	return q;
}
示例#4
0
Polynomial* polynomial_power(const Polynomial *polynomial, int power) {
  assert(polynomial != NULL);
  assert(power > 0);

  Polynomial *result = polynomial_copy(polynomial);

  while(power > 1) {
    Polynomial *tmp = result;
    result = polynomial_product(result, polynomial);

    if(tmp != polynomial) {
      polynomial_free(&tmp);
    }
    power--;
  }

  return result;
}
示例#5
0
文件: manager.c 项目: rigma/polymorph
void entry_free(entry_t **list)
{
	entry_t *next = NULL;

	if (*list == NULL)
		return;

	while (*list != NULL)
	{
		next = (*list)->next;

		if ((*list)->type == POLYNOMIAL)
			polynomial_free((polynomial_t*) (*list)->polynomial);
		else if ((*list)->type == POLYMORPH)
			polymorph_free((polymorph_t*) (*list)->polynomial);
		else
			free((*list)->polynomial);

		free(*list);
		*list = next;
	}
}
示例#6
0
Polynomial* polynomial_create_from_string(char *string) {
  assert(string != NULL);

  // read a random number of numbers
  // example string: 7x^3 + x^2 -9x + 30
  // allowed characters: digits, +, -, x, ^

  Polynomial* new_polynomial = polynomial_create_empty();
  Monomial* previous_monomial = NULL;

  char *cursor = string;
  long max_monomial_degree = 0;
  int first_monomial = 1;

  while(*cursor != '\0') {
    monomials_errno = MONOMIAL_SUCCESS;

    char *tmp_cursor = cursor;
    Monomial *new_monomial = monomial_create_from_string(cursor, &tmp_cursor);
    switch(monomials_errno) {
      case MONOMIAL_INPUT_ERROR:
        fprintf(stderr, "Fatal error: malformatted input!\nExiting\n");
        exit(EXIT_FAILURE);

      default: break;
    }

    cursor = tmp_cursor;

    long new_degree = monomial_get_degree(new_monomial);
    if(max_monomial_degree < new_degree) {
      max_monomial_degree = new_degree;
    }

    if(first_monomial) {
      new_polynomial->first = new_monomial;
      first_monomial = 0;
    } else {
      monomial_set_next(previous_monomial, new_monomial);
    }

    previous_monomial = new_monomial;

    // skip spaces
    while(*cursor == ' ') {
      cursor++;
    }
  }

  new_polynomial->degree = max_monomial_degree;

  // remove null monomials which may be there (e.g. 0x^12)
  polynomial_remove_null_monomials(new_polynomial);

  if(!new_polynomial->first) {
    // empty polynomial
    polynomial_free(&new_polynomial);
    return NULL;
  }

  // reduct the polynomial (e.g. in case the user has input 2x - 6x)
  Polynomial *reducted = polynomial_reduct(new_polynomial);
  polynomial_free(&new_polynomial);

  if(!reducted->first) {
    // empty polynomial
    polynomial_free(&reducted);
    return NULL;
  }

  return reducted;
}