Exemplo n.º 1
0
} T_END_TEST

T_TEST(t_pqueue_alt_insert) {
	struct pqueue* q = pqueue_create(comp);
	int i;
	int num = 10;

	T_ASSERT(q);

	for (i = 0; i < num; i++) {
		T_ASSERT(0 == pqueue_insert(q, (void*)((i % 2) ? i : num - i - (num % 2 ? 1 : 2))));
	}
	
	T_ASSERT(!pqueue_is_empty(q));

	i = 0;
	while (!pqueue_is_empty(q)) {
		T_ASSERT(i == (int)pqueue_peek(q));
		T_ASSERT(i == (int)pqueue_pop(q));
		i++;
	}

	T_ASSERT(i == num);

	pqueue_destroy(q);
} T_END_TEST
Exemplo n.º 2
0
static void test_push_existing()
{
	size_t i, j;
	int top;
	struct pqueue pq;
	int elt;

	for (i = 0; i < count; i++) {
		pqueue_init_copy(&pq, &pqueue);
		elt = elts[i];

		pqueue_push(&pq, &elt);

		assert_int_equal(pqueue_count(&pq), count + 1);

		for (j = 0; j < count + 1; j++) {
			top = *(int *)pqueue_top(&pq);
			pqueue_pop(&pq);
			if (j <= i) {
				assert_int_equal(top, elts[j]);
			} else {
				assert_int_equal(top, elts[j - 1]);
			}
		}

		pqueue_destroy(&pq);
	}
}
Exemplo n.º 3
0
void enddown(){
	pqueue_destroy(pq);
	free(pq);
	pq = NULL;

	for(int i=0; i<10; i++){
		free(u[i]);
		u[i] = NULL;
	}
}
Exemplo n.º 4
0
/* Main sender function. Taken from the state diagram on slide 6, chapter 5 */
void sender(int window, int timeout) {
	 int base = 1;
	 int nextseqnum = 1;
	 bool allsent = false;
	 PQueue sendQ;
	 pqueue_init(&sendQ, window);

	 while ( !(allsent && pqueue_empty(&sendQ)) ) {
		  int acknum = -1;

		  /* Send new data */
		  if (!allsent && nextseqnum < base + window) {
			   Packet* packet = add_packet(&sendQ, nextseqnum);
			   if (packet == NULL) {
					allsent = true;
			   } else {
					send_packet(packet);
					if (base == nextseqnum)
						 start_timer(timeout);
					nextseqnum++;
			   }
		  }
		  
		  /* Attempt to receive an ACK. */
		  acknum = get_ack(0);
		  if (acknum > 0) {
			   base = acknum + 1;
			   if (base == nextseqnum)
					stop_timer();
			   else
					start_timer(timeout);
		  }

		  /* Clean up the queue */
		  while (!pqueue_empty(&sendQ) && pqueue_head(&sendQ)->seqn < base) {
			   pqueue_pop(&sendQ);
		  }
		  
		  /* Handle timeouts */
		  if (cnt_active && cnt_time >= cnt_timeout) {
			   start_timer(cnt_timeout);
			   pqueue_map(&sendQ, &send_packet);
		  }
		  pqueue_debug_print(&sendQ);
	 }
	 
	 pqueue_destroy(&sendQ);
}
Exemplo n.º 5
0
int main() {
	unsigned int maxSize = 10;
	pqueue_t pq = pqueue_empty(maxSize);
	bool exit = false;
	char *option = NULL;
	unsigned int *u = calloc(1,sizeof(unsigned int));
	unsigned int v;
	do {
		option = print_menu();
		switch(*option) {
			case ADD:
				printf("\nPor favor ingrese el nodo: ");
				if(!pqueue_is_full(pq)) {
					scanf("%u",&v);
					*u = v;
					pqueue_enqueue(pq, *u);
					printf("\nExito.\n");
				} else {
					printf("La cola esta llena\n");
				}
				
				break;
			case SHOW:
				if(!pqueue_is_empty(pq)) {
					*u = pqueue_fst(pq);
					printf("\nEl maximo es: %u\n",*u);
				} else {
					printf("La cola está vacia\n");
				}
				break;
			case POP:
				if(!pqueue_is_empty(pq)) {
					pqueue_dequeue(pq);
					printf("\nSe elimino correctamente\n");
				}
				break;
			case EXIT:
				exit = true;
				break;
			
		}
		free(option);
        option = NULL;
	} while(!exit);
	pq = pqueue_destroy(pq);

}
Exemplo n.º 6
0
int main (int argc, char *argv[])
{
	int i;
	struct pqueue *pqueue;
	pqueue = pqueue_create(0, compare, NULL);
	if (pqueue == NULL) {
		return -1;
	}
	for (i = 1; i < argc; i++) {
		pqueue_insert(pqueue, argv[i]);
	}
	printf("%s ", (char *) pqueue_pop(pqueue));
	printf("%s ", (char *) pqueue_pop(pqueue));
	printf("%s ", (char *) pqueue_pop(pqueue));
	pqueue_destroy(pqueue);
	return 0;
}
Exemplo n.º 7
0
graph_t kruskal(graph_t graph) {
    graph_t result = graph_empty(graph_vertices_count(graph));
    unsigned int L, R, num_edges = graph_edges_count(graph);
    vertex_t l = NULL, r = NULL;
    pqueue_t Q = pqueue_empty(num_edges);
    union_find_t C = union_find_create(graph_vertices_count(graph));
    edge_t E = NULL, *edges = graph_edges(graph);
    for (unsigned int i = 0; i < num_edges; i++) {
        pqueue_enqueue(Q, edges[i]);
    }

    free(edges);
    edges = NULL;

    while (!pqueue_is_empty(Q) && union_find_count(C) > 1) {
        E = edge_copy(pqueue_fst(Q));
        l = edge_left_vertex(E);
        r = edge_right_vertex(E);
        L = union_find_find(C, vertex_label(l));
        R = union_find_find(C, vertex_label(r));

        if (L != R) {
            union_find_union(C, L, R);
            E = edge_set_primary(E, true);
        } else {
            E = edge_set_primary(E, false);
        }
        result = graph_add_edge(result, E);
        pqueue_dequeue(Q);
    }

    while (!pqueue_is_empty(Q)) {
        E = edge_copy(pqueue_fst(Q));
        pqueue_dequeue(Q);
        E = edge_set_primary(E, false);
        result = graph_add_edge(result, E);
    }

    Q = pqueue_destroy(Q);
    C = union_find_destroy(C);

    return result;
}
Exemplo n.º 8
0
} T_END_TEST

