PHP Класс MathPHP\SetTheory\Set

https://en.wikipedia.org/wiki/Set_(mathematics) Sets can contain numbers, strings, arrays, objects, and other sets. Implementation: For performance reasons, PHP arrays are used as a hash for quick access via hash keys. The hash keys are as follows: - Numbers and strings: value itself - Sets: Set as a string. - Arrays: Array(array_serialization) - Objects: Object\Name(object_hash) - Resource: Resource(Resource id: #) - Null: '' The values of the associative array (hash) are the actual values or objects themselves. If the set is iterated in a foreach loop you will get back the original value, set, array, or object. An object cannot be in the set multiple times. For a regular value, like a number or string, this is straight forward. For arrays and objects, the behavior is based on whether they are the same thing. What that means depends on whether it is an array or object. Example (arrays): $array1 = [1, 2, 3]; $array2 = [1, 2, 3]; $set = new Set([$array1, $array2]); The set will have only one element, because the arrays are equal. $array2 === $array2 evaluates to true. Example (different objects): $object1 = new \StdClass(); $object2 = new \StdClass(); $set = new Set([$object1, $object2]); The set will have two elements, because they are different objects. $object1 === $object2 evaluates to false. Example (same objects): $object1 = new \StdClass(); $object2 = $object1; $set = new Set([$object1, $object2]); The set will have only one element, because the objects are the same. $object1 === $object2 evaluates to true. Example (Sets, a special case of object) $set1 = new Set([1, 2, 3]); $set2 = new Set([1, 2, 3]); $set3 = new Set([$set1, $set2]); Set3 will have only one element, because sets 1 and 2 are the same. Sets are not based on whether the object is the same, but whether the content of the set are the same. Sets and arrays act similarly. When storing a Set object as a member of a set, its key will be a string that uses mathematical set notation with the addtion of the word 'Set'. For example: Set{1, 2, 3} The one edge case of this, is that the Set object {1, 2, 3} and the string 'Set{1, 2, 3}' would appear identical in the case of adding one when the other already is a member of the set. When accessing the actual set member, you will always get back the original one added, whether it was a Set object or a string.
Наследование: implements Countable, implements Iterator
Показать файл Открыть проект Примеры использования класса

Защищенные свойства (Protected)

Свойство Тип Описание
$A array Keys are a representation of the members of the set. Values are the values/objects themselves.
$iterator_keys array Iterator interface array to iterate over
$iterator_position mixed Iterator interface position of iterator keys we are at (the key)

Открытые методы

Метод Описание
__construct ( array $members = [] ) Constructor - Initialize set members
__toString ( ) : string Return the set as a string Set{a, b, c, .
add ( mixed $x ) : Set Add an element to the set Does nothing if element already exists in the set.
addMulti ( array $members ) : Set Add an array of elements to the set Does nothing if element already exists in the set.
asArray ( ) : array Get the set as an array
cartesianProduct ( Set $B ) : Set Cartesian product (A×B) Produces a new set by associating every element of the set with every element of the other set.
clear ( ) : Set Clear the set. Removes all members.
copy ( ) : Set Copy Produces a new set with the same elements as the set.
count ( ) : integer Countable interface Computes cardinality of a set S, |S|
current ( ) Current (Iterator interface)
difference ( variadic $Bs ) : Set Difference (relative complement) (A ∖ B) or (A - B) Produces a new set with elements that are not in the other sets.
intersect ( variadic $Bs ) : Set Intersect (A ∩ B) Produces a new set with all the elements common to all sets.
isDisjoint ( Set $other ) : boolean Disjoint Does the set have no elements in common with the other set?
isEmpty ( ) : boolean ************************************************************************ SET PROPERTIES - Empty set ************************************************************************
isMember ( mixed $x ) : boolean Set membership (x ∈ A) Is x a member of the set?
isNotMember ( mixed $x ) : boolean Set non-membership (x ∉ A) Is x not a member of the set?
isProperSubset ( Set $B ) : boolean Proper subset (A ⊆ B & A ≠ B) Is the set a proper subset of the other set? In other words, does the other set contain all the elements of the set, and the set is not the same set as the other set?
isProperSuperset ( Set $B ) : boolean Superset (A ⊇ B & A ≠ B) Is the set a superset of the other set? In other words, does the the set contain all the elements of the other set, and the set is not the same set as the other set?
isSubset ( Set $B ) : boolean Subset (A ⊆ B) Is the set a subset of the other set? In other words, does the other set contain all the elements of the set?
isSuperset ( Set $B ) : boolean Superset (A ⊇ B) Is the set a superset of the other set? In other words, does the the set contain all the elements of the other set?
key ( ) Key (Iterator interface)
length ( ) : integer Get length of set (number of members in set)
next ( ) Next (Iterator interface)
powerSet ( ) : Set Power set P(S) The set of all subsets of S, including the empty set and S itself.
remove ( mixed $x ) : Set Remove an element from the set Does nothing if the element does not exist in the set.
removeMulti ( array $x ) : Set Remove elements from the set Does nothing if the element does not exist in the set.
rewind ( ) Rewind (Iterator interface)
symmetricDifference ( Set $B ) : Set Symmetric Difference (A Δ B) = (A ∖ B) ∪ (B ∖ A) Produces a new set with elements that are in the set or the other, but not both.
union ( variadic $Bs ) : Set Union (A ∪ B) Produces a new set with all elements from all sets.
valid ( ) : boolean Valid (Iterator interface)

Защищенные методы

Метод Описание
getKey ( mixed $x ) : string Determine the key for the member to be added

Описание методов

__construct() публичный Метод

Constructor - Initialize set members
public __construct ( array $members = [] )
$members array

__toString() публичный Метод

..}
public __toString ( ) : string
Результат string

