Classes | |
class | lite::array< signature_, traits_type_, rep_ > |
This class represents a general sinlge/multidimensional array with constant/variable size dimensions. More... |
You can use the lite::array class to represent a general sinlge/multidimensional array with constant/variable size dimensions. The array elements could be of any type that has an appropriate default constructor and an assignment operator.
The lite::array class provides several specializations. The following pseudo definition explains the (almost) common interface of all the specializations. There are other features specific to some of the specializations that are not included below but are explained in lite::array.
template< typename signature_, typename traits_type_ = default_array_traits, typename rep_ = typename traits_type_::template representation_type<signature_>::type> class array<signature_, traits_type_, rep_> { public: typedef signature_ signature; typedef traits_type_ traits_type; typedef ??? value_type; typedef ??? iterator; typedef ??? size_type; typedef ??? const_iterator; typedef ??? reference; typedef ??? const_reference; typedef ??? temporary_array; typedef ??? fwd_temporary_array; typedef ??? rev_temporary_array; static const int dimensions = ???; array(); array(const array& other); // constructs an array by copying from other template<typename other_rep_> array(const array<signature_, traits_type_, other_rep_>& other); // constructs an array with the given dimension sizes explicit array(int n0, ..., int nN); // constructs an array with the given size explicit array(const size_type& size); // constructs an array using the provided values array(const value_type& a0, ..., const value_type& aT); array& operator=(const array& other); template<typename other_rep_> array& operator=(const array<signature_, traits_type_, other_rep_>& other); const_iterator begin() const; iterator begin(); size_type size() const; const_reference operator()(int i0, ..., int iN) const; reference operator()(int i0, ..., int iN); const_reference operator[](int i0) const; reference operator[](int i0); template<typename transform_type_> const typename transform_traits<array, transform_type_>::const_array operator[](const transform_type_& trans) const; template<typename transform_type_> const typename transform_traits<array, transform_type_>::array operator[](const transform_type_& trans); void resize(int n0, ..., int nN); void resize(const size_type& sz); void release(); };
To declare an array simply provide the array signature as the template parameter of the lite::array class. Any dimension that is declared with size 1 will have a variable size. Other dimensions will have fixed sizes. The size of the dimensions that have a non-constant size can be specified as a constructor argument or later by calling the lite::array::resize(). For example:
array<float[10][10]> m1; array<float[3]> v; int n; std::cin >> n; array<float[10][1][1]> m2(10, n, n); // 10 by n by n array array<float[10][1][1]> m3; m3.resize(10, n, n);
The size information of an array type A
is represented by an object of type A::size_type. This is a an object of template class lite::pack and has an int
element corresponding to each dimension of the array holding the size of that dimension. You can also use a size object to construct or resize and array. For example:
typedef array<float[1][10]> A; A::size_type sz(10, 10); A m1(sz), m2; std::cin >> sz; m2.resize(sz); std::cin >> m2; A::size_type sz2 = m2.size(); if (sz2 != m1.size()) { // size objects can be compared // error } for (int i=0; i < sz.i0; i++) for (int j=0; j < sz.i1; j++) m1(i,j)= i+j; assert(sizeof(sz)==sizeof(int)); // true because sz.i1 is a static const member and won't occupy // space in the object. However, sz.i1 is a non-static member of sz
To get the current size of an array, you can call lite::array::size().
Fixed size arrays can also be filled with some value at construction time:
array<float[20][5]> m1 = 4.1;
// is equivalent to
array<float[20][5]> m2;
m2 = 4.1;
You can also provide the initial value of elements of small 1-dimensional arrays (vectors) as a constructor parameter. For example:
array<float[2]> v2(1.1, 2.2);
array<float[3]> v3(1.1, 2.2, 3.3);
// is equivalent to
array<float[2]> u2;
u2[0] = 1.1; u2[1] = 2.2;
Dimensions that have a fixed size are declared as constant elements of the lite::pack and won't occupy memory in the size_type
(see lite::pack for more information on constant element). Arrays with fixed size are allocated at the time of construction. Arrays with variable size are allocated once you construct or resize them with a non-zero size.
You can access the element of an array using lite::array::operator()(). For 1-dimensional arrays you can also use the lite::array::operator[](int) as seen in the previous examples.
This parameter takes a traits class the is used to choose the proper representation type for an array based on its signature and possibly to specify other policies. You normally don't to specify this parameter and the default value of lite::default_array_traits should be fine. For more information see Array Traits.
The last template parameter of the class lite::array specifies the representation type of the array. Normally, you don't need to specify this parameter and the default value works fine. However, should you need to change the default array storage order or to use a non-default representation for another reason, you may need to specify a different value for this parameter.
Basically, there are two category of array representation types. primary arrays and reference arrays. Each category is explained bellow:
Whenever you declare an array by itself (as in all the previous examples), the array class chooses a primary representation. Primary arrays are standalone objects that (in most cases) own their storage. The storage might be allocated internally as part of the array object (see Internal Representation) if the array has only fixed size dimensions and is not too large. Or it might be allocated on the heap (see Hybrid Representation) if it has a non-constant size dimension or is too large.
Reference arrays are usually returned as a result of applying an operation or applying a transformation to another array. They are virtual arrays that don't own their storage and usually reference all or parts of another array or arrays. For example:
array<float[10][10]> a, b; std::cout << (5*a).size(); std::cout << (a+b)(5,5);
In the above example (a+b)
returns a reference array that computes each of its elements on-demand by adding the corresponding element of a
and b
. Some reference arrays (like the previous example) are read-only. Other reference arrays like the following example allow modification of the original array through the reference array:
The expression a[row(0)]
returns a reference array to the first row of a
. see Reference Representation for more information on the standard representation type for reference arrays.