Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/mir/ndslice/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ $(TR $(TDNW $(SUBMODULE topology) $(BR)
$(TD
$(SUBREF topology, alongDim)
$(SUBREF topology, as)
$(SUBREF topology, asKindOf)
$(SUBREF topology, assumeCanonical)
$(SUBREF topology, assumeContiguous)
$(SUBREF topology, assumeHypercube)
Expand Down
64 changes: 64 additions & 0 deletions source/mir/ndslice/topology.d
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ $(T2 evertPack, Reverses dimension packs.)
$(BOOKTABLE $(H2 SliceKind Selectors),
$(TR $(TH Function Name) $(TH Description))

$(T2 asKindOf, Converts a slice to a user provied kind $(SUBREF slice, SliceKind).)
$(T2 universal, Converts a slice to universal $(SUBREF slice, SliceKind).)
$(T2 canonical, Converts a slice to canonical $(SUBREF slice, SliceKind).)
$(T2 assumeCanonical, Converts a slice to canonical $(SUBREF slice, SliceKind). Does only `assert` checks.)
Expand Down Expand Up @@ -129,6 +130,69 @@ version (D_Exceptions) private immutable choppedException = new Exception(choppe

@optmath:

/++
Converts a slice to user provided kind.

Contiguous slices can be converted to any kind.
Canonical slices can't be converted to contiguous slices.
Universal slices can be converted only to the same kind.

See_also:
$(LREF canonical),
$(LREF universal),
$(LREF assumeCanonical),
$(LREF assumeContiguous).
+/
template asKindOf(SliceKind kind)
{
static if (kind == Contiguous)
{
auto asKindOf(Iterator, size_t N, Labels...)(Slice!(Iterator, N, Contiguous, Labels) slice)
{
return slice;
}
}
else
static if (kind == Canonical)
{
alias asKindOf = canonical;
}
else
{
alias asKindOf = universal;
}
}

/// Universal
@safe pure nothrow
version(mir_test) unittest
{
auto slice = iota(2, 3).asKindOf!Universal;
assert(slice == [[0, 1, 2], [3, 4, 5]]);
assert(slice._lengths == [2, 3]);
assert(slice._strides == [3, 1]);
}

/// Canonical
@safe pure nothrow
version(mir_test) unittest
{
auto slice = iota(2, 3).asKindOf!Canonical;
assert(slice == [[0, 1, 2], [3, 4, 5]]);
assert(slice._lengths == [2, 3]);
assert(slice._strides == [3]);
}

/// Contiguous
@safe pure nothrow
version(mir_test) unittest
{
auto slice = iota(2, 3).asKindOf!Contiguous;
assert(slice == [[0, 1, 2], [3, 4, 5]]);
assert(slice._lengths == [2, 3]);
assert(slice._strides == []);
}

/++
Converts a slice to universal kind.

Expand Down