Imagine++
Public Types | Public Member Functions | Friends | List of all members
Imagine::MultiArray< T, dim > Class Template Reference

nD array of variable size. More...

#include "Imagine/Common.h"

Inheritance diagram for Imagine::MultiArray< T, dim >:
Imagine::Array< T > Imagine::Image< T, dim >

Public Types

typedef const T * const_iterator
 Const iterator type.
 
typedef T * iterator
 Iterator type.
 
- Public Types inherited from Imagine::Array< T >
typedef const T * const_iterator
 Const iterator type.
 
typedef T * iterator
 Iterator type.
 

Public Member Functions

 MultiArray ()
 Empty constructor. More...
 
 MultiArray (const Coords< dim > &sz)
 Constructor (known size). More...
 
 MultiArray (int s0, int s1)
 Constructor (2D shorcut). More...
 
 MultiArray (int s0, int s1, int s2)
 Constructor (3D shorcut). More...
 
 MultiArray (T *ptr, const Coords< dim > &sz, bool handleDelete=false)
 Constructor (pre-allocated). More...
 
 MultiArray (T *ptr, int s0, int s1, bool handleDelete=false)
 Constructor (pre-allocated) 2D alias. More...
 
 MultiArray (T *ptr, int s0, int s1, int s2, bool handleDelete=false)
 Constructor (pre-allocated) 3D alias. More...
 
 MultiArray (const MultiArray &A)
 Copy constructor. More...
 
template<typename T2 >
 MultiArray (const MultiArray< T2, dim > &A)
 Constructor (different type). More...
 
virtual ~MultiArray ()
 Destructor. More...
 
MultiArray clone () const
 Cloning. More...
 
CoordsIterator< dim > coordsBegin () const
 Begin coords iterator. More...
 
CoordsIterator< dim > coordsEnd () const
 End coords iterator. More...
 
int depth () const
 Size alias 2. More...
 
MultiArrayfill (T x)
 Filling. More...
 
MultiArray getSubArray (const Coords< dim > &offset, const Coords< dim > &sz) const
 Sub array. More...
 
int height () const
 Size alias 1. More...
 
size_t offset (const Coords< dim > &c) const
 Offset. More...
 
size_t offset (int x, int y) const
 Offset (2D alias). More...
 
size_t offset (int x, int y, int z) const
 Offset (3D alias). More...
 
const T & operator() (const Coords< dim > &c) const
 Read access. More...
 
T & operator() (const Coords< dim > &c)
 Write access. More...
 
const T & operator() (int x, int y) const
 Read access 2D alias. More...
 
T & operator() (int x, int y)
 Write access 2D alias. More...
 
const T & operator() (int x, int y, int z) const
 Read access 3D alias. More...
 
T & operator() (int x, int y, int z)
 Write access 3D alias. More...
 
MultiArrayoperator= (const MultiArray &A)
 Assignment. More...
 
template<typename T2 >
MultiArrayoperator= (const MultiArray< T2, dim > &A)
 Assignment (different type). More...
 
void setSize (const Coords< dim > &sz)
 Change sizes. More...
 
void setSize (int s0, int s1)
 Change size 2D alias. More...
 
void setSize (int s0, int s1, int s2)
 Change size 3D alias. More...
 
int size (int i) const
 ith size. More...
 
Coords< dim > sizes () const
 Sizes. More...
 
FArray< size_t, dim > stride () const
 Stride. More...
 
size_t stride (int i) const
 ith stride. More...
 
size_t totalSize () const
 Total Size. More...
 
int width () const
 Size alias 0. More...
 
- Public Member Functions inherited from Imagine::Array< T >
 Array ()
 Empty constructor. More...
 
 Array (size_t size)
 Constructor (known size). More...
 
 Array (T *ptr, size_t size, bool handleDelete=false)
 Constructor (pre-allocated). More...
 
 Array (const Array &A)
 Copy constructor. More...
 
template<typename T2 >
 Array (const Array< T2 > &A)
 Constructor (different type). More...
 
 Array (const std::list< T > &L)
 Constructor (from list). More...
 
virtual ~Array ()
 Destructor. More...
 
