Advertisement
giovani-rubim

Generic Heap Compressed

Sep 9th, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #define A (1 << 15)
  2. typedef int B;
  3. typedef int C;
  4. typedef struct D {
  5.     C E;
  6.     B F;
  7.     int G;
  8. } D;
  9. D H[A], *I, *J[A];
  10. int R;
  11. void init_heap() {
  12.     I = H - 1;
  13.     R = 0;
  14. }
  15. void S(D* K) {
  16.     int G = K->G, L, M, N;
  17.     D *O, *Q, *P;
  18.     C E = K->E;
  19.     while (G) {
  20.         O = J[L = (G - 1) >> 1];
  21.         if (O->E >= E) break;
  22.         J[G] = O;
  23.         O->G = G;
  24.         G = L;
  25.     }
  26.     while ((M = (G << 1) | 1) < R) {
  27.         Q = J[M];
  28.         if ((N = M + 1) < R && (P = J[N])->E > Q->E) {
  29.             Q = P;
  30.             M = N;
  31.         }
  32.         if (Q->E <= E) break;
  33.         J[G] = Q;
  34.         Q->G = G;
  35.         G = M;
  36.     }
  37.     J[G] = K;
  38.     K->G = G;
  39. }
  40. D* heap_push(B F, C E) {
  41.     D* K = ++I;
  42.     K->F = F;
  43.     K->E = E;
  44.     K->G = R++;
  45.     S(K);
  46.     return K;
  47. }
  48. B heap_pop() {
  49.     D* T = *J;
  50.     if (--R) {
  51.         D* K = J[R];
  52.         K->G = 0;
  53.         S(K);
  54.     }
  55.     return T->F;
  56. }
  57. /* A = heap_size
  58.  * B = t_val
  59.  * C = t_key
  60.  * D = heap_node
  61.  * E = key
  62.  * F = val
  63.  * G = index
  64.  * H = v_heap_nodes
  65.  * I = last_heap_node
  66.  * J = heap
  67.  * K = node
  68.  * L = parent_index
  69.  * M = child_index
  70.  * N = second_child_index
  71.  * O = parent
  72.  * P = second_child
  73.  * Q = child
  74.  * R = heap_size
  75.  * S = heap_update_node
  76.  * T = first */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement