makeIndex

Computes an index for r based on the comparison less. The index is a sorted array of indices into the original range.

This technique is similar to sorting, but it is more flexible because (1) it allows "sorting" of immutable collections, (2) allows binary search even if the original collection does not offer random access, (3) allows multiple indices, each on a different predicate, and (4) may be faster when dealing with large objects. However, using an index may also be slower under certain circumstances due to the extra indirection, and is always larger than a sorting-based solution because it needs space for the index in addition to the original collection. The complexity is the same as sort's.

Can be combined with indexed to create a view that is sorted based on the index.

  1. Slice!(I*) makeIndex(Slice!(Iterator, 1, kind) r)
    @optmath
    Slice!(I*)
    makeIndex
    (
    I = size_t
    alias less = "a < b"
    Iterator
    SliceKind kind
    )
    (
    Slice!(Iterator, 1, kind) r
    )
  2. I[] makeIndex(T[] r)

Parameters

less

The comparison to use.

r Slice!(Iterator, 1, kind)

The slice/array to index.

Return Value

Type: Slice!(I*)

Index slice/array.

See Also

Meta