iterator begin ()
 Begin iterator. More...
 
const_iterator begin () const
 Begin const iterator. More...
 
Array clone () const
 Cloning. More...
 
T * data ()
 Data pointer (read/write). More...
 
const T * data () const
 Data pointer (read). More...
 
bool empty () const
 Is empty. More...
 
iterator end ()
 End iterator. More...
 
const_iterator end () const
 End const iterator. More...
 
Arrayfill (const T &x)
 Filling. More...
 
Array getSubArray (size_t offset, size_t size) const
 Sub array. More...
 
bool operator!= (const Array &A) const
 Inequality test. More...
 
Arrayoperator= (const Array &A)
 Assignment. More...
 
template<typename T2 >
Arrayoperator= (const Array< T2 > &A)
 Assignment (different type). More...
 
bool operator== (const Array &A) const
 Equality test. More...
 
const T & operator[] (size_t i) const
 Read access. More...
 
T & operator[] (size_t i)
 Write access. More...
 
void setSize (size_t size)
 Change size. More...
 
size_t size () const
 Size. More...
 

Friends

std::ostream & operator<< (std::ostream &out, const MultiArray &A)
 ASCII write. More...
 
std::istream & operator>> (std::istream &in, MultiArray &A)
 ASCII read. More...
 
void read (std::istream &in, MultiArray &A)
 Binary read. More...
 
void write (std::ostream &out, const MultiArray &A)
 Binary write. More...
 

Detailed Description

template<typename T, int dim>
class Imagine::MultiArray< T, dim >

nD array of variable size. Memory is reference counted, i.e.:

Parameters
Tvalue type
dimdimension
Examples:
Common/test/test.cpp.

Constructor & Destructor Documentation

◆ MultiArray() [1/9]

template<typename T, int dim>
Imagine::MultiArray< T, dim >::MultiArray ( )
inline

Constructs an unallocated array of variables of type T and dimension dim

MultiArray<char,2> a; // non allocated

◆ MultiArray() [2/9]

template<typename T, int dim>
Imagine::MultiArray< T, dim >::MultiArray ( const Coords< dim > &  sz)
inlineexplicit

Constructs an allocated array of variables of type T and dimension dim. sz[i] specifies size for dimension i.

Parameters
szarray sizes
MultiArray<char,2> b(Coords<2>(3,4)); // allocated with specified size

◆ MultiArray() [3/9]

template<typename T, int dim>
Imagine::MultiArray< T, dim >::MultiArray ( int  s0,
int  s1 
)
inline

Constructs an allocated array of variables of type T, dimension 2 and specified size

Parameters
s0array size for dimension 0
s1array size for dimension 1
MultiArray<char,2> c2(2,3); // constructor, 2D alias

◆ MultiArray() [4/9]

template<typename T, int dim>
Imagine::MultiArray< T, dim >::MultiArray ( int  s0,
int  s1,
int  s2 
)
inline

Constructs an allocated array of variables of type T, dimension 3 and specified size

Parameters
s0array size for dimension 0
s1array size for dimension 1
s2array size for dimension 2
MultiArray<char,3> c3(2,3,4); // constructor, 3D alias

◆ MultiArray() [5/9]

template<typename T, int dim>
Imagine::MultiArray< T, dim >::MultiArray ( T *  ptr,
const Coords< dim > &  sz,
bool  handleDelete = false 
)
inline

Constructs an array of variables of type T and dimension dim, stored at an already allocated memory. ptr contains elements (0,0,...), (1,0,...), ... Does not allocate fresh memory. Does not free given memory at object destruction unless handleDelete=true. This memory must indeed stay available during object life.

Parameters
ptraddress of memory
szarray sizes
handleDeletedelete memory at destruction? (default=false)
char t[6]={'a','b','c','d','e','f'}; // pre-allocated
MultiArray<char,2> e(t,Coords<2>(2,3));
char *t2=new char[6];
MultiArray<char,2> e2(t2,Coords<2>(2,3),true); // ...

◆ MultiArray() [6/9]

