Given a range of sorted forward ranges
ror, 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 elements are returned.
void largestPartialIntersection(alias less, RangeOfRanges, Range) (
SortOutput sorted = No
|less||The predicate the ranges are sorted by.|
|ror|| A range of forward ranges sorted by |
|tgt||The target range to copy common elements to.|
|sorted|| Whether the elements copied should be in sorted order.
The function |
largestPartialIntersection does not allocate
extra memory, it will leave
ror modified. Namely,
largestPartialIntersection assumes ownership of
discretionarily swaps and advances elements of it. If you want
ror to preserve its contents after the call, you may want to pass a
largestPartialIntersection (and perhaps cache the
duplicate in between calls).
.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); // 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); // 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); largestPartialIntersection(a, c); writeln(c); // tuple(1.0, 3u) // 1.0 occurs in 3 inputs // multiset double x = [ [1, 1, 1, 1, 4, 7, 8], [1, 7], [1, 7, 8], [4, 7],  ]; auto y = new Tuple!(double, uint); largestPartialIntersection(x .dup, y); // 7.0 occurs 5 times writeln(y); // tuple(7.0, 5u) // 1.0 occurs 6 times writeln(y); // tuple(1.0, 6u)