changeset 10937:f42e8c6196c3

tweaks in concatenation of empty structs
author Jaroslav Hajek <highegg@gmail.com>
date Wed, 01 Sep 2010 12:41:05 +0200
parents 1d761a30c3fb
children 300636502235
files src/ChangeLog src/oct-map.cc src/pt-mat.cc
diffstat 3 files changed, 72 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Wed Sep 01 08:49:57 2010 +0200
+++ b/src/ChangeLog	Wed Sep 01 12:41:05 2010 +0200
@@ -1,3 +1,12 @@
+2010-09-01  Jaroslav Hajek  <highegg@gmail.com>
+
+	* oct-map.cc (octave_map::cat): Search first for an index with 
+	nonzero nfields and use it for comparisons.
+	(permute_to_correct_order1): New overloaded helper func.
+	(permute_to_correct_order): use it here. Pass the index here.
+	* pt-mat.cc (single_type_concat): Don't subst 0x0 for all empty
+	arrays. Use the respective cat() capability instead.
+
 2010-09-01  Jaroslav Hajek  <highegg@gmail.com>
 
 	* DLD-FUNCTIONS/dlmread.cc (Fdlmread): Support "emptyvalue" option.
--- a/src/oct-map.cc	Wed Sep 01 08:49:57 2010 +0200
+++ b/src/oct-map.cc	Wed Sep 01 12:41:05 2010 +0200
@@ -618,18 +618,39 @@
     }
 }
 