template<typename T, int dim>
Imagine::MultiArray< T, dim >::MultiArray ( T *  ptr,
int  s0,
int  s1,
bool  handleDelete = false 
)
inline

Constructs an array of variables of type T and dimension 2, stored at an already allocated memory. ptr contains elements (0,0), (1,0), ... Does not allocate fresh memory. Does not free given memory at object destruction unless handleDelete=true. This memory must indeed stay available during object life.

Parameters
ptraddress of memory
s0array size for dimension 0
s1array size for dimension 1
handleDeletedelete memory at destruction? (default=false)
MultiArray<char,2> g(t,2,3); // pre-allocated, 2D alias

◆ MultiArray() [7/9]

template<typename T, int dim>
Imagine::MultiArray< T, dim >::MultiArray ( T *  ptr,
int  s0,
int  s1,
int  s2,
bool  handleDelete = false 
)
inline

Constructs an array of variables of type T and dimension 3, stored at an already allocated memory. ptr contains elements (0,0,0), (1,0,0), ... Does not allocate fresh memory. Does not free given memory at object destruction unless handleDelete=true. This memory must indeed stay available during object life.

Parameters
ptraddress of memory
s0array size for dimension 0
s1array size for dimension 1
s2array size for dimension 2
handleDeletedelete memory at destruction? (default=false)
MultiArray<char,3> i(t,1,2,3); // pre-allocated, 3D alias

◆ MultiArray() [8/9]

template<typename T, int dim>
Imagine::MultiArray< T, dim >::MultiArray ( const MultiArray< T, dim > &  A)
inlineexplicit

Constructs an nD array from another one (sharing memory!)

Parameters
AnD array to copy
MultiArray<char,2> jc(e); // copy constructor

◆ MultiArray() [9/9]

template<typename T, int dim>
template<typename T2 >
Imagine::MultiArray< T, dim >::MultiArray ( const MultiArray< T2, dim > &  A)
inline

Constructs an nD array of type T from one of another type (thus without sharing memory!)

Parameters
AnD array to copy
Template Parameters
T2type of A
MultiArray<int,2> ji(e); // from different type

◆ ~MultiArray()

template<typename T, int dim>
virtual Imagine::MultiArray< T, dim >::~MultiArray ( )
inlinevirtual

Reference counted desctructor: frees memory if the object is the last one to use it.

Member Function Documentation

◆ clone()

template<typename T, int dim>
MultiArray Imagine::MultiArray< T, dim >::clone ( ) const
inline

Clones: creates a new nD array, with fresh memory, copying values to it

Returns
cloned array
a=b.clone(); // cloning (fresh memory)

◆ coordsBegin()

template<typename T, int dim>
CoordsIterator<dim> Imagine::MultiArray< T, dim >::coordsBegin ( ) const
inline

Coordinates iterator over the array domain. Use this to iterates over an nD array if coordinates are needed (otherwise, use a standard Array::iterator)

Returns
begin iterator
for (CoordsIterator<3> it=i.coordsBegin(); it != i.coordsEnd(); ++it ) // coords iterator
cout << "[" << *it << "]=" << i(*it) << endl; // ...

◆ coordsEnd()

template<typename T, int dim>
CoordsIterator<dim> Imagine::MultiArray< T, dim >::coordsEnd ( ) const
inline

Coordinates iterator over the array domain. Use this to iterates over an nD array if coordinates are needed (otherwise, use a standard Array::iterator)

Returns
end iterator
for (CoordsIterator<3> it=i.coordsBegin(); it != i.coordsEnd(); ++it ) // coords iterator
cout << "[" << *it << "]=" << i(*it) << endl; // ...

◆ depth()

template<typename T, int dim>
int Imagine::MultiArray< T, dim >::depth ( ) const
inline

Size of nD Array along dimension 2.

Returns
depth
/*int d=*/c3.depth(); // Size. alias 2

◆ fill()

template<typename T, int dim>
MultiArray& Imagine::MultiArray< T, dim >::fill ( x)
inline

Fills with constant value

Parameters
xvalue to be copied to each element
Returns
self reference
a.fill('x'); // filling with constant value

◆ getSubArray()

