tags:

views:

47

answers:

2

Here is a boost example:

  typedef boost::multi_array<double, 1> array_type;
  typedef array_type::index index;
  array_type A(boost::extents[100]);

  for(index i = 0; i != A.size(); ++i) {
        A[i] = (double)i;
  }

  // creating view
  array_type::index_gen indices;
  typedef boost::multi_array_types::index_range range;

  array_type::array_view<1>::type myview = A[ indices[range(0,50)] ];

What this code does is creating a subarray or view mapping onto the original array. This view is continuous and covers from 0th to 50th elements of an original array.

What if I need to explicitly define elements I'd like to see in the view? How can I create a view with indices like [1, 5, 35, 23] ? Any ideas?

A: 

I don't think you can do anything besides introducing an indirection in the code, e.g. a[b[i]] where b[i] contains things like [1,5,35,23]. AFAIK, the MultiArray concept requires well-defined, constant strides.

Rhys Ulerich
Yes, this is very straightforward solution, but it's not nice. Why do I need to use all this overheaded stuff with multy_arrays then if I'm gonna use it as simple C-style arrays? :)
Fair enough, but I'm unsure how you plan to create a view of a C-style array where indices {0,1,2,3} access offsets {1,5,35,23} without introducing the a[b[i]]-like indirection also.
Rhys Ulerich
A: 

Looks like a good feature. I don't think Boost.MultiArray supports this, but it could be a good idea to request it on the Boost ML.

Instead of using range that generates an index_range we could think on defining a variadic function enumeration that gives the range(co-domain) of the mapping for the index

enumeration(1, 5, 35, 23) will map to (0,1,2,3)

Your view could be declared as

array_type::index_gen indices; typedef boost::multi_array_types::index_enumeration_map enumeration; // note that index_enumeration_map is not part of MultiArray

array_type::array_view<1>::type myview = A[indices[enumeration(1, 5, 35, 23)];

Of course this will need some modifications on the way MultiArray manage the indexes.

Vicente Botet Escriba