chunks

Creates Chunks.

  1. template chunks(Dimensions...)
  2. Chunks!([0], Iterator, N, kind) chunks(Slice!(Iterator, N, kind) slice, size_t chunkLength)
    Chunks!([0], Iterator, N, kind)
    chunks
    (
    Iterator
    size_t N
    SliceKind kind
    )
    (
    Slice!(Iterator, N, kind) slice
    ,)

Examples

1Dx1D

import mir.ndslice.chunks: chunks, isChunks;
import mir.ndslice.topology: iota;

// 0 1 2 3 4 5 6 7 8 9 10
auto sl = iota(11);
// 0 1 2 | 3 4 5 | 6 7 8 | 9 10
auto ch = sl.chunks(3);

static assert(isChunks!(typeof(ch)) == [0]); // isChunks returns dimension indices

assert(ch.length == 4);
assert(ch.shape == cast(size_t[1])[4]);

// 0 1 2
assert(ch.front == iota([3], 0));
ch.popFront;

// 3 4 5
assert(ch.front == iota([3], 3));
assert(ch.length == 3);

// 9 10
assert(ch[$ - 1] == ch.back);
assert(ch.back == iota([2], 9));

ch.popBack;
assert(ch.back == iota([3], 6));

assert(ch[$ - 1 .. $].length == 1);
assert(ch[$ .. $].length == 0);
assert(ch[0 ..  0].empty);

import std.range.primitives: isRandomAccessRange;
static assert(isRandomAccessRange!(typeof(ch)));

2Dx2D

1 import mir.ndslice.chunks: chunks, isChunks;
2 import mir.ndslice.topology: iota;
3 
4 //   0   1   2   3   4   5   6   7   8   9
5 //  10  11  12  13  14  15  16  17  18  19
6 //  20  21  22  23  24  25  26  27  28  29
7 //  30  31  32  33  34  35  36  37  38  39
8 //  40  41  42  43  44  45  46  47  48  49
9 //  50  51  52  53  54  55  56  57  58  59
10 //  60  61  62  63  64  65  66  67  68  69
11 //  70  71  72  73  74  75  76  77  78  79
12 //  80  81  82  83  84  85  86  87  88  89
13 //  90  91  92  93  94  95  96  97  98  99
14 // 100 101 102 103 104 105 106 107 108 109
15 auto sl = iota(11, 10); // [0, 1, .. 10]
16 
17 //   ----------------   ----------------   --------
18 //  |  0   1   2   3 | |  4   5   6   7 | |  8   9 |
19 //  | 10  11  12  13 | | 14  15  16  17 | | 18  19 |
20 //  | 20  21  22  23 | | 24  25  26  27 | | 28  29 |
21 //  |----------------| |----------------| |--------|
22 //  | 30  31  32  33 | | 34  35  36  37 | | 38  39 |
23 //  | 40  41  42  43 | | 44  45  46  47 | | 48  49 |
24 //  | 50  51  52  53 | | 54  55  56  57 | | 58  59 |
25 //  |----------------| |----------------| |--------|
26 //  | 60  61  62  63 | | 64  65  66  67 | | 68  69 |
27 //  | 70  71  72  73 | | 74  75  76  77 | | 78  79 |
28 //  | 80  81  82  83 | | 84  85  86  87 | | 88  89 |
29 //  |----------------| |----------------| |--------|
30 //  | 90  91  92  93 | | 94  95  96  97 | | 98  99 |
31 //  |100 101 102 103 | |104 105 106 107 | |108 109 |
32 //   ----------------   ----------------   --------
33 // Chunk columns first, then blocks rows.
34 auto ch = sl.chunks!(1, 0)(4, 3);
35 
36 assert(ch.shape == [3, 4]);
37 assert(ch.slice == sl);
38 assert(ch.front.slice == sl[0 .. $, 0 .. 4]);
39 
40 assert(ch.front.front == sl[0 .. 3, 0 .. 4]);
41 
42 assert(ch.front!0[1] == sl[3 .. 6, 0 .. 4]);
43 assert(ch.front!1[1] == sl[0 .. 3, 4 .. 8]);
44 
45 assert (ch[$ - 1, $ - 1] == [[98, 99], [108, 109]]);
46 
47 static assert(isChunks!(typeof(ch)) == [1, 0]); // isChunks returns dimension indices
48 
49 assert(ch.length == 3);
50 assert(ch.length!1 == 4);
51 
52 ch.popFront;
53 assert(ch.front.front == sl[0 .. 3, 4 .. 8]);
54 ch.popFront!1;
55 assert(ch.front.front == sl[3 .. 6, 4 .. 8]);
56 
57 assert(ch.back.slice == sl[3 .. $, 8 .. $]);
58 ch.popBack;
59 assert(ch.back.slice == sl[3 .. $, 4 .. 8]);
60 
61 import std.range.primitives: isRandomAccessRange;
62 static assert(isRandomAccessRange!(typeof(ch)));

1Dx2D

import mir.ndslice.chunks: chunks, isChunks;
import mir.ndslice.topology: iota;

//   0   1   2   3   4   5   6   7   8   9
//  10  11  12  13  14  15  16  17  18  19
//  20  21  22  23  24  25  26  27  28  29
//  30  31  32  33  34  35  36  37  38  39
auto sl = iota(4, 10); // [0, 1, .. 10]

//   ----------------   ----------------   --------
//  |  0   1   2   3 | |  4   5   6   7 | |  8   9 |
//  | 10  11  12  13 | | 14  15  16  17 | | 18  19 |
//  | 20  21  22  23 | | 24  25  26  27 | | 28  29 |
//  | 30  31  32  33 | | 34  35  36  37 | | 38  39 |
//   ----------------   ----------------   --------
// Chunk columns
auto ch = sl.chunks!1(4);

assert(ch.slice == sl);
assert(ch.front == sl[0 .. $, 0 .. 4]);

assert(ch.back == sl[0 .. $, 8 .. $]);

import std.range.primitives: isRandomAccessRange;
static assert(isRandomAccessRange!(typeof(ch)));

See Also

Meta