Update all #include directives to the currently preferred style.
[pspp-builds.git] / src / libpspp / ll.h
index 4414e2e68e0a19b4d2bae59fb433be693e278792..9e6e35f04ad72ee4af7264883cf080bc49359ad0 100644 (file)
@@ -1,21 +1,18 @@
-/* PSPP - computes sample statistics.
-   Copyright (C) 2006 Free Software Foundation, Inc.
-   Written by Ben Pfaff <blp@gnu.org>.
+/* PSPP - a program for statistical analysis.
+   Copyright (C) 2006, 2009, 2011 Free Software Foundation, Inc.
 
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 /* Circular doubly linked lists.
 
@@ -53,6 +50,7 @@
 #include <assert.h>
 #include <stdbool.h>
 #include <stddef.h>
+#include "libpspp/cast.h"
 
 /* Embedded, circular doubly linked list.
 
      ll_for_each (foo, struct foo, ll, &list)
        {
          ...do something with foo->x...
-       }      
+       }
 */
 
 /* Returns the data structure corresponding to the given node LL,
    assuming that LL is embedded as the given MEMBER name in data
    type STRUCT. */
-#define ll_data(LL, STRUCT, MEMBER)                                    \
-        ((STRUCT *) ((char *) (LL) - offsetof (STRUCT, MEMBER)))
+#define ll_data(LL, STRUCT, MEMBER)                     \
+        (CHECK_POINTER_HAS_TYPE(LL, struct ll *),       \
+         UP_CAST(LL, STRUCT, MEMBER))
 
 /* Linked list node. */
 struct ll
@@ -229,7 +228,8 @@ struct ll *ll_find_partition (const struct ll *r0, const struct ll *r1,
 \f
 /* Iteration helper macros. */
 
-/* Sets DATA to each object in LIST in turn, assuming that each
+/* Sets DATA to each object in LIST in turn, in forward or
+   reverse order, assuming that each
    `struct ll' in LIST is embedded as the given MEMBER name in
    data type STRUCT.
 
@@ -239,11 +239,16 @@ struct ll *ll_find_partition (const struct ll *r0, const struct ll *r1,
         for (DATA = ll_head__ (STRUCT, MEMBER, LIST);           \
              DATA != NULL;                                      \
              DATA = ll_next__ (DATA, STRUCT, MEMBER, LIST))
+#define ll_for_each_reverse(DATA, STRUCT, MEMBER, LIST)         \
+        for (DATA = ll_tail__ (STRUCT, MEMBER, LIST);           \
+             DATA != NULL;                                      \
+             DATA = ll_prev__ (DATA, STRUCT, MEMBER, LIST))
 
 /* Continues a iteration of LIST, starting from the object
-   currently in DATA and continuing forward through the remainder
-   of the list, assuming that each `struct ll' in LIST is
-   embedded as the given MEMBER name in data type STRUCT.
+   currently in DATA and continuing, in forward or reverse order,
+   through the remainder of the list, assuming that each `struct
+   ll' in LIST is embedded as the given MEMBER name in data type
+   STRUCT.
 
    Behavior is undefined if DATA is removed from the list between
    loop iterations. */
@@ -251,11 +256,15 @@ struct ll *ll_find_partition (const struct ll *r0, const struct ll *r1,
         for (;                                                  \
              DATA != NULL;                                      \
              DATA = ll_next__ (DATA, STRUCT, MEMBER, LIST))
+#define ll_for_each_reverse_continue(DATA, STRUCT, MEMBER, LIST)        \
+        for (;                                                          \
+             DATA != NULL;                                              \
+             DATA = ll_prev__ (DATA, STRUCT, MEMBER, LIST))
 
-/* Sets DATA to each object in LIST in turn, assuming that each
-   `struct ll' in LIST is embedded as the given MEMBER name in
-   data type STRUCT.  NEXT must be another variable of the same
-   type as DATA.
+/* Sets DATA to each object in LIST in turn, in forward or
+   reverse order, assuming that each `struct ll' in LIST is
+   embedded as the given MEMBER name in data type STRUCT.  NEXT
+   (or PREV) must be another variable of the same type as DATA.
 
    Behavior is well-defined even if DATA is removed from the list
    between iterations. */
@@ -265,12 +274,19 @@ struct ll *ll_find_partition (const struct ll *r0, const struct ll *r1,
               ? (NEXT = ll_next__ (DATA, STRUCT, MEMBER, LIST), 1)      \
               : 0);                                                     \
              DATA = NEXT)
+#define ll_for_each_reverse_safe(DATA, PREV, STRUCT, MEMBER, LIST)      \
+        for (DATA = ll_tail__ (STRUCT, MEMBER, LIST);                   \
+             (DATA != NULL                                              \
+              ? (PREV = ll_prev__ (DATA, STRUCT, MEMBER, LIST), 1)      \
+              : 0);                                                     \
+             DATA = PREV)
 
-/* Continues a iteration of LIST, starting from the object
-   currently in DATA and continuing forward through the remainder
-   of the list, assuming that each `struct ll' in LIST is
-   embedded as the given MEMBER name in data type STRUCT.  NEXT
-   must be another variable of the same type as DATA.
+/* Continues a iteration of LIST, in forward or reverse order,
+   starting from the object currently in DATA and continuing
+   forward through the remainder of the list, assuming that each
+   `struct ll' in LIST is embedded as the given MEMBER name in
+   data type STRUCT.  NEXT (or PREV) must be another variable of
+   the same type as DATA.
 
    Behavior is well-defined even if DATA is removed from the list
    between iterations. */
@@ -280,43 +296,57 @@ struct ll *ll_find_partition (const struct ll *r0, const struct ll *r1,
               ? (NEXT = ll_next__ (DATA, STRUCT, MEMBER, LIST), 1)      \
               : 0);                                                     \
              DATA = NEXT)
