Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
|
|
|
|
namespace Phpml\Math;
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
use ArrayIterator;
|
|
|
|
use IteratorAggregate;
|
|
|
|
|
|
|
|
class Set implements IteratorAggregate
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string[]|int[]|float[]
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
private $elements = [];
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[]|int[]|float[] $elements
|
|
|
|
*/
|
|
|
|
public function __construct(array $elements = [])
|
|
|
|
{
|
|
|
|
$this->elements = self::sanitize($elements);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the union of A and B.
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
public static function union(self $a, self $b): self
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
return new self(array_merge($a->toArray(), $b->toArray()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the intersection of A and B.
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
public static function intersection(self $a, self $b): self
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
return new self(array_intersect($a->toArray(), $b->toArray()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the difference of A and B.
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
public static function difference(self $a, self $b): self
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
return new self(array_diff($a->toArray(), $b->toArray()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the Cartesian product of A and B.
|
|
|
|
*
|
|
|
|
* @return Set[]
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
public static function cartesian(self $a, self $b): array
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
$cartesian = [];
|
|
|
|
|
|
|
|
foreach ($a as $multiplier) {
|
|
|
|
foreach ($b as $multiplicand) {
|
|
|
|
$cartesian[] = new self(array_merge([$multiplicand], [$multiplier]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cartesian;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the power set of A.
|
|
|
|
*
|
|
|
|
* @return Set[]
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
public static function power(self $a): array
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
$power = [new self()];
|
|
|
|
|
|
|
|
foreach ($a as $multiplicand) {
|
|
|
|
foreach ($power as $multiplier) {
|
|
|
|
$power[] = new self(array_merge([$multiplicand], $multiplier->toArray()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $power;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|int|float $element
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
public function add($element): self
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
return $this->addAll([$element]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[]|int[]|float[] $elements
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
public function addAll(array $elements): self
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
$this->elements = self::sanitize(array_merge($this->elements, $elements));
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|int|float $element
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
public function remove($element): self
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
return $this->removeAll([$element]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[]|int[]|float[] $elements
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
public function removeAll(array $elements): self
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
$this->elements = self::sanitize(array_diff($this->elements, $elements));
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|int|float $element
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
public function contains($element): bool
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
return $this->containsAll([$element]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[]|int[]|float[] $elements
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
public function containsAll(array $elements): bool
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
return !array_diff($elements, $this->elements);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]|int[]|float[]
|
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
public function toArray(): array
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
return $this->elements;
|
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
public function getIterator(): ArrayIterator
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
2017-11-22 21:16:10 +00:00
|
|
|
return new ArrayIterator($this->elements);
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
public function isEmpty(): bool
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
return $this->cardinality() == 0;
|
|
|
|
}
|
|
|
|
|
2017-11-22 21:16:10 +00:00
|
|
|
public function cardinality(): int
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
return count($this->elements);
|
|
|
|
}
|
2017-11-22 21:16:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes duplicates and rewrites index.
|
|
|
|
*
|
|
|
|
* @param string[]|int[]|float[] $elements
|
|
|
|
*
|
|
|
|
* @return string[]|int[]|float[]
|
|
|
|
*/
|
|
|
|
private static function sanitize(array $elements): array
|
|
|
|
{
|
|
|
|
sort($elements, SORT_ASC);
|
|
|
|
|
|
|
|
return array_values(array_unique($elements, SORT_ASC));
|
|
|
|
}
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
}
|