Apache C++ Standard Library Reference Guide

## gslice_array

Library: Numerics

    Does not inherit

- Local Index
- Summary
- Synopsis
- Description
- Interface
- Constructors
- Assignment Operators
- Computed Assignment Operators
- Example
- See Also
- Standards Conformance

### Local Index

#### Members

gslice_array() operator%=() operator&=() | operator>>=() operator<<=() operator*=() | operator+=() operator-=() operator/=() | operator=() operator^=() operator|=()

### Summary

A numeric array class used to represent a BLAS-like slice from a valarray

### Synopsis

    #include <valarray>

    namespace std {
      template <class T>
      class gslice_array;
    }

### Description

gslice_array creates a gslice view into a valarray. A gslice_array can only be produced by applying the gslice subscript operator to a valarray. The elements in a gslice_array are references to selected elements in the valarray (so changing an element in the gslice_array really changes the corresponding element in the valarray). A gslice_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 gslice_array to a valarray using either the valarray copy constructor or the assignment operator. Reference semantics are lost at that point.

### Interface

    namespace std{

      template <class T> class gslice_array {
      public:

        // types
        typedef T value_type;

        // destructor
        ~gslice_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
        gslice_array();
        gslice_array(const gslice_array<T>&);

        // operator =
        gslice_array<T>& operator= (const gslice_array<T>& array);
      };
    }

### Constructors

    gslice_array();
    gslice_array(const gslice_array&);

All gslice_array constructors are private and cannot be called directly. This prevents copy construction of gslice_arrays.

### Assignment Operators

    void operator=(const valarray<T>& x) const;

Assigns values from `x` to the selected elements of the valarray that self refers to. Remember that a gslice_array never holds any elements itself; it simply refers to selected elements in the valarray used to generate it.

    gslice_array<T>&
    operator=(const gslice_array<T>& x);

Private assignment operator. Cannot be called directly, thus preventing assignment between gslice_arrays.

    void operator=(const T& x) const;

Assigns `x` to the selected elements of the valarray that self refers to.

### Computed Assignment Operators

    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 a gslice_array never holds any elements itself; it simply refers to selected elements in the valarray used to generate it.

### Example

    //
    //  gslice_array.cpp
    //

    #include <valarray.h>  // Includes valarray and
                           // provides stream inserter.

    typedef std::valarray<int> valarray_t;

    int main(void) {

      valarray_t::value_type
          ibuf[22] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10,
                       11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 };

      // Create a valarray of ints
      valarray_t  vi (ibuf,(sizeof ibuf / sizeof *ibuf));

      // Print out the valarray
      std::cout << "original valarray vi:\n\n" << vi << "\n\n";

      // Get a two dimensional diagonal slice out of the middle
      size_t length_ary[] = {4, 2};
      size_t stride_ary[] = {4, 6};

      std::valarray<size_t> length_val(length_ary, 2);
      std::valarray<size_t> stride_val(stride_ary, 2);

      // Print out the slices starting at positions 0 and 2
      // respectively.
      std::cout << "vi[gslice(0,[4,2],[4,6])]\n\n"
                << vi[std::gslice(0,length_val,stride_val)]
                << "\n\n";

      std::cout << "vi[gslice(2,[4,2],[4,6])]\n\n"
                << vi[std::gslice(2,length_val,stride_val)]
                << "\n\n";

      // Multiply the first slice by the second.
      vi[std::gslice(0,length_val,stride_val)]
          *= static_cast<valarray_t > (vi[std::gslice(2,length_val,
                                                      stride_val)]);

      std::cout << "vi[gslice(0,[4,2],[4,8])] *= "
                << "vi[gslice(2,[4,2],[4,6])]\n\n"
                << vi << std::endl;

      return 0;

    }

    Program Output:

    original valarray vi:

    [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]

    vi[gslice(0,[4,2],[4,6])]

    [0,6,4,10,8,14,12,18]

    vi[gslice(2,[4,2],[4,6])]

    [2,8,6,12,10,16,14,20]

    vi[gslice(0,[4,2],[4,8])] *= vi[gslice(2,[4,2],[4,6])]

    [0,1,2,3,24,5,48,7,80,9,120,11,168,13,224,15,16,17,360,19,20,21]

### See Also

slice, valarray, gslice, slice_array, mask_array, indirect_array

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 26.3.7