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

Array of variable size. More...

#include "Imagine/Common.h"

Inheritance diagram for Imagine::Array< T >:
Imagine::MultiArray< T, dim > Imagine::SymMatrix< T > Imagine::Vector< T > Imagine::MultiArray< T, 2 > Imagine::Image< T, dim > Imagine::Matrix< T >

Public Types

typedef const T * const_iterator
 Const iterator type.
 
typedef T * iterator
 Iterator type.
 

Public Member Functions

 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 Array &A)
 ASCII write. More...
 
std::istream & operator>> (std::istream &in, Array &A)
 ASCII read. More...
 
std::pair< T, T > prange (const Array &A)
 Pointwise range. More...
 
std::pair< T, T > range (const Array &A)
 Range. More...
 
void read (std::istream &in, Array &A)
 Binary read. More...
 
void write (std::ostream &out, const Array &A)
 Binary write. More...
 

Detailed Description

template<typename T>
class Imagine::Array< T >

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

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

Constructor & Destructor Documentation

◆ Array() [1/6]

template<typename T>
Imagine::Array< T >::Array ( )
inline

Constructs an unallocated array of variables of type T

Array<char> a; // non allocated

◆ Array() [2/6]

template<typename T>
Imagine::Array< T >::Array ( size_t  size)
inlineexplicit

Constructs an allocated array of size variables of type T

Parameters
sizearray size
Array<char> b(3); // allocated with specified size

◆ Array() [3/6]

template<typename T>
Imagine::Array< T >::Array ( T *  ptr,
size_t  size,
bool  handleDelete = false 
)
inline

Constructs an array pointing to variables of type T stored at an already allocated memory. Does not allocate fresh memory. This memory must indeed stay available during object life. Does not free given memory at object destruction unless handleDelete=true.

Parameters
ptraddress of memory
sizearray size
handleDeletedelete memory at destruction? (default=false)
char t[4]={'a','b','c','d'}; // pre-allocated
Array<char> c(t,4);
char* t2=new char[3];
Array<char> c2(t2,3,true); // ...

◆ Array() [4/6]

template<typename T>
Imagine::Array< T >::Array ( const Array< T > &  A)
inlineexplicit

Constructs an array from another one (sharing memory!)

Parameters
Aarray to copy
Array<char> dc(b); // copy constructor (sharing memory)

◆ Array() [5/6]

template<typename T>
template<typename T2 >
Imagine::Array< T >::Array ( const Array< T2 > &  A)
inline

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

Parameters
Aarray to copy
Template Parameters
T2type of A
Array<int> di(b); // from different type

◆ Array() [6/6]

template<typename T>
Imagine::Array< T >::Array ( const std::list< T > &  L)
inline

Constructs an array of variables of type T from a list.

Parameters
Lthe list
list<char>l; // from list
l.push_front('a');
Array<char>e(l); // ...

◆ ~Array()

template<typename T>
virtual Imagine::Array< T >::~Array ( )
inlinevirtual

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

Member Function Documentation

◆ begin() [1/2]

template<typename T>
iterator Imagine::Array< T >::begin ( )
inline

Begin iterator

for (Array<char>::iterator it=a.begin() ; it!=a.end(); ++it) // Iterator
*it='x'; // ...

◆ begin() [2/2]

template<typename T>
const_iterator Imagine::Array< T >::begin ( ) const
inline

Begin const iterator

for (Array<char>::const_iterator cit=a.begin() ; cit!=a.end(); ++cit) // const iterator
cout << *cit << endl; // ...

◆ clone()

template<typename T>
Array Imagine::Array< T >::clone ( ) const
inline

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

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

◆ data() [1/2]

template<typename T>
T* Imagine::Array< T >::data ( )
inline

Pointer to data for reading and writing

Returns
pointer to array elements
b.data()[2]=x; // data pointer (write)

◆ data() [2/2]

template<typename T>
const T* Imagine::Array< T >::data ( ) const
inline

Pointer to data for reading only

Returns
const pointer to elements
x=b.data()[2]; // data pointer (read)

◆ empty()

template<typename T>
bool Imagine::Array< T >::empty ( ) const
inline

True if array is not allocated