T_TEST(t_pqueue_duplicate) {
	struct pqueue* q = pqueue_create(comp);
	void* v = (void*)0xdeadbeef;

	T_ASSERT(q);

	T_ASSERT(0 == pqueue_insert(q, v));
	T_ASSERT(0 == pqueue_insert(q, v));

	T_ASSERT(!pqueue_is_empty(q));
	T_ASSERT(v == pqueue_peek(q));
	T_ASSERT(v == pqueue_pop(q));
	
	T_ASSERT(!pqueue_is_empty(q));
	T_ASSERT(v == pqueue_peek(q));
	T_ASSERT(v == pqueue_pop(q));

	T_ASSERT(pqueue_is_empty(q));

	pqueue_destroy(q);

} T_END_TEST
Exemplo n.º 9
0
static int build_tree(int *freqs, BiTree **tree) {

BiTree             *init,
                   *merge,
                   *left,
                   *right;

PQueue             pqueue;

HuffNode           *data;

int                size,
                   c;

/*****************************************************************************
*                                                                            *
*  Initialize the priority queue of binary trees.                            *
*                                                                            *
*****************************************************************************/

*tree = NULL;

pqueue_init(&pqueue, compare_freq, destroy_tree);

for (c = 0; c <= UCHAR_MAX; c++) {

   if (freqs[c] != 0) {

      /***********************************************************************
      *                                                                      *
      *  Set up a binary tree for the current symbol and its frequency.      *
      *                                                                      *
      ***********************************************************************/

      if ((init = (BiTree *)malloc(sizeof(BiTree))) == NULL) {

         pqueue_destroy(&pqueue);
         return -1;

      }

      bitree_init(init, free);

      if ((data = (HuffNode *)malloc(sizeof(HuffNode))) == NULL) {

         pqueue_destroy(&pqueue);
         return -1;

      }

      data->symbol = c;
      data->freq = freqs[c];

      if (bitree_ins_left(init, NULL, data) != 0) {

         free(data);
         bitree_destroy(init);
         free(init);
         pqueue_destroy(&pqueue);
         return -1;

      }

      /***********************************************************************
      *                                                                      *
      *  Insert the binary tree into the priority queue.                     *
      *                                                                      *
      ***********************************************************************/

      if (pqueue_insert(&pqueue, init) != 0) {

         bitree_destroy(init);
         free(init);
         pqueue_destroy(&pqueue);
         return -1;

      }

   }

}

/*****************************************************************************
*                                                                            *
*  Build a Huffman tree by merging trees in the priority queue.              *
*                                                                            *
*****************************************************************************/

size = pqueue_size(&pqueue);

for (c = 1; c <= size - 1; c++) {

   /**************************************************************************
   *                                                                         *
   *  Allocate storage for the next merged tree.                             *
   *                                                                         *
   **************************************************************************/

   if ((merge = (BiTree *)malloc(sizeof(BiTree))) == NULL) {

      pqueue_destroy(&pqueue);
      return -1;

   }

   /**************************************************************************
   *                                                                         *
   *  Extract the two trees whose root nodes have the smallest frequencies.  *
   *                                                                         *
   **************************************************************************/

   if (pqueue_extract(&pqueue, (void **)&left) != 0) {

      pqueue_destroy(&pqueue);
      free(merge);
      return -1;

   }

   if (pqueue_extract(&pqueue, (void **)&right) != 0) {

      pqueue_destroy(&pqueue);
      free(merge);
      return -1;

   }

   /**************************************************************************
   *                                                                         *
   *  Allocate storage for the data in the root node of the merged tree.     *
   *                                                                         *
   **************************************************************************/

   if ((data = (HuffNode *)malloc(sizeof(HuffNode))) == NULL) {

      pqueue_destroy(&pqueue);
      free(merge);
      return -1;

   }

   memset(data, 0, sizeof(HuffNode));

   /**************************************************************************
   *                                                                         *
   *  Sum the frequencies in the root nodes of the trees being merged.       *
   *                                                                         *
   **************************************************************************/

   data->freq = ((HuffNode *)bitree_data(bitree_root(left)))->freq +
      ((HuffNode *)bitree_data(bitree_root(right)))->freq;

   /**************************************************************************
   *                                                                         *
   *  Merge the two trees.                                                   *
   *                                                                         *
   **************************************************************************/

   if (bitree_merge(merge, left, right, data) != 0) {

      pqueue_destroy(&pqueue);
      free(merge);
      return -1;

   }

   /**************************************************************************
   *                                                                         *
   *  Insert the merged tree into the priority queue and free the others.    *
   *                                                                         *
   **************************************************************************/

   if (pqueue_insert(&pqueue, merge) != 0) {

      pqueue_destroy(&pqueue);
      bitree_destroy(merge);
      free(merge);
      return -1;

   }

   free(left);
   free(right);

}

/*****************************************************************************
*                                                                            *
*  The last tree in the priority queue is the Huffman tree.                  *
*                                                                            *
*****************************************************************************/

if (pqueue_extract(&pqueue, (void **)tree) != 0) {

   pqueue_destroy(&pqueue);
   return -1;

   }

else {

   pqueue_destroy(&pqueue);

}

return 0;

}
Exemplo n.º 10
0
int main(int argc, char **argv) {

PQueue             pqueue;

void               *data;

int                intval[30],
                   i;

/*****************************************************************************
*                                                                            *
*  Initialize the priority queue.                                            *
*                                                                            *
*****************************************************************************/

pqueue_init(&pqueue, compare_int, NULL);

/*****************************************************************************
*                                                                            *
*  Perform some priority queue operations.                                   *
*                                                                            *
*****************************************************************************/

i = 0;

intval[i] = 5;
fprintf(stdout, "Inserting %03d\n", intval[i]);
if (pqueue_insert(&pqueue, &intval[i]) != 0)
   return 1;
print_pqueue(&pqueue);
i++;

intval[i] = 10;
fprintf(stdout, "Inserting %03d\n", intval[i]);
if (pqueue_insert(&pqueue, &intval[i]) != 0)
   return 1;
print_pqueue(&pqueue);
i++;

intval[i] = 20;
fprintf(stdout, "Inserting %03d\n", intval[i]);
if (pqueue_insert(&pqueue, &intval[i]) != 0)
   return 1;
print_pqueue(&pqueue);
i++;

intval[i] = 1;
fprintf(stdout, "Inserting %03d\n", intval[i]);
if (pqueue_insert(&pqueue, &intval[i]) != 0)
   return 1;
print_pqueue(&pqueue);
i++;

intval[i] = 25;
fprintf(stdout, "Inserting %03d\n", intval[i]);
if (pqueue_insert(&pqueue, &intval[i]) != 0)
   return 1;
print_pqueue(&pqueue);
i++;

intval[i] = 22;
fprintf(stdout, "Inserting %03d\n", intval[i]);
if (pqueue_insert(&pqueue, &intval[i]) != 0)
   return 1;
print_pqueue(&pqueue);
i++;

intval[i] = 12;
fprintf(stdout, "Inserting %03d\n", intval[i]);
if (pqueue_insert(&pqueue, &intval[i]) != 0)
   return 1;
print_pqueue(&pqueue);
i++;

while (pqueue_size(&pqueue) > 0) {

   fprintf(stdout, "Peeking at the highest priority element..Value=%03d\n",
      *(int *)pqueue_peek(&pqueue));
   if (pqueue_extract(&pqueue, (void **)&data) != 0)
      return 1;
   fprintf(stdout, "Extracting %03d\n", *(int *)data);
   print_pqueue(&pqueue);

}

/*****************************************************************************
*                                                                            *
*  Destroy the priority queue.                                               *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Destroying the pqueue\n");
pqueue_destroy(&pqueue);

return 0;

}
Exemplo n.º 11
0
Arquivo: main.c Projeto: Tinix/kruskal
graph_t kruskal(graph_t graph) {
/* Computes a MST of the given graph.
 *
 * This function returns a copy of the input graph in which
 * only the edges of the MST are marked as primary. The
 * remaining edges are marked as secondary. 
 *
 * The input graph does not change. 
 * 
*/
    graph_t mst;
    union_find_t uf;
    pqueue_t pq;
    edge_t *edges;
    edge_t e;
    unsigned int left, right, n, m;

    /* Inicialización */
    n = graph_vertices_count(graph);
    m = graph_edges_count(graph);
    mst = graph_empty(n);
    uf = union_find_create(n);
    pq = pqueue_empty(m);

    /* Llenar `pq` */
    edges = graph_edges(graph);
    for (unsigned int j = 0; j < m; j++) {
        pqueue_enqueue(pq, edges[j]);
    }
    /* Ahora las aristas están en `pq` */
    free(edges);
    edges = NULL;


    /* Principal */
    while (!pqueue_is_empty(pq) && union_find_count(uf) > 1) {
        e = edge_copy(pqueue_fst(pq));
        left = vertex_label(edge_left_vertex(e));
        right = vertex_label(edge_right_vertex(e));
        left = union_find_find(uf, left);
        right = union_find_find(uf, right);
        if (!union_find_connected(uf, left, right)) {
            e = edge_set_primary(e, true);
            union_find_union(uf, left, right);
        } else {
            e = edge_set_primary(e, false);
        }
        mst = graph_add_edge(mst, e);
        pqueue_dequeue(pq);
    }

    /* Agregar aristas restantes como secundarias */
    while (!pqueue_is_empty(pq)) {
        e = edge_copy(pqueue_fst(pq));
        e = edge_set_primary(e, false);
        mst = graph_add_edge(mst, e);
        pqueue_dequeue(pq);
    }

    /* Destroy */
    uf = union_find_destroy(uf);
    pq = pqueue_destroy(pq);

    return (mst);
}
Exemplo n.º 12
0
} T_END_TEST

T_TEST(t_pqueue_destroy_empty) {
	struct pqueue* q = pqueue_create(comp);
	pqueue_destroy(q);
} T_END_TEST
Exemplo n.º 13
0
} T_END_TEST

T_TEST(t_pqueue_destroy_null) {
	pqueue_destroy(NULL);
} T_END_TEST
Exemplo n.º 14
0
static void teardown()
{
	pqueue_destroy(&pqueue);
}