Report a bug
If you spot a problem with this page, click here to create a Github issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.ndslice.allocation

This is a submodule of mir.ndslice.
It contains allocation utilities.

Transpose operators

Function Name Description
makeNdarray Allocates a common n-dimensional array from a slice using an allocator.
makeSlice Allocates a slice using an allocator.
makeUninitSlice Allocates an uninitialized slice using an allocator.
ndarray Allocates a common n-dimensional array from a slice.
shape Returns a shape of a common n-dimensional array.
slice Allocates a slice using GC.
uninitSlice Allocates an uninitialized slice using GC.
Authors:
Ilya Yaroshenko
ContiguousSlice!(N, T) slice(T, size_t N)(size_t[N] lengths...);

ContiguousSlice!(N, T) slice(T, size_t N)(size_t[N] lengths, T init);

auto slice(SliceKind kind, size_t[] packs, Iterator)(Slice!(kind, packs, Iterator) slice);

auto slice(size_t dim, Slices...)(Concatenation!(dim, Slices) concatenation);
Allocates an array and an n-dimensional slice over it.
Parameters:
size_t[N] lengths List of lengths for each dimension.
T init Value to initialize with.
Slice!(kind, packs, Iterator) slice Slice to copy shape and data from.
Returns:
n-dimensional slice
Examples:
auto tensor = slice!int(5, 6, 7);
assert(tensor.length == 5);
assert(tensor.elementsCount == 5 * 6 * 7);
static assert(is(typeof(tensor) == Slice!(Contiguous, [3], int*)));

// creates duplicate using `slice`
auto dup = tensor.slice;
assert(dup == tensor);
Examples:
auto tensor = slice([2, 3], 5);
assert(tensor.elementsCount == 2 * 3);
assert(tensor[1, 1] == 5);
auto uninitSlice(T, size_t N)(size_t[N] lengths...);
Allocates an uninitialized array and an n-dimensional slice over it.
Parameters:
size_t[N] lengths list of lengths for each dimension
Returns:
uninitialized n-dimensional slice
Examples:
auto tensor = uninitSlice!int(5, 6, 7);
assert(tensor.length == 5);
assert(tensor.elementsCount == 5 * 6 * 7);
static assert(is(typeof(tensor) == Slice!(Contiguous, [3], int*)));
auto makeSlice(Allocator, size_t N, Iterator)(auto ref Allocator alloc, Slice!(N, Iterator) slice);

ContiguousSlice!(N, T) makeSlice(T, Allocator, size_t N)(auto ref Allocator alloc, size_t[N] lengths...);

ContiguousSlice!(N, T) makeSlice(T, Allocator, size_t N)(auto ref Allocator alloc, size_t[N] lengths, T init);
Allocates an array through a specified allocator and creates an n-dimensional slice over it. See also std.experimental.allocator.
Parameters:
Allocator alloc allocator
size_t[N] lengths list of lengths for each dimension
T init default value for array initialization
Slice!(N, Iterator) slice slice to copy shape and data from
Returns:
a structure with fields array and slice

Note: makeSlice always returns slice with mutable elements

Examples:
ditto
Initialization with default value
import std.experimental.allocator;
import std.experimental.allocator.mallocator;

auto sl = makeSlice(Mallocator.instance, [2, 3, 4], 10);
auto ar = sl.field;
assert(sl[1, 1, 1] == 10);
Mallocator.instance.dispose(ar);
ContiguousSlice!(N, T) makeUninitSlice(T, Allocator, size_t N)(auto ref Allocator alloc, size_t[N] lengths...);
Allocates an uninitialized array through a specified allocator and creates an n-dimensional slice over it. See also std.experimental.allocator.
Parameters:
Allocator alloc allocator
size_t[N] lengths list of lengths for each dimension
Returns:
a structure with fields array and slice
Examples:
import std.experimental.allocator;
import std.experimental.allocator.mallocator;

auto sl = makeUninitSlice!int(Mallocator.instance, 2, 3, 4);
auto ar = sl.field;
assert(ar.ptr is sl.iterator);
assert(ar.length           == 24);
assert(sl.elementsCount    == 24);

Mallocator.instance.dispose(ar);
auto ndarray(SliceKind kind, size_t[] packs, Iterator)(Slice!(kind, packs, Iterator) slice);
Allocates a common n-dimensional array from a slice.
Parameters:
Slice!(kind, packs, Iterator) slice slice
Returns:
multidimensional D array
Examples:
import mir.ndslice.topology : iota;
auto slice = iota(3, 4);
auto m = slice.ndarray;
static assert(is(typeof(m) == size_t[][]));
assert(m == [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]);
auto makeNdarray(T, Allocator, SliceKind kind, size_t[] packs, Iterator)(auto ref Allocator alloc, Slice!(kind, packs, Iterator) slice);
Allocates a common n-dimensional array using data from a slice.
Parameters:
Allocator alloc allocator (optional)
Slice!(kind, packs, Iterator) slice slice
Returns:
multidimensional D array
Examples:
import std.experimental.allocator;
import std.experimental.allocator.mallocator;
import mir.ndslice.topology : iota;

auto slice = iota(3, 4);
auto m = Mallocator.instance.makeNdarray!long(slice);

static assert(is(typeof(m) == long[][]));

static immutable ar = [[0L, 1, 2, 3], [4L, 5, 6, 7], [8L, 9, 10, 11]];
assert(m == ar);

foreach (ref row; m)
    Mallocator.instance.dispose(row);
Mallocator.instance.dispose(m);
auto shape(T)(T[] array, ref int err);
Shape of a common n-dimensional array.
Parameters:
T[] array common n-dimensional array
int err error flag passed by reference
Returns:
static array of dimensions type of size_t[n]
Examples:
int err;
size_t[2] shape = [[1, 2, 3], [4, 5, 6]].shape(err);
assert(err == 0);
assert(shape == [2, 3]);

[[1, 2], [4, 5, 6]].shape(err);
assert(err == 1);
Examples:
Slice from ndarray
import mir.ndslice.allocation: slice, shape;
int err;
auto array = [[1, 2, 3], [4, 5, 6]];
auto s = array.shape(err).slice!int;
s[] = [[1, 2, 3], [4, 5, 6]];
assert(s == array);