add() публичный Метод

Add an element to the set Does nothing if element already exists in the set.
public add ( mixed $x ) : Set
$x mixed
Результат Set (this set)

addMulti() публичный Метод

Add an array of elements to the set Does nothing if element already exists in the set.
public addMulti ( array $members ) : Set
$members array
Результат Set (this set)

asArray() публичный Метод

Get the set as an array
public asArray ( ) : array
Результат array (values are the set members)

cartesianProduct() публичный Метод

Example: A = (1, 2) B = (a, b) A×B = ((1, a), (1, b), (2, 1), (2, b))
public cartesianProduct ( Set $B ) : Set
$B Set
Результат Set

clear() публичный Метод

Results in an empty set.
public clear ( ) : Set
Результат Set (this set)

copy() публичный Метод

Copy Produces a new set with the same elements as the set.
public copy ( ) : Set
Результат Set

count() публичный Метод

Countable interface Computes cardinality of a set S, |S|
public count ( ) : integer
Результат integer

current() публичный Метод

Current (Iterator interface)
public current ( )

difference() публичный Метод

Difference (relative complement) (A ∖ B) or (A - B) Produces a new set with elements that are not in the other sets.
public difference ( variadic $Bs ) : Set
$Bs variadic One or more sets
Результат Set

getKey() защищенный Метод

Based on the type of member to be added, the key differs: - Number: value as is - String: value as is - Set: String representation of set. Example: {1, 2} - Array: Array(array_serialization) - Object: Class\Name(object_hash) - Resource: Resource(Resource id #) - Null: ''
protected getKey ( mixed $x ) : string
$x mixed
Результат string

intersect() публичный Метод

Example: {1, 2} ∩ {2, 3} = {2}
public intersect ( variadic $Bs ) : Set
$Bs variadic One or more sets
Результат Set

isDisjoint() публичный Метод

Example of disjoint sets: A = {1, 2, 3} B = {4, 5, 6}
public isDisjoint ( Set $other ) : boolean
$other Set
Результат boolean

isEmpty() публичный Метод

************************************************************************ SET PROPERTIES - Empty set ************************************************************************
public isEmpty ( ) : boolean
Результат boolean

isMember() публичный Метод

Set membership (x ∈ A) Is x a member of the set?
public isMember ( mixed $x ) : boolean
$x mixed
Результат boolean

isNotMember() публичный Метод

Set non-membership (x ∉ A) Is x not a member of the set?
public isNotMember ( mixed $x ) : boolean
$x mixed
Результат boolean

isProperSubset() публичный Метод

Proper subset (A ⊆ B & A ≠ B) Is the set a proper subset of the other set? In other words, does the other set contain all the elements of the set, and the set is not the same set as the other set?
public isProperSubset ( Set $B ) : boolean
$B Set
Результат boolean

isProperSuperset() публичный Метод

Superset (A ⊇ B & A ≠ B) Is the set a superset of the other set? In other words, does the the set contain all the elements of the other set, and the set is not the same set as the other set?
public isProperSuperset ( Set $B ) : boolean
$B Set
Результат boolean

isSubset() публичный Метод

Subset (A ⊆ B) Is the set a subset of the other set? In other words, does the other set contain all the elements of the set?
public isSubset ( Set $B ) : boolean
$B Set
Результат boolean

isSuperset() публичный Метод

Superset (A ⊇ B) Is the set a superset of the other set? In other words, does the the set contain all the elements of the other set?
public isSuperset ( Set $B ) : boolean
$B Set
Результат boolean

key() публичный Метод

Key (Iterator interface)
public key ( )

length() публичный Метод

Get length of set (number of members in set)
public length ( ) : integer
Результат integer

next() публичный Метод

Next (Iterator interface)
public next ( )

powerSet() публичный Метод

Example: S = {x, y, z} P(S) = {Ø, {x}, {y}, {z}, {x,y}, {x,z}, {y,z}, {x,y,z}} Algorithm: Setup: - n: size of the original set - 2ⁿ: size of the power set - A: original set as an array with numbered indices 0 to n - 1 - P(S): power set to be created Iterative loop algorithm: - Loop i from 0 to < 2ⁿ - Create empty temporary Set - Loop j from 0 to < n - If the jᵗʰ bit of the i counter is set, add A[j] to temporary Set - Add temporary set to power set Time complexity: O(n2ⁿ) Reference: http://www.geeksforgeeks.org/power-set/
public powerSet ( ) : Set
Результат Set

remove() публичный Метод

Remove an element from the set Does nothing if the element does not exist in the set.
public remove ( mixed $x ) : Set
$x mixed
Результат Set (this set)

removeMulti() публичный Метод

Remove elements from the set Does nothing if the element does not exist in the set.
public removeMulti ( array $x ) : Set
$x array
Результат Set (this set)

rewind() публичный Метод

Rewind (Iterator interface)
public rewind ( )

symmetricDifference() публичный Метод

Example: {7, 8, 9, 10} Δ {9, 10, 11, 12} = {7, 8, 11, 12}
public symmetricDifference ( Set $B ) : Set
$B Set
Результат Set

union() публичный Метод

Example: {1, 2} ∪ {2, 3} = {1, 2, 3}
public union ( variadic $Bs ) : Set
$Bs variadic One or more sets
Результат Set

valid() публичный Метод

Valid (Iterator interface)
public valid ( ) : boolean
Результат boolean

Описание свойств

$A защищенное свойство

Keys are a representation of the members of the set. Values are the values/objects themselves.
protected array $A
Результат array

$iterator_keys защищенное свойство

Iterator interface array to iterate over
protected array $iterator_keys
Результат array

$iterator_position защищенное свойство

Iterator interface position of iterator keys we are at (the key)
protected mixed $iterator_position
Результат mixed