Skip to content

Commit f808323

Browse files
committed
fix sorting for zip ndslices
1 parent 6f73b33 commit f808323

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

source/mir/functional.d

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ struct RefTuple(T...)
9898
mixin _RefTupleMixin!T;
9999
}
100100

101+
/// Removes $(LREF Ref) shell.
102+
alias Unref(V : Ref!T, T) = T;
103+
/// ditto
104+
alias Unref(V : RefTuple!T, T...) = RefTuple!(staticMap!(.Unref, T));
105+
/// ditto
106+
alias Unref(V) = V;
107+
108+
101109
/++
102110
Returns: a $(LREF RefTuple) structure.
103111
+/
@@ -106,6 +114,28 @@ RefTuple!Args tuple(Args...)(auto ref Args args)
106114
return RefTuple!Args(args);
107115
}
108116

117+
/// Removes $(LREF Ref) shell.
118+
T unref(V : Ref!T, T)(V value)
119+
{
120+
return *value.__ptr;
121+
}
122+
123+
/// ditto
124+
Unref!(RefTuple!T) unref(V : RefTuple!T, T...)(V value)
125+
{
126+
typeof(return) ret;
127+
foreach(i, ref elem; ret.expand)
128+
elem = value.expand[i].unref;
129+
return ret;
130+
}
131+
132+
/// ditto
133+
V unref(V)(V value)
134+
{
135+
return value;
136+
}
137+
138+
109139
private string joinStrings()(string[] strs)
110140
{
111141
if (strs.length)

source/mir/ndslice/iterator.d

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,19 @@ struct ZipIterator(Iterators...)
410410
mixin(op ~ `_iterator;`);
411411
}
412412

413-
auto ref opIndex()(ptrdiff_t index)
413+
auto opIndex()(ptrdiff_t index)
414414
{ return mixin("RefTuple!(_zip_types!Iterators)(" ~ _zip_index!Iterators ~ ")"); }
415415

416+
auto opIndexAssign(Types...)(RefTuple!(Types) value, ptrdiff_t index)
417+
if (Types.length == Iterators.length)
418+
{
419+
foreach(i, val; value.expand)
420+
{
421+
_iterators[i][index] = val;
422+
}
423+
return opIndex(index);
424+
}
425+
416426
void opOpAssign(string op)(ptrdiff_t index)
417427
if (op == "+" || op == "-")
418428
{

source/mir/ndslice/sorting.d

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ void quickSortImpl(alias less, Iterator)(Slice!(Contiguous, [1], Iterator) slice
148148
// })))
149149
//{
150150
auto d = p;
151-
auto temp = *d;
151+
import mir.functional: unref;
152+
auto temp = unref(*d);
152153
auto c = d;
153154
++c;
154155
if (less(*c, temp))
@@ -191,7 +192,8 @@ void quickSortImpl(alias less, Iterator)(Slice!(Contiguous, [1], Iterator) slice
191192
--r;
192193
auto pivotIdx = l + slice.length / 2;
193194
setPivot!less(slice.length, l, pivotIdx, r);
194-
auto pivot = *pivotIdx;
195+
import mir.functional: unref;
196+
auto pivot = unref(*pivotIdx);
195197
--lessI;
196198
auto greaterI = r;
197199
swapStars(pivotIdx, greaterI);

source/mir/utility.d

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ void swapStars(I1, I2)(auto ref I1 i1, auto ref I2 i2)
3030
}
3131
else
3232
{
33-
auto e = *i1;
33+
import mir.functional: unref;
34+
auto e = unref(*i1);
3435
i1[0] = *i2;
3536
i2[0] = e;
3637
}

0 commit comments

Comments
 (0)