+#define ll_for_each_safe_reverse_continue(DATA, PREV, STRUCT, MEMBER, LIST) \
+        for (;                                                          \
+             (DATA != NULL                                              \
+              ? (PREV = ll_prev__ (DATA, STRUCT, MEMBER, LIST), 1)      \
+              : 0);                                                     \
+             DATA = PREV)
 
-/* Sets DATA to each object in LIST in turn, assuming that each
-   `struct ll' in LIST is embedded as the given MEMBER name in
-   data type STRUCT.
+/* Sets DATA to each object in LIST in turn, in forward or
+   reverse order, assuming that each `struct ll' in LIST is
+   embedded as the given MEMBER name in data type STRUCT.
    Each object is removed from LIST before its loop iteration. */
 #define ll_for_each_preremove(DATA, STRUCT, MEMBER, LIST)                 \
         while (!ll_is_empty (LIST)                                        \
                ? (DATA = ll_data (ll_pop_head (LIST), STRUCT, MEMBER), 1) \
                : 0)
+#define ll_for_each_reverse_preremove(DATA, STRUCT, MEMBER, LIST)         \
+        while (!ll_is_empty (LIST)                                        \
+               ? (DATA = ll_data (ll_pop_tail (LIST), STRUCT, MEMBER), 1) \
+               : 0)
 
-/* Sets DATA to each object in LIST in turn, assuming that each
-   `struct ll' in LIST is embedded as the given MEMBER name in
-   data type STRUCT.
+/* Sets DATA to each object in LIST in turn, in forward or
+   reverse order, assuming that each `struct ll' in LIST is
+   embedded as the given MEMBER name in data type STRUCT.
    At the end of each loop iteration, DATA is removed from the
    list. */
 #define ll_for_each_postremove(DATA, STRUCT, MEMBER, LIST)      \
         for (;                                                  \
              (DATA = ll_head__ (STRUCT, MEMBER, LIST)) != NULL; \
              ll_remove (&DATA->MEMBER))
+#define ll_for_each_reverse_postremove(DATA, STRUCT, MEMBER, LIST)      \
+        for (;                                                          \
+             (DATA = ll_tail__ (STRUCT, MEMBER, LIST)) != NULL;         \
+             ll_remove (&DATA->MEMBER))
 
 /* Macros for internal use only. */
 #define ll_data__(LL, STRUCT, MEMBER, LIST)                             \
         ((LL) != ll_null (LIST) ? ll_data (LL, STRUCT, MEMBER) : NULL)
 #define ll_head__(STRUCT, MEMBER, LIST)                         \
         ll_data__ (ll_head (LIST), STRUCT, MEMBER, LIST)
+#define ll_tail__(STRUCT, MEMBER, LIST)                         \
+        ll_data__ (ll_tail (LIST), STRUCT, MEMBER, LIST)
 #define ll_next__(DATA, STRUCT, MEMBER, LIST)                           \