template<typename T, int dim>
MultiArray Imagine::MultiArray< T, dim >::getSubArray ( const Coords< dim > &  offset,
const Coords< dim > &  sz 
) const
inline

Construct a sub array, i.e. a new array with fresh memory, initialized from a part of values of *this

Parameters
offsetcoordinates of first element of sub array
szsizes of sub array
Returns
sub array
a=c2.getSubArray(Coords<2>(1,1),Coords<2>(1,2));// sub array (fresh memory)

◆ height()

template<typename T, int dim>
int Imagine::MultiArray< T, dim >::height ( ) const
inline

Size of nD Array along dimension 1.

Returns
height
/*int h=*/c3.height(); // Size. alias 1

◆ offset() [1/3]

template<typename T, int dim>
size_t Imagine::MultiArray< T, dim >::offset ( const Coords< dim > &  c) const
inline

Offset (i.e. index of coordinates in the underlying Array) given coordinates

Parameters
ccoordinates in the MultiArray domain
Returns
offset
/*size_t o=*/c3.offset(Coords<3>(1,0,1)); // offset

◆ offset() [2/3]

template<typename T, int dim>
size_t Imagine::MultiArray< T, dim >::offset ( int  x,
int  y 
) const
inline

Offset (2D alias)

Parameters
x,ycoordinates
Returns
offset
/*size_t o2=*/c2.offset(1,0); // offset 2D alias

◆ offset() [3/3]

template<typename T, int dim>
size_t Imagine::MultiArray< T, dim >::offset ( int  x,
int  y,
int  z 
) const
inline

Offset (3D alias)

Parameters
x,y,zcoordinates
Returns
offset
/*size_t o3=*/c3.offset(1,0,1); // offset 3D alias

◆ operator()() [1/6]

template<typename T, int dim>
const T& Imagine::MultiArray< T, dim >::operator() ( const Coords< dim > &  c) const
inline

Reads element at coordinates c

Parameters
ccoordinates
Returns
Const reference to element.
x=a(Coords<2>(0,1)); // read access ()

◆ operator()() [2/6]

template<typename T, int dim>
T& Imagine::MultiArray< T, dim >::operator() ( const Coords< dim > &  c)
inline

Writes element at coordinates c

Parameters
ccoordinates
Returns
Reference to element.
a(Coords<2>(0,1))=x; // write access ()

◆ operator()() [3/6]

template<typename T, int dim>
const T& Imagine::MultiArray< T, dim >::operator() ( int  x,
int  y 
) const
inline

Read access 2D alias

Parameters
x,ycoordinates
Returns
Const reference to element.
x=a(0,1); // read access 2D alias

◆ operator()() [4/6]

template<typename T, int dim>
T& Imagine::MultiArray< T, dim >::operator() ( int  x,
int  y 
)
inline

Write access 2D alias

Parameters
x,ycoordinates
Returns
Reference to element.
a(0,1)=x; // write access 2D alias

◆ operator()() [5/6]

template<typename T, int dim>
const T& Imagine::MultiArray< T, dim >::operator() ( int  x,
int  y,
int  z 
) const
inline

Read access 3D alias

Parameters
x,y,zcoordinates
Returns
Const reference to element.
x=c3(1,0,1); // read access 3D alias

◆ operator()() [6/6]

template<typename T, int dim>
T& Imagine::MultiArray< T, dim >::operator() ( int  x,
int  y,
int  z 
)
inline

Write access 3D alias

Parameters
x,y,zcoordinates
Returns
Reference to element.
c3(1,0,1)=x; // write access 3D alias

◆ operator=() [1/2]

template<typename T, int dim>
MultiArray& Imagine::MultiArray< T, dim >::operator= ( const MultiArray< T, dim > &  A)
inline

Assigns from another nD Array (sharing its memory)

Parameters
Aarray to be assigned to
Returns
self reference
a=b; // assignment

◆ operator=() [2/2]

template<typename T, int dim>
template<typename T2 >
MultiArray& Imagine::MultiArray< T, dim >::operator= ( const MultiArray< T2, dim > &  A)
inline

Assign from an array of another type (thus without sharing memory!)

