From c489ad9041918ca8c80dadceade988daab1d25f8 Mon Sep 17 00:00:00 2001
From: Ben Pfaff <blp@gnu.org>
Date: Sat, 6 May 2006 22:37:30 +0000
Subject: [PATCH] Make dictionary compacting functions a little more general.

---
 src/data/ChangeLog         | 16 ++++++++++++++++
 src/data/dictionary.c      | 31 ++++++++++++++++++++++++++++---
 src/data/dictionary.h      |  3 ++-
 src/data/procedure.c       |  8 ++++----
 src/data/scratch-writer.c  |  2 +-
 src/data/sys-file-writer.c | 24 +-----------------------
 6 files changed, 52 insertions(+), 32 deletions(-)

diff --git a/src/data/ChangeLog b/src/data/ChangeLog
index c643a2eb2d..1b91918686 100644
--- a/src/data/ChangeLog
+++ b/src/data/ChangeLog
@@ -1,3 +1,19 @@
+Sat May  6 15:36:59 2006  Ben Pfaff  <blp@gnu.org>
+
+	Make dictionary compacting functions a little more general.
+	
+	* sys-file-writer.c (sfm_open_writer): Use
+	dict_compacting_would_change().
+	(does_dict_need_translation) Removed.
+
+Sat May  6 15:35:42 2006  Ben Pfaff  <blp@gnu.org>
+
+	Make dictionary compacting functions a little more general.
+	
+	* dictionary.c (dict_needs_compaction): Rename
+	dict_compacting_would_shrink().  Update all callers.
+	(dict_compacting_would_change) New function.
+	
 Sat May  6 14:25:49 2006  Ben Pfaff  <blp@gnu.org>
 
 	* sys-file-writer.c: (does_dict_need_translation) Fix bug:
diff --git a/src/data/dictionary.c b/src/data/dictionary.c
index e9a5a01450..f74ae00761 100644
--- a/src/data/dictionary.c
+++ b/src/data/dictionary.c
@@ -855,19 +855,44 @@ dict_get_compacted_idx_to_fv (const struct dictionary *d)
 }
 
 /* Returns true if a case for dictionary D would be smaller after
-   compaction, false otherwise.  Compacting a case eliminates
+   compacting, false otherwise.  Compacting a case eliminates
    "holes" between values and after the last value.  Holes are
    created by deleting variables (or by scratch variables).
 
    The return value may differ from whether compacting a case
-   from dictionary D would *change* the case: compaction could
+   from dictionary D would *change* the case: compacting could
    rearrange values even if it didn't reduce space
    requirements. */
 bool
-dict_needs_compaction (const struct dictionary *d) 
+dict_compacting_would_shrink (const struct dictionary *d) 
 {
   return dict_get_compacted_value_cnt (d) < dict_get_next_value_idx (d);
 }
+
+/* Returns true if a case for dictionary D would be smaller after
+   compacting, false otherwise.  Compacting a case eliminates
+   "holes" between values and after the last value.  Holes are
+   created by deleting variables (or by scratch variables).
+
+   The return value may differ from whether compacting a case
+   from dictionary D would *shrink* the case: compacting could
+   rearrange values without reducing space requirements. */
+bool
+dict_compacting_would_change (const struct dictionary *d) 
+{
+  size_t case_idx;
+  size_t i;
+
+  case_idx = 0;
+  for (i = 0; i < dict_get_var_cnt (d); i++) 
+    {
+      struct variable *v = dict_get_var (d, i);
+      if (v->fv != case_idx)
+        return true;
+      case_idx += v->nv;
+    }
+  return false;
+}
 
 /* How to copy a contiguous range of values between cases. */
 struct copy_map
diff --git a/src/data/dictionary.h b/src/data/dictionary.h
index 50c9db578d..1a7e8de059 100644
--- a/src/data/dictionary.h
+++ b/src/data/dictionary.h
@@ -83,7 +83,8 @@ size_t dict_get_case_size (const struct dictionary *);
 void dict_compact_values (struct dictionary *);
 size_t dict_get_compacted_value_cnt (const struct dictionary *);
 int *dict_get_compacted_idx_to_fv (const struct dictionary *);
-bool dict_needs_compaction (const struct dictionary *);
+bool dict_compacting_would_shrink (const struct dictionary *);
+bool dict_compacting_would_change (const struct dictionary *);
 
 struct dict_compactor *dict_make_compactor (const struct dictionary *);
 void dict_compactor_compact (const struct dict_compactor *,
diff --git a/src/data/procedure.c b/src/data/procedure.c
index 16dcd74592..1b1e628b1e 100644
--- a/src/data/procedure.c
+++ b/src/data/procedure.c
@@ -47,7 +47,7 @@ struct write_case_data
 
     struct ccase trns_case;     /* Case used for transformations. */
     struct ccase sink_case;     /* Case written to sink, if
-                                   compaction is necessary. */
+                                   compacting is necessary. */
     size_t cases_written;       /* Cases output so far. */
   };
 
@@ -281,8 +281,8 @@ open_active_file (void)
   if (permanent_dict == NULL)
     permanent_dict = default_dict;
 
-  /* Figure out compaction. */
-  compactor = (dict_needs_compaction (permanent_dict)
+  /* Figure out whether to compact. */
+  compactor = (dict_compacting_would_shrink (permanent_dict)
                ? dict_make_compactor (permanent_dict)
                : NULL);
 
@@ -411,7 +411,7 @@ close_active_file (void)
   /* Dictionary from before TEMPORARY becomes permanent. */
   proc_cancel_temporary_transformations ();
 
-  /* Finish compaction. */
+  /* Finish compacting. */
   if (compactor != NULL) 
     {
       dict_compactor_destroy (compactor);
diff --git a/src/data/scratch-writer.c b/src/data/scratch-writer.c
index 8bedef6567..4802f4b203 100644
--- a/src/data/scratch-writer.c
+++ b/src/data/scratch-writer.c
@@ -61,7 +61,7 @@ scratch_writer_open (struct file_handle *fh,
 
   /* Copy the dictionary and compact if needed. */
   scratch_dict = dict_clone (dictionary);
-  if (dict_needs_compaction (scratch_dict)) 
+  if (dict_compacting_would_shrink (scratch_dict)) 
     {
       compactor = dict_make_compactor (scratch_dict);
       dict_compact_values (scratch_dict);
diff --git a/src/data/sys-file-writer.c b/src/data/sys-file-writer.c
index fe2c27c810..11ca1ef9fe 100644
--- a/src/data/sys-file-writer.c
+++ b/src/data/sys-file-writer.c
@@ -103,7 +103,6 @@ static void write_variable_display_parameters (struct sfm_writer *w,
                                                const struct dictionary *dict);
 
 static void write_documents (struct sfm_writer *, const struct dictionary *);
-static int does_dict_need_translation (const struct dictionary *);
 
 static inline int
 var_flt64_cnt (const struct variable *v) 
@@ -194,7 +193,7 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d,
   w->fh = fh;
   w->file = fdopen (fd, "w");
 
-  w->needs_translation = does_dict_need_translation (d);
+  w->needs_translation = dict_compacting_would_change (d);
   w->compress = opts.compress;
   w->case_cnt = 0;
   w->flt64_cnt = 0;
@@ -328,27 +327,6 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d,
   goto error;
 }
 
-/* Returns zero if dictionary D's cases are ordered in the
-   natural manner, with the first variable followed by the
-   second, and so on,
-   nonzero otherwise. */
-static int
-does_dict_need_translation (const struct dictionary *d)
-{
-  size_t case_idx;
-  size_t i;
-
-  case_idx = 0;
-  for (i = 0; i < dict_get_var_cnt (d); i++) 
-    {
-      struct variable *v = dict_get_var (d, i);
-      if (v->fv != case_idx)
-        return 1;
-      case_idx += v->nv;
-    }
-  return 0;
-}
-
 /* Returns value of X truncated to two least-significant digits. */
 static int
 rerange (int x)
-- 
2.30.2