The OpenD Programming Language

largestPartialIntersectionWeighted

Similar to largestPartialIntersection, but associates a weight with each distinct element in the intersection.

If at least one of the ranges is a multiset, then all occurences of a duplicate element are taken into account. The result is equivalent to merging all input ranges and picking the highest tgt.length, weight-based ranking elements.

void
largestPartialIntersectionWeighted
(
alias less = "a < b"
RangeOfRanges
Range
WeightsAA
)
(
RangeOfRanges ror
,
Range tgt
,
WeightsAA weights
,
SortOutput sorted = No.sortOutput
)

Parameters

less

The predicate the ranges are sorted by.

ror RangeOfRanges

A range of forward ranges sorted by less.

tgt Range

The target range to copy common elements to.

weights WeightsAA

An associative array mapping elements to weights.

sorted SortOutput

Whether the elements copied should be in sorted order.

Examples

 import std.typecons : tuple, Tuple;

 // Figure which number can be found in most arrays of the set of
 // arrays below, with specific per-element weights
 double[][] a =
 [
     [ 1, 4, 7, 8 ],
     [ 1, 7 ],
     [ 1, 7, 8],
     [ 4 ],
     [ 7 ],
 ];
 auto b = new Tuple!(double, uint)[1];
 double[double] weights = [ 1:1.2, 4:2.3, 7:1.1, 8:1.1 ];
 largestPartialIntersectionWeighted(a, b, weights);
 // First member is the item, second is the occurrence count
 assert(b[0] == tuple(4.0, 2u));
 // 4.0 occurs 2 times -> 4.6 (2 * 2.3)
 // 7.0 occurs 3 times -> 4.4 (3 * 1.1)

// multiset
 double[][] x =
 [
     [ 1, 1, 1, 4, 7, 8 ],
     [ 1, 7 ],
     [ 1, 7, 8],
     [ 4 ],
     [ 7 ],
 ];
 auto y = new Tuple!(double, uint)[1];
 largestPartialIntersectionWeighted(x, y, weights);
 assert(y[0] == tuple(1.0, 5u));
 // 1.0 occurs 5 times -> 1.2 * 5 = 6

Meta