Guest User

dl_sorted_insert

a guest
Jan 16th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #define DL_SORTED_INSERT(head,add,cmp)                       \
  2.   do {                                                       \
  3.     LDECLTYPE(head) _pos;                                    \
  4.     _pos = (head);                                           \
  5.     DL_FIND_POS(_pos,add,cmp);                               \
  6.     DL_INSERT(head,_pos,add);                                \
  7.   } while (0)
  8.  
  9. #define DL_INSERT(head,pos,add)                              \
  10.   DL_INSERT2(head, pos,add,prev,next)                        \
  11.  
  12. #define DL_INSERT2(head,pos,add,prev,next)                   \
  13.   do {                                                       \
  14.     if ((pos) == NULL) {                                     \
  15.       DL_PREPEND(head,add);                                  \
  16.     } else {                                                 \
  17.       DL_INSERT_AFTER(pos,add);                              \
  18.       if ((add)->next == NULL) {                             \
  19.         (head)->prev = (add);                                \
  20.       }                                                      \
  21.     }                                                        \
  22.   } while (0)
  23.  
  24. #define DL_INSERT_AFTER(prior,add)                           \
  25.   DL_INSERT_AFTER2(prior,add,prev,next)                      \
  26.  
  27. #define DL_INSERT_AFTER2(prior,add,prev,next)                \
  28.   do {                                                       \
  29.     (add)->next = (prior)->next;                             \
  30.     (prior)->next = (add);                                   \
  31.     (add)->prev = (prior);                                   \
  32.     if ((add)->next) {                                       \
  33.       (add)->next->prev = (add);                             \
  34.     }                                                        \
  35.   } while (0)
  36.  
  37. #define DL_FIND_POS(head,node,cmp)                           \
  38.   DL_FIND_POS2(head,node,cmp,prev,next)                      \
  39.  
  40. #define DL_FIND_POS2(head,node,cmp,prev,next)                \
  41.   do {                                                       \
  42.     if (cmp((head), (node)) > 0) {                           \
  43.       (head) = NULL;                                         \
  44.     } else {                                                 \
  45.       for (; (head)->next != NULL; head = (head)->next ) {   \
  46.         if (cmp((head)->next, (node)) > 0) {                 \
  47.           break;                                             \
  48.         }                                                    \
  49.       }                                                      \
  50.     }                                                        \
  51.   } while (0)
Advertisement
Add Comment
Please, Sign In to add comment