📄 valarray
字号:
// The template and inlines for the -*- C++ -*- valarray class.// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004// Free Software Foundation, Inc.//// This file is part of the GNU ISO C++ Library. This library is free// software; you can redistribute it and/or modify it under the// terms of the GNU General Public License as published by the// Free Software Foundation; either version 2, or (at your option)// any later version.// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.// You should have received a copy of the GNU General Public License along// with this library; see the file COPYING. If not, write to the Free// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,// USA.// As a special exception, you may use this file as part of a free software// library without restriction. Specifically, if other files instantiate// templates or use macros or inline functions from this file, or you compile// this file and link it with other files to produce an executable, this// file does not by itself cause the resulting executable to be covered by// the GNU General Public License. This exception does not however// invalidate any other reasons why the executable file might be covered by// the GNU General Public License.// Written by Gabriel Dos Reis <Gabriel.Dos-Reis@DPTMaths.ENS-Cachan.Fr>/** @file valarray * This is a Standard C++ Library header. You should @c #include this header * in your programs, rather than any of the "st[dl]_*.h" implementation files. */#ifndef _GLIBCXX_VALARRAY#define _GLIBCXX_VALARRAY 1#pragma GCC system_header#include <bits/c++config.h>#include <cstddef>#include <cmath>#include <cstdlib>#include <numeric>#include <algorithm>#include <debug/debug.h>namespace std{ template<class _Clos, typename _Tp> class _Expr; template<typename _Tp1, typename _Tp2> class _ValArray; template<class _Oper, template<class, class> class _Meta, class _Dom> struct _UnClos; template<class _Oper, template<class, class> class _Meta1, template<class, class> class _Meta2, class _Dom1, class _Dom2> class _BinClos; template<template<class, class> class _Meta, class _Dom> class _SClos; template<template<class, class> class _Meta, class _Dom> class _GClos; template<template<class, class> class _Meta, class _Dom> class _IClos; template<template<class, class> class _Meta, class _Dom> class _ValFunClos; template<template<class, class> class _Meta, class _Dom> class _RefFunClos; template<class _Tp> class valarray; // An array of type _Tp class slice; // BLAS-like slice out of an array template<class _Tp> class slice_array; class gslice; // generalized slice out of an array template<class _Tp> class gslice_array; template<class _Tp> class mask_array; // masked array template<class _Tp> class indirect_array; // indirected array} // namespace std#include <bits/valarray_array.h>#include <bits/valarray_before.h> namespace std{ /** * @brief Smart array designed to support numeric processing. * * A valarray is an array that provides constraints intended to allow for * effective optimization of numeric array processing by reducing the * aliasing that can result from pointer representations. It represents a * one-dimensional array from which different multidimensional subsets can * be accessed and modified. * * @param Tp Type of object in the array. */ template<class _Tp> class valarray { template<class _Op> struct _UnaryOp { typedef typename __fun<_Op, _Tp>::result_type __rt; typedef _Expr<_UnClos<_Op, _ValArray, _Tp>, __rt> _Rt; }; public: typedef _Tp value_type; // _lib.valarray.cons_ construct/destroy: /// Construct an empty array. valarray(); /// Construct an array with @a n elements. explicit valarray(size_t); /// Construct an array with @a n elements initialized to @a t. valarray(const _Tp&, size_t); /// Construct an array initialized to the first @a n elements of @a t. valarray(const _Tp* __restrict__, size_t); /// Copy constructor. valarray(const valarray&); /// Construct an array with the same size and values in @a sa. valarray(const slice_array<_Tp>&); /// Construct an array with the same size and values in @a ga. valarray(const gslice_array<_Tp>&); /// Construct an array with the same size and values in @a ma. valarray(const mask_array<_Tp>&); /// Construct an array with the same size and values in @a ia. valarray(const indirect_array<_Tp>&); template<class _Dom> valarray(const _Expr<_Dom,_Tp>& __e); ~valarray(); // _lib.valarray.assign_ assignment: /** * @brief Assign elements to an array. * * Assign elements of array to values in @a v. Results are undefined * if @a v is not the same size as this array. * * @param v Valarray to get values from. */ valarray<_Tp>& operator=(const valarray<_Tp>&); /** * @brief Assign elements to a value. * * Assign all elements of array to @a t. * * @param t Value for elements. */ valarray<_Tp>& operator=(const _Tp&); /** * @brief Assign elements to an array subset. * * Assign elements of array to values in @a sa. Results are undefined * if @a sa is not the same size as this array. * * @param sa Array slice to get values from. */ valarray<_Tp>& operator=(const slice_array<_Tp>&); /** * @brief Assign elements to an array subset. * * Assign elements of array to values in @a ga. Results are undefined * if @a ga is not the same size as this array. * * @param ga Array slice to get values from. */ valarray<_Tp>& operator=(const gslice_array<_Tp>&); /** * @brief Assign elements to an array subset. * * Assign elements of array to values in @a ma. Results are undefined * if @a ma is not the same size as this array. * * @param ma Array slice to get values from. */ valarray<_Tp>& operator=(const mask_array<_Tp>&); /** * @brief Assign elements to an array subset. * * Assign elements of array to values in @a ia. Results are undefined * if @a ia is not the same size as this array. * * @param ia Array slice to get values from. */ valarray<_Tp>& operator=(const indirect_array<_Tp>&); template<class _Dom> valarray<_Tp>& operator= (const _Expr<_Dom,_Tp>&); // _lib.valarray.access_ element access: /** * Return a reference to the i'th array element. * * @param i Index of element to return. * @return Reference to the i'th element. */ _Tp& operator[](size_t); // _GLIBCXX_RESOLVE_LIB_DEFECTS // 389. Const overload of valarray::operator[] returns by value. const _Tp& operator[](size_t) const; // _lib.valarray.sub_ subset operations: /** * @brief Return an array subset. * * Returns a new valarray containing the elements of the array * indicated by the slice argument. The new valarray is the size of * the input slice. @see slice. * * @param s The source slice. * @return New valarray containing elements in @a s. */ _Expr<_SClos<_ValArray,_Tp>, _Tp> operator[](slice) const; /** * @brief Return a reference to an array subset. * * Returns a new valarray containing the elements of the array * indicated by the slice argument. The new valarray is the size of * the input slice. @see slice. * * @param s The source slice. * @return New valarray containing elements in @a s. */ slice_array<_Tp> operator[](slice); /** * @brief Return an array subset. * * Returns a slice_array referencing the elements of the array * indicated by the slice argument. @see gslice. * * @param s The source slice. * @return Slice_array referencing elements indicated by @a s. */ _Expr<_GClos<_ValArray,_Tp>, _Tp> operator[](const gslice&) const; /** * @brief Return a reference to an array subset. * * Returns a new valarray containing the elements of the array * indicated by the gslice argument. The new valarray is * the size of the input gslice. @see gslice. * * @param s The source gslice. * @return New valarray containing elements in @a s. */ gslice_array<_Tp> operator[](const gslice&); /** * @brief Return an array subset. * * Returns a new valarray containing the elements of the array * indicated by the argument. The input is a valarray of bool which * represents a bitmask indicating which elements should be copied into * the new valarray. Each element of the array is added to the return * valarray if the corresponding element of the argument is true. * * @param m The valarray bitmask. * @return New valarray containing elements indicated by @a m. */ valarray<_Tp> operator[](const valarray<bool>&) const; /** * @brief Return a reference to an array subset. * * Returns a new mask_array referencing the elements of the array * indicated by the argument. The input is a valarray of bool which * represents a bitmask indicating which elements are part of the * subset. Elements of the array are part of the subset if the * corresponding element of the argument is true. * * @param m The valarray bitmask. * @return New valarray containing elements indicated by @a m. */ mask_array<_Tp> operator[](const valarray<bool>&); /** * @brief Return an array subset. * * Returns a new valarray containing the elements of the array * indicated by the argument. The elements in the argument are * interpreted as the indices of elements of this valarray to copy to * the return valarray. * * @param i The valarray element index list. * @return New valarray containing elements in @a s. */ _Expr<_IClos<_ValArray, _Tp>, _Tp> operator[](const valarray<size_t>&) const; /** * @brief Return a reference to an array subset. * * Returns an indirect_array referencing the elements of the array * indicated by the argument. The elements in the argument are * interpreted as the indices of elements of this valarray to include * in the subset. The returned indirect_array refers to these * elements. * * @param i The valarray element index list. * @return Indirect_array referencing elements in @a i. */ indirect_array<_Tp> operator[](const valarray<size_t>&); // _lib.valarray.unary_ unary operators: /// Return a new valarray by applying unary + to each element. typename _UnaryOp<__unary_plus>::_Rt operator+() const; /// Return a new valarray by applying unary - to each element. typename _UnaryOp<__negate>::_Rt operator-() const; /// Return a new valarray by applying unary ~ to each element.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -