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
a local clone.

std.algorithm.setops

This is a submodule of std.algorithm.
It contains generic algorithms that implement set operations.

Given a range of sortedforward rangesror, copies to tgt the elements that are common to most ranges, along with their number
of occurrences. All ranges in ror are assumed to be sorted by less. Only the most frequent tgt.length elements are returned.

Parameters:

less

The predicate the ranges are sorted by.

RangeOfRanges ror

A range of forward ranges sorted by less.

Range tgt

The target range to copy common elements to.

SortOutput sorted

Whether the elements copied should be in sorted order.

The function largestPartialIntersection is useful for
e.g. searching an inverted index for the documents most
likely to contain some terms of interest. The complexity of the search
is Ο(n * log(tgt.length)), where n is the sum of lengths of
all input ranges. This approach is faster than keeping an associative
array of the occurrences and then selecting its top items, and also
requires less memory (largestPartialIntersection builds its
result directly in tgt and requires no extra memory).

Warning:
Because largestPartialIntersection does not allocate
extra memory, it will leave ror modified. Namely, largestPartialIntersection assumes ownership of ror and
discretionarily swaps and advances elements of it. If you want ror to preserve its contents after the call, you may want to pass a
duplicate to largestPartialIntersection (and perhaps cache the
duplicate in between calls).

Examples:

import std.typecons : tuple, Tuple;
// Figure which number can be found in most arrays of the set of
// arrays below.
double[][] a =
[
[ 1, 4, 7, 8 ],
[ 1, 7 ],
[ 1, 7, 8],
[ 4 ],
[ 7 ],
];
auto b = new Tuple!(double, uint)[1];
// it will modify the input range, hence we need to create a duplicate
largestPartialIntersection(a.dup, b);
// First member is the item, second is the occurrence count
writeln(b[0]); // tuple(7.0, 4u)
// 7.0 occurs in 4 out of 5 inputs, more than any other number
// If more of the top-frequent numbers are needed, just create a larger
// tgt range
auto c = new Tuple!(double, uint)[2];
largestPartialIntersection(a, c);
writeln(c[0]); // tuple(1.0, 3u)
// 1.0 occurs in 3 inputs

Computes the union of multiple sets. The input sets are passed as a
range of ranges and each is assumed to be sorted by less. Computation is done lazily, one union element at a time. The
complexity of one popFront operation is Ο(log(ror.length)). However, the length of ror decreases as ranges
in it are exhausted, so the complexity of a full pass through NWayUnion is dependent on the distribution of the lengths of ranges
contained within ror. If all ranges have the same length n
(worst case scenario), the complexity of a full pass through NWayUnion is Ο(n * ror.length * log(ror.length)), i.e., log(ror.length) times worse than just spanning all ranges in
turn. The output comes sorted (unstably) by less.

Parameters:

less

Predicate the given ranges are sorted by.

RangeOfRanges ror

A range of ranges sorted by less to compute the union for.

Returns:

A range of the union of the ranges in ror.

Warning:
Because NWayUnion does not allocate extra memory, it
will leave ror modified. Namely, NWayUnion assumes ownership
of ror and discretionarily swaps and advances elements of it. If
you want ror to preserve its contents after the call, you may
want to pass a duplicate to NWayUnion (and perhaps cache the
duplicate in between calls).

Lazily computes the symmetric difference of r1 and r2,
i.e. the elements that are present in exactly one of r1 and r2. The two ranges are assumed to be sorted by less, and the
output is also sorted by less. The element types of the two
ranges must have a common type.

If both arguments are ranges of L-values of the same type then
SetSymmetricDifference will also be a range of L-values of
that type.