Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/valarray/slice"

From cppreference.com
< cpp‎ | numeric‎ | valarray
m (Non-member functions: synthesized !=)
m (more "indices".)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{cpp/title|slice}}
 
{{cpp/title|slice}}
 
{{cpp/numeric/valarray/navbar}}
 
{{cpp/numeric/valarray/navbar}}
{{ddcl | header=valarray |
+
{{ddcl|header=valarray|
 
class slice;
 
class slice;
 
}}
 
}}
  
{{tt|std::slice}} is the selector class that identifies a subset of {{lc|std::valarray}} similar to [[enwiki:BLAS|BLAS]] slice. An object of type {{tt|std::slice}} holds three values: the starting index, the stride, and the total number of values in the subset. Objects of type {{tt|std::slice}} can be used as indexes with valarray's {{tt|operator[]}}.
+
{{tt|std::slice}} is the selector class that identifies a subset of {{lc|std::valarray}} similar to {{enwiki|BLAS}} slice. An object of type {{tt|std::slice}} holds three values: the starting index, the stride, and the total number of values in the subset. Objects of type {{tt|std::slice}} can be used as indices with valarray's {{c/core|operator[]}}.
  
 
===Member functions===
 
===Member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc mem ctor | cpp/numeric/valarray/slice | inlinemem=true | constructs a slice}}
+
{{dsc mem ctor|cpp/numeric/valarray/slice|inlinemem=true|constructs a slice}}
{{dsc mem fun | cpp/numeric/valarray/slice | title=start<br/>size<br/>stride | inlinemem=true | returns the parameters of the slice}}
+
{{dsc mem fun|cpp/numeric/valarray/slice|title=start<br>size<br>stride|inlinemem=true|returns the parameters of the slice}}
 
{{dsc end}}
 
{{dsc end}}
  
{{member | {{small|std::slice::}}slice |
+
{{member|{{small|std::slice::}}slice|
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | num=1 |
+
{{dcl|num=1|
 
slice()
 
slice()
 
}}
 
}}
{{dcl | num=2 |
+
{{dcl|num=2|
 
slice( std::size_t start, std::size_t size, std::size_t stride );
 
slice( std::size_t start, std::size_t size, std::size_t stride );
 
}}
 
}}
{{dcl | num=3 |
+
{{dcl|num=3|
 
slice( const slice& other );
 
slice( const slice& other );
 
}}
 
}}
Line 30: Line 30:
 
@1@ Default constructor. Equivalent to {{c|slice(0, 0, 0)}}. This constructor exists only to allow construction of arrays of slices.
 
@1@ Default constructor. Equivalent to {{c|slice(0, 0, 0)}}. This constructor exists only to allow construction of arrays of slices.
  
@2@ Constructs a new slice with parameters {{tt|start}}, {{tt|size}}, {{tt|stride}}. This slice will refer to {{tt|size}} number of elements, each with the position:
+
@2@ Constructs a new slice with parameters {{c|start}}, {{c|size}}, {{c|stride}}. This slice will refer to {{c|size}} number of elements, each with the position:
  
:@@ {{math|start + 0*stride}}
+
:@@ {{math|start + 0 * stride}}
:@@ {{math|start + 1*stride}}
+
:@@ {{math|start + 1 * stride}}
 
:@@ {{math|...}}
 
:@@ {{math|...}}
:@@ {{math|start + (size-1)*stride}}
+
:@@ {{math|start + (size - 1) * stride}}
  
@3@ Constructs a copy of {{tt|other}}.
+
@3@ Constructs a copy of {{c|other}}.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | start | the position of the first element}}
+
{{par|start|the position of the first element}}
{{par | size | the number of elements in the slice}}
+
{{par|size|the number of elements in the slice}}
{{par | stride | the number of positions between successive elements in the slice}}
+
{{par|stride|the number of positions between successive elements in the slice}}
{{par | other | another slice to copy}}
+
{{par|other|another slice to copy}}
 
