Skip to content

Commit 36a31f9

Browse files
committed
use mixins to remove code duplication
1 parent 9fc723e commit 36a31f9

File tree

1 file changed

+37
-118
lines changed

1 file changed

+37
-118
lines changed

source/mir/ndslice/iterator.d

Lines changed: 37 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,38 @@ import mir.ndslice.internal;
3434

3535
@fastmath:
3636

37+
enum std_ops = q{
38+
void opUnary(string op)()
39+
if (op == "--" || op == "++")
40+
{ mixin(op ~ "_iterator;"); }
41+
42+
void opOpAssign(string op)(ptrdiff_t index)
43+
if (op == "-" || op == "+")
44+
{ mixin("_iterator " ~ op ~ "= index;"); }
45+
46+
auto opBinary(string op)(ptrdiff_t index)
47+
if (op == "+" || op == "-")
48+
{
49+
auto ret = this;
50+
mixin(`ret ` ~ op ~ `= index;`);
51+
return ret;
52+
}
53+
54+
ptrdiff_t opBinary(string op : "-")(auto ref const typeof(this) right) const
55+
{ return this._iterator - right._iterator; }
56+
57+
bool opEquals()(ref const typeof(this) right) const
58+
{ return this._iterator == right._iterator; }
59+
60+
ptrdiff_t opCmp()(ref const typeof(this) right) const
61+
{
62+
static if (isPointer!Iterator)
63+
return this._iterator - right._iterator;
64+
else
65+
return this._iterator.opCmp(right._iterator);
66+
}
67+
};
68+
3769
/++
3870
Step counter.
3971
@@ -519,10 +551,6 @@ struct MapIterator(Iterator, alias fun)
519551
return fun(*_iterator);
520552
}
521553

522-
void opUnary(string op)()
523-
if (op == "--" || op == "++")
524-
{ mixin(op ~ "_iterator;"); }
525-
526554
auto ref opIndex()(ptrdiff_t index)
527555
{
528556
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
@@ -570,31 +598,7 @@ struct MapIterator(Iterator, alias fun)
570598
}
571599
}
572600

573-
void opOpAssign(string op)(ptrdiff_t index)
574-
if (op == "-" || op == "+")
575-
{ mixin("_iterator " ~ op ~ "= index;"); }
576-
577-
auto opBinary(string op)(ptrdiff_t index)
578-
if (op == "+" || op == "-")
579-
{
580-
auto ret = this;
581-
mixin(`ret ` ~ op ~ `= index;`);
582-
return ret;
583-
}
584-
585-
ptrdiff_t opBinary(string op : "-")(auto ref const typeof(this) right) const
586-
{ return this._iterator - right._iterator; }
587-
588-
bool opEquals()(ref const typeof(this) right) const
589-
{ return this._iterator == right._iterator; }
590-
591-
ptrdiff_t opCmp()(ref const typeof(this) right) const
592-
{
593-
static if (isPointer!Iterator)
594-
return this._iterator - right._iterator;
595-
else
596-
return this._iterator.opCmp(right._iterator);
597-
}
601+
mixin(std_ops);
598602
}
599603

600604
/++
@@ -632,41 +636,12 @@ struct ConvolutionIterator(Iterator, size_t params, alias fun)
632636
return mixin("fun(" ~ _iotaArgs!(params, "_iterator[", "], ") ~ ")");
633637
}
634638

635-
void opUnary(string op)()
636-
if (op == "--" || op == "++")
637-
{ mixin(op ~ "_iterator;"); }
638-
639639
auto ref opIndex()(ptrdiff_t index)
640640
{
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;
641+
return mixin("fun(" ~ _iotaArgs!(params, "_iterator[index + ", "], ") ~ ")");
655642
}
656643

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-
}
644+
mixin(std_ops);
670645
}
671646

672647
///
@@ -727,10 +702,6 @@ struct IndexIterator(Iterator, Field)
727702
return _field[*_iterator];
728703
}
729704

730-
void opUnary(string op)()
731-
if (op == "--" || op == "++")
732-
{ mixin(op ~ "_iterator;"); }
733-
734705
auto ref opIndex(ptrdiff_t index)
735706
{
736707
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
@@ -778,31 +749,7 @@ struct IndexIterator(Iterator, Field)
778749
}
779750
}
780751

781-
void opOpAssign(string op)(ptrdiff_t index)
782-
if (op == "-" || op == "+")
783-
{ mixin("_iterator " ~ op ~ "= index;"); }
784-
785-
auto opBinary(string op)(ptrdiff_t index)
786-
if (op == "+" || op == "-")
787-
{
788-
auto ret = this;
789-
mixin(`ret ` ~ op ~ `= index;`);
790-
return ret;
791-
}
792-
793-
ptrdiff_t opBinary(string op : "-")(auto ref const typeof(this) right) const
794-
{ return this._iterator - right._iterator; }
795-
796-
bool opEquals()(ref const typeof(this) right) const
797-
{ return this._iterator == right._iterator; }
798-
799-
ptrdiff_t opCmp()(ref const typeof(this) right) const
800-
{
801-
static if (isPointer!Iterator)
802-
return this._iterator - right._iterator;
803-
else
804-
return this._iterator.opCmp(right._iterator);
805-
}
752+
mixin(std_ops);
806753
}
807754

808755
/++
@@ -826,38 +773,10 @@ struct SliceIterator(SliceKind kind, size_t[] packs, Iterator)
826773
auto opUnary(string op : "*")()
827774
{ return Elem(_lengths, _strides, _iterator); }
828775

829-
void opUnary(string op)()
830-
if (op == "--" || op == "++")
831-
{ mixin(op ~ "_iterator;"); }
832-
833776
auto opIndex()(ptrdiff_t index)
834777
{ return Elem(_lengths, _strides, _iterator + index); }
835778

836-
void opOpAssign(string op)(ptrdiff_t index)
837-
if (op == "-" || op == "+")
838-
{ mixin("_iterator " ~ op ~ "= index;"); }
839-
840-
auto opBinary(string op)(ptrdiff_t index)
841-
if (op == "+" || op == "-")
842-
{
843-
auto ret = this;
844-
mixin(`ret ` ~ op ~ `= index;`);
845-
return ret;
846-
}
847-
848-
ptrdiff_t opBinary(string op : "-")(auto ref const typeof(this) right) const
849-
{ return this._iterator - right._iterator; }
850-
851-
bool opEquals()(ref const typeof(this) right) const
852-
{ return this._iterator == right._iterator; }
853-
854-
ptrdiff_t opCmp()(ref const typeof(this) right) const
855-
{
856-
static if (isPointer!Iterator)
857-
return this._iterator - right._iterator;
858-
else
859-
return this._iterator.opCmp(right._iterator);
860-
}
779+
mixin(std_ops);
861780
}
862781

863782
public auto FieldIterator__map(Field, alias fun)(ref FieldIterator!(Field) it)

0 commit comments

Comments
 (0)