octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: int* uint* types as indexes


From: John W. Eaton
Subject: Re: int* uint* types as indexes
Date: Mon, 9 Aug 2004 16:21:39 -0400

On  8-Aug-2004, David Bateman <address@hidden> wrote:

| How about the attached patch. Not the most efficient as it converts
| the int/uint types to NDArrays before then reconverting them to
| idx_vectors, but something like this works fine. Doing it directly,
| will be a pain, since all of the constructors like
| idx_vector::idx_vector(ArrayN<octave_uint64>) will need to be written.
| That plus the scalar versions means 16 new constructors in idx-vector.h.
| I think I prefer the patch below :-)

How about the following patch instead?  I think it has the advantage
of defining specific constructors without having to write a lot of
duplicate code.

jwe

liboctave/ChangeLog:

2004-08-09  John W. Eaton  <address@hidden>

        * idx-vector.h (idx_vector::idx_vector_rep::tree_to_mat_idx
        (const octave_int<U>&)): New member function. 
        (idx_vector::idx_vector_rep::tree_to_mat_idx (double, bool&),
        idx_vector::idx_vector_rep::tree_to_mat_idx (int)):
        Now member functions instead of static in idx-vector.cc.
        (idx_vector::idx_vector_rep::idx_vector_rep (const octave_int<U>&),
        idx_vector::idx_vector_rep::idx_vector_rep (const intNDArray<U>&)):
        New template constructors.


src/ChangeLog:

2004-08-09  John W. Eaton  <address@hidden>

        * ov-intx.h (OCTAVE_VALUE_INT_MATRIX_T::index_vector,
        OCTAVE_VALUE_INT_SCALAR_T::index_vector): New functions.


Index: liboctave/idx-vector.cc
===================================================================
RCS file: /usr/local/cvsroot/octave/liboctave/idx-vector.cc,v
retrieving revision 1.58
diff -u -r1.58 idx-vector.cc
--- a/liboctave/idx-vector.cc   4 Feb 2004 04:32:48 -0000       1.58
+++ b/liboctave/idx-vector.cc   9 Aug 2004 20:18:42 -0000
@@ -59,8 +59,8 @@
     }
 }
 
-static inline int
-tree_to_mat_idx (double x, bool& conversion_error)
+int
+IDX_VEC_REP::tree_to_mat_idx (double x, bool& conversion_error)
 {
   int retval = -1;
 
@@ -79,12 +79,6 @@
   return retval;
 }
 
-static inline int
-tree_to_mat_idx (int i)
-{
-  return i - 1;
-}
-
 static inline bool
 idx_is_inf_or_nan (double x)
 {
Index: liboctave/idx-vector.h
===================================================================
RCS file: /usr/local/cvsroot/octave/liboctave/idx-vector.h,v
retrieving revision 1.44
diff -u -r1.44 idx-vector.h
--- a/liboctave/idx-vector.h    24 Nov 2003 05:02:46 -0000      1.44
+++ b/liboctave/idx-vector.h    9 Aug 2004 20:18:42 -0000
@@ -30,6 +30,8 @@
 #include <iostream>
 
 #include "dim-vector.h"
+#include "oct-inttypes.h"
+#include "intNDArray.h"
 
 class ColumnVector;
 class boolNDArray;
@@ -56,6 +58,35 @@
 
     idx_vector_rep (const NDArray& nda);
 
+    template <class U>
+    idx_vector_rep (const intNDArray<U>& inda)
+      : data (0), len (inda.length ()), num_zeros (0), num_ones (0),
+       max_val (0), min_val (0), count (1), frozen_at_z_len (0),
+       frozen_len (0), colon (0), one_zero (0), initialized (0),
+       frozen (0), colon_equiv_checked (0), colon_equiv (0),
+       orig_dims (inda.dims ())
+    {
+      if (len == 0)
+       {
+         initialized = 1;
+         return;
+       }
+      else
+       {
+         data = new int [len];
+
+         bool conversion_error = false;
+
+         for (int i = 0; i < len; i++)
+           data[i] = tree_to_mat_idx (inda.elem (i), conversion_error);
+
+         if (conversion_error)
+           return;
+       }
+
+      init_state ();
+    }
+
     idx_vector_rep (const Range& r);
 
     idx_vector_rep (double d);
@@ -66,6 +97,21 @@
 
     idx_vector_rep (bool b);
 
+    template <class U>
+    idx_vector_rep (const octave_int<U>& i)
+      : data (0), len (1), num_zeros (0), num_ones (0),
+       max_val (0), min_val (0), count (1), frozen_at_z_len (0),
+       frozen_len (0), colon (0), one_zero (0), initialized (0),
+       frozen (0), colon_equiv_checked (0), colon_equiv (0),
+       orig_dims (1, 1)
+    {
+      data = new int [len];
+
+      data[0] = tree_to_mat_idx (i);
+
+      init_state ();
+    }
+
     idx_vector_rep (const boolNDArray& bnda);
 
     idx_vector_rep (const idx_vector_rep& a);
@@ -134,6 +180,13 @@
     void init_state (void);
 
     void maybe_convert_one_zero_to_idx (int z_len);
+
+    int tree_to_mat_idx (double x, bool& conversion_error);
+
+    int tree_to_mat_idx (int i) { return i - 1; }
+
+    template <class U> int tree_to_mat_idx (const octave_int<U>& i)
+      { return i.value () - 1; }
   };
 
 public:
@@ -156,6 +209,13 @@
       rep->count = 1;
     }
 
+  template <class U>
+  idx_vector (const intNDArray<U>& inda)
+    {
+      rep = new idx_vector_rep (inda);
+      rep->count = 1;
+    }
+
   idx_vector (const Range& r)
     {
       rep = new idx_vector_rep (r);
@@ -186,6 +246,13 @@
       rep->count = 1;
     }
 
+  template <class U>
+  idx_vector (const octave_int<U>& i)
+    {
+      rep = new idx_vector_rep (i);
+      rep->count = 1;
+    }
+
   idx_vector (const boolNDArray& bnda)
     {
       rep = new idx_vector_rep (bnda);
Index: src/ov-intx.h
===================================================================
RCS file: /usr/local/cvsroot/octave/src/ov-intx.h,v
retrieving revision 1.3
diff -u -r1.3 ov-intx.h
--- a/src/ov-intx.h     23 Jul 2004 19:01:23 -0000      1.3
+++ b/src/ov-intx.h     9 Aug 2004 20:18:47 -0000
@@ -71,6 +71,8 @@
       return retval;
     }
 
+  idx_vector index_vector (void) const { return idx_vector (matrix); }
+
 private:
 
   DECLARE_OCTAVE_ALLOCATOR
@@ -119,6 +121,8 @@
       return retval;
     }
 
+  idx_vector index_vector (void) const { return idx_vector (scalar); }
+
 private:
 
   DECLARE_OCTAVE_ALLOCATOR



reply via email to

[Prev in Thread] Current Thread [Next in Thread]