Skip to content

Commit 95fe143

Browse files
committed
fix few bugs with iterators
add makeIndex fix safety update meson rm mir-core submodule
1 parent c4ab750 commit 95fe143

File tree

8 files changed

+74
-27
lines changed

8 files changed

+74
-27
lines changed

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ mir_algorithm_src = [
4848
'source/mir/ndslice/sorting.d',
4949
'source/mir/ndslice/topology.d',
5050
'source/mir/ndslice/traits.d',
51+
'source/mir/numeric.d',
5152
'source/mir/range.d',
5253
'source/mir/rcarray.d',
5354
'source/mir/series.d',

source/mir/ndslice/field.d

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,29 +297,24 @@ struct RepeatField(T)
297297
import std.traits: Unqual;
298298

299299
@optmath:
300-
static if (is(T == class) || is(T == interface) || is(T : Unqual!T) && is(Unqual!T : T))
301-
///
302-
alias UT = Unqual!T;
303-
else
304-
///
305-
alias UT = T;
300+
alias UT = Unqual!T;
306301

307302
///
308303
UT _value;
309304

310305
///
311-
auto lightConst()() const @property
306+
auto lightConst()() const @property @trusted
312307
{
313308
return RepeatField!(const T)(cast(UT) _value);
314309
}
315310

316311
///
317-
auto lightImmutable()() immutable @property
312+
auto lightImmutable()() immutable @property @trusted
318313
{
319314
return RepeatField!(immutable T)(cast(UT) _value);
320315
}
321316

322-
auto ref T opIndex()(ptrdiff_t)
317+
auto ref T opIndex()(ptrdiff_t) @trusted
323318
{ return cast(T) _value; }
324319
}
325320

source/mir/ndslice/iterator.d

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ struct IotaIterator(I)
136136
return ret;
137137
}
138138

139-
ptrdiff_t opBinary(string op : "-")(typeof(this) right) scope const
139+
ptrdiff_t opBinary(string op : "-")(const typeof(this) right) scope const
140140
{ return this._index - right._index; }
141141

142-
bool opEquals()(typeof(this) right) scope const
142+
bool opEquals()(const typeof(this) right) scope const
143143
{ return this._index == right._index; }
144144

145-
ptrdiff_t opCmp()(typeof(this) right) scope const
145+
ptrdiff_t opCmp()(const typeof(this) right) scope const
146146
{ return this._index - right._index; }
147147
}
148148

@@ -661,7 +661,7 @@ struct CachedIterator(Iterator, CacheIterator, FlagIterator)
661661
{
662662
if (_expect(!*_flags, false))
663663
{
664-
*_flags = true;
664+
_flags[0] = true;
665665
emplaceRef!T(*cast(UT*)&*_caches, *_iterator);
666666
}
667667
return *_caches;
@@ -708,7 +708,7 @@ struct CachedIterator(Iterator, CacheIterator, FlagIterator)
708708
}
709709

710710
ptrdiff_t opBinary(string op : "-")(scope ref const typeof(this) right) scope const
711-
{ return (this._iterator - right._iterator) / count; }
711+
{ return this._iterator - right._iterator; }
712712

713713
bool opEquals()(scope ref const typeof(this) right) scope const
714714
{ return this._iterator == right._iterator; }