+// This is just a wrapper.
+void permute_to_correct_order1 (const octave_scalar_map& ref, const octave_scalar_map& src,
+                                octave_scalar_map& dest, Array<octave_idx_type>& perm)
+{
+  dest = src.orderfields (ref, perm);
+}
+
+// In non-scalar case, we also promote empty structs without fields.
+void permute_to_correct_order1 (const octave_map& ref, const octave_map& src,
+                                octave_map& dest, Array<octave_idx_type>& perm)
+{
+  if (src.nfields () == 0 && src.is_empty ())
+     dest = octave_map (src.dims (), ref.keys ());
+  else
+     dest = src.orderfields (ref, perm);
+}
+
 template <class map>
 static void
 permute_to_correct_order (octave_idx_type n, octave_idx_type nf,
-                          const map *map_list, map *new_map_list)
+                          octave_idx_type idx, const map *map_list, 
+                          map *new_map_list)
 {
-  new_map_list[0] = map_list[0];
+  new_map_list[idx] = map_list[idx];
 
   Array<octave_idx_type> perm (1, nf);
 
-  for (octave_idx_type i = 1; i < n; i++)
+  for (octave_idx_type i = 0; i < n; i++)
     {
-      new_map_list[i] = map_list[i].orderfields (map_list[0], perm);
+      if (i == idx)
+         continue;
+
+      permute_to_correct_order1 (map_list[idx], map_list[i], new_map_list[i], perm);
 
       if (error_state)
         {
@@ -655,15 +676,24 @@
 
   if (n > 0)
     {
-      retval.xkeys = map_list[0].xkeys;
-      octave_idx_type nf = map_list[0].nfields ();
+      octave_idx_type idx, nf = 0;
+      for (idx = 0; idx < n; idx++)
+        {
+          nf = map_list[idx].nfields ();
+          if (nf > 0)
+            {
+              retval.xkeys = map_list[idx].xkeys;
+              break;
+            }
+        }
+
       if (nf > 0)
         {
           // Try the fast case.
           bool all_same = true;
-          for (octave_idx_type i = 1; i < n; i++)
+          for (octave_idx_type i = 0; i < n; i++)
             {
-              all_same = map_list[0].xkeys.is_same (map_list[i].xkeys);
+              all_same = map_list[idx].xkeys.is_same (map_list[i].xkeys);
               if (! all_same)
                 break;
             }
@@ -675,7 +705,7 @@
               // permute all structures to common order.
               OCTAVE_LOCAL_BUFFER (octave_scalar_map, new_map_list, n);
 
-              permute_to_correct_order (n, nf, map_list, new_map_list);
+              permute_to_correct_order (n, nf, idx, map_list, new_map_list);
 
               do_cat (dim, n, new_map_list, retval);
             }
@@ -701,14 +731,22 @@
   octave_map retval;
   if (n > 0)
     {
-      retval.xkeys = map_list[0].xkeys;
-      octave_idx_type nf = map_list[0].nfields ();
+      octave_idx_type idx, nf = 0;
+      for (idx = 0; idx < n; idx++)
+        {
+          nf = map_list[idx].nfields ();
+          if (nf > 0)
+            {
+              retval.xkeys = map_list[idx].xkeys;
+              break;
+            }
+        }
 
       // Try the fast case.
       bool all_same = true;
-      for (octave_idx_type i = 1; i < n; i++)
+      for (octave_idx_type i = 0; i < n; i++)
         {
-          all_same = map_list[0].xkeys.is_same (map_list[i].xkeys);
+          all_same = map_list[idx].xkeys.is_same (map_list[i].xkeys);
           if (! all_same)
             break;
         }
@@ -720,7 +758,7 @@
           // permute all structures to correct order.
           OCTAVE_LOCAL_BUFFER (octave_map, new_map_list, n);
 
-          permute_to_correct_order (n, nf, map_list, new_map_list);
+          permute_to_correct_order (n, nf, idx, map_list, new_map_list);
 
           if (nf > 0)
             do_cat (dim, n, new_map_list, retval);
@@ -729,7 +767,7 @@
               // Use dummy arrays. FIXME: Need(?) a better solution.
               OCTAVE_LOCAL_BUFFER (Array<char>, dummy, n);
               for (octave_idx_type i = 0; i < n; i++)
-                dummy[i].clear (map_list[0].dimensions);
+                dummy[i].clear (map_list[i].dimensions);
               Array<char>::cat (dim, n, dummy);
             }
         }
--- a/src/pt-mat.cc	Wed Sep 01 08:49:57 2010 +0200
+++ b/src/pt-mat.cc	Wed Sep 01 12:41:05 2010 +0200
@@ -701,14 +701,12 @@
         {
           octave_quit ();
 
-          // Use 0x0 in place of all empty arrays to allow looser rules.
-          if (! q->is_empty ())
-            array_list[i] = octave_value_extract<TYPE> (*q);
+          array_list[i] = octave_value_extract<TYPE> (*q);
           i++;
         }
 
       if (! error_state)
-        result = Array<T>::cat (1, ncols, array_list);
+        result = Array<T>::cat (-2, ncols, array_list);
     }
   else
     {
@@ -746,20 +744,16 @@
         {
           octave_quit ();
 
-          // Use 0x0 in place of all empty arrays to allow looser rules.
-          if (! q->is_empty ())
-            sparse_list[i] = octave_value_extract<TYPE> (*q);
+          sparse_list[i] = octave_value_extract<TYPE> (*q);
           i++;
         }
 
-      Sparse<T> stmp = Sparse<T>::cat (1, ncols, sparse_list);
-      // Use 0x0 in place of all empty arrays to allow looser rules.
-      if (! stmp.is_empty ())
-        sparse_row_list[j] = stmp;
+      Sparse<T> stmp = Sparse<T>::cat (-2, ncols, sparse_list);
+      sparse_row_list[j] = stmp;
       j++;
     }
 
-  result = Sparse<T>::cat (0, nrows, sparse_row_list);
+  result = Sparse<T>::cat (-1, nrows, sparse_row_list);
 }
 
 template<class MAP>
@@ -788,21 +782,16 @@
         {
           octave_quit ();
 
-          // Use 0x0 in place of all empty arrays to allow looser rules.
-          // If MAP is octave_scalar_map, the condition is vacuously true.
-          if (! q->is_empty ())
-            map_list[i] = octave_value_extract<MAP> (*q);
+          map_list[i] = octave_value_extract<MAP> (*q);
           i++;
         }
 
-      octave_map mtmp = octave_map::cat (1, ncols, map_list);
-      // Use 0x0 in place of all empty arrays to allow looser rules.
-      if (! mtmp.is_empty ())
-        map_row_list[j] = mtmp;
+      octave_map mtmp = octave_map::cat (-2, ncols, map_list);
+      map_row_list[j] = mtmp;
       j++;
     }
 
-  result = octave_map::cat (0, nrows, map_row_list);
+  result = octave_map::cat (-1, nrows, map_row_list);
 }
 
 template<class TYPE>