Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualISDCaptain01

Posted 07 December 2012 - 11:44 PM

Well here it is. My 4D array resize algorithm:

[source lang="cpp"]#pragma once#include <iostream>using namespace std;template <class Datatype>class Array4D{public://ConstructorArray4D(int p_time, int p_depth, int p_height, int p_width){  m_array  =  new Datatype[ p_time * p_depth * p_height * p_width];  m_time   = p_time;  m_depth  = p_depth;  m_height = p_height;  m_width  = p_width;}//Destructor~Array4D(){  if(m_array != 0)  {   delete[] m_array;  }  m_array = 0;}//Get: Retrieves the value from passed in locationDatatype& Get(int p_t, int p_z, int p_y, int p_x){  return m_array[ ( (p_t * m_depth * m_height * m_width) + (p_z * m_height * m_width) + (p_y * m_width) + (p_x) ) ];}//Width: Returns the width of the arrayint Width(){  return m_width;}//Height: Returns the height of the arrayint Height(){  return m_height;}//Depth: Returns the depth of the arrayint Depth(){  return m_depth;}//Time: Returns the time of the arrayint Time(){  return m_time;}//Resize: Resizes the arrayvoid Resize(int p_time, int p_depth, int p_height, int p_width){  //Declare a pointer to a new array and allocate enouh memory  Datatype *newarray = new [p_time * p_depth * p_height * p_width];  //Determine the minimum of all four dimensions  int minx = (p_width  < m_width  ? p_width  : m_width);  int miny = (p_height < m_height ? p_height : m_height);  int minz = (p_depth  < m_depth  ? p_depth  : m_depth);  int mint = (p_time   < m_time   ? p_time   : m_time);  //Declare four dimensional coordinates  int x;  int y;  int x;  int t;  //Declare temporary variables  int t1;  int t2;  int t3;  int t4;  int t5;  int t6;  //Now loop through each cell and paste the values from the old array into the new array  for(t = 0; t < mint; t++)  {   t1 = t * p_depth * m_height * m_width   t2 = t * m_depth * m_height * m_width   for(z = 0; z < minz; z++)   { t3 = z * p_width * m_height; t4 = z * m_width * m_height; for(y = 0; y < miny; y++) { t5 = y * p_width; t6 = y * m_width; for(x = 0; x < minx ; x++) {   newarray[t1 + t3 + t5 + x] = m_array[t2 + t4 + t6 + x]; } }   }  }  //Deallocate the old array  if(m_array != 0)  {   delete[] m_array;  }  m_array = newarray;  //set the new dimesions  m_width  = p_width;  m_height = p_height;  m_depth  = p_depth;  m_time   = p_time;}//Size: Returns the total size of the arrayint Size(){  return m_time * m_depth * m_height * m_width;}private://Private variablesDatatype *m_array;int m_width;int m_height;int m_depth;int m_time;};[/source]

and heres an example implementation file to test it:

[source lang="cpp"]#include <iostream>#include "Array4D.h"using namespace std;int main(){ //Create two different 4-Dimensional arrays Array4D<int> iarray(5, 5, 5, 5); Array4D<float> farray(7, 7, 7, 7); //Create some variables to show output int i; int f; //Insert 10 into an array iarray.Get(2, 1, 3, 0) = 10; i = iarray.Get(2, 1, 3, 0); cout << "The value at time 2, depth 1, height 3, and width 0 is: " << i << endl;     farray.Get(2, 1, 3, 0) = 25.0f; f = farray.Get(2, 1, 3, 0); cout << "The value at time 2, depth 1, height 3, and width 0 is: " << f << endl; //Display the size of the arrays cout << "The size of iarray is: " << iarray.Size() << endl; cout << "The size of farray is: " << farray.Size() << endl; cin.get(); return 0;}[/source]

#1ISDCaptain01

Posted 07 December 2012 - 11:43 PM

Well here it is. My 4D array resize algorithm:

[source lang="cpp"]#pragma once#include <iostream>using namespace std;template <class Datatype>class Array4D{public: //Constructor Array4D(int p_time, int p_depth, int p_height, int p_width) {  m_array  =  new Datatype[ p_time * p_depth * p_height * p_width];  m_time   = p_time;  m_depth  = p_depth;  m_height = p_height;  m_width  = p_width; } //Destructor ~Array4D() {  if(m_array != 0)  {   delete[] m_array;  }  m_array = 0; } //Get: Retrieves the value from passed in location Datatype& Get(int p_t, int p_z, int p_y, int p_x) {  return m_array[ ( (p_t * m_depth * m_height * m_width) + (p_z * m_height * m_width) + (p_y * m_width) + (p_x) ) ]; } //Width: Returns the width of the array int Width() {  return m_width; } //Height: Returns the height of the array int Height() {  return m_height; } //Depth: Returns the depth of the array int Depth() {  return m_depth; } //Time: Returns the time of the array int Time() {  return m_time; } //Resize: Resizes the array void Resize(int p_time, int p_depth, int p_height, int p_width) {  //Declare a pointer to a new array and allocate enouh memory  Datatype *newarray = new [p_time * p_depth * p_height * p_width];  //Determine the minimum of all four dimensions  int minx = (p_width  < m_width  ? p_width  : m_width);  int miny = (p_height < m_height ? p_height : m_height);  int minz = (p_depth  < m_depth  ? p_depth  : m_depth);  int mint = (p_time   < m_time   ? p_time   : m_time);  //Declare four dimensional coordinates  int x;  int y;  int x;  int t;  //Declare temporary variables  int t1;  int t2;  int t3;  int t4;  int t5;  int t6;  //Now loop through each cell and paste the values from the old array into the new array  for(t = 0; t < mint; t++)  {   t1 = t * p_depth * m_height * m_width   t2 = t * m_depth * m_height * m_width   for(z = 0; z < minz; z++)   { t3 = z * p_width * m_height; t4 = z * m_width * m_height; for(y = 0; y < miny; y++) { t5 = y * p_width; t6 = y * m_width; for(x = 0; x < minx ; x++) {   newarray[t1 + t3 + t5 + x] = m_array[t2 + t4 + t6 + x]; } }   }  }  //Deallocate the old array  if(m_array != 0)  {   delete[] m_array;  }  m_array = newarray;  //set the new dimesions  m_width  = p_width;  m_height = p_height;  m_depth  = p_depth;  m_time   = p_time; } //Size: Returns the total size of the array int Size() {  return m_time * m_depth * m_height * m_width; }private: //Private variables Datatype *m_array; int m_width; int m_height; int m_depth; int m_time;};[/source]

and heres an example implementation file to test it:
#include <iostream>
#include "Array4D.h"
using namespace std;



int main()
{
//Create two different 4-Dimensional arrays
Array4D<int> iarray(5, 5, 5, 5);
Array4D<float> farray(7, 7, 7, 7);

//Create some variables to show output
int i;
int f;

//Insert 10 into an array
iarray.Get(2, 1, 3, 0) = 10;
i = iarray.Get(2, 1, 3, 0);
cout << "The value at time 2, depth 1, height 3, and width 0 is: " << i << endl;

    farray.Get(2, 1, 3, 0) = 25.0f;
f = farray.Get(2, 1, 3, 0);
cout << "The value at time 2, depth 1, height 3, and width 0 is: " << f << endl;

//Display the size of the arrays
cout << "The size of iarray is: " << iarray.Size() << endl;
cout << "The size of farray is: " << farray.Size() << endl;


cin.get();
return 0;
}

PARTNERS