名前空間
変種
操作

std::gslice

From cppreference.com
< cpp‎ | numeric‎ | valarray
 
 
 
 
ヘッダ <valarray> で定義
class gslice;

std::gslice は、多層的なストライドとサイズのセットによって定義される std::valarray のインデックスの部分集合を識別するセレクタクラスです。std::gslice 型のオブジェクトは、valarray の operator[] のインデックスとして使用でき、たとえば、 valarray として表される多次元配列の列を選択するために使用できます。

開始値 s、ストライドのリスト ij、およびサイズのリスト dj が与えられた場合、これらの値から構築された std::gslice は、インデックスのセット kj=s+Σj(ijdj) を選択します。

たとえば、開始インデックス 3、ストライド {19,4,1}、および長さ {2,4,3} の gslice は、次の 24=2*4*3 個のインデックスのセットを生成します。

3 + 0*19 + 0*4 + 0*1 = 3,
3 + 0*19 + 0*4 + 1*1 = 4,
3 + 0*19 + 0*4 + 2*1 = 5,
3 + 0*19 + 1*4 + 0*1 = 7,
3 + 0*19 + 1*4 + 1*1 = 8,
3 + 0*19 + 1*4 + 2*1 = 9,
3 + 0*19 + 2*4 + 0*1 = 11,
...
3 + 1*19 + 3*4 + 1*1 = 35,
3 + 1*19 + 3*4 + 2*1 = 36

一部のインデックスを複数回選択する std::gslice オブジェクトを構築することも可能です。たとえば、上記の例でストライド {1,1,1} を使用した場合、インデックスは {3, 4, 5, 4, 5, 6, ...} になります。このような gslice は、std::valarray::operator[] の const バージョンの引数としてのみ使用できます。それ以外の場合、動作は未定義です。

目次

[edit] メンバ関数

(コンストラクタ)
汎用スライスを構築します。
(public member function)
startsizestride
スライスのパラメータを返します。
(public member function)

std::gslice::gslice

gslice()
(1)
gslice( std::size_t start, const std::valarray<std::size_t>& sizes,
                           const std::valarray<std::size_t>& strides );
(2)
gslice( const gslice& other );
(3)

新しい汎用スライスを構築します。

1) デフォルトコンストラクタ。 gslice(0, std::valarray<std::size_t>(), std::valarray<std::size_t>()) と同等です。このコンストラクタは、スライスの配列を構築できるようにするためにのみ存在します。
2) パラメータ startsizesstrides を持つ新しいスライスを構築します。
3) other のコピーを構築します。

パラメータ

start - 最初の要素の位置
サイズ - 各次元の要素数を定義する配列
ストライド - 各次元で連続する要素間の位置数を定義する配列
その他 - コピーする別のスライス


std::slice::start, size, stride

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

構築時にスライスに渡されたパラメータ(それぞれ start、sizes、strides)を返します。

パラメータ

(なし)

戻り値

スライスのパラメータ — それぞれ start、sizes、strides。

計算量

定数。

[edit]

3D配列の列のアドレス指定に gslice を使用する方法を示します。

#include <iostream>
#include <valarray>
 
void test_print(std::valarray<int>& v, int planes, int rows, int cols)
{
    for (int r = 0; r < rows; ++r)
    {
        for (int z = 0; z < planes; ++z)
        {
            for (int c = 0; c < cols; ++c)
                std::cout << v[z * rows * cols + r * cols + c] << ' ';
            std::cout << "  ";
        }
        std::cout << '\n';
    }
}
 
int main()
{
    std::valarray<int> v = // 3d array: 2 x 4 x 3 elements
        {111,112,113 , 121,122,123 , 131,132,133 , 141,142,143,
         211,212,213 , 221,222,223 , 231,232,233 , 241,242,243};
    // int ar3d[2][4][3]
    std::cout << "Initial 2x4x3 array:\n";
    test_print(v, 2, 4, 3);
 
    // update every value in the first columns of both planes
    v[std::gslice(0, {2, 4}, {4 * 3, 3})] = 1; // two level one strides of 12 elements
                                               // then four level two strides of 3 elements
 
    // subtract the third column from the second column in the 1st plane
    v[std::gslice(1, {1, 4}, {4 * 3, 3})] -= v[std::gslice(2, {1, 4}, {4 * 3, 3})];
 
    std::cout << "\n" "After column operations:\n";
    test_print(v, 2, 4, 3);
}

出力

Initial 2x4x3 array:
111 112 113   211 212 213
121 122 123   221 222 223
131 132 133   231 232 233
141 142 143   241 242 243
 
After column operations:
1 -1 113   1 212 213
1 -1 123   1 222 223
1 -1 133   1 232 233
1 -1 143   1 242 243

[edit] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 543 C++98 デフォルトコンストラクトされた汎用スライスが使用可能かどうか不明でした。 使用可能です(空のサブセットとして)。

[edit] 関連項目

valarrayの要素、スライス、またはマスクを取得/設定する
(public member function) [編集]
valarrayのBLAS風スライス: 開始インデックス、長さ、ストライド
(class) [編集]
gsliceを適用した後のvalarrayのサブセットへのプロキシ
(class template) [編集]
English 日本語 中文(简体) 中文(繁體)