treewide: Replace <name>_cnt by n_<name>s and <name>_cap by allocated_<name>.
[pspp] / src / libpspp / llx.c
index 5188d38ec71cd8673ff5b774946651829309f696..eb4e5d400f10a9ae8d86da310109a3819516907a 100644 (file)
@@ -1,20 +1,18 @@
-/* PSPP - computes sample statistics.
-   Copyright (C) 2006 Free Software Foundation, Inc.
+/* 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/>. */
 
 /* External, circular doubly linked list. */
 
@@ -30,9 +28,8 @@
 #include <config.h>
 #endif
 
-#include <libpspp/llx.h>
-#include "compiler.h"
-#include <assert.h>
+#include "libpspp/llx.h"
+#include "libpspp/compiler.h"
 #include <stdlib.h>
 
 /* Destroys LIST and frees all of its nodes using MANAGER.
@@ -167,7 +164,7 @@ llx_remove_range (struct llx *r0, struct llx *r1,
 {
   struct llx *llx;
 
-  for (llx = r0; llx != r1; )
+  for (llx = r0; llx != r1;)
     llx = llx_remove (llx, manager);
 }
 
@@ -184,7 +181,7 @@ llx_remove_equal (struct llx *r0, struct llx *r1, const void *target,
   size_t count;
 
   count = 0;
-  for (x = r0; x != r1; )
+  for (x = r0; x != r1;)
     if (compare (llx_data (x), target, aux) == 0)
       {
         x = llx_remove (x, manager);
@@ -209,7 +206,7 @@ llx_remove_if (struct llx *r0, struct llx *r1,
   size_t count;
 
   count = 0;
-  for (x = r0; x != r1; )
+  for (x = r0; x != r1;)
     if (predicate (llx_data (x), aux))
       {
         x = llx_remove (x, manager);
@@ -221,6 +218,20 @@ llx_remove_if (struct llx *r0, struct llx *r1,
   return count;
 }
 
+/* Returns the first node in R0...R1 that has data TARGET.
+   Returns NULL if no node in R0...R1 equals TARGET. */
+struct llx *
+llx_find (const struct llx *r0, const struct llx *r1, const void *target)
+{
+  const struct llx *x;
+
+  for (x = r0; x != r1; x = llx_next (x))
+    if (llx_data (x) == target)
+      return CONST_CAST (struct llx *, x);
+
+  return NULL;
+}
+
 /* Returns the first node in R0...R1 that equals TARGET
    according to COMPARE given auxiliary data AUX.
    Returns R1 if no node in R0...R1 equals TARGET. */
@@ -234,7 +245,7 @@ llx_find_equal (const struct llx *r0, const struct llx *r1,
   for (x = r0; x != r1; x = llx_next (x))
     if (compare (llx_data (x), target, aux) == 0)
       break;
-  return (struct llx *) x;
+  return CONST_CAST (struct llx *, x);
 }
 
 /* Returns the first node in R0...R1 for which PREDICATE returns
@@ -250,7 +261,7 @@ llx_find_if (const struct llx *r0, const struct llx *r1,
   for (x = r0; x != r1; x = llx_next (x))
     if (predicate (llx_data (x), aux))
       break;
-  return (struct llx *) x;
+  return CONST_CAST (struct llx *, x);
 }
 
 /* Compares each pair of adjacent nodes in R0...R1
@@ -268,10 +279,10 @@ llx_find_adjacent_equal (const struct llx *r0, const struct llx *r1,
 
       for (x = r0, y = llx_next (x); y != r1; x = y, y = llx_next (y))
         if (compare (llx_data (x), llx_data (y), aux) == 0)
-          return (struct llx *) x;
+          return CONST_CAST (struct llx *, x);
     }
 
-  return (struct llx *) r1;
+  return CONST_CAST (struct llx *, r1);
 }
 
 /* Returns the number of nodes in R0...R1.
@@ -331,7 +342,7 @@ llx_max (const struct llx *r0, const struct llx *r1,
         if (compare (llx_data (x), llx_data (max), aux) > 0)
           max = x;
     }
-  return (struct llx *) max;
+  return CONST_CAST (struct llx *, max);
 }
 
 /* Returns the least node in R0...R1 according to COMPARE given
@@ -350,7 +361,7 @@ llx_min (const struct llx *r0, const struct llx *r1,
         if (compare (llx_data (x), llx_data (min), aux) < 0)
           min = x;
     }
-  return (struct llx *) min;
+  return CONST_CAST (struct llx *, min);
 }
 
 /* Lexicographically compares A0...A1 to B0...B1.
@@ -482,7 +493,7 @@ void
 llx_sort (struct llx *r0, struct llx *r1, llx_compare_func *compare, void *aux)
 {
   struct llx *pre_r0;
-  size_t output_run_cnt;
+  size_t output_run_len;
 
   if (r0 == r1 || llx_next (r0) == r1)
     return;
@@ -491,7 +502,7 @@ llx_sort (struct llx *r0, struct llx *r1, llx_compare_func *compare, void *aux)
   do
     {
       struct llx *a0 = llx_next (pre_r0);
-      for (output_run_cnt = 1; ; output_run_cnt++)
+      for (output_run_len = 1; ; output_run_len++)
         {
           struct llx *a1 = llx_find_run (a0, r1, compare, aux);
           struct llx *a2 = llx_find_run (a1, r1, compare, aux);
@@ -501,7 +512,7 @@ llx_sort (struct llx *r0, struct llx *r1, llx_compare_func *compare, void *aux)
           a0 = llx_merge (a0, a1, a1, a2, compare, aux);
         }
     }
-  while (output_run_cnt > 1);
+  while (output_run_len > 1);
 }
 
 /* Finds the extent of a run of nodes of increasing value
@@ -523,7 +534,7 @@ llx_find_run (const struct llx *r0, const struct llx *r1,
                                   llx_data (r0), aux) <= 0);
     }
 
-  return (struct llx *) r0;
+  return CONST_CAST (struct llx *, r0);
 }
 
 /* Merges B0...B1 into A0...A1 according to COMPARE given
@@ -736,7 +747,7 @@ llx_find_partition (const struct llx *r0, const struct llx *r1,
     if (predicate (llx_data (x), aux))
       return NULL;
 
-  return (struct llx *) partition;
+  return CONST_CAST (struct llx *, partition);
 }
 \f
 /* Allocates and returns a node using malloc. */