Library: Numerics
Does not inherit
|
indirect_array() operator%=() operator&=() |
operator>>=() operator<<=() operator*=() |
operator+=() operator-=() operator/=() |
operator=() operator^=() operator|=() |
A numeric array class used to represent elements selected from a valarray
#include <valarray>
namespace std {
template <class T>
class indirect_array;
}
indirect_array creates a selective view into a valarray. An indirect_array is produced by applying the indirect subscript operator to a valarray. The indirect array produced by this subscript contains only the elements of the valarray whose indices appear as values in the argument. The elements in an indirect_array are references to selected elements in the valarray. For this reason, changing an element in the indirect_array really changes the corresponding element in the valarray. An indirect_array does not itself hold any distinct elements. The template cannot be instantiated directly since all its constructors are private. However, you can copy an indirect_array to a valarray using either the valarray copy constructor or the assignment operator. Reference semantics are lost at that point.
namespace std{
template <class T> class indirect_array {
public:
// types
typedef T value_type;
// destructor
~indirect_array();
// public assignment
void operator=(const valarray<T>& array) const;
// computed assignment
void operator*=(const valarray<T>& array) const;
void operator/=(const valarray<T>& array) const;
void operator%=(const valarray<T>& array) const;
void operator+=(const valarray<T>& array) const;
void operator-=(const valarray<T>& array) const;
void operator^=(const valarray<T>& array) const;
void operator&=(const valarray<T>& array) const;
void operator|=(const valarray<T>& array) const;
void operator<<=(const valarray<T>& array) const;
void operator>>=(const valarray<T>& array) const;
// fill function
void operator=(const T&);
private:
// constructors
indirect_array();
indirect_array(const indirect_array<T>&);
// operator =
indirect_array<T>&
operator=(const indirect_array<T>& array);
};
}
indirect_array(); indirect_array(const indirect_array&);
All indirect_array constructors are private and cannot be called directly. This prevents copy construction of indirect_arrays.
void operator=(const valarray<T>& x) const;
Assigns values from x to the selected elements of the valarray that self refers to. Remember that an indirect_array never holds any elements itself; it simply refers to selected elements in the valarray used to generate it.
indirect_array<T>& operator=(const indirect-_array<T>& x);
Private assignment operator. Cannot be called directly, thus preventing assignment between indirect_arrays.
void operator=(const T& x);
Assigns x to the selected elements of the valarray that self refers to.
void operator*=(const valarray<T>& val) const; void operator/=(const valarray<T>& val) const; void operator%=(const valarray<T>& val) const; void operator+=(const valarray<T>& val) const; void operator-=(const valarray<T>& val) const; void operator^=(const valarray<T>& val) const; void operator&=(const valarray<T>& val) const; void operator|=(const valarray<T>& val) const; void operator<<=(const valarray<T>& val) const; void operator>>=(const valarray<T>& val) const;
Applies the indicated operation using elements from val to the selected elements of the valarray that self refers to. Remember that an indirect_array never holds any elements itself; it simply refers to selected elements in the valarray used to generate it.
//
// indirect_array.cpp
//
#include <valarray.h> // Includes valarray and
// provides stream inserter.
typedef std::valarray<int> valarray_t;
typedef std::valarray<size_t> selector_t;
int main(void) {
// Create a valarray of integers.
valarray_t::value_type vbuf[10] = {0,1,2,3,4,5,6,7,8,9};
valarray_t vi(vbuf, (sizeof vbuf / sizeof *vbuf));
// Create a valarray of indices for a selector.
selector_t::value_type sbuf[6] = {0,2,3,4,7,8};
selector_t selector(sbuf, (sizeof sbuf / sizeof *sbuf));
// Print out the valarray<int>.
std::cout << "original valarray vi\n\n" << vi << "\n\n";
// Print out the selective array.
std::cout << "vi[0,2,3,4,7,8]\n\n" << vi[selector] << "\n\n";
// Double the selected values.
vi[selector] += vi[selector];
// Print out the modified valarray.
std::cout << "vi[0,2,3,4,7,8] += vi[0,2,3,4,7,8]\n\n" << vi
<< std::endl;
return 0;
}
Program Output:
original valarray vi [0,1,2,3,4,5,6,7,8,9] vi[0,2,3,4,7,8] [0,2,3,4,7,8] vi[0,2,3,4,7,8] += vi[0,2,3,4,7,8] [0,1,4,6,8,5,6,14,16,9]
slice, slice_array, valarray, gslice, gslice_array, mask_array
ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 26.3.9