Advertisement
avp210159

cyclist.h C circular doubly linked list

Apr 21st, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 27.07 KB | None | 0 0
  1. // avp 2016 http://pastebin.com/TGdUA9Se
  2. #ifndef _CYCLIST_H
  3. #define _CYCLIST_H
  4.  
  5. /*
  6.    
  7.    Circular doubly linked list with a single pointer list head:
  8.    the attempt of answer to line 592 in
  9.    https://github.com/torvalds/linux/blob/master/include/linux/list.h
  10.    
  11. 589  * Double linked lists with a single pointer list head.
  12. 590  * Mostly useful for hash tables where the two pointer list head is
  13. 591  * too wasteful.
  14. 592  * You lose the ability to access the tail in O(1).
  15.  
  16.    API
  17.  
  18.    Circular doubly linked list included in data struct.
  19.    struct cyclist {
  20.      struct cyclist *next, *prev;
  21.    };
  22.  
  23.    struct cyclist * list_header;
  24.  
  25.    get the struct for this entry macros:
  26.    container_of (field_addr, type, field_name)
  27.    CONTAINER_OF (field_addr, type, field_name)
  28.    list_entry (ptr, type, member)
  29.  
  30.    iterate over list forward macros:
  31.      struct cyclist *pos, *head;
  32.    cyclist_foreach (pos, head)
  33.    cyclist_foreach_entry (pos, head, member)
  34.    cyclist_foreach_from (pos, head)
  35.    cyclist_foreach_cont (pos, head)
  36.  
  37.    iterate over list backward macros:
  38.    cyclist_foreach_rev (pos, head)
  39.    cyclist_foreach_rev_entry (pos, head, member)
  40.    cyclist_foreach_rev_from (pos, head)
  41.    cyclist_foreach_rev_cont (pos, head)
  42.  
  43.    iterate over a list safe against removal of list entry macros:
  44.      struct cyclist_save s;
  45.    cyclist_foreach_safe (pos, s, head)
  46.    cyclist_foreach_from_safe (pos, s, head)
  47.    cyclist_foreach_cont_safe (pos, s, head)
  48.    cyclist_foreach_rev_safe (pos, s, head)
  49.    cyclist_foreach_rev_from_safe (pos, s, head)
  50.    cyclist_foreach_rev_cont_safe (pos, s, head)
  51.  
  52.    list manipulation functions:
  53.      struct cyclist *cyclist, *new, *old, *head;
  54.      struct cyclist_save save;
  55.    cyclist_is_empty (cyclist) tests whether a list is empty
  56.    cyclist_is_singular (cyclist) tests whether a list has just one entry
  57.    cyclist_singular (cyclist)  convert any entry to singular list
  58.    cyclist_add_tail (new, &head) add a new entry to tail
  59.    cyclist_add_tail_safe (new, &head, &save) add a new entry to tail
  60.                                         in foreach_..._safe () { ... } loops
  61.    cyclist_add (new, &head) add a new head entry
  62.    cyclist_add_safe (new, &head, &save) add a new entry to tail
  63.                                         in foreach_..._safe () { ... } loops
  64.    cyclist_insert (new, old) add a new entry after old
  65.    cyclist_insert_safe (new, old, &save) add a new entry after old
  66.                                         in foreach_..._safe () { ... } loops
  67.    cyclist_remove (old) delete entry from any list, make it singular
  68.    cyclist_del (old, &head) delete entry from list, correct head,
  69.                           make it singular
  70.    cyclist_del_safe (old, &head, &save) delete entry from list, correct head,
  71.                           make it singular in foreach_..._safe () { ... } loops
  72.    cyclist_replace (new, old) replace entry in list, make it singular
  73.    cyclist_replace_safe (new, old, &head, &save) replace entry in list and
  74.                       make it singular in foreach_..._safe () { ... } loops
  75.    cyclist_move (old, &to__add_tail)
  76.    cyclist_move_safe (old, &list1, &save1, &to, &save2)
  77.    cyclist_swap (&elem1, &elem2)
  78.    cyclist_cswap (elem1, elem2)
  79.    cyclist_swap_safe (&elem1, &list1, &save1, &elem2, &list2, &save2)
  80.    cyclist_split (cyclist, new) cut a list into two
  81.    cyclist_join (cyclist, cyclist) join 2 lists
  82.    cyclist_cut (cyclist, struct cyclist *lis2head, how) how: 0 - simple, 1 - linus
  83.    cyclist_roll (struct cyclist *lis2head)
  84.  
  85.    TODO
  86. +-   debug,
  87. -   doc?
  88.  
  89.    Is it good? I don't know.
  90.    May be the best practice in difficult cases will be to cut() such lists
  91.    to simple double linked lists, operate with them, and make roll()
  92.    after all.
  93.  */
  94.  
  95. // circular list, embedded in your data
  96. struct cyclist {
  97.   struct cyclist *next, *prev;
  98. };
  99.  
  100. // used as temporary storage in cyclist_foreach_..._safe macros
  101. struct cyclist_save {
  102.   struct cyclist *next, /* next item  for cyclist iterations
  103.              if p is current item, then .next = p->next for forward
  104.              and .next = p->prev for backwark
  105.                */
  106.     *savehead; /* when we iterate over list forward and delete current item,
  107.           the list head moves forward too and overtake current item,
  108.           so for breaking loop we need to compare (.next) and
  109.           loop endpoint (head) with previous head position,
  110.           and that position saved in this field (updates every step)
  111.         */
  112.   int direction; // 1 forward, -1 backward, 0 stop iterations
  113. #define S_FORWARD  1
  114. #define S_BACKWARD -1
  115. };
  116.  
  117. /**
  118.  * Get offset of a structure member
  119.  */
  120. #ifndef offsetof
  121. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  122. #endif
  123.  
  124. /**
  125.  * Casts a member of a structure out to the containing structure
  126.  * container_of, CONTAINER_OF, list_entry - get the struct for this entry
  127.  * @ptr:    the &struct cyclist pointer.
  128.  * @type:   the type of the struct this is embedded in.
  129.  * @member: the name of the l2item_struct within the struct.
  130.  */
  131. #ifndef  container_of
  132. #define container_of(field_addr,type,field_name)            \
  133.   ((type *)((char *)field_addr -                    \
  134.         (char *)&((type *)0)->field_name))
  135. #endif // container_of
  136. #ifndef list_entry
  137. #define list_entry(ptr, type, member) \
  138.     container_of(ptr, type, member)
  139. #endif // list_entry
  140. #ifndef CONTAINER_OF
  141. // more safe version of container_of
  142. #define CONTAINER_OF(field_addr,type,field_name)            \
  143.   ((field_addr) ? container_of(field_addr,type,field_name) : 0)
  144. #endif
  145.  
  146. // internal macros for cyclist forward/backward iterations
  147. #define _CL_STEP(newpos, head, endpos)          \
  148.   (((head) && (newpos) && (endpos)) ? ((newpos) == (endpos) ? 0 : (newpos)) : 0)
  149. #define _CL_SAFESTEP_BACK(h, p, ss)             \
  150.   ({ if ((h) && (ss).direction && (p = (ss).next)) {        \
  151.       (ss).savehead = (h); (ss).next = (ss).next->prev;     \
  152.       if ((ss).next == (h)->prev)               \
  153.     (ss).next = 0; } else if (!(ss).direction) p = 0; p; })
  154.  
  155.  
  156. /**
  157.  * cyclist_foreach - iterate over a list
  158.  * @pos:    the &struct cyclist to use as a loop cursor.
  159.  * @head:   the head for your list.
  160.  */
  161. #define cyclist_foreach(pos, head) \
  162.   for (pos = head; pos; pos = _CL_STEP(pos->next, head, head))
  163.  
  164. /**
  165.  * cyclist_foreach_entry - iterate over a list of given type
  166.  * @pos:    the type * to use as a loop cursor.
  167.  * @head:   the head for your list (cyclist *, not type *)!!!
  168.  * @member: the name of the cyclist within the struct.
  169.  */
  170. #define cyclist_foreach_entry(pos, head, member)                \
  171.   for (pos = CONTAINER_OF(head, __typeof__(*pos), member);      \
  172.        pos;                             \
  173.        pos = (head) ? (pos->member.next == (head) ? 0           \
  174.                : list_entry(pos->member.next,           \
  175.                     __typeof__(*pos), member)) : 0)
  176.  
  177. /**
  178.  * cyclist_foreach_from - iterate over a list from the given point
  179.  * @pos:    the &struct cyclist to use as a loop cursor.
  180.  * @head:   the head for your list.
  181.  *
  182.  * Start to iterate over list  from @pos point
  183.  */
  184. #define cyclist_foreach_from(pos, head) \
  185.   for (; pos; pos = _CL_STEP(pos->next, head, head))
  186.  
  187. /**
  188.  * cyclist_foreach_cont - continue list iteration from the current point
  189.  * @pos:    the &struct cyclist to use as a loop cursor.
  190.  * @head:   the head for your list.
  191.  *
  192.  * Start to iterate over list, continuing  after @pos point
  193.  */
  194. #define cyclist_foreach_cont(pos, head)             \
  195.   for (pos = pos ? _CL_STEP(pos->next, head, head) : 0;     \
  196.        pos; pos = _CL_STEP(pos->next, head, head))
  197.  
  198. /**
  199.  * cyclist_foreach_rev - iterate over a list backwards
  200.  * @pos:    the &struct cyclist to use as a loop cursor.
  201.  * @head:   the head for your list.
  202.  */
  203. #define cyclist_foreach_rev(pos, head)          \
  204.   for (pos = head ? (head)->prev : 0; pos;      \
  205.        pos = _CL_STEP(pos->prev, head, head->prev))
  206.  
  207. /**
  208.  * cyclist_foreach_rev_entry - iterate over a list of given type backwards
  209.  * @pos:    the type * to use as a loop cursor.
  210.  * @head:   the head for your list (cyclist *, not type *)!!!
  211.  * @member: the name of the cyclist within the struct.
  212.  */
  213. #define cyclist_foreach_rev_entry(pos, head, member)            \
  214.   for (pos = (head) ? list_entry((head)->prev,__typeof__(*pos), member) : 0; \
  215.        pos;                             \
  216.        pos = (head) ? (pos->member.prev == (head)->prev ? 0     \
  217.                : list_entry(pos->member.prev,           \
  218.                     __typeof__(*pos), member)) : 0)
  219.  
  220. /**
  221.  * cyclist_foreach_rev_from - iterate backwards over a list from the given point
  222.  * @pos:    the &struct cyclist to use as a loop cursor.
  223.  * @head:   the head for your list.
  224.  *
  225.  * Start to iterate backwards over list  from @pos point
  226.  */
  227. #define cyclist_foreach_rev_from(pos, head)         \
  228.   for (; pos; pos = _CL_STEP(pos->prev, head, head->prev))
  229.  
  230. /**
  231.  * cyclist_foreach_rev_cont - continue list iteration from the current point
  232.  * @pos:    the &struct cyclist to use as a loop cursor.
  233.  * @head:   the head for your list.
  234.  *
  235.  * Start to iterate backwards over list, continuing  after @pos point
  236.  */
  237. #define cyclist_foreach_rev_cont(pos, head)         \
  238.   for (pos = pos ? _CL_STEP(pos->prev, head, head->prev) : 0;   \
  239.        pos; pos = _CL_STEP(pos->prev, head, head->prev))
  240.  
  241. /**
  242.  * cyclist_foreach_safe - iterate over a list,
  243.  *              safe against removal of list entry
  244.  * @pos:    the &struct cyclist to use as a loop cursor.
  245.  * @s:      struct cyclist_save to use as temporary storage
  246.  * @head:   the head for your list.
  247.  */
  248. #define cyclist_foreach_safe(pos, s, head)              \
  249.   for (s.direction = S_FORWARD, s.savehead = pos = (head),      \
  250.      s.next = pos ? pos->next : 0; head && pos;         \
  251.        pos = ((!s.direction || s.next == s.savehead) ? 0 : s.next), \
  252.      pos ? s.next = s.next->next, s.savehead = (head) : 0)
  253.  
  254. /**
  255.  * cyclist_foreach_from_safe - iterate over list from given point,
  256.  *              safe against removal list entry
  257.  * @pos:    the &struct cyclist to use as a loop cursor.
  258.  * @s:      struct cyclist_save to use as temporary storage
  259.  * @head:   the head for your list.
  260.  */
  261. #define cyclist_foreach_from_safe(pos, s, head)             \
  262.   for (s.direction = S_FORWARD, s.savehead = head,          \
  263.      s.next = pos ? pos->next : 0; head && pos;         \
  264.        pos = ((!s.direction || s.next == s.savehead) ? 0 : s.next), \
  265.      pos ? s.next = s.next->next, s.savehead = (head) : 0)
  266.  
  267. /**
  268.  * cyclist_foreach_cont_safe - continue list iteration from the current point,
  269.  *              safe against removal list entry
  270.  * @pos:    the &struct cyclist to use as a loop cursor.
  271.  * @s:      struct cyclist_save to use as temporary storage
  272.  * @head:   the head for your list.
  273.  */
  274. #define cyclist_foreach_cont_safe(pos, s, head)             \
  275.   for (s.direction = S_FORWARD, s.savehead = (head),            \
  276.      s.next = (pos = pos ? _CL_STEP(pos->next, head, head) : 0) ?   \
  277.      pos->next : 0;                         \
  278.        head && pos;                         \
  279.        pos = ((!s.direction || s.next == s.savehead) ? 0 : s.next), \
  280.      pos ? s.next = s.next->next, s.savehead = (head) : 0)
  281.  
  282. /**
  283.  * cyclist_foreach_rev_safe - iterate over a list backwards,
  284.  *              safe against removal list entry
  285.  * @pos:    the &struct list_head to use as a loop cursor.
  286.  * @s:      struct cyclist_save to use as temporary storage
  287.  * @head:   the head for your list.
  288.  */
  289. #define cyclist_foreach_rev_safe(pos, s, head)              \
  290.   for (s.direction = S_BACKWARD, s.savehead = (head),           \
  291.      pos = (head) ? (head)->prev : 0,               \
  292.      s.next = pos ? pos == (head)? 0 : pos->prev : 0;       \
  293.        (head) && pos;                           \
  294.        _CL_SAFESTEP_BACK(head, pos, s))
  295.  
  296. /**
  297.  * cyclist_foreach_rev_from_safe - iterate over a list backwards from given
  298.  *              point, safe against removal of list entry
  299.  * @pos:    the &struct list_head to use as a loop cursor.
  300.  * @s:      struct cyclist_save to use as temporary storage
  301.  * @head:   the head for your list.
  302.  */
  303. #define cyclist_foreach_rev_from_safe(pos, s, head)         \
  304.   for (s.direction = S_BACKWARD, s.savehead = (head),           \
  305.      s.next = pos ? pos == (head)? 0 : pos->prev : 0;       \
  306.        (head) && pos;                           \
  307.        _CL_SAFESTEP_BACK(head, pos, s))
  308.  
  309. /**
  310.  * cyclist_foreach_rev_cont_safe - continue list iteration backwards,
  311.  *              safe against removal list entry
  312.  * @pos:    the &struct cyclist to use as a loop cursor.
  313.  * @s:      struct cyclist_save to use as temporary storage
  314.  * @head:   the head for your list.
  315.  */
  316. #define cyclist_foreach_rev_cont_safe(pos, s, head)         \
  317.   for (s.direction = S_BACKWARD, s.savehead = (head),           \
  318.      pos = pos ? _CL_STEP(pos->prev, head, head->prev) : 0,     \
  319.      s.next = pos ? pos == (head)? 0 : pos->prev : 0;       \
  320.        (head) && pos;                           \
  321.        _CL_SAFESTEP_BACK(head, pos, s))
  322.  
  323. /**
  324.  * cyclist_is_empty - tests whether a list is empty
  325.  * @head: the list to test.
  326.  */
  327. static inline int cyclist_is_empty (struct cyclist *head)
  328. {
  329.   return !head;
  330. }
  331.  
  332. /**
  333.  * cyclist_is_singular - tests whether a list has just one entry.
  334.  * @head: the list to test.
  335.  */
  336. static inline int cyclist_is_singular (struct cyclist *head)
  337. {
  338.   return head && head->next == head;
  339. }
  340.  
  341. /**
  342.  * cyclist_singular - convert any entry to singular list
  343.  * @entry: the item
  344.  */
  345. static inline struct cyclist * cyclist_singular (struct cyclist *entry) {
  346.   return entry ? ((entry)->next = (entry)->prev = (entry)) : 0;
  347. }
  348.  
  349. /**
  350.  * cyclist_add_tail - add a new entry to tail
  351.  * @newe: new entry to be added
  352.  * @head: list head to add item before it
  353.  *
  354.  * Insert a new entry before the specified head.
  355.  * This is useful for implementing queues.
  356.  * Returns: @newe
  357.  */
  358. static inline struct cyclist *
  359. cyclist_add_tail (struct cyclist *newe, struct cyclist **head)
  360. {
  361.   if (!newe || !head)
  362.     return 0;
  363.   if (cyclist_is_empty(*head)) {
  364.     newe->prev = newe->next = newe;
  365.     *head = newe;
  366.   } else {
  367.     newe->next = *head;
  368.     newe->prev = (*head)->prev;
  369.     (*head)->prev->next = newe;
  370.     (*head)->prev = newe;
  371.   }
  372.   return newe;
  373. }
  374.  
  375. /**
  376.  * cyclist_add_tail_safe - add a new entry to tail safty for iteration forward
  377.  * @newe: new entry to be added
  378.  * @head: list head to add item before it
  379.  *
  380.  * Insert a new entry before the specified head.
  381.  * This is useful for implementing queues.
  382.  * Returns: @newe
  383.  */
  384. static inline struct cyclist *
  385. cyclist_add_tail_safe (struct cyclist *newe, struct cyclist **head,
  386.              struct cyclist_save *s)
  387. {
  388.   if (!newe || !head)
  389.     return 0;
  390.   if (cyclist_is_empty(*head)) {
  391.     newe->prev = newe->next = newe;
  392.     *head = newe;
  393.   } else {
  394.     newe->next = *head;
  395.     newe->prev = (*head)->prev;
  396.     (*head)->prev->next = newe;
  397.     (*head)->prev = newe;
  398.     if (s && s->direction == S_FORWARD && s->next == *head)
  399.       s->next = newe;
  400.   }
  401.   return newe;
  402. }
  403.  
  404.  
  405. /**
  406.  * cyclist_add - add a new head entry
  407.  * @newe: new entry to be added
  408.  * @head: list head to add item after it
  409.  *
  410.  * Insert a new entry head before the specified head.
  411.  * This is good for implementing stacks.
  412.  * Returns: @newe
  413.  */
  414. static inline struct cyclist *
  415. cyclist_add (struct cyclist *newe, struct cyclist **head)
  416. {
  417.   return cyclist_add_tail(newe, head) ? *head = newe : 0;
  418. }
  419.  
  420. /**
  421.  * cyclist_add_safe - add a new head entry, safety for iteration backward
  422.  * @newe: new entry to be added
  423.  * @head: list head to add it after
  424.  * @s:    pointer to struct cyclist_save which used as the @head list iterator
  425.  *
  426.  * Insert a new entry head before the specified head.
  427.  * Returns: @newe
  428.  */
  429. static inline struct cyclist *
  430. cyclist_add_safe (struct cyclist *newe,
  431.           struct cyclist **head, struct cyclist_save *s)
  432. {
  433.   if (newe && head) {
  434.     cyclist_add_tail(newe, head);
  435.     if (s && s->direction == S_BACKWARD && s->next == 0)
  436.       s->next = newe;
  437.     return *head = newe;
  438.   }
  439.   return 0;
  440. }
  441.  
  442. /**
  443.  * cyclist_insert - add a new entry after old
  444.  * @newe: new entry to be added
  445.  * @old: existed list entry
  446.  *
  447.  * Insert a new entry after the specified one.
  448.  * Returns: @newe
  449.  */
  450. static inline struct cyclist *
  451. cyclist_insert (struct cyclist *newe, struct cyclist *old)
  452. {
  453.   if (old && newe) {
  454.     newe->next = old->next;
  455.     old->next = newe;
  456.     newe->prev = old;
  457.     newe->next->prev = newe;
  458.     return newe;
  459.   } else
  460.     return 0;
  461. }
  462.  
  463. /**
  464.  * cyclist_insert_safe - add a new entry after old
  465.  *                     in foreach_..._safe () { ... }
  466.  * @newe: new entry to be added
  467.  * @old:  existed list entry
  468.  * @s:    pointer to struct cyclist_save which used as the @old list iterator
  469.  *
  470.  * Insert a new entry after the specified one,
  471.  * correct iterator to not miss new entry.
  472.  * Returns: @newe
  473.  */
  474. static inline struct cyclist *
  475. cyclist_insert_safe (struct cyclist *newe,
  476.              struct cyclist *old, struct cyclist_save *s)
  477. {
  478.   if (old && newe) {
  479.     newe->next = old->next;
  480.     old->next = newe;
  481.     newe->prev = old;
  482.     newe->next->prev = newe;
  483.     if (s &&
  484.     ((s->direction == S_FORWARD && s->next == newe->next) ||
  485.      (s->direction == S_BACKWARD && s->next == old)))
  486.       s->next = newe;
  487.     return newe;
  488.   } else
  489.     return 0;
  490. }
  491.  
  492. /**
  493.  * cyclist_remove - delete entry from any list and make it singular.
  494.  * @entry: entry to be deleted
  495.  *
  496.  * If entry is the only head of the list, then other list items will be lost
  497.  * Returns: next entry or NULL if deleted entry was singular list
  498.  */
  499. static inline struct cyclist * cyclist_remove (struct cyclist *entry)
  500. {
  501.   struct cyclist *ret = 0;
  502.   if (entry) {
  503.     ret = entry->next;
  504.  
  505.     entry->prev->next = entry->next;
  506.     entry->next->prev = entry->prev;
  507.     cyclist_singular(entry);
  508.   }
  509.   return ret == entry ? 0 : ret;
  510. }
  511.  
  512. /**
  513.  * cyclist_del - delete entry from list and make it singular.
  514.  * @entry: entry to be deleted
  515.  * @head:  list head to delete it
  516.  *
  517.  * If entry is the head of the list, then next entry became new head
  518.  * Returns: @entry or NULL if cyclist_is_empty(@head)
  519.  */
  520. static inline struct cyclist *
  521. cyclist_del (struct cyclist *entry, struct cyclist **head)
  522. {
  523.   if (!head || !(entry && *head))
  524.     return 0;
  525.   if (*head) {
  526.     struct cyclist *next_entry = cyclist_remove(entry);
  527.     if (*head == entry)
  528.       *head = next_entry;
  529.   }
  530.   return entry;
  531. }
  532.  
  533. /**
  534.  * cyclist_del_safe - delete entry from list and make it singular
  535.  *                  in foreach_..._safe () { ... }
  536.  * @entry: entry to be deleted
  537.  * @head:  list head to delete it
  538.  * @s:     pointer to struct cyclist_save which used as the @head list iterator
  539.  *
  540.  * If entry is the head of the list, then next entry became new head
  541.  * correct iterator to not take deleted entry.
  542.  * Returns: @entry or NULL if cyclist_is_empty(@head)
  543.  */
  544. static inline struct cyclist *
  545. cyclist_del_safe (struct cyclist *entry,
  546.           struct cyclist **head, struct cyclist_save *s)
  547. {
  548.   if (entry) {
  549.     if (head && *head) {
  550.       if (s && s->direction && entry && s->next == entry) {
  551.     if (*head == entry)
  552.       s->direction = 0;
  553.     else
  554.       s->next = (s->direction == S_FORWARD) ? entry->next : entry->prev;
  555.       }
  556.       struct cyclist *next_entry = cyclist_remove(entry);
  557.       if (*head == entry)
  558.     //  *head = next_entry;
  559. #if 1  
  560.     if ((*head = next_entry) == 0 && s)
  561.       //      if (s)
  562.         s->direction = 0;
  563. #endif      
  564.     } else {
  565.       cyclist_remove(entry);
  566.       return 0;
  567.     }
  568.   }
  569.  
  570.   return entry;
  571. }
  572.  
  573. /**
  574.  * cyclist_replace - replace entry in list and make it singular.
  575.  * @newe:  new entry
  576.  * @entry: entry to be deleted
  577.  * @head:  head of list, containing @entry
  578.  *
  579.  * If @entry is the head of the list, then new entry became new head
  580.  * Returns: @newe->next after replace
  581.  */
  582. static inline struct cyclist *
  583. cyclist_replace (struct cyclist *newe, struct cyclist *entry)
  584. {
  585.   if (newe && entry) {
  586.     if (cyclist_is_singular(entry))
  587.       cyclist_singular(newe);
  588.     else {
  589.       *newe = *entry;
  590.       newe->next->prev = newe;
  591.       newe->prev->next = newe;
  592.       cyclist_singular(entry);
  593.     }
  594.    
  595.     return newe->next;
  596.   }
  597.   return 0;
  598. }
  599.  
  600. /**
  601.  * cyclist_replace_safe - replace entry in list and make it singular
  602.  *                  in foreach_..._safe () { ... }
  603.  * @newe:  new entry
  604.  * @entry: entry to be deleted
  605.  * @head:  list head to replace it
  606.  * @s:     pointer to struct cyclist_save which used as the @head list iterator
  607.  *
  608.  * If @entry is the head of the list, then new entry became new head
  609.  * correct iterator to take replaced entry.
  610.  * Returns: @newe->next after replace
  611.  */
  612. static inline struct cyclist *
  613. cyclist_replace_safe (struct cyclist *newe, struct cyclist *entry,
  614.             struct cyclist **head, struct cyclist_save *s)
  615.  
  616. {
  617.   if (newe && entry && head) {
  618.     if (cyclist_is_empty(*head))
  619.       return cyclist_add(newe, head);
  620.     cyclist_replace(newe, entry);
  621.     if (*head == entry)
  622.       *head = newe;
  623.     if (s && s->next == entry)
  624.       s->next = newe;
  625.     return newe->next;
  626.   }
  627.   return 0;
  628. }
  629.  
  630. /**
  631.  * cyclist_move - move entry
  632.  * @entry:  list entry to move
  633.  * @before: list (may be another list) entry to move @entry before it
  634.  *
  635.  * If *@before == NULL move @entry to this new list
  636.  * Returns: initial @entry->next or NULL if moved entry was singular list
  637.  */
  638. static inline struct cyclist *
  639. cyclist_move (struct cyclist *entry, struct cyclist **before)
  640. {
  641.   if (!entry || !before)
  642.     return 0;
  643.   struct cyclist *ret = cyclist_is_singular(entry) ? 0 : entry->next;
  644.  
  645.   if (entry->next != *before) {
  646.     cyclist_remove(entry);
  647.     cyclist_add_tail(entry, before);
  648.   }
  649.   return ret;
  650. }
  651.  
  652. /**
  653.  * cyclist_move_safe - move entry while iterate over it
  654.  * @entry:  list entry to move
  655.  * @head:   iterated list head with @entry
  656.  * @f:      pointer to struct cyclist_save for the @head list iterator
  657.  * @before: list (may be another list) entry to move @entry before it
  658.  * @t:      pointer to struct cyclist_save for the @before list iterator
  659.  *
  660.  * If *@before == NULL move @entry to this new list
  661.  * Returns: initial @entry->next or NULL if moved entry was singular list
  662.  */
  663. static inline struct cyclist *
  664. cyclist_move_safe (struct cyclist *entry,
  665.            struct cyclist **head, struct cyclist_save *f,
  666.            struct cyclist **before, struct cyclist_save *t)
  667. {
  668.   if (!entry || !before)
  669.     return 0;
  670.   struct cyclist *ret = cyclist_is_singular(entry) ? 0 : entry->next;
  671.  
  672.   if (entry->next != *before) {
  673.     cyclist_del_safe(entry, head, f);
  674.     cyclist_add_tail_safe(entry, before, t);
  675.   }
  676.   return ret;
  677. }
  678.  
  679. #define _CL_SWAP(a, b) ({ __typeof__(a) t = a; a = b; b = t; })
  680. /**
  681.  * cyclist_swap - swap 2 elements and its pointers in one or different lists
  682.  * @px:     addr of pointer to first cyclist element
  683.  * @py:     addr of pointer to other cyclist element
  684.  *
  685.  * Content of pointers swaps too, so if @px pointed to `E1` in `list1`
  686.  * and @py pointed to `E2` in `list2`, then after swap
  687.  * @px pointed to `E2` in list1 and @py pointed to `E1` in list2,
  688.  * or in other words, pointers here acts as indices in arrays.
  689.  */
  690. static inline void cyclist_swap (struct cyclist **px, struct cyclist **py)
  691. {
  692.   if (!(*px && *py))
  693.     return;
  694.   struct cyclist *x = *px, *y = *py;
  695.   int sx = cyclist_is_singular(x);
  696.  
  697.   _CL_SWAP(*px, *py); // swap pointers
  698.  
  699.   if ((sx && cyclist_is_singular(y)) // both are singular
  700.       || (x->next == y && y->next == x)) // 2-items list: x -> y
  701.     return;
  702.  
  703.   if (sx || cyclist_is_singular(y)) {
  704.     if (sx) // x singular, y not
  705.       cyclist_replace(x, y);
  706.     else    // y singular
  707.       cyclist_replace(y, x);
  708.     return;
  709.   }
  710.  
  711.   if (x->next == y || y->next == x) { // neighbors
  712.     /*    ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccqwwwwwwvvvvvvaaaaaaaaaaaaaaaaaazzzcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccczzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzmmmmm,+
  713.  
  714.      */ // Desi's comment !!!
  715.     if (y->next == x)
  716.       _CL_SWAP(x, y); // reoder names    
  717.     // ... -> x -> y -> ... x before y
  718.     y->next->prev = x;
  719.     x->prev->next = y;
  720.     x->next = y->next;
  721.     y->prev = x->prev;
  722.     x->prev = y;
  723.     y->next = x;
  724.   } else {
  725.     _CL_SWAP(*x, *y); // SWAP content
  726.     // correct neighbors links
  727.     x->prev->next = x;
  728.     x->next->prev = x;
  729.     y->prev->next = y;
  730.     y->next->prev = y;
  731.   }
  732. }
  733.  
  734.  
  735. /**
  736.  * cyclist_cswap - swap only elements in one or different lists
  737.  * @x:     first cyclist element
  738.  * @y:     other cyclist element
  739.  */
  740. static inline void cyclist_cswap (struct cyclist *x, struct cyclist *y)
  741. {
  742.   cyclist_swap(&x, &y);
  743. }
  744.  
  745. /**
  746.  * cyclist_swap_safe - swap 2 elements in one or different lists
  747.  * @px:     addr of pointer to first cyclist element
  748.  * @xhead:  iterated list head with @px
  749.  * @sx:     pointer to struct cyclist_save for the @xhead list iterator
  750.  * @py:     addr of pointer to other cyclist element
  751.  * @yhead:  iterated list head with @py
  752.  * @sy:     pointer to struct cyclist_save for the @yhead list iterator
  753.  *
  754.  * lists headers and iterators may be NULL
  755.  */
  756. static inline void
  757. cyclist_swap_safe (struct cyclist **px,
  758.            struct cyclist **xhead, struct cyclist_save *sx,
  759.            struct cyclist **py,
  760.            struct cyclist **yhead, struct cyclist_save *sy)
  761. {
  762. #define CORRECT(ptr, pval, x, y) if (ptr && pval == x) pval = y;
  763.   CORRECT(xhead, *xhead, *px, *py);
  764.   CORRECT(yhead, *yhead, *py, *px);
  765.   CORRECT(sx, sx->next, *px, *py);
  766.   CORRECT(sy, sy->next, *py, *px);
  767. #undef CORRECT
  768.   cyclist_swap(px, py);
  769. }
  770.  
  771. /**
  772.  * cyclist_split - cut a list into two
  773.  * @head: source list
  774.  * @newl: entry in source list, cut point
  775.  *
  776.  * New list contains entries from @newl to @head->prev (inclusive)
  777.  * Old list contains entries from @head to @newl->prev (inclusive)
  778.  * Returns: new list (@newl) or NULL, if @head == @newl
  779.  */
  780. static inline struct cyclist *
  781. cyclist_split (struct cyclist *head, struct cyclist *newl)
  782. {
  783.   struct cyclist *ret = 0, *tail;
  784.   if (head && newl && head != newl) {
  785.     ret = newl;
  786.     tail = newl->prev;
  787.     newl->prev = head->prev;
  788.     newl->prev->next = newl;
  789.     head->prev = tail;
  790.     tail->next = head;
  791.   }
  792.   return ret;
  793. }
  794.  
  795. /**
  796.  * cyclist_join - join 2 lists
  797.  * @head: old list
  798.  * @newl: list append to the tail of old
  799.  *
  800.  * Note: @newl must be not in old list
  801.  * Returns: joined list (@head)
  802.  */
  803. static inline struct cyclist *
  804. cyclist_join (struct cyclist *head, struct cyclist *newl)
  805. {
  806.   struct cyclist *tail;
  807.   if (head && newl && head != newl) {
  808.     tail = head->prev;
  809.     tail->next = newl;
  810.     head->prev = newl->prev;
  811.     newl->prev = tail;
  812.     head->prev->next = head;
  813.   }
  814.   return head;
  815. }
  816.  
  817. /**
  818.  * cyclist_cut - convert circular list to linear 2-links list
  819.  * @head:    cyclist element
  820.  * @l2head:  pointer to usual (or linux) double linked list head {head, tail}
  821.  * @how:     0 -- make usual 2-linked list, other -- make linux/list.h list
  822.  *
  823.  * Returns:  pointer to first item in new 2-linked list
  824.  */
  825. static inline struct cyclist *cyclist_cut (struct cyclist *head,
  826.                        struct cyclist *l2head, int how)
  827. {
  828.   struct cyclist *ptr = how ? l2head : 0;
  829.  
  830.   if (head) {
  831.     l2head->next = head;
  832.     l2head->prev = head->prev;
  833.     head->prev = ptr;
  834.     l2head->prev->next = ptr;
  835.   } else if (ptr)
  836.     l2head->next = l2head->prev = ptr;
  837.   else
  838.     *l2head = (__typeof__ (*l2head)){0};
  839.  
  840.   return head;
  841. }
  842.  
  843. /**
  844.  * cyclist_roll make circular list from linear 2-links list
  845.  * @l2head:  pointer to usual (or linux) double linked list head {head, tail}
  846.  *
  847.  * Returns:  pointer to first item in new cyclist
  848.  */
  849. static inline struct cyclist *cyclist_roll (struct cyclist *l2head)
  850. {
  851.   if (l2head && l2head->next && l2head->next != l2head) {
  852.     l2head->next->prev = l2head->prev;
  853.     return l2head->prev->next = l2head->next;
  854.   } else
  855.     return 0;
  856. }
  857.  
  858. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement