Skip to main content
gamedev.net
🔒 Locked

Swapping data within a multi-dimensional array..

Started by OpenGL_Guru 0 replies 300+ views
Original Post
OpenGL_Guru
OpenGL_Guru
Ok i am back again with another stepping stone to this multi-dimensional array problem. i actually have gotten this to work successfully:

template <class T>
T *reverse_array(T *array, long x_size, long y_size, long depth_size, long time_size, int array_size)
{
 int i, j, k, l;
 int offset;
 int count = 0;
 T swap;
 T *temp_array = new short[array_size];

 std::cout << x_size << "x" << y_size << "x" << depth_size << "x" << time_size << endl;

 for(l = 0; l < time_size; l++)
 {
    for(k = 0; k < depth_size; k++)
    {
     for(j = y_size; j > 0; j--)
     {
       offset = x_size * y_size * depth_size * l + (x_size * y_size * k) + (x_size * (j-1));
       for(int x = 0; x < x_size; x++)
       {
        temp_array[count] = array[offset+x];
        //swap = array[count];
        //array[offset+x] = array[count];
        //array[count] = swap;
         count++;
       }
      }
    }
  }

  return temp_array;
  //return array;

}

i access the data successfully here and copy it over to a temp_array then return this array and basically what i have done is write out the data in this 4-D array backwards. thanks all for the help and after much paper i got this to work. what i would like to do to finish this off and make it less of a memory hog. that is dont use another array which is at least temporarily another copy of the original and just to reiterate we are talking about 8 million cells here. so as you can see i have commented out what i thought would work but unfortunately does not..that is trying to swap the data around in the array itself. i tried setting the outer loop to go half way but nothing much is accomplished by this. can anyone help? thanks a lot!
heh
Zahlman
Zahlman
template <class T>T *reverse_array(T *array, long x_size, long y_size, long depth_size, long time_size) {  long chunk_size = y_size * depth_size * time_size;  // I have no idea where those names come from - something to do with your  // problem domain? If you want this thing to be reused, you might want  // more general names. After all, it's the *calling* code that cares about  // concepts like "depth" and "time", not the array reversal.  // Anyway, the basic idea is that we can view the array as x-many "chunks",  // each of which is a 3d slice of the 4d array that's all packed into  // contiguous elements. This is just like the 2d case.  for (int i = 0; i < x_size / 2; ++i) {    // swap chunk_size many items     // between [i * chunk_size, (i+1) * chunk_size - 1]    // and [(x_size - 1 - i) * chunk_size, (x_size - i) * chunk_size - 1].    // As with other standard library algorithms accepting iterator ranges, the    // range provided to swap_ranges is [begin, end), i.e. you supply a     // one-past-the-end iterator.    std::swap_ranges(array + i * chunk_size,                     array + (i+1) * chunk_size,                     array + (x_size - i - 1) * chunk_size);  }}


But SERIOUSLY, just use boost::array. Please. You will be much happier.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.