/** * Allocates memory for a new Priority Queue structure . * 'cmp' function: * returns 0 if d1 and d2 have the same priorities * returns [negative value] if d1 have a smaller priority than d2 * returns [positive value] if d1 have a greater priority than d2 */ PQueue *pqueue_new(int (*cmp)(const void *d1, const void *d2), size_t capacity) { PQueue *res = NULL; NP_CHECK(cmp); res = malloc(sizeof(*res)); NP_CHECK(res); res->cmp = cmp; /* The inner representation of data inside the queue is an array of void* */ res->data = malloc(capacity * sizeof(*(res->data))); NP_CHECK(res->data); res->size = 0; res->capacity = capacity; return (res); }
void pqueue_minheapify(PQueue *q, size_t idx) { void *tmp = NULL; size_t l_idx, r_idx, smlr_idx; NP_CHECK(q); l_idx = LEFT(idx); r_idx = RIGHT(idx); if (l_idx < q->size && q->cmp(q->data[l_idx], q->data[idx]) < 0) { smlr_idx = l_idx; } else { smlr_idx = idx; } if (r_idx < q->size && q->cmp(q->data[r_idx], q->data[smlr_idx]) < 0) { smlr_idx = r_idx; } if (smlr_idx != idx) { tmp = q->data[smlr_idx]; q->data[smlr_idx] = q->data[idx]; q->data[idx] = tmp; pqueue_minheapify(q, smlr_idx); } }
/** * * Turn an "almost-heap" into a heap . * */ void pqueue_heapify(PQueue *q, size_t idx) { /* left index, right index, largest */ void *tmp = NULL; size_t l_idx, r_idx, lrg_idx; NP_CHECK(q); l_idx = LEFT(idx); r_idx = RIGHT(idx); /* Left child exists, compare left child with its parent */ if (l_idx < q->size && q->cmp(q->data[l_idx], q->data[idx]) < 0) { lrg_idx = l_idx; } else { lrg_idx = idx; } /* Right child exists, compare right child with the largest element */ if (r_idx < q->size && q->cmp(q->data[r_idx], q->data[lrg_idx]) < 0) { lrg_idx = r_idx; } /* At this point largest element was determined */ if (lrg_idx != idx) { /* Swap between the index at the largest element */ tmp = q->data[lrg_idx]; q->data[lrg_idx] = q->data[idx]; q->data[idx] = tmp; /* Heapify again */ pqueue_heapify(q, lrg_idx); } }
double *InitCond(double *p, int N, int distr_type, double center, double gamma) { int i; /* Counters */ double k; gsl_rng *r; gsl_rng_default_seed = rand()*RAND_MAX; const gsl_rng_type *T; T = gsl_rng_default; r = gsl_rng_alloc(T); NP_CHECK(p); switch(distr_type) { case 0: /* Cauchy ??? */ for(i=0 ;i<N ;i++ ) p[i] = center + gsl_ran_cauchy(r, gamma); break; case 1: /* Gaussian (random) */ for(i=0 ;i<N ;i++ ) p[i] = center + gsl_ran_gaussian(r,gamma); break; case 2: /* Cauchy ordered */ for(i=0 ;i<N ;i++ ) { k = (2.0*(i+1) - N -1.0)/(N+1.0); p[i] = center + gamma*tan((PI/2.0)*k); } break; default: ARG_ERROR; break; } return p; }
Test *test_new(int priority) { Test *t = NULL; t = malloc(sizeof(*t)); NP_CHECK(t); t->priority = priority; t->log = "234mtrhrtngre"; printf("Inserindo %d - %s\n", t->priority, t->log); return (t); }
void *pqueue_mindequeue(PQueue *q) { void *data = NULL; NP_CHECK(q); if (q->size < 1) { return NULL; } data = q->data[0]; q->data[0] = q->data[q->size-1]; q->size--; pqueue_minheapify(q, 0); return (data); }
/** * * Returns the element with the biggest priority from the queue . * */ void *pqueue_dequeue(PQueue *q) { void *data = NULL; NP_CHECK(q); if (q->size < 1) { /* Priority Queue is empty */ //DEBUG("Priority Queue underflow . Cannot remove another element ."); return NULL; } data = q->data[0]; q->data[0] = q->data[q->size-1]; q->size--; /* Restore heap property */ pqueue_heapify(q, 0); return (data); }
void pqueue_minenqueue(PQueue *q, const void *data) { size_t i; void *tmp = NULL; NP_CHECK(q); if (q->size >= q->capacity) { return; } q->data[q->size] = (void*) data; i = q->size; q->size++; while(i > 0 && q->cmp(q->data[i], q->data[PARENT(i)]) < 0) { tmp = q->data[i]; q->data[i] = q->data[PARENT(i)]; q->data[PARENT(i)] = tmp; i = PARENT(i); } }
/** * * Adds a new element to the Priority Queue . * */ void pqueue_enqueue(PQueue *q, const void *data) { size_t i; void *tmp = NULL; NP_CHECK(q); if (q->size >= q->capacity) { DEBUG("Priority Queue is full. Cannot add another element ."); return; } /* Adds element last */ q->data[q->size] = (void*) data; i = q->size; q->size++; /* The new element is swapped with its parent as long as its * precedence is higher */ while(i > 0 && q->cmp(q->data[i], q->data[PARENT(i)]) < 0) { tmp = q->data[i]; q->data[i] = q->data[PARENT(i)]; q->data[PARENT(i)] = tmp; i = PARENT(i); } }