Re: 3D array memory allocation dimensions
If you're not interested in dynamically resizing the 3D array, then the approach I would use is to wrap the whole thing in a template.
No new, no delete, copyable & assignable.
Code:
template<typename T, const size_t HEIGHT, const size_t WIDTH, const size_t DEPTH>
class Array3d
{
public:
T &operator ()(size_t x, size_t y, size_t z)
{
return (data[y + (x * WIDTH) + (z * WIDTH * HEIGHT)]);
}
const T &operator ()(size_t x, size_t y, size_t z) const
{
return (data[y + (x * WIDTH) + (z * WIDTH * HEIGHT)]);
}
private:
T data[WIDTH * HEIGHT * DEPTH];
};
int main()
{
const int HEIGHT = 2;
const int WIDTH = 3;
const int DEPTH = 4;
Array3d<short, HEIGHT, WIDTH, DEPTH> test;
for (int z = 0; z < DEPTH; z++) //filling it with ones
{
for (int y = 0; y < WIDTH; y++)
{
for (int x = 0; x < HEIGHT; x++)
{
test(x, y, z) = 1;
}
}
}
}
Re: 3D array memory allocation dimensions
Quote:
Originally Posted by
Kositch
Paul, so my second try of deleting array is now right (code is above)?
Yes.
Regards,
Paul McKenzie
Re: 3D array memory allocation dimensions
Thank you for all your patience and help!
Re: 3D array memory allocation dimensions
If the array doesn't have to be a tree, then here is an example of a simple dynamic multiarray of any length
Code:
#include <iostream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <vector>
namespace demo
{
std::ostream& os = std::cout;
template <typename T_, const unsigned S_>
struct array
{
typedef std::vector<T_> dimension;
typedef typename dimension::size_type size_type;
typedef size_type initializer_list[S_];
template <typename Initializer_>
explicit array(const Initializer_& i, const T_& v = T_())
:
m_dim(i, i + S_),
m_array(std::accumulate(m_dim.begin(), m_dim.end(),
1, std::multiplies<size_type>()), v)
{
}
// example methods
array& operator=(const size_type i)
{
m_array.at(calculate_pos()) = i;
return *this;
}
array& operator[](const size_type i)
{
m_seq.push_back(i);
return *this;
}
operator T_&()
{
return m_array.at(calculate_pos());
}
// others ...
private:
const size_type calculate_pos()
{
dimension i(m_seq);
m_seq.clear();
return std::inner_product(i.begin(), i.end(), m_dim.begin(), 0,
std::plus<size_type>(), std::modulus<size_type>());
}
dimension m_seq;
dimension m_dim;
dimension m_array;
};
} // end namespace
int main()
{
{
using namespace demo;
array<int, 7>::initializer_list a = {4, 2, 3, 5 ,3, 2, 2};
// pass a plain array,
// or overload ctor to use [][][]... intiailizer syntax
array<int, 7> arr(a, 666);
arr[0][0][0][0][0][0][0] = 100;
arr[3][1][2][4][2][1][1] = 1000;
os << arr[0][0][0][0][0][0][0] << " "
<< arr[2][1][1][4][2][0][0];
}
}
EDIT:
No range checking has been provided on this example!
EDIT2:
calculate_pos() looks wrong but too sleepy to go over it now xD
Re: 3D array memory allocation dimensions
Re: 3D array memory allocation dimensions
Bah!
Laying in bed this morning I realised that my code did nothing more than a straight array declaration! My solution was much better suited to a 3D array sized at runtime.
Never code when half asleep :blush:
Code:
template<typename T>
class Array3d
{
public:
Array3d(size_t height, size_t width, size_t depth)
: HEIGHT(height),
WIDTH(width),
DEPTH(depth),
p_data(new T[height * width * depth])
{
}
~Array3d()
{
delete[] p_data;
}
T &operator ()(size_t x, size_t y, size_t z)
{
return (data[y + (x * WIDTH) + (z * WIDTH * HEIGHT)]);
}
const T &operator ()(size_t x, size_t y, size_t z) const
{
return (data[y + (x * WIDTH) + (z * WIDTH * HEIGHT)]);
}
private:
T p_data;
const size_t HEIGHT;
const size_t WIDTH;
const size_t DEPTH;
};
int main()
{
const int HEIGHT = 2;
const int WIDTH = 3;
const int DEPTH = 4;
Array3d<short> test(HEIGHT, WIDTH, DEPTH);
for (int z = 0; z < DEPTH; z++) //filling it with ones
{
for (int y = 0; y < WIDTH; y++)
{
for (int x = 0; x < HEIGHT; x++)
{
test(x, y, z) = 1;
}
}
}
}
Re: 3D array memory allocation dimensions
That is a pretty nice example, but you probably want to take the filling code and make it into a public method fill(const T& fill_value); :)
And change to
Code:
private:
T* p_data;
for it to work. ;)
Re: 3D array memory allocation dimensions
Quote:
Originally Posted by
Xupicor
That is a pretty nice example, but you probably want to take the filling code and make it into a public method fill(const T& fill_value); :)
That depends whether you want to follow the STL convention of making algorithms separate from the containers they work on, or not.
Quote:
And change
to
Code:
private:
T* p_data;
for it to work. ;)
Doh! That what comes of posting at 6:30 in the morning, before the first mug of tea :blush:
Re: 3D array memory allocation dimensions
Quote:
Originally Posted by
JohnW@Wessex
That depends whether you want to follow the STL convention of making algorithms separate from the containers they work on, or not.
Fair enough. :)
Quote:
Originally Posted by
JohnW@Wessex
Doh! That what comes of posting at 6:30 in the morning, before the first mug of tea :blush:
Happens to the best of us... In untested code that is. :) But tea? Really? Hm, you made me want to drink one instead of the usual coffee. You malicious... fellow programmer! x]
Re: 3D array memory allocation dimensions
Quote:
Originally Posted by
Xupicor
But tea? Really?
Yes, Earl Grey.