Skip to content

Commit 33a3bc8

Browse files
authored
Add makeIndex UT, improve docs (#346)
1 parent ee774b7 commit 33a3bc8

1 file changed

Lines changed: 35 additions & 6 deletions

File tree

source/mir/ndslice/sorting.d

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Note:
55
The combination of
66
$(SUBREF topology, pairwise) with lambda `"a <= b"` (`"a < b"`) and $(SUBREF algorithm, all) can be used
77
to check if an ndslice is sorted (strictly monotonic).
8-
$(SUBREF topology iota) can be used to make an index.
9-
$(SUBREF topology map) and $(SUBREF topology zip) can be used to create Schwartzian transform.
8+
$(SUBREF topology, iota) can be used to make an index.
9+
$(SUBREF topology, map) and $(SUBREF topology, zip) can be used to create Schwartzian transform.
1010
See also the examples in the module.
1111
1212
@@ -667,7 +667,9 @@ version(mir_test)
667667
/++
668668
Computes an index for `r` based on the comparison `less`. The
669669
index is a sorted array of indices into the original
670-
range. This technique is similar to sorting, but it is more flexible
670+
range.
671+
672+
This technique is similar to sorting, but it is more flexible
671673
because (1) it allows "sorting" of immutable collections, (2) allows
672674
binary search even if the original collection does not offer random
673675
access, (3) allows multiple indices, each on a different predicate,
@@ -676,10 +678,19 @@ an index may also be slower under certain circumstances due to the
676678
extra indirection, and is always larger than a sorting-based solution
677679
because it needs space for the index in addition to the original
678680
collection. The complexity is the same as `sort`'s.
681+
682+
Can be combined with $(SUBREF topology, indexed) to create a view that is sorted
683+
based on the index.
684+
679685
Params:
680686
less = The comparison to use.
681687
r = The slice/array to index.
682-
Returns: Index slice/array.
688+
689+
Returns:
690+
Index slice/array.
691+
692+
See_Also:
693+
$(HTTPS numpy.org/doc/stable/reference/generated/numpy.argsort.html, numpy.argsort)
683694
+/
684695
Slice!(I*) makeIndex(I = size_t, alias less = "a < b", Iterator, SliceKind kind)(Slice!(Iterator, 1, kind) r)
685696
{
@@ -701,7 +712,8 @@ I[] makeIndex(I = size_t, alias less = "a < b", T)(scope T[] r)
701712

702713
///
703714
version(mir_test)
704-
@system unittest
715+
@safe pure nothrow
716+
unittest
705717
{
706718
import mir.algorithm.iteration: all;
707719
import mir.ndslice.topology: indexed, pairwise;
@@ -712,6 +724,23 @@ version(mir_test)
712724
assert(arr.indexed(index).pairwise!"a < b".all);
713725
}
714726

727+
/// Sort based on index created from a separate array
728+
version(mir_test)
729+
@safe pure nothrow
730+
unittest
731+
{
732+
import mir.algorithm.iteration: equal;
733+
import mir.ndslice.topology: indexed;
734+
735+
immutable arr0 = [ 2, 3, 1, 5, 0 ];
736+
immutable arr1 = [ 1, 5, 4, 2, -1 ];
737+
738+
auto index = makeIndex(arr0);
739+
assert(index.equal([4, 2, 0, 1, 3]));
740+
auto view = arr1.indexed(index);
741+
assert(view.equal([-1, 4, 1, 5, 2]));
742+
}
743+
715744
/++
716745
Partitions `slice` around `pivot` using comparison function `less`, algorithm
717746
akin to $(LINK2 https://en.wikipedia.org/wiki/Quicksort#Hoare_partition_scheme,
@@ -741,7 +770,7 @@ Params:
741770
742771
Returns:
743772
The new position of the pivot
744-
773+
745774
See_Also:
746775
$(HTTP jgrcs.info/index.php/jgrcs/article/view/142, Engineering of a Quicksort
747776
Partitioning Algorithm), D. Abhyankar, Journal of Global Research in Computer

0 commit comments

Comments
 (0)