Refactoring

This commit is contained in:
2015-03-18 07:15:35 -04:00
parent ad8e20c62b
commit 67ca00a0e0
13 changed files with 485 additions and 363 deletions

View File

@@ -1,103 +1,111 @@
/****************************************************************************/
/* */
/* Utility Functions for CO759 */
/* */
/****************************************************************************/
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <stdarg.h>
#include "util.h"
double util_get_current_time(void)
double get_current_time(void)
{
struct rusage ru;
getrusage (RUSAGE_SELF, &ru);
getrusage(RUSAGE_SELF, &ru);
return ((double) ru.ru_utime.tv_sec) +
((double) ru.ru_utime.tv_usec)/1000000.0;
return ((double) ru.ru_utime.tv_sec)
+ ((double) ru.ru_utime.tv_usec) / 1000000.0;
}
/* function for creating a random set of points in unit square */
int CO759_build_xy (int ncount, double *xlist, double *ylist, int gridsize)
int build_random_2d_points(
int node_count, double *x_list, double *y_list, int grid_size)
{
int rval = 0, i, j, winner, x, y;
int **hit = (int **) NULL, *hitcount = (int *) NULL;
printf ("Random %d point set, gridsize = %d\n", ncount, gridsize);
fflush (stdout);
printf("Random %d point set, grid_size = %d\n", node_count, grid_size);
fflush(stdout);
hit = (int **) malloc (gridsize * sizeof (int *));
if (!hit) {
fprintf (stderr, "out of memory for hit\n");
rval = 1; goto CLEANUP;
hit = (int **) malloc(grid_size * sizeof(int *));
if (!hit)
{
fprintf(stderr, "out of memory for hit\n");
rval = 1;
goto CLEANUP;
}
for (i = 0; i < gridsize; i++) hit[i] = (int *) NULL;
for (i = 0; i < grid_size; i++) hit[i] = (int *) NULL;
hitcount = (int *) malloc (gridsize * sizeof (int));
if (!hitcount) {
fprintf (stderr, "out of memory for hitcount\n");
rval = 1; goto CLEANUP;
hitcount = (int *) malloc(grid_size * sizeof(int));
if (!hitcount)
{
fprintf(stderr, "out of memory for hitcount\n");
rval = 1;
goto CLEANUP;
}
for (i = 0; i < gridsize; i++) hitcount[i] = 0;
for (i = 0; i < grid_size; i++) hitcount[i] = 0;
for (i = 0; i < ncount; i++) {
for (i = 0; i < node_count; i++)
{
winner = 0;
do {
x = (int) (rand () % gridsize);
y = (int) (rand () % gridsize);
do
{
x = (int) (rand() % grid_size);
y = (int) (rand() % grid_size);
/* check to see if (x,y) is a duplicate point */
for (j = 0; j < hitcount[x]; j++) {
for (j = 0; j < hitcount[x]; j++)
{
if (hit[x][j] == y) break;
}
if (j == hitcount[x]) {
if (j == hitcount[x])
{
void *tmp_ptr = (void *) hit[x];
tmp_ptr = realloc (tmp_ptr, (hitcount[x]+1)*sizeof (int));
if (!tmp_ptr) {
fprintf (stderr, "out of member in realloc of hit\n");
rval = 1; goto CLEANUP;
tmp_ptr = realloc(tmp_ptr, (hitcount[x] + 1) * sizeof(int));
if (!tmp_ptr)
{
fprintf(stderr, "out of member in realloc of hit\n");
rval = 1;
goto CLEANUP;
}
hit[x] = (int *) tmp_ptr;
hit[x][hitcount[x]] = y;
hitcount[x]++;
winner = 1;
}
if (!winner) {
printf ("X"); fflush (stdout);
if (!winner)
{
printf("X");
fflush(stdout);
}
} while (!winner);
xlist[i] = (double) x;
ylist[i] = (double) y;
x_list[i] = (double) x;
y_list[i] = (double) y;
}
CLEANUP:
CLEANUP:
printf ("\n");
printf("\n");
if (hit) {
for (i = 0; i < gridsize; i++) {
if (hit[i]) free (hit[i]);
if (hit)
{
for (i = 0; i < grid_size; i++)
{
if (hit[i]) free(hit[i]);
}
free (hit);
free(hit);
}
if (hitcount) free (hitcount);
if (hitcount) free(hitcount);
return rval;
}
double initial_time = 0;
static double initial_time = 0;
void time_printf(const char *fmt, ...)
{
if(initial_time == 0)
initial_time = util_get_current_time();
if (initial_time == 0)
initial_time = get_current_time();
printf("[%10.2lf] ", util_get_current_time() - initial_time);
printf("[%10.2lf] ", get_current_time() - initial_time);
va_list args;
va_start(args, fmt);
@@ -107,9 +115,9 @@ void time_printf(const char *fmt, ...)
fflush(stdout);
}
void next_set (int sz, int *Set)
void next_set(int sz, int *set)
{
int i;
for (i=0; i < sz-1 && Set[i]+1 == Set[i+1]; i++) Set[i] = i;
Set[i] = Set[i]+1;
int i;
for (i = 0; i < sz - 1 && set[i] + 1 == set[i + 1]; i++) set[i] = i;
set[i] = set[i] + 1;
}