View source code Display the source code in std/algorithm/sorting.d from which this page was generated on github. Improve this page 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. Page wiki View or edit the community-maintained wiki page associated with this page.

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

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

Prototypes

void topNIndex(alias less, std.algorithm.mutation.SwapStrategy ss, Range, RangeIndex)(
  Range r,
  RangeIndex index,
  SortOutput sorted = SortOutput.no
)
if (isRandomAccessRange!Range && isRandomAccessRange!RangeIndex && hasAssignableElements!RangeIndex && isIntegral!(ElementType!RangeIndex));

void topNIndex(alias less, std.algorithm.mutation.SwapStrategy ss, Range, RangeIndex)(
  Range r,
  RangeIndex index,
  SortOutput sorted = SortOutput.no
)
if (isRandomAccessRange!Range && isRandomAccessRange!RangeIndex && hasAssignableElements!RangeIndex && is(ElementType!RangeIndex == ElementType!Range*));

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.

See also

topN, topNCopy.

BUGS

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

Example

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

Authors

Andrei Alexandrescu

License

Boost License 1.0.

Comments