Skip to content

Commit 9fc723e

Browse files
committed
add convolution iterator
1 parent 8f8bc10 commit 9fc723e

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

source/mir/ndslice/iterator.d

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,78 @@ auto mapIterator(alias fun, Iterator)(Iterator iterator)
608608
return MapIterator!(Iterator, fun)(iterator);
609609
}
610610

611+
auto ConvolutionIterator__map(Iterator, size_t params, alias fun0, alias fun)(ref ConvolutionIterator!(Iterator, params, fun0) it)
612+
{
613+
return ConvolutionIterator!(Iterator, params, fun)(it._iterator);
614+
}
615+
616+
/++
617+
`ConvolutionIterator` is used by $(SUBREF topology, map).
618+
+/
619+
struct ConvolutionIterator(Iterator, size_t params, alias fun)
620+
if (params > 1)
621+
{
622+
@fastmath:
623+
///
624+
Iterator _iterator;
625+
626+
import mir.functional: pipe;
627+
///
628+
static alias __map(alias fun1) = ConvolutionIterator__map!(Iterator, params, fun, pipe!(fun, fun1));
629+
630+
auto ref opUnary(string op : "*")()
631+
{
632+
return mixin("fun(" ~ _iotaArgs!(params, "_iterator[", "], ") ~ ")");
633+
}
634+
635+
void opUnary(string op)()
636+
if (op == "--" || op == "++")
637+
{ mixin(op ~ "_iterator;"); }
638+
639+
auto ref opIndex()(ptrdiff_t index)
640+
{
641+
auto s = this + index;
642+
return *s;
643+
}
644+
645+
void opOpAssign(string op)(ptrdiff_t index)
646+
if (op == "-" || op == "+")
647+
{ mixin("_iterator " ~ op ~ "= index;"); }
648+
649+
auto opBinary(string op)(ptrdiff_t index)
650+
if (op == "+" || op == "-")
651+
{
652+
auto ret = this;
653+
mixin(`ret ` ~ op ~ `= index;`);
654+
return ret;
655+
}
656+
657+
ptrdiff_t opBinary(string op : "-")(auto ref const typeof(this) right) const
658+
{ return this._iterator - right._iterator; }
659+
660+
bool opEquals()(ref const typeof(this) right) const
661+
{ return this._iterator == right._iterator; }
662+
663+
ptrdiff_t opCmp()(ref const typeof(this) right) const
664+
{
665+
static if (isPointer!Iterator)
666+
return this._iterator - right._iterator;
667+
else
668+
return this._iterator.opCmp(right._iterator);
669+
}
670+
}
671+
672+
///
673+
unittest
674+
{
675+
import mir.functional: naryFun;
676+
auto data = [1, 3, 8, 18];
677+
auto diff = ConvolutionIterator!(int*, 2, naryFun!"b - a")(data.ptr);
678+
assert(*diff == 2);
679+
assert(diff[1] == 5);
680+
assert(diff[2] == 10);
681+
}
682+
611683
auto IndexIterator__map(Iterator, Field, alias fun)(ref IndexIterator!(Iterator, Field) it)
612684
{
613685
import mir.ndslice.field: mapField;

0 commit comments

Comments
 (0)