/* The function completely rebuilds the nodelist.  Once sort()'ed it
   keeps being sorted when removing or inserting new circuits. */
void nodelist::sort (void) {
  nodelist * nodes = new nodelist ();
  struct nodelist_t * nl, * cand;
  int p, i, ports, MaxPorts, len = length ();

  // go through the list until there is no node left
  for (i = 0; i < len; i++) {
    // find last order node
    cand = NULL;
    for (MaxPorts = -1, p = 0, nl = root; nl != NULL; nl = nl->next) {
      ports = sortfunc (nl);
      if (ports > MaxPorts || MaxPorts < 0 || ports == -1) {
	cand = nl;
	MaxPorts = ports;
      }
      if (ports == -1) break;
    }
    // add last order node
    remove (cand, 1);
    nodes->add (cand);
  }

  // store sorted node list in current object
  root = nodes->getRoot ();
  last = nodes->getLastNode ();
  nodes->root = NULL;
  sorting = 1;

  // delete temporary node list
  delete nodes;
}
/* The following function can be used to insert a new circuit to the
   node list.  It goes through each node of the circuit and rearranges
   the node list appropriately. */
void nodelist::insert (circuit * c) {
  // go through each node of the circuit
  for (int i = 0; i < c->getSize (); i++) {
    struct nodelist_t * nl;
    node * n = c->getNode (i);
    // is this node already in the nodelist?
    if (contains (n->getName ()) == 0) {
      // no, create new node and put it into the list
      nl = create (n->getName (), n->getInternal ());
      addCircuitNode (nl, n);
      if (sorting) {
	if (c->getPort ())
	  append (nl);
	else
	  insert (nl);
      }
      else add (nl);
    }
    else {
      // yes, put additional node into nodelist structure
      if ((nl = getNode (n->getName ())) != NULL) {
	addCircuitNode (nl, n);
	if (sorting && sortfunc (nl) > 0) {
	  // rearrange sorting
	  remove (nl, 1);
	  insert (nl);
	}
      }
    }
  }
}
示例#3
0
文件: ls.c 项目: Kaikaiw/apue
/**
 * main comparation function passed to fts_open
 * it takes pointer to pointer to FTSENT
 * using '->' operator is better than using ``(*(*)).'' here.
 */
static int
comp(const FTSENT **a, const FTSENT **b)
{
    u_short ai=(*a)->fts_info;
    u_short bi=(*b)->fts_info;
    
    if (ai==FTS_ERR || ai==FTS_DNR || bi==FTS_ERR || bi==FTS_DNR) return 0;
    if (ai!=FTS_NS && bi==FTS_NS) return -1;
    if (ai==FTS_NS && bi!=FTS_NS) return 1;
    if (ai==FTS_NS && bi==FTS_NS) return namecmp(*a, *b);
    return sortfunc(*a, *b);
}
示例#4
0
void calc_sort_time(int *parray,int size,void (*sortfunc)(int *,int),char *funcname)
{
	struct timeval start,stop,diff;

	memset(&start,0,sizeof(struct timeval));
	memset(&stop,0,sizeof(struct timeval));
	memset(&diff,0,sizeof(struct timeval));

	gettimeofday(&start,NULL);
	sortfunc(parray,size);
	gettimeofday(&stop,NULL);
	
	time_substract(&diff,&start,&stop);

	printf("%s:%d s,%d us\n",funcname,(int)diff.tv_sec,(int)diff.tv_usec);

}
/* This function removes the nodes associated with the given circuit
   from the node list.  If the node list is sorted then the order gets
   rearranged properly. */
