.\" ident @(#)mask_array.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH mask_array 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2mask_array\fP \ - A numeric array class that gives a masked view of a valarray. .SH SYNOPSIS .RE .RS 0 #include .br template .br class mask_array; .SH DESCRIPTION mask_array gives a masked view into a valarray. mask_arrays are only produced by applying the mask subscript operator to a valarray. This subscript operator takes a \f2valarray\fP argument and produces a mask_array. Only the elements in the valarray whose corresponding elements in the \f2valarray\fP argument were \f2true\fP are selected by the mask_array. The elements in a mask_array are references to selected elements in the valarray (so changing an element in the mask_array really changes the corresponding element in the valarray). A mask_array does not itself hold any distinct elements. The template cannot be instantiated directly since all its constructors are private. However, you can copy a slice_array to a valarray using either the valarray copy constructor or the assignment operator. Reference semantics are lost at that point. .SH INTERFACE .br template class mask_array { .br public: .br .RE .RS 2 // types .RE .RS 1 typedef T value_type; .RE .RS 0 .RE .RS 2 // destructor .br ~mask_array(); .RE .RS 0 .RE .RS 2 // public assignment .RE .RS 1 void operator= (const valarray& array) const; .RE .RS 2 // computed assignment .RE .RS 1 void operator*= (const valarray& array) const; .br void operator/= (const valarray& array) const; .br void operator%= (const valarray& array) const; .br void operator+= (const valarray& array) const; .br void operator-= (const valarray& array) const; .br void operator^= (const valarray& array) const; .br void operator&= (const valarray& array) const; .br void operator|= (const valarray& array) const; .br void operator<<= (const valarray& array) const; .br void operator>>= (const valarray& array) const; .br .RE .RS 2 // other .RE .RS 1 void operator= (const T&) const; .RE .RS 0 .br private: .RE .RS 2 // constructors .RE .RS 1 mask_array(); .br mask_array(const mask_array&); .RE .RS 2 // operator = .RE .RS 1 mask_array& operator= (const mask_array& array); .RE .RS 0 }; .SH CONSTRUCTORS .br mask_array(); .br mask_array(const mask_array&); .RE .RS 3 All mask_array constructors are private and cannot be called directly. This prevents copy construction of mask_arrays. .RE .SH ASSIGNMENT OPERATORS .br void operator=(const valarray& x) const; .RE .RS 3 Assigns values from \f2x \fPto the selected elements of the valarray that self refers to. Remember that a mask_array never holds any elements itself, it simply refers to selected elements in the valarray used to generate it. .RE .br mask_array& .br operator=(const mask-_array& x); .RE .RS 3 Private assignment operator. Cannot be called directly, thus preventing assignment between mask_arrays. .RE .SH COMPUTED ASSIGNMENT OPERATORS .br void operator*=(const valarray& val) const; .br void operator/=(const valarray& val) const; .br void operator%=(const valarray& val) const; .br void operator+=(const valarray& val) const; .br void operator-=(const valarray& val) const; .br void operator^=(const valarray& val) const; .br void operator&=(const valarray& val) const; .br void operator|=(const valarray& val) const; .br void operator<<=(const valarray& val) const; .br void operator>>=(const valarray& val) const; .RE .RS 3 Applies the indicated operation using elements from \f2val\fP to the selected elements of the valarray that self refers to. Remember that a mask_array never holds any elements itself; it simply refers to selected elements in the valarray used to generate it. .RE .SH MEMBER FUNCTIONS .br void operator= (const T& x) const; .RE .RS 3 Assigns \f2x\fP to the selected elements of the valarray that self refers to. .RE .SH EXAMPLE .br // .br // mask_array.cpp .br // .br #include "valarray.h" // Contains a valarray stream inserter .br using namespace std; .br .br int main(void) .br { .RE .RS 1 int ibuf[10] = {0,1,2,3,4,5,6,7,8,9}; .br bool mbuf[10] = {1,0,1,1,1,0,0,1,1,0}; .RE .RS 0 .RE .RS 2 // create a valarray of ints .RE .RS 1 valarray vi(ibuf,10); .RE .RS 0 .RE .RS 2 // create a valarray of bools for a mask .RE .RS 1 valarray mask(mbuf,10); .RE .RS 0 .RE .RS 2 // print out the valarray .RE .RS 1 cout << vi << endl; .RE .RS 0 .RE .RS 2 // Get a mask array and assign that mask to another array .RE .RS 1 mask_array msk = vi[mask]; .br valarray vi3 = msk; .RE .RS 0 .RE .RS 2 // print out the masked_array .RE .RS 1 cout << vi3 << endl; .RE .RS 0 .RE .RS 2 // Double the masked values .RE .RS 1 msk += vi3; .RE .RS 0 .RE .RS 2 // print out vi1 again .RE .RS 1 cout << vi << endl; .RE .RS 0 .RE .RS 1 return 0; .RE .RS 0 } .br .RE .RS 0 Program Output .br .br [0,1,2,3,4,5,6,7,8,9] .br [0,2,3,4,7,8] .br [0,1,4,6,8,5,6,14,16,9] .SH WARNINGS If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO slice, slice_array, valarray, gslice, gslice_array, indirect_array