1

How can I copy (deep-copy) a selcted range (view) from a boost::multi_array to another array?

1 Answer 1

2

The values can be copied via =operator.

#include <iostream>
#include <string>
#include <boost/multi_array.hpp>

template<typename T>
void initArray(T& to_initialize)
{
  int value = 0;
  for(size_t i=0;i<to_initialize.shape()[0];++i){
      for(size_t j=0;j<to_initialize.shape()[1];++j){
          to_initialize[i][j]=value;
          ++value;
      }
  }
}

template<typename T>
void printArray(T& to_print, std::string additional_text)
{
    std::cout<<"**************"<<additional_text<<"*****************"<<std::endl;
    for(size_t i=0;i<to_print.shape()[0];++i){
      for(size_t j=0;j<to_print.shape()[1];++j){
         std::cout<<to_print[i][j]<<", ";
      }
      std::cout<<std::endl;
    }
    std::cout<<"*******************************"<<std::endl;
}

int main()
{
  auto big_array = boost::multi_array<double, 2>(boost::extents[10][10]);
  initArray(big_array);
  printArray(big_array, "initial big array");
  
  using range = boost::multi_array_types::index_range;
  boost::multi_array<double, 2>::index_gen indices;
  boost::multi_array<double, 2>::array_view<2>::type big_array_view = big_array[ indices[range(0,2)][range(0,4)] ];
  
  auto small_array = boost::multi_array<double, 2>(boost::extents[2][4]);
  small_array = big_array_view; //Deep Copy
  
  printArray(small_array, "small array after copy");
  
  big_array_view[0][0]=999; // Does not affect small_array
  printArray(big_array,  "big array after modifying"); 
  printArray(small_array, "small array is not affected after modifying big array");
}

Output is:

**************initial big array*****************
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 
*******************************
**************small array after copy*****************
0, 1, 2, 3, 
10, 11, 12, 13, 
*******************************
**************big array after modifying*****************
999, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 
*******************************
**************small array is not affected after modifying big array*****************
0, 1, 2, 3, 
10, 11, 12, 13, 
*******************************
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, the key is that small_array needs to have the right shape (by declaration or resizing) before assignment, otherwise you'll get a runtime error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.