Skip to content

Commit b0285a7

Browse files
committed
rework map-like iterators
1 parent 315d5ab commit b0285a7

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

dub.sdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ authors "Ilya Yaroshenko" "John Michael Hall" "Shigeki Karita" "Sebastian Wilzba
55
copyright "2020 Ilya Yaroshenko, Kaleidic Associates Advisory Limited, Symmetry Investments"
66
license "Apache-2.0"
77

8-
dependency "mir-core" version=">=1.1.88"
8+
dependency "mir-core" version=">=1.1.106"
99

1010
buildType "unittest" {
1111
buildOptions "unittests" "debugMode" "debugInfo"

source/mir/ndslice/iterator.d

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -860,14 +860,14 @@ struct CachedIterator(Iterator, CacheIterator, FlagIterator)
860860

861861
private enum map_primitives = q{
862862

863-
import mir.functional: RefTuple, unref;
863+
import mir.functional: RefTuple, autoExpandAndForward;
864864

865865
auto ref opUnary(string op : "*")()
866866
{
867867
static if (is(typeof(*_iterator) : RefTuple!T, T...))
868868
{
869869
auto t = *_iterator;
870-
return mixin("_fun(" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ ")");
870+
return _fun(autoExpandAndForward!t);
871871
}
872872
else
873873
return _fun(*_iterator);
@@ -878,7 +878,7 @@ private enum map_primitives = q{
878878
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
879879
{
880880
auto t = _iterator[index];
881-
return mixin("_fun(" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ ")");
881+
return _fun(autoExpandAndForward!t);
882882
}
883883
else
884884
return _fun(_iterator[index]);
@@ -891,7 +891,7 @@ private enum map_primitives = q{
891891
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
892892
{
893893
auto t = _iterator[index];
894-
return mixin("_fun(" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ ") = value");
894+
return _fun(autoExpandAndForward!t) = value;
895895
}
896896
else
897897
return _fun(_iterator[index]) = value;
@@ -902,7 +902,7 @@ private enum map_primitives = q{
902902
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
903903
{
904904
auto t = _iterator[index];
905-
return mixin(op ~ "_fun(" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ ")");
905+
return _fun(autoExpandAndForward!t);
906906
}
907907
else
908908
return mixin(op ~ "_fun(_iterator[index])");
@@ -913,7 +913,7 @@ private enum map_primitives = q{
913913
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
914914
{
915915
auto t = _iterator[index];
916-
return mixin("_fun(" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ ")" ~ op ~ "= value");
916+
return mixin("_fun(autoExpandAndForward!t)" ~ op ~ "= value");
917917
}
918918
else
919919
return mixin("_fun(_iterator[index])" ~ op ~ "= value");
@@ -945,14 +945,14 @@ struct VmapIterator(Iterator, Fun)
945945
return VmapIterator!(LightImmutableOf!Iterator, LightImmutableOf!Fun)(.lightImmutable(_iterator), .lightImmutable(_fun));
946946
}
947947

948-
import mir.functional: RefTuple, unref;
948+
import mir.functional: RefTuple, autoExpandAndForward;
949949

950950
auto ref opUnary(string op : "*")()
951951
{
952952
static if (is(typeof(*_iterator) : RefTuple!T, T...))
953953
{
954954
auto t = *_iterator;
955-
return mixin("_fun(" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ ")");
955+
return _fun(autoExpandAndForward!t);
956956
}
957957
else
958958
return _fun(*_iterator);
@@ -963,7 +963,7 @@ struct VmapIterator(Iterator, Fun)
963963
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
964964
{
965965
auto t = _iterator[index];
966-
return mixin("_fun(" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ ")");
966+
return _fun(autoExpandAndForward!t);
967967
}
968968
else
969969
return _fun(_iterator[index]);
@@ -976,7 +976,7 @@ struct VmapIterator(Iterator, Fun)
976976
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
977977
{
978978
auto t = _iterator[index];
979-
return mixin("_fun(" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ ") = value");
979+
return _fun(autoExpandAndForward!t) = value;
980980
}
981981
else
982982
return _fun(_iterator[index]) = value;
@@ -987,7 +987,7 @@ struct VmapIterator(Iterator, Fun)
987987
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
988988
{
989989
auto t = _iterator[index];
990-
return mixin(op ~ "_fun(" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ ")");
990+
return mixin(op ~ "_fun(autoExpandAndForward!t)");
991991
}
992992
else
993993
return mixin(op ~ "_fun(_iterator[index])");
@@ -998,7 +998,7 @@ struct VmapIterator(Iterator, Fun)
998998
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
999999
{
10001000
auto t = _iterator[index];
1001-
return mixin("_fun(" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ ")" ~ op ~ "= value");
1001+
return mixin("_fun(autoExpandAndForward!t)" ~ op ~ "= value");
10021002
}
10031003
else
10041004
return mixin("_fun(_iterator[index])" ~ op ~ "= value");
@@ -1041,18 +1041,18 @@ struct MapIterator(Iterator, alias _fun)
10411041
return MapIterator!(LightImmutableOf!Iterator, _fun)(.lightImmutable(_iterator));
10421042
}
10431043

1044-
import mir.functional: pipe;
1044+
import mir.functional: pipe, autoExpandAndForward;
10451045
///
10461046
static alias __map(alias fun1) = MapIterator__map!(Iterator, _fun, pipe!(_fun, fun1));
10471047

1048-
import mir.functional: RefTuple, unref;
1048+
import mir.functional: RefTuple, autoExpandAndForward;
10491049

10501050
auto ref opUnary(string op : "*")()
10511051
{
10521052
static if (is(typeof(*_iterator) : RefTuple!T, T...))
10531053
{
10541054
auto t = *_iterator;
1055-
return mixin("_fun(" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ ")");
1055+
return _fun(autoExpandAndForward!t);
10561056
}
10571057
else
10581058
return _fun(*_iterator);
@@ -1063,7 +1063,7 @@ struct MapIterator(Iterator, alias _fun)
10631063
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
10641064
{
10651065
auto t = _iterator[index];
1066-
return mixin("_fun(" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ ")");
1066+
return _fun(autoExpandAndForward!t);
10671067
}
10681068
else
10691069
return _fun(_iterator[index]);
@@ -1076,7 +1076,7 @@ struct MapIterator(Iterator, alias _fun)
10761076
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
10771077
{
10781078
auto t = _iterator[index];
1079-
return mixin("_fun(" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ ") = value");
1079+
return _fun(autoExpandAndForward!t) = value;
10801080
}
10811081
else
10821082
return _fun(_iterator[index]) = value;
@@ -1087,7 +1087,7 @@ struct MapIterator(Iterator, alias _fun)
10871087
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
10881088
{
10891089
auto t = _iterator[index];
1090-
return mixin(op ~ "_fun(" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ ")");
1090+
return mixin(op ~ "_fun(autoExpandAndForward!t)");
10911091
}
10921092
else
10931093
return mixin(op ~ "_fun(_iterator[index])");
@@ -1098,7 +1098,7 @@ struct MapIterator(Iterator, alias _fun)
10981098
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
10991099
{
11001100
auto t = _iterator[index];
1101-
return mixin("_fun(" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ ")" ~ op ~ "= value");
1101+
return mixin("_fun(autoExpandAndForward!t)" ~ op ~ "= value");
11021102
}
11031103
else
11041104
return mixin("_fun(_iterator[index])" ~ op ~ "= value");
@@ -1516,7 +1516,7 @@ Iterates a field using an iterator.
15161516
+/
15171517
struct IndexIterator(Iterator, Field)
15181518
{
1519-
import mir.functional: RefTuple, unref;
1519+
import mir.functional: RefTuple, autoExpandAndForward;
15201520

15211521
@optmath:
15221522
///
@@ -1544,7 +1544,7 @@ struct IndexIterator(Iterator, Field)
15441544
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
15451545
{
15461546
auto t = *_iterator;
1547-
return mixin("_field[" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ "]");
1547+
return _field[autoExpandAndForward!t];
15481548
}
15491549
else
15501550
return _field[*_iterator];
@@ -1555,7 +1555,7 @@ struct IndexIterator(Iterator, Field)
15551555
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
15561556
{
15571557
auto t = _iterator[index];
1558-
return mixin("_field[" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ "]");
1558+
return _field[autoExpandAndForward!t];
15591559
}
15601560
else
15611561
return _field[_iterator[index]];
@@ -1568,7 +1568,7 @@ struct IndexIterator(Iterator, Field)
15681568
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
15691569
{
15701570
auto t = _iterator[index];
1571-
return mixin("_field[" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ "] = value");
1571+
return _field[autoExpandAndForward!t] = value;
15721572
}
15731573
else
15741574
return _field[_iterator[index]] = value;
@@ -1579,7 +1579,7 @@ struct IndexIterator(Iterator, Field)
15791579
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
15801580
{
15811581
auto t = _iterator[index];
1582-
return mixin(op ~ "_field[" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ "]");
1582+
return mixin(op ~ "_field[autoExpandAndForward!t]");
15831583
}
15841584
else
15851585
return mixin(op ~ "_field[_iterator[index]]");
@@ -1590,7 +1590,7 @@ struct IndexIterator(Iterator, Field)
15901590
static if (is(typeof(_iterator[0]) : RefTuple!T, T...))
15911591
{
15921592
auto t = _iterator[index];
1593-
return mixin("_field[" ~ _iotaArgs!(T.length, "t.expand[", "].unref, ") ~ "]" ~ op ~ "= value");
1593+
return mixin("_field[autoExpandAndForward!t]" ~ op ~ "= value");
15941594
}
15951595
else
15961596
return mixin("_field[_iterator[index]]" ~ op ~ "= value");

source/mir/ndslice/package.d

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,3 +751,17 @@ pure nothrow version(mir_ndslice_test) unittest
751751
assert(matrix[0, 2] == tensor[0, 1, 2]);
752752
assert(&matrix[0, 2] is &tensor[0, 1, 2]);
753753
}
754+
755+
version(mir_ndslice_test)
756+
pure nothrow
757+
unittest
758+
{
759+
auto x = iota(2, 3);
760+
auto y = iota([2, 3], 1);
761+
auto combine1 = x.zip(y).map!"b";
762+
auto combine2 = x.zip(y).map!("b", "a * b").map!(a => a[0]);
763+
764+
assert(combine1[0, 0] == 1);
765+
assert(combine2[0, 0] == 1);
766+
static assert(is(typeof(combine2[0, 0]) == long));
767+
}

0 commit comments

Comments
 (0)