Search
View source code
Display the source code in std/algorithm/sorting.d from which this page was generated on github.
Report a bug
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

# Function std.algorithm.sorting.topNIndex

Given a range of elements, constructs an index of its top n elements (i.e., the first n elements if the range were sorted).

void topNIndex(alias less, SwapStrategy ss = SwapStrategy.unstable, Range, RangeIndex) (
Range r,
RangeIndex index,
SortOutput sorted = No.sortOutput
)
if (isRandomAccessRange!Range && isRandomAccessRange!RangeIndex && hasAssignableElements!RangeIndex);

Similar to topN, except that the range is not modified.

## Parameters

NameDescription
less A binary predicate that defines the ordering of range elements. Defaults to a < b.
ss (Not implemented yet.) Specify the swapping strategy.
r A random-access range of elements to make an index for.
index A random-access range with assignable elements to build the index in. The length of this range determines how many top elements to index in r. This index range can either have integral elements, in which case the constructed index will consist of zero-based numerical indices into r; or it can have pointers to the element type of r, in which case the constructed index will be pointers to the top elements in r.
sorted Determines whether to sort the index by the elements they refer to.

## BUGS

The swapping strategy parameter is not implemented yet; currently it is ignored.

## Example

import std.typecons : Yes;

// Construct index to top 3 elements using numerical indices:
int[] a = [ 10, 2, 7, 5, 8, 1 ];
int[] index = new int[3];
topNIndex(a, index, Yes.sortOutput);
assert(index == [5, 1, 3]); // because a[5]==1, a[1]==2, a[3]==5

// Construct index to top 3 elements using pointer indices:
int*[] ptrIndex = new int*[3];
topNIndex(a, ptrIndex, Yes.sortOutput);
writeln(ptrIndex); // [&a[5], &a[1], &a[3]]