source/mir/ndslice/package.d

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -272,17 +272,17 @@ $(TR $(TDNW $(SUBMODULE chunks)
272272
$(TR $(TDNW $(SUBMODULE traits)
273273
$(BR) $(SMALL Declarations))
274274
$(TD
275-
$(SUBREF field, isVector)
276-
$(SUBREF field, isMatrix)
277-
$(SUBREF field, isContiguousSlice)
278-
$(SUBREF field, isCanonicalSlice)
279-
$(SUBREF field, isUniversalSlice)
280-
$(SUBREF field, isContiguousVector)
281-
$(SUBREF field, isUniversalVector)
282-
$(SUBREF field, isContiguousMatrix)
283-
$(SUBREF field, isCanonicalMatrix)
284-
$(SUBREF field, isUniversalMatrix)
285-
275+
$(SUBREF traits, isIterator)
276+
$(SUBREF traits, isVector)
277+
$(SUBREF traits, isMatrix)
278+
$(SUBREF traits, isContiguousSlice)
279+
$(SUBREF traits, isCanonicalSlice)
280+
$(SUBREF traits, isUniversalSlice)
281+
$(SUBREF traits, isContiguousVector)
282+
$(SUBREF traits, isUniversalVector)
283+
$(SUBREF traits, isContiguousMatrix)
284+
$(SUBREF traits, isCanonicalMatrix)
285+
$(SUBREF traits, isUniversalMatrix)
286286
)
287287
)
288288

source/mir/ndslice/slice.d

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import mir.ndslice.concatenation;
3838
import mir.ndslice.field;
3939
import mir.ndslice.internal;
4040
import mir.ndslice.iterator;
41+
import mir.ndslice.traits: isIterator;
4142
import mir.primitives;
4243
import mir.qualifier;
4344
import mir.utility;
@@ -632,7 +633,7 @@ Slice!(Universal, N, Iterator)
632633
-------
633634
+/
634635
struct mir_slice(Iterator_, size_t N_ = 1, SliceKind kind_ = Contiguous, Labels_...)
635-
if (0 < N_ && N_ < 255 && !(kind_ == Canonical && N_ == 1) && Labels_.length <= N_)
636+
if (0 < N_ && N_ < 255 && !(kind_ == Canonical && N_ == 1) && Labels_.length <= N_ && isIterator!Iterator_)
636637
{
637638
@optmath:
638639

source/mir/ndslice/sorting.d

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,52 @@ template transitionIndex(alias test = "a < b")
534534
assert(j == 3);
535535
auto upperBound = a[j .. $];
536536
}
537+
538+
/++
539+
Computes an index for `r` based on the comparison `less`. The
540+
index is a sorted array of indices into the original
541+
range. This technique is similar to sorting, but it is more flexible
542+
because (1) it allows "sorting" of immutable collections, (2) allows
543+
binary search even if the original collection does not offer random
544+
access, (3) allows multiple indexes, each on a different predicate,
545+
and (4) may be faster when dealing with large objects. However, using
546+
an index may also be slower under certain circumstances due to the
547+
extra indirection, and is always larger than a sorting-based solution
548+
because it needs space for the index in addition to the original
549+
collection. The complexity is the same as `sort`'s.
550+
Params:
551+
less = The comparison to use.
552+
ss = The swapping strategy.
553+
r = The slice/array to index.
554+
index = The resulting index.
555+
Returns: Index array.
556+
+/
557+
Slice!(I*) makeIndex(I = size_t, alias less = "a < b", Iterator, SliceKind kind)(scope Slice!(Iterator, 1, kind) r)
558+
{
559+
import mir.functional: naryFun;
560+
import mir.ndslice.allocation: slice;
561+
import mir.ndslice.topology: iota;
562+
return r
563+
.length
564+
.iota!I
565+
.slice
566+
.sort!((a, b) => naryFun!less(r[a], r[b]));
567+
}
568+
569+
///
570+
I[] makeIndex(I = size_t, alias less = "a < b", T)(scope T[] r)
571+
{
572+
return .makeIndex!(I, less)(r.sliced).field;
573+
}
574+
575+
///
576+
@system unittest
577+
{
578+
import mir.algorithm.iteration: all;
579+
import mir.ndslice.topology: indexed, pairwise;
580+
581+
immutable arr = [ 2, 3, 1, 5, 0 ];
582+
auto index = arr.makeIndex;
583+
584+
assert(arr.indexed(index).pairwise!"a < b".all);
585+
}

source/mir/ndslice/topology.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1814,7 +1814,7 @@ Returns:
18141814
`n`-dimensional slice composed of identical values, where `n` is dimension count.
18151815
+/
18161816
Slice!(FieldIterator!(RepeatField!T), M, Universal)
1817-
repeat(T, size_t M)(T value, size_t[M] lengths...)
1817+
repeat(T, size_t M)(T value, size_t[M] lengths...) @trusted
18181818
if (M && !isSlice!T)
18191819
{
18201820
size_t[M] ls = lengths;

source/mir/ndslice/traits.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $(T2 isUniversalVector, Test if type is a universal one-dimensional slice.)
1616
$(T2 isContiguousMatrix, Test if type is a contiguous two-dimensional slice.)
1717
$(T2 isCanonicalMatrix, Test if type is a canonical two-dimensional slice.)
1818
$(T2 isUniversalMatrix, Test if type is a universal two-dimensional slice.)
19+
$(T2 isIterator, Test if type is a random access iterator.)
1920
)
2021
2122
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).

0 commit comments

Comments
 (0)