Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.81 KB | None | 0 0
  1. #ifndef PQUEUE_H
  2. #define PQUEUE_H
  3.  
  4. #include <stdlib.h>
  5.  
  6. #define Swap(a,b) {     \
  7.     typeof(a) _t = (a); \
  8.     (a) = (b);          \
  9.     (b) = _t;           \
  10. }
  11.  
  12. typedef void ** PQ;
  13.  
  14. #define PQinit(this) {                                                      \
  15.     (this) = (size_t *)malloc(2*sizeof(size_t) + 0x100*sizeof(void *)) + 2; \
  16.     PQsize(this) = 0x100;                                                   \
  17.     PQnitem(this) = 0;                                                      \
  18. }
  19.  
  20. #define PQsize(this) (((size_t *)(this))[-2])
  21.  
  22. #define PQnitem(this) (((size_t *)(this))[-1])
  23.  
  24. #define PQpush(this, item, higher)                                      \
  25. {                                                                       \
  26.     if (++PQnitem(this) == PQsize(this)) {                              \
  27.         (this) = (size_t *)realloc(&PQsize(this),                       \
  28.                                    sizeof(size_t)*2 +                   \
  29.                                    sizeof(void *)*2*PQsize(this)) + 2;  \
  30.     }                                                                   \
  31.     (this)[PQnitem(this)] = (item);                                     \
  32.                                                                         \
  33.     for (int _i = PQnitem(this); _i/2 > 0; _i /= 2) {                   \
  34.         if (higher((this)[_i/2], (this)[_i])) break;                    \
  35.         Swap((this)[_i], (this)[_i/2]);                                 \
  36.     }                                                                   \
  37. }
  38.  
  39. #define PQpop(this, item, higher) if (PQnitem(this) != 0)       \
  40. {                                                               \
  41.     (item) = (this)[1];                                         \
  42.     (this)[1] = (this)[PQnitem(this)];                          \
  43.                                                                 \
  44.     for (int _i = 1, _j; ; _i = _j)                             \
  45.     {                                                           \
  46.         if (_i*2   < PQnitem(this) &&                           \
  47.             higher((this)[_i*2  ], (this)[_i])) _j = _i*2;      \
  48.         else                                                    \
  49.         if (_i*2+1 < PQnitem(this) &&                           \
  50.             higher((this)[_i*2+1], (this)[_i])) _j = _i*2+1;    \
  51.         else break;                                             \
  52.                                                                 \
  53.         Swap((this)[_i], (this)[_j]);                           \
  54.     }                                                           \
  55.     --PQnitem(this);                                            \
  56. }                                                               \
  57. else (item) = NULL;
  58.  
  59. #define PQfree(this) (free(&PQsize(this)))
  60.  
  61. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement