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).

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

void
topNIndex
(
alias less = "a < b"
Range
RangeIndex
)
(
Range r
,
RangeIndex index
,
SortOutput sorted = No.sortOutput
)

Parameters

less

A binary predicate that defines the ordering of range elements. Defaults to a < b.

ss

(Not implemented yet.) Specify the swapping strategy.

r Range

A random-access range of elements to make an index for.

index RangeIndex

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 SortOutput

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.

Examples

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);
assert(ptrIndex == [ &a[5], &a[1], &a[3] ]);

See Also

Meta