Returns
emptyness
if (a.empty()) // is empty?
cout<< "a is empty" << endl;// ...

◆ end() [1/2]

template<typename T>
iterator Imagine::Array< T >::end ( )
inline

End iterator

for (Array<char>::iterator it=a.begin() ; it!=a.end(); ++it) // Iterator
*it='x'; // ...

◆ end() [2/2]

template<typename T>
const_iterator Imagine::Array< T >::end ( ) const
inline

End const iterator

for (Array<char>::const_iterator cit=a.begin() ; cit!=a.end(); ++cit) // const iterator
cout << *cit << endl; // ...

◆ fill()

template<typename T>
Array& Imagine::Array< T >::fill ( const T &  x)
inline

Fills with constant value

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

◆ getSubArray()

template<typename T>
Array Imagine::Array< T >::getSubArray ( size_t  offset,
size_t  size 
) const
inline

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

Parameters
offsetposition of first element of sub array
sizesize of sub array
Returns
sub array
e=b.getSubArray(1,2); // sub array (fresh memory)

◆ operator!=()

template<typename T>
bool Imagine::Array< T >::operator!= ( const Array< T > &  A) const
inline

Inequality test (component wise)

Parameters
Aarray for comparison
Returns
true if *this is different from b
for (Array<char>::const_iterator cit=a.begin() ; cit!=a.end(); ++cit) // const iterator

◆ operator=() [1/2]

template<typename T>
Array& Imagine::Array< T >::operator= ( const Array< T > &  A)
inline

Assigns from another Array (sharing its memory)

Parameters
Aarray to be assigned to
Returns
self reference
a=b; // assignment (sharing memory)

◆ operator=() [2/2]

template<typename T>
template<typename T2 >
Array& Imagine::Array< T >::operator= ( const Array< T2 > &  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
di=b; // different type assignment

◆ operator==()

template<typename T>
bool Imagine::Array< T >::operator== ( const Array< T > &  A) const
inline

Equality test, component wise, i.e. if arrays are of the same size and with the same values

Parameters
Aarray for comparison
Returns
true if *this equals A
if (a==b) cout << "a equals b" << endl; // ==

◆ operator[]() [1/2]

template<typename T>
const T& Imagine::Array< T >::operator[] ( size_t  i) const
inline

Reads ith element

Parameters
ielement index (from 0 to size-1)
Returns
Const reference to ith element.
x=b[2]; // read access []

◆ operator[]() [2/2]

template<typename T>
T& Imagine::Array< T >::operator[] ( size_t  i)
inline

Writes ith element

Parameters
ielement index (from 0 to size-1)
Returns
Reference to ith element.
b[2]=x; // write access []

◆ setSize()

template<typename T>
void Imagine::Array< T >::setSize ( size_t  size)
inline

Change array size:

  • (re)allocates 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 the old memory)
  • if the new size is the same as the old one, does nothing
b.setSize(5); // setSize

◆ size()

template<typename T>
size_t Imagine::Array< T >::size ( ) const
inline

Number of elements

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

Friends And Related Function Documentation

◆ operator<<

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

Writes Array to stream (size and values)

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

◆ operator>>

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

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

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

◆ prange

template<typename T>
std::pair<T,T> prange ( const Array< T > &  A)
friend

Compute pointwise min and pointwise max of component values (provided pointwise comparisons are defined for type T, e.g. FVector)

Parameters
Ainput array
Returns
pair of pmin, pmax values
Array< Coords<2> > f(2); // pointwise range
f[0]=Coords<2>(1,5);
f[1]=Coords<2>(4,2);
/*pair<Coords<2>,Coords<2> > p2=*/prange(f); // ...

◆ range

template<typename T>
std::pair<T,T> range ( const Array< T > &  A)
friend

Compute min and max of component values (provided comparisons are defined for type T)

Parameters
Ainput array
Returns
pair of min,max values
/*pair<char,char> p=*/range(a); // range
Array< Coords<2> > f(2); // pointwise range
f[0]=Coords<2>(1,5);
f[1]=Coords<2>(4,2);
/*pair<Coords<2>,Coords<2> > p2=*/prange(f); // ...

◆ read

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

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

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

◆ write

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

Writes Array to binary stream (size and values)

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


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