Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include "pqueue.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. priorityqueue_t * pqueue_create() {
  6.     priorityqueue_t* prioq = (priorityqueue_t*)malloc(sizeof(priorityqueue_t));
  7.     prioq->length = 0;
  8.     prioq->maxLength = 0;
  9.  
  10.     return prioq;
  11. }
  12.  
  13. void pqueue_insert(priorityqueue_t* pq, char * value, float p) {
  14.     int index = -1;
  15.  
  16.     if (isFull(pq))increase(pq);
  17.  
  18.     if (pq->length > 0) {
  19.         if (pq->elements[0]->priority >= p) {
  20.             index = 0;
  21.             memmove(pq->elements + 1, pq->elements, (pq->length) * sizeof(pquentry_t*));
  22.         } else if (pq->elements[pq->length - 1]->priority <= p) {
  23.             index = pq->length;
  24.         } else {
  25.             index = getIndexFromPriority(pq, &p);
  26.             memmove(pq->elements + index + 1, pq->elements + index, (pq->length - index) * sizeof(pquentry_t*));
  27.         }
  28.  
  29.         if (index < 0) index = pq->length;
  30.     } else index = 0;
  31.  
  32.     pq->elements[index] = (pquentry_t*)malloc(sizeof(pquentry_t));
  33.     pq->elements[index]->value = value;
  34.     pq->elements[index]->priority = p;
  35.     pq->length++;
  36. }
  37.  
  38. char* pqueue_extractMin(priorityqueue_t* pq) {
  39.     pquentry_t min;
  40.     pquentry_t *cache;
  41.     if (pq->length < 1) return "List empty!";
  42.  
  43.     min = *pq->elements[0];
  44.     cache = pq->elements[0];
  45.     pq->elements++;
  46.     pq->length--;
  47.     free(cache);
  48.     return min.value;
  49. }
  50.  
  51. void pqueue_decreaseKey(priorityqueue_t* pq, char * value, float p) {
  52.     for (int i = 0; i < pq->length; i++) {
  53.         if (pq->elements[i]->value == value) {
  54.             if (i + 1 >= pq->length) {
  55.                 free(pq->elements[i]);
  56.             } else {
  57.                 memmove(pq->elements + i, pq->elements + i + 1, (pq->length - i - 1) * sizeof(pquentry_t*));
  58.             }
  59.             pq->length--;
  60.             pqueue_insert(pq, value, p);
  61.             return;
  62.         }
  63.     }
  64. }
  65.  
  66. void pqueue_remove(priorityqueue_t* pq, char* value) {
  67.     int i;
  68.     for (i = 0; i < pq->length; i++) {
  69.         if (pq->elements[i]->value == value) {
  70.             memmove(pq->elements + i, pq->elements + i+1, (pq->length - i - 1) * sizeof(pquentry_t*));
  71.  
  72.             pq->length--;
  73.         }
  74.     }
  75. }
  76.  
  77. void pqueue_destroy(priorityqueue_t* pq) {
  78.     int i;
  79.     for (i = 0; i < pq->length; i++) {
  80.         free(pq->elements[i]);
  81.     }
  82.  
  83.     free(pq);
  84. }
  85.  
  86. char isFull(priorityqueue_t* pq) {
  87.     return pq->length >= pq->maxLength;
  88. }
  89.  
  90. void increase(priorityqueue_t * pq) {
  91.     if (pq->maxLength == 0) {
  92.         pq->elements = (pquentry_t**)malloc(1 *sizeof(pquentry_t*));
  93.         pq->maxLength = 1;
  94.     } else {
  95.         pq->elements = (pquentry_t**)realloc(pq->elements, (pq->length * 2) * sizeof(pquentry_t*));
  96.         pq->maxLength = (pq->length * 2);
  97.     }
  98. }
  99.  
  100. void printQueue(priorityqueue_t* pq) {
  101.     int i;
  102.     printf("(%i/%i) -> {\n", pq->length, pq->maxLength);
  103.     for (i = 0; i < pq->length; i++) {
  104.         printf("    [%i] -> (Wert: %s - Prio: %f)\n",i, pq->elements[i]->value, pq->elements[i]->priority);
  105.     }
  106.     printf("}\n");
  107. }
  108.  
  109. int getIndexFromPriority(priorityqueue_t* pq, float* p) {
  110.     float s = (float)pq->length / (float)(pq->elements[pq->length - 1]->priority);
  111.     int index = s * (*p);
  112.  
  113.     if (index < 0) index = 0;
  114.     if (index > (pq->length -1)) index = pq->length - 1;
  115.  
  116.     int prio = pq->elements[index]->priority;
  117.  
  118.     if (prio < (*p)) {
  119.         for (int i = index; i < pq->length; i++) {
  120.             if (pq->elements[i]->priority >= (*p)) {
  121.                 return i;
  122.             }
  123.         }
  124.     } else if (prio >(*p)) {
  125.         for (int i = index; i >= 0; i--) {
  126.             float pr = pq->elements[i]->priority;
  127.             if (pr <= (*p)) {
  128.                 return i + 1;
  129.             }
  130.         }
  131.     } else {
  132.         return index;
  133.     }
  134.     return pq->length;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement