From 1e84687727b7fdad133fd7d2b9b20737fb13e167 Mon Sep 17 00:00:00 2001
From: Ben Pfaff <blp@gnu.org>
Date: Sun, 7 May 2006 05:44:18 +0000
Subject: [PATCH] Improvements to case implementation.

---
 src/data/ChangeLog |  7 +++++++
 src/data/case.c    | 36 ++++++++++++++++++------------------
 src/data/case.h    | 19 ++++++++++++-------
 3 files changed, 37 insertions(+), 25 deletions(-)

diff --git a/src/data/ChangeLog b/src/data/ChangeLog
index 6277dbe5..c3b44971 100644
--- a/src/data/ChangeLog
+++ b/src/data/ChangeLog
@@ -1,3 +1,10 @@
+Sat May  6 22:42:23 2006  Ben Pfaff  <blp@gnu.org>
+
+	* case.c (case_move): Do nothing if dst and src are the same
+	object.
+	(case_try_create) Merge two similar cases.
+	(case_copy) Unshare only if data must be actually copied.
+
 Sun May  7 10:04:06 WST 2006 John Darrington <john@darrington.wattle.id.au>
 
 	* data-in.c data-out.c dictionary.c sys-file-reader.c
diff --git a/src/data/case.c b/src/data/case.c
index eab966b1..6e65af97 100644
--- a/src/data/case.c
+++ b/src/data/case.c
@@ -126,10 +126,13 @@ case_move (struct ccase *dst, struct ccase *src)
   assert (src->case_data != NULL);
   assert (src->case_data->ref_cnt > 0);
   assert (dst != NULL);
-
-  *dst = *src;
-  dst->this = dst;
-  case_nullify (src);
+  
+  if (dst != src) 
+    {
+      *dst = *src;
+      dst->this = dst;
+      case_nullify (src); 
+    }
 }
 #endif /* DEBUGGING */
 
@@ -180,23 +183,18 @@ case_swap (struct ccase *a, struct ccase *b)
 int
 case_try_create (struct ccase *c, size_t value_cnt) 
 {
+#ifdef DEBUGGING
+  c->this = c;
+#endif
   c->case_data = malloc (case_size (value_cnt));
   if (c->case_data != NULL) 
     {
-#ifdef DEBUGGING
-      c->this = c;
-#endif
       c->case_data->value_cnt = value_cnt;
       c->case_data->ref_cnt = 1;
       return 1;
     }
   else 
-    {
-#ifdef DEBUGGING
-      c->this = c;
-#endif
-      return 0;
-    }
+    return 0;
 }
 
 /* Tries to initialize CLONE as a copy of ORIG.
@@ -229,12 +227,14 @@ case_copy (struct ccase *dst, size_t dst_idx,
   assert (src->case_data->ref_cnt > 0);
   assert (src_idx + value_cnt <= dst->case_data->value_cnt);
 
-  if (dst->case_data->ref_cnt > 1)
-    case_unshare (dst);
   if (dst->case_data != src->case_data || dst_idx != src_idx) 
-    memmove (dst->case_data->values + dst_idx,
-             src->case_data->values + src_idx,
-             sizeof *dst->case_data->values * value_cnt); 
+    {
+      if (dst->case_data->ref_cnt > 1)
+        case_unshare (dst);
+      memmove (dst->case_data->values + dst_idx,
+               src->case_data->values + src_idx,
+               sizeof *dst->case_data->values * value_cnt); 
+    }
 }
 #endif /* DEBUGGING */
 
diff --git a/src/data/case.h b/src/data/case.h
index bdb68dc6..05c4568c 100644
--- a/src/data/case.h
+++ b/src/data/case.h
@@ -115,8 +115,11 @@ case_clone (struct ccase *clone, const struct ccase *orig)
 static inline void
 case_move (struct ccase *dst, struct ccase *src) 
 {
-  *dst = *src;
-  src->case_data = NULL;
+  if (dst != src) 
+    {
+      *dst = *src;
+      src->case_data = NULL; 
+    }
 }
 
 static inline void
@@ -132,12 +135,14 @@ case_copy (struct ccase *dst, size_t dst_idx,
            const struct ccase *src, size_t src_idx,
            size_t value_cnt) 
 {
-  if (dst->case_data->ref_cnt > 1)
-    case_unshare (dst);
   if (dst->case_data != src->case_data || dst_idx != src_idx) 
-    memmove (dst->case_data->values + dst_idx,
-             src->case_data->values + src_idx,
-             sizeof *dst->case_data->values * value_cnt); 
+    {
+      if (dst->case_data->ref_cnt > 1)
+        case_unshare (dst);
+      memmove (dst->case_data->values + dst_idx,
+               src->case_data->values + src_idx,
+               sizeof *dst->case_data->values * value_cnt); 
+    }
 }
 
 static inline void
-- 
2.30.2