void nodelist::remove (circuit * c) {
  // go through each node of the circuit
  for (int i = 0; i < c->getSize (); i++) {
    node * n = c->getNode (i);
    struct nodelist_t * nl;
    if ((nl = getNode (n->getName ())) != NULL) {
      // remove node from node structure
      delCircuitNode (nl, n);
      if (nl->nNodes <= 0) {
	// completely remove the node structure
	remove (nl);
      }
      else if (sorting && sortfunc (nl) > 0) {
	// rearrange sorting
	remove (nl, 1);
	insert (nl);
      }
    }
  }
}
示例#6
0
void
bubblesort (int *ary,
            int  len,
            int (*sortfunc) (int, int))
{
  int i, j;
  int temp;

  for (i = 0;  i < len - 1; i++)
    {
      for (j = 0; j < (len - i); j++)
        {
          if (sortfunc (ary[j], ary[j + 1]))
            {
              temp = ary[j];
              ary[j] = ary[j + 1];
              ary[j + 1] = temp;
            }
        }
    }
}
示例#7
0
void	printdir(t_lst *lst, char *param, int nblst)
{
	t_lst *file;

	if (!lst)
		return ;
	if (lst->info.perm[0] == 'd')
	{
		if (nblst > 1)
		{
			ft_putchar('\n');
			ft_putstr(ft_strjoin(lst->info.name, ":\n"));
		}
		if (!(file = getinfo(lst->info.name)))
		{
			permdenied(lst->info.name);
			return ;
		}
		sortfunc(param, &file, 0, lst->info.name);
	}
	printdir(lst->next, param, nblst);
}
示例#8
0
int main(int argc, char* argv[])
{
	int input[] = { 5, -1, 4, 12 };
	int input_size = 4;

	int input_sorted[] = { -4, 1, 22, 66, 89, 120, 238 };
	int input_sorted_size = 7;

	// 2. function pointers
	// direct invocation
	printf("Max is %d\n", max(input, input_size));
	// function pointer invocation
	int(*sortfunc)(int elements[], int size) = max;
	printf("Max (function pointer) %d\n", sortfunc(input, input_size));

	// 3. recursivity
	printf("recursive fibonacci(12) = %d\n", fibonacci_recursive(12));
	printf("iterative fibonacci(12) = %d\n", fibonacci_iterative(12));

	// 4. search
	// 4a. simple search 
	harness_search(input, input_size, 4, "search", search);
	harness_search(input, input_size, 2, "search", search);
	// 4b. binary search
	harness_search(input_sorted, input_sorted_size, 89, "search_binary", search_binary);
	harness_search(input_sorted, input_sorted_size, 500, "search_binary", search_binary);
	harness_search(input_sorted, input_sorted_size, -40, "search_binary", search_binary);
	harness_search(input_sorted, input_sorted_size, 140, "search_binary", search_binary);

	// 4. merge
	int to_sort[] = { 9, -1, 3, 11, 4, 99, 11, 0 };
	int to_sort_size = 8;

	printf("Performing merge sort\n");
	printf("- unsorted: ");
	print_array(to_sort, to_sort_size);
	sort_merge(to_sort, to_sort_size);
	printf("- sorted: ");
	print_array(to_sort, to_sort_size);

	// 5. file access
	int matrix[4][4] = {
		{ 1, 2, 3, 4 },
		{ 5, 6, 7, 8 },
		{ -1, -2, -3, -4 },
		{ 9, 78, 12, -1 }
	};
	write_matrix_to_file(matrix, "matrix.txt");
	int matrix_out[4][4];
	read_matrix_from_file(matrix_out, "matrix.txt");
	printf("Matrix read from file:\n");
	print_matrix(matrix_out);

	// 6/7. binary file access
	// delete 'db' before using
	unlink("students.dat");
	STUDENT student1;
	student1.an_studiu = 1;
	student1.grupa = 1014;
	student1.nume = "Salut";

	STUDENT student2;
	student2.an_studiu = 2;
	student2.grupa = 1084;
	student2.nume = "Pa";
	
	student_append(student1, "students.dat");
	student_append(student2, "students.dat");

	student_print_all("students.dat");

	// XX. bisection method
	// f(x) = x^3 - x - 2
	// f(1) = -2
	// f(2) = -4
	int res = bisection(1, 2, equation);
	printf("Bisected x to be %d\n", res);

	printf("GCD: %d\n", gcd(299792458, 6447287));

	return EXIT_SUCCESS;
}
/* The function evaluates the sorting criteria of the given two nodes.
   It returns non-zero if 'n1' should be inserted before 'n2'. */
static int insfunc (struct nodelist_t * n1, struct nodelist_t * n2) {
  int p1 = sortfunc (n1);
  int p2 = sortfunc (n2);
  return p1 >= 0 && (p1 <= p2 || p2 < 0);
}