Smoren / partial-intersection-php

M-partial intersection of sets and multisets explanation and examples

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

M-partial intersection of sets and multisets explanation

Scrutinizer Code Quality Coverage Status Build and test License: MIT

Theory

Definition

An M-partial intersection (for M > 0) of N sets is a set elements in which are contained in at least M initial sets.

Properties

For any N sets:

  1. 1-partial intersection is equivalent to the union of these sets.
  2. N-partial intersection is equivalent to the common (complete) intersection of these sets.
  3. For any M > N M-partial intersection always equals to the empty set.

Examples

Simple integer sets example

Given: sets A, B, C, D (N = 4).

$a = [1, 2, 3, 4, 5];
$b = [1, 2, 10, 11];
$c = [1, 2, 3, 12];
$d = [1, 4, 13, 14];

M = 1

It is equivalent to A ∪ B ∪ C ∪ D.

image

use Smoren\PartialIntersection\IntegerSetArrayImplementation;

$r = IntegerSetArrayImplementation::partialIntersection(1, $a, $b, $c, $d);
// [1, 2, 3, 4, 5, 10, 11, 12, 13, 14]

M = 2

image

use Smoren\PartialIntersection\IntegerSetArrayImplementation;

$r = IntegerSetArrayImplementation::partialIntersection(2, $a, $b, $c, $d);
// [1, 2, 3, 4]

M = 3

image

use Smoren\PartialIntersection\IntegerSetArrayImplementation;

$r = IntegerSetArrayImplementation::partialIntersection(3, $a, $b, $c, $d);
// [1, 2]

M = 4 (M = N)

It is equivalent to A ∩ B ∩ C ∩ D.

image

use Smoren\PartialIntersection\IntegerSetArrayImplementation;

$r = IntegerSetArrayImplementation::partialIntersection(4, $a, $b, $c, $d);
// [1]

M = 5 (M > N)

Equals to an empty set.

image

use Smoren\PartialIntersection\IntegerSetArrayImplementation;

$r = IntegerSetArrayImplementation::partialIntersection(5, $a, $b, $c, $d);
// []

Iterable integer sets example

$a = [1, 2, 3, 4, 5];
$b = [1, 2, 10, 11];
$c = [1, 2, 3, 12];
$d = [1, 4, 13, 14];

use Smoren\PartialIntersection\IntegerSetIterableImplementation;

$r = IntegerSetArrayImplementation::partialIntersection(1, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// [1, 2, 3, 4, 5, 10, 11, 12, 13, 14]

$r = IntegerSetArrayImplementation::partialIntersection(2, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// [1, 2, 3, 4]

$r = IntegerSetArrayImplementation::partialIntersection(3, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// [1, 2]

$r = IntegerSetArrayImplementation::partialIntersection(4, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// [1]

$r = IntegerSetArrayImplementation::partialIntersection(5, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// []

Mixed iterable sets example

$a = ['1', 2, 3, 4, 5];
$b = ['1', 2, 10, 11];
$c = ['1', 2, 3, 12];
$d = ['1', 4, 13, 14];

use Smoren\PartialIntersection\MixedSetIterableImplementation;

$r = MixedSetIterableImplementation::partialIntersection(true, 1, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// ['1', 2, 3, 4, 5, 10, 11, 12, 13, 14]

$r = MixedSetIterableImplementation::partialIntersection(true, 2, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// ['1', 2, 3, 4]

$r = MixedSetIterableImplementation::partialIntersection(true, 3, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// ['1', 2]

$r = MixedSetIterableImplementation::partialIntersection(true, 4, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// ['1']

$r = IntegerSetArrayImplementation::partialIntersection(true, 5, $a, $b, $c, $d);
print_r(iterator_to_array($r));
// []

Multisets example

Note: If input collections contains duplicate items, then multiset intersection rules apply.

$a = [1, 1, 1, 1, 1];
$b = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5];
$c = [5, 5, 5, 5, 5, 1, 5, 5, 1];

use Smoren\PartialIntersection\MultisetIterableImplementation;

$r = MultisetIterableImplementation::partialIntersection(true, 1, $a, $b, $c);
print_r(iterator_to_array($r));
// [1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5, 5, 5, 5]

$r = MultisetIterableImplementation::partialIntersection(true, 2, $a, $b, $c);
print_r(iterator_to_array($r));
// [1, 1, 5, 5]

$r = MultisetIterableImplementation::partialIntersection(true, 3, $a, $b, $c);
print_r(iterator_to_array($r));
// [1, 1]

$r = MultisetIterableImplementation::partialIntersection(true, 4, $a, $b, $c);
print_r(iterator_to_array($r));
// []

Unit testing

composer install
composer test-init
composer test

About

M-partial intersection of sets and multisets explanation and examples

License:MIT License


Languages

Language:PHP 100.0%