Parameters
Aarray to copy
Template Parameters
T2type of A
Returns
self reference
ji=jc; // different type assignment

◆ setSize() [1/3]

template<typename T, int dim>
void Imagine::MultiArray< T, dim >::setSize ( const Coords< dim > &  sz)
inline

Resize nD array:

  • reallocates fresh memory
  • data is not copied from old to new memory
  • concerns only this object, not the ones that shared the old memory with it (and continue to use this old memory)
  • if the new size is the same as the old one, does nothing
    Parameters
    szarray sizes
    e.setSize(Coords<2>(4,4)); // setSize

◆ setSize() [2/3]

template<typename T, int dim>
void Imagine::MultiArray< T, dim >::setSize ( int  s0,
int  s1 
)
inline

Resize 2D array. 2D alias.

Parameters
s0array size for dimension 0
s1array size for dimension 1
g.setSize(3,3); // setSize, 2D alias

◆ setSize() [3/3]

template<typename T, int dim>
void Imagine::MultiArray< T, dim >::setSize ( int  s0,
int  s1,
int  s2 
)
inline

Resize 3D array. 3D alias.

Parameters
s0array size for dimension 0
s1array size for dimension 1
s2array size for dimension 2
i.setSize(3,2,1); // setSize, 3D alias

◆ size()

template<typename T, int dim>
int Imagine::MultiArray< T, dim >::size ( int  i) const
inline

Size of nD Array along dimension i.

Returns
ith size

◆ sizes()

template<typename T, int dim>
Coords<dim> Imagine::MultiArray< T, dim >::sizes ( ) const
inline

Sizes of nD Array.

Returns
sizes
/*Coords<2> sizes=*/a.sizes(); // sizes

◆ stride() [1/2]

template<typename T, int dim>
FArray<size_t,dim> Imagine::MultiArray< T, dim >::stride ( ) const
inline

Stride of nD Array (stride(i) gives the distance to next element along dimension i)

Returns
stride
/*FArray<size_t,3> stride=*/c3.stride(); // stride

◆ stride() [2/2]

template<typename T, int dim>
size_t Imagine::MultiArray< T, dim >::stride ( int  i) const
inline

ith stride of nD Array (distance to element along dimension i)

Returns
ith stride

◆ totalSize()

template<typename T, int dim>
size_t Imagine::MultiArray< T, dim >::totalSize ( ) const
inline

Number of elements

Returns
total size
/*size_t s=*/a.totalSize(); // number of elements

◆ width()

template<typename T, int dim>
int Imagine::MultiArray< T, dim >::width ( ) const
inline

Size of nD Array along dimension 0.

Returns
width
/*int w=*/c3.width(); // Size. alias 0

Friends And Related Function Documentation

◆ operator<<

template<typename T, int dim>
std::ostream& operator<< ( std::ostream &  out,
const MultiArray< T, dim > &  A 
)
friend

Writes Array to stream (size and values)

Parameters
outoutput stream
AMultiArray to write
Returns
updated stream
out.open("tmp.txt"); // ASCII write
out << i << endl; // ...

◆ operator>>

template<typename T, int dim>
std::istream& operator>> ( std::istream &  in,
MultiArray< T, dim > &  A 
)
friend

Reads and allocates Array from stream (providing size and values)

Parameters
ininput stream
AMultiArray to read
Returns
updated stream
in.open("tmp.txt"); // ASCII read
in >> i; // ...

◆ read

template<typename T, int dim>
void read ( std::istream &  in,
MultiArray< T, dim > &  A 
)
friend

Reads and allocates Array from binary stream (providing size and values)

Parameters
ininput stream
AMultiArray to read
ifstream in("tmp.bin",ios::binary); // binary read
read(in,i); // ...

◆ write

template<typename T, int dim>
void write ( std::ostream &  out,
const MultiArray< T, dim > &  A 
)
friend

Writes Array to binary stream (size and values)

Parameters
outoutput stream
AMultiArray to write
ofstream out("tmp.bin",ios::binary); // binary write
write(out,i); // ...


The documentation for this class was generated from the following file: