help-octave
[Top][All Lists]
Advanced

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

Re: Allocating N-dimensional Array in C++


From: Jaroslav Hajek
Subject: Re: Allocating N-dimensional Array in C++
Date: Wed, 17 Mar 2010 10:06:35 +0100

2010/3/15 John W. Eaton <address@hidden>:
> On  7-Mar-2010, S ren Hauberg wrote:
>
> | man, 08 03 2010 kl. 07:17 +0100, skrev Jaroslav Hajek:
> | > On Mon, Mar 8, 2010 at 12:08 AM, S ren Hauberg <address@hidden> wrote:
> | > > How about the attached changeset? With this, I can do
> | > >
> | > >    octave_idx_type tmp_size [] = {4, 5, 6, 7};
> | > >    const dim_vector size (tmp_size, 4);
> | > >    Array<double> A (size, 0);
> | > >
> | > > I was a bit confused about the use of the 'explicit' keyword in the
> | > > other constructors. I thought this keyword only made a difference for
> | > > constructors with one input argument, but I see it is used with all
> | > > constructors.
> | > >
> | > > S ren
> | > >
> | >
> | > Seems OK to me.
> |
> | I've pushed it.
>
> I'm not a big fan of constructors like this because there is no way
> for the constructor to ensure that the arguments are consistent.
>
> Why not just introduce more constructors of the form
>
>  explicit dim_vector (octave_idx_type d0, octave_idx_type d1,
>                       octave_idx_type d2, octave_idx_type d3)
>    : rep (newrep (4))
>  {
>    rep[0] = d0;
>    rep[1] = d1;
>    rep[2] = d2;
>    rep[3] = d3;
>  }
>
> up to say 7 dimensions?  I Would think that would cover most cases,
> and to me, it seems just as easy to write
>
>  Array<double> A (dim_vector (4, 5, 6, 7), 0);
>
> as what you have above.
>
> jwe
>

I supplied some generic macro magic for such tasks, then did it like this:

#define ASSIGN_REP(i) rep[i] = d ## i;
#define DIM_VECTOR_CTOR(N) \
  dim_vector (OCT_MAKE_DECL_LIST(octave_idx_type, d, N)) \
    : rep (newrep (N)) \
  { \
    OCT_ITERATE_MACRO(ASSIGN_REP, N) \
  }

  // Add more if needed.
  DIM_VECTOR_CTOR(4)
  DIM_VECTOR_CTOR(5)
  DIM_VECTOR_CTOR(6)
  DIM_VECTOR_CTOR(7)

#undef ASSIGN_REP
#undef DIM_VECTOR_CTOR

regards

-- 
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

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