#ifndef ULIB_DLINKLIST_H_
#define ULIB_DLINKLIST_H_

#define DL_APPEND(head, new) \
  do { \
    if ((new) != NULL) { \
      (new)->next = NULL; \
      if ((head) != NULL) { \
        typeof((head)) __tmp = (head); \
        while (__tmp->next != NULL) { \
          __tmp = __tmp->next; \
        } \
        __tmp->next = (new); \
        (new)->prev = __tmp; \
      } else { \
        (new)->prev = NULL; \
        (head) = (new); \
      } \
    } \
  } while (0)

#define DL_PREPEND(head, new) \
  do { \
    if ((new) != NULL) { \
      (new)->prev = NULL; \
      (new)->next = (head); \
      if ((head) != NULL) { \
        (head)->prev = (new); \
      } \
      (head) = (new); \
    } \
  } while (0)

#define DL_REMOVE(head, ele) \
  do { \
    if ((ele) != NULL) { \
      if ((ele)->prev != NULL) { \
        (ele)->prev->next = (ele)->next; \
      } else { \
        (head) = (ele)->next; \
      } \
      if ((ele)->next != NULL) { \
        (ele)->next->prev = (ele)->prev; \
      } \
      (ele)->next = NULL; \
      (ele)->prev = NULL; \
    } \
  } while (0)

#define DL_FINDPROP(head, out, propname, propvalue) \
  do { \
    (out) = NULL; \
    typeof((head)) __tmp = (head); \
    while (__tmp) { \
      if (__tmp->propname == (propvalue)) { \
        (out) = __tmp; \
        break; \
      } \
      __tmp = __tmp->next; \
    } \
  } while (0)

#define DL_FOREACH_SAFE(head, var, tmp) \
  for (var = (head), tmp = (var ? var->next : NULL); \
       var != NULL; \
       var = tmp, tmp = (var ? var->next : NULL))

#define DL_FOREACH_SAFE_IDX(head, var, tmp, idx) \
  for ((idx) = 0, var = (head), tmp = (var ? var->next : NULL); \
       var != NULL; \
       var = tmp, tmp = (var ? var->next : NULL), (idx)++)

#define DL_FOREACH_SAFE_IDX_LIMIT(head, var, tmp, idx, max) \
  for ((idx) = 0, var = (head), tmp = (var ? var->next : NULL); \
       var != NULL && (idx) < (max); \
       var = tmp, tmp = (var ? var->next : NULL), (idx)++)

#define DL_BACK(head, out) \
  do { \
    (out) = NULL; \
    if ((head) != NULL) { \
      typeof((head)) __tmp = (head); \
      while (__tmp->next != NULL) { \
        __tmp = __tmp->next; \
      } \
      (out) = __tmp; \
    } \
  } while (0)

#define DL_FRONT(head, out) \
  do { \
    (out) = NULL; \
    if ((head) != NULL) { \
      typeof((head)) __tmp = (head); \
      while (__tmp->prev != NULL) { \
        __tmp = __tmp->prev; \
      } \
      (out) = __tmp; \
    } \
  } while (0)

#define DL_INSERT_AFTER(head, pos, new) \
  do { \
    if ((pos) != NULL && (new) != NULL) { \
      (new)->prev = (pos); \
      (new)->next = (pos)->next; \
      if ((pos)->next != NULL) { \
        (pos)->next->prev = (new); \
      } \
      (pos)->next = (new); \
    } else if ((pos) == NULL && (head) == NULL) { \
      /* Empty list: make new head */ \
      (new)->prev = NULL; \
      (new)->next = NULL; \
      (head) = (new); \
    } \
  } while (0)

#define DL_INSERT_BEFORE(head, pos, new) \
  do { \
    if ((pos) != NULL && (new) != NULL) { \
      (new)->next = (pos); \
      (new)->prev = (pos)->prev; \
      if ((pos)->prev != NULL) { \
        (pos)->prev->next = (new); \
      } else { \
        (head) = (new); \
      } \
      (pos)->prev = (new); \
    } else if ((pos) == NULL && (head) == NULL) { \
      /* Empty list: make new head */ \
      (new)->prev = NULL; \
      (new)->next = NULL; \
      (head) = (new); \
    } \
  } while (0)

#endif // ULIB_DLINKLIST_H_