-        ll_data__ (ll_next (&DATA->MEMBER), STRUCT, MEMBER, LIST)
-#define ll_remove__(DATA, STRUCT, MEMBER, LIST)                         \
-        (ll_next (&DATA->MEMBER) == ll_null (LIST)                      \
-         ? ll_remove (&DATA->MEMBER), NULL                              \
-         : ll_data (ll_remove (&DATA->MEMBER), STRUCT, MEMBER))       
+        ll_data__ (ll_next (&(DATA)->MEMBER), STRUCT, MEMBER, LIST)
+#define ll_prev__(DATA, STRUCT, MEMBER, LIST)                           \
+        ll_data__ (ll_prev (&(DATA)->MEMBER), STRUCT, MEMBER, LIST)
 \f
 /* Inline functions. */
 
 /* Initializes LIST as an empty list. */
 static inline void
-ll_init (struct ll_list *list) 
+ll_init (struct ll_list *list)
 {
   list->null.next = list->null.prev = &list->null;
 }
@@ -325,7 +355,7 @@ ll_init (struct ll_list *list)
    false if LIST is not empty (has at least one other node).
    Executes in O(1) time. */
 static inline bool
-ll_is_empty (const struct ll_list *list) 
+ll_is_empty (const struct ll_list *list)
 {
   return ll_head (list) == ll_null (list);
 }
@@ -341,23 +371,23 @@ ll_head (const struct ll_list *list)
 /* Returns the last node in LIST,
    or the null node if LIST is empty. */
 static inline struct ll *
-ll_tail (const struct ll_list *list) 
+ll_tail (const struct ll_list *list)
 {
   return ll_prev (ll_null (list));
 }
 
 /* Returns LIST's null node. */
 static inline struct ll *
-ll_null (const struct ll_list *list) 
+ll_null (const struct ll_list *list)
 {
-  return (struct ll *) &list->null;
+  return CONST_CAST (struct ll *, &list->null);
 }
 
 /* Returns the node following LL in its list,
    or the null node if LL is at the end of its list.
    (In an empty list, the null node follows itself.) */
 static inline struct ll *
-ll_next (const struct ll *ll) 
+ll_next (const struct ll *ll)
 {
   return ll->next;
 }
@@ -373,14 +403,14 @@ ll_prev (const struct ll *ll)
 
 /* Inserts LL at the head of LIST. */
 static inline void
-ll_push_head (struct ll_list *list, struct ll *ll) 
+ll_push_head (struct ll_list *list, struct ll *ll)
 {
   ll_insert (ll_head (list), ll);
 }
 
 /* Inserts LL at the tail of LIST. */
 static inline void
-ll_push_tail (struct ll_list *list, struct ll *ll) 
+ll_push_tail (struct ll_list *list, struct ll *ll)
 {
   ll_insert (ll_null (list), ll);
 }
@@ -400,7 +430,7 @@ ll_pop_head (struct ll_list *list)
 /* Removes and returns the last node in LIST,
    which must not be empty. */
 static inline struct ll *
-ll_pop_tail (struct ll_list *list) 
+ll_pop_tail (struct ll_list *list)
 {
   struct ll *tail;
   assert (!ll_is_empty (list));
@@ -412,7 +442,7 @@ ll_pop_tail (struct ll_list *list)
 /* Inserts NEW_ELEM just before BEFORE.
    (NEW_ELEM must not already be in a list.) */
 static inline void
-ll_insert (struct ll *before, struct ll *new_elem) 
+ll_insert (struct ll *before, struct ll *new_elem)
 {
   struct ll *before_prev = ll_prev (before);
   new_elem->next = before;
@@ -433,9 +463,9 @@ ll_remove (struct ll *ll)
 
 /* Removes R0...R1 from their list. */
 static inline void
-ll_remove_range (struct ll *r0, struct ll *r1) 
+ll_remove_range (struct ll *r0, struct ll *r1)
 {
-  if (r0 != r1) 
+  if (r0 != r1)
     {
       r1 = r1->prev;
       r0->prev->next = r1->next;
@@ -449,7 +479,7 @@ ll_remove_range (struct ll *r0, struct ll *r1)
    before moving LL, then ll_insert() afterward, but more
    efficient. */
 static inline void
-ll_moved (struct ll *ll) 
+ll_moved (struct ll *ll)
 {
   ll->prev->next = ll->next->prev = ll;
 }