Skip to content

Commit ef7026f

Browse files
committed
add field property
1 parent 9d1be3f commit ef7026f

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

source/mir/ndslice/iterator.d

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,21 @@ struct FieldIterator(Field)
812812
///
813813
static alias __map(alias fun) = FieldIterator__map!(Field, fun);
814814

815+
///
816+
_Slice!() opSlice()(size_t a, size_t b)
817+
{
818+
return typeof(return)(a, b);
819+
}
820+
821+
/++
822+
Returns:
823+
`_field[slice.i .. slice.j]`.
824+
+/
825+
auto opIndex()(_Slice!() slice)
826+
{
827+
return _field[_index + slice.i .. _index + slice.j];
828+
}
829+
815830
auto ref opUnary(string op : "*")()
816831
{ return _field[_index]; }
817832

source/mir/ndslice/slice.d

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -766,24 +766,36 @@ struct Slice(SliceKind kind, size_t[] packs, Iterator)
766766
}
767767

768768
/++
769+
Iterator
769770
Returns:
770-
Pointer to the first element of a slice if slice is defined as `Slice!(N, T*)`
771-
or plain structure with two fields `shift` and `range` otherwise.
772-
In second case the expression `range[shift]` refers to the first element.
773-
For slices with named elements the type of a return value
774-
has the same behavior like a pointer.
775-
Note:
776-
`iterator` is defined only for non-packed slices.
777-
Attention:
778-
`iterator` refers to the first element in the memory representation
779-
if and only if all strides are positive.
771+
Iterator (pointer) to the $(LREF Slice.first) element.
780772
+/
781-
static if (is(Iterator == Iterator))
782773
auto iterator()() @property
783774
{
784775
return _iterator;
785776
}
786777

778+
/++
779+
Field (array) data.
780+
Returns:
781+
Raw data slice.
782+
Constraints:
783+
Field is defined only for contiguous slices.
784+
+/
785+
auto field()() @property
786+
{
787+
static assert(kind == Contiguous, "Slice.field is defined only for contiguous slices. Slice kind is " ~ kind.stringof);
788+
static if (is(typeof(_iterator[size_t(0) .. elementsCount])))
789+
{
790+
return _iterator[size_t(0) .. elementsCount];
791+
}
792+
else
793+
{
794+
import mir.ndslice.topology: flattened;
795+
return this.flattened;
796+
}
797+
}
798+
787799
/++
788800
Returns: static array of lengths
789801
See_also: $(LREF .Slice.structure)

source/mir/primitives.d

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,3 @@ template DimensionCount(T)
7474
else
7575
enum size_t DimensionCount = 1;
7676
}
77-
78-
///
79-
alias elementsCount = length;

0 commit comments

Comments
 (0)