{{par end}}
 
{{par end}}
 
}}
 
}}
  
 
+
{{member|{{small|std::slice::}}start, size, stride|
{{member | {{small|std::slice::}}start, size, stride |
+
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | num=1 |
+
{{dcl|num=1|
 
std::size_t start() const;
 
std::size_t start() const;
 
}}
 
}}
{{dcl | num=2 |
+
{{dcl|num=2|
 
std::size_t size() const;
 
std::size_t size() const;
 
}}
 
}}
{{dcl | num=3 |
+
{{dcl|num=3|
 
std::size_t stride() const;
 
std::size_t stride() const;
 
}}
 
}}
Line 76: Line 75:
 
===Non-member functions===
 
===Non-member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc fun | cpp/numeric/valarray/slice | title=operator=={{dsc small|(std::slice)}} | notes={{mark c++20}} | inlinemem=true | checks if two slices are equal }}
+
{{dsc fun|cpp/numeric/valarray/slice|title=operator=={{dsc small|(std::slice)}}|notes={{mark c++20}}|inlinemem=true|checks if two slices are equal}}
 
{{dsc end}}
 
{{dsc end}}
  
{{member | 1=operator==<small>(std::slice)</small> | 2=
+
{{member|1=operator=={{petty|(std::slice)}}|2=
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | since=c++20 | 1=
+
{{dcl|since=c++20|1=
 
friend bool operator==( const slice& lhs, const slice& rhs );
 
friend bool operator==( const slice& lhs, const slice& rhs );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
Checks if the parameters of {{tt|lhs}} and {{tt|rhs}} - start, size and stride are equal respectively.
+
Checks if the parameters of {{c|lhs}} and {{c|rhs}} - start, size and stride are equal respectively.
  
 
{{cpp/hidden friend|std::slice}}
 
{{cpp/hidden friend|std::slice}}
Line 94: Line 93:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | lhs, rhs | slices to compare }}
+
{{par|lhs, rhs|slices to compare}}
 
{{par end}}
 
{{par end}}
  
Line 103: Line 102:
 
===Example===
 
===Example===
 
{{example
 
{{example
| Barebones valarray-backed Matrix class with a [[enwiki:Trace (linear algebra)|trace]] calculating function.
+
|Barebones valarray-backed Matrix class with a {{enwiki|Trace (linear algebra)|trace}} calculating function.
| code=
+
|code=
 
#include <iostream>
 
#include <iostream>
 
#include <valarray>
 
#include <valarray>
class Matrix {
+
 
 +
class Matrix
 +
{
 
     std::valarray<int> data;
 
     std::valarray<int> data;
 
     int dim;
 
     int dim;
public:
+
public:
 
     Matrix(int r, int c) : data(r*c), dim(c) {}
 
     Matrix(int r, int c) : data(r*c), dim(c) {}
     int& operator()(int r, int c) {return data[r*dim + c];}
+
     int& operator()(int r, int c) { return data[r * dim + c]; }
     int trace() const {
+
     int trace() const { return data[std::slice(0, dim, dim + 1)].sum(); }
        return data[std::slice(0, dim, dim+1)].sum();
+
    }
+
 
};
 
};
 +
 
int main()
 
int main()
 
{
 
{
     Matrix m(3,3);
+
     Matrix m(3, 3);
 
     int n = 0;
 
     int n = 0;
     for(int r=0; r<3; ++r)
+
     for (int r = 0; r < 3; ++r)
       for(int c=0; c<3; ++c)
+
       for (int c = 0; c < 3; ++c)
 
           m(r, c) = ++n;
 
           m(r, c) = ++n;
 
     std::cout << "Trace of the matrix (1,2,3) (4,5,6) (7,8,9) is " << m.trace() << '\n';
 
     std::cout << "Trace of the matrix (1,2,3) (4,5,6) (7,8,9) is " << m.trace() << '\n';
 
}
 
}
| output=
+
|output=
 
Trace of the matrix (1,2,3) (4,5,6) (7,8,9) is 15
 
Trace of the matrix (1,2,3) (4,5,6) (7,8,9) is 15
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=543|std=C++98|before=it was unclear whether a default constructed slice is usable|after=it is usable (as an empty subset)}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/numeric/valarray/dsc operator_at}}
+
{{dsc inc|cpp/numeric/valarray/dsc operator_at}}
{{dsc inc | cpp/numeric/valarray/dsc gslice}}
+
{{dsc inc|cpp/numeric/valarray/dsc gslice}}
{{dsc inc | cpp/numeric/valarray/dsc slice_array}}
+
{{dsc inc|cpp/numeric/valarray/dsc slice_array}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 22:56, 8 April 2024

 
 
 
 
Defined in header <valarray>
class slice;

std::slice is the selector class that identifies a subset of std::valarray similar to BLAS slice. An object of type std::slice holds three values: the starting index, the stride, and the total number of values in the subset. Objects of type std::slice can be used as indices with valarray's operator[].

Contents

[edit] Member functions

(constructor)
constructs a slice
(public member function)
startsizestride
returns the parameters of the slice
(public member function)

std::slice::slice

slice()
(1)
slice( std::size_t start, std::size_t size, std::size_t stride );
(2)
slice( const slice& other );
(3)

Constructs a new slice.

1) Default constructor. Equivalent to slice(0, 0, 0). This constructor exists only to allow construction of arrays of slices.
2) Constructs a new slice with parameters start, size, stride. This slice will refer to size number of elements, each with the position:
start + 0 * stride
start + 1 * stride
...
start + (size - 1) * stride
3) Constructs a copy of other.

Parameters

start - the position of the first element
size - the number of elements in the slice
stride - the number of positions between successive elements in the slice
other - another slice to copy

std::slice::start, size, stride

std::size_t start() const;
(1)
std::size_t size() const;
(2)
std::size_t stride() const;
(3)

Returns the parameters passed to the slice on construction - start, size and stride respectively.

Parameters

(none)

Return value

The parameters of the slice -- start, size and stride respectively.

Complexity

Constant.

[edit] Non-member functions

operator==(std::slice)
(C++20)
checks if two slices are equal
(function)

operator==(std::slice)

friend bool operator==( const slice& lhs, const slice& rhs );
(since C++20)

Checks if the parameters of lhs and rhs - start, size and stride are equal respectively.

This function is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::slice is an associated class of the arguments.

The != operator is synthesized from operator==.

Parameters

lhs, rhs - slices to compare

Return value

lhs.start() == rhs.start() && lhs.size() == rhs.size() && lhs.stride() == rhs.stride()

[edit] Example

Barebones valarray-backed Matrix class with a trace calculating function.

#include <iostream>
#include <valarray>
 
class Matrix
{
    std::valarray<int> data;
    int dim;
public:
    Matrix(int r, int c) : data(r*c), dim(c) {}
    int& operator()(int r, int c) { return data[r * dim + c]; }
    int trace() const { return data[std::slice(0, dim, dim + 1)].sum(); }
};
 
int main()
{
    Matrix m(3, 3);
    int n = 0;
    for (int r = 0; r < 3; ++r)
       for (int c = 0; c < 3; ++c)
           m(r, c) = ++n;
    std::cout << "Trace of the matrix (1,2,3) (4,5,6) (7,8,9) is " << m.trace() << '\n';
}

Output:

Trace of the matrix (1,2,3) (4,5,6) (7,8,9) is 15

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 543 C++98 it was unclear whether a default constructed slice is usable it is usable (as an empty subset)

[edit] See also

get/set valarray element, slice, or mask
(public member function) [edit]
generalized slice of a valarray: starting index, set of lengths, set of strides
(class) [edit]
proxy to a subset of a valarray after applying a slice
(class template) [edit]