code analysis fixes

This commit is contained in:
Dennis Eichhorn 2023-10-15 12:45:18 +00:00
parent 2655c816a9
commit 624d6b517e
8 changed files with 234 additions and 29 deletions

View File

@ -107,6 +107,16 @@ final class MarkovChain
$this->data = $values;
}
/**
* Generate a markov chain based on the training data.
*
* @param int $length Length of the markov chain
* @param array $start Start values of the markov chain
*
* @return array
*
* @since 1.0.0
*/
public function generate(int $length, array $start = null) : array
{
$orderKeys = \array_keys($this->data);
@ -148,6 +158,15 @@ final class MarkovChain
return $output;
}
/**
* Calculate the probability for a certain markov chain.
*
* @param array $path Markov chain
*
* @return float
*
* @since 1.0.0
*/
public function pathProbability(array $path) : float
{
$length = \count($path);
@ -168,6 +187,16 @@ final class MarkovChain
return $prob;
}
/**
* Calculate the probability for a certain state change in a markov chain
*
* @param array $state Current state of the markov chain
* @param mixed $next Next markov state
*
* @return float
*
* @since 1.0.0
*/
public function stepProbability(array $state, mixed $next) : float
{
if (\count($state) !== $this->order) {

View File

@ -24,4 +24,4 @@ namespace phpOMS\Business;
*/
final class BusinessHelper
{
}
}

View File

@ -204,7 +204,7 @@ abstract class GrammarAbstract
$expression .= $element->toSql() . (\is_string($key) ? ' as ' . $key : '') . ', ';
} elseif (\is_int($element)) {
$expression .= $element . ', ';
}else {
} else {
throw new \InvalidArgumentException();
}
}

View File

@ -89,4 +89,4 @@ class RegionEnum extends Enum
public const ANTARCTICA = 'Antarctica';
public const CONTINENTS = 'Continents';
}
}

View File

@ -60,18 +60,16 @@ final class GrahamScan
}
}
$temp = $points[1];
$points[1] = $points[$min];
$temp = $points[1];
$points[1] = $points[$min];
$points[$min] = $temp;
$c = $points[1];
$subpoints = \array_slice($points, 2, $count);
\usort($subpoints, function (array $a, array $b) use ($c) : bool
{
return atan2($a['y'] - $c['y'], $a['x'] - $c['x']) < atan2( $b['y'] - $c['y'], $b['x'] - $c['x']);
}
);
\usort($subpoints, function (array $a, array $b) use ($c) : bool {
return atan2($a['y'] - $c['y'], $a['x'] - $c['x']) < atan2( $b['y'] - $c['y'], $b['x'] - $c['x']);
});
$points = \array_merge([$points[0], $points[1]], $subpoints);
$points[0] = $points[$count];
@ -88,9 +86,9 @@ final class GrahamScan
}
}
$temp = $points[$i];
$temp = $points[$i];
$points[$size + 1] = $points[$i];
$points[$i] = $points[$size + 1];
$points[$i] = $points[$size + 1];
++$size;
}
@ -102,7 +100,18 @@ final class GrahamScan
return $hull;
}
public static function ccw(array $a, array $b, array $c)
/**
* Counterclockwise rotation
*
* @param float[] $a Vector
* @param float[] $b Vector
* @param float[] $c Vector
*
* @return int|float
*
* @since 1.0.0
*/
public static function ccw(array $a, array $b, array $c) : int|float
{
return (($b['x'] - $a['x']) * ($c['y'] - $a['y']) - ($b['y'] - $a['y']) * ($c['x'] - $a['x']));
}

View File

@ -360,26 +360,68 @@ class SmartDateTime extends \DateTime
return $days;
}
/**
* Get the start of the year based on a custom starting month
*
* @param int $month Start of the year (i.e. fiscal year)
*
* @return \DateTime
*
* @since 1.0.0
*/
public static function startOfYear(int $month = 1) : \DateTime
{
return new \DateTime(\date('Y') . '-' . \sprintf('%02d', $month) . '-01');
}
/**
* Get the end of the year based on a custom starting month
*
* @param int $month Start of the year (i.e. fiscal year)
*
* @return \DateTime
*
* @since 1.0.0
*/
public static function endOfYear(int $month = 1) : \DateTime
{
return new \DateTime(\date('Y') . '-' . self::calculateMonthIndex(13 - $month, $month) . '-31');
}
/**
* Get the start of the month
*
* @return \DateTime
*
* @since 1.0.0
*/
public static function startOfMonth() : \DateTime
{
return new \DateTime(\date('Y-m') . '-01');
}
/**
* Get the end of the month
*
* @return \DateTime
*
* @since 1.0.0
*/
public static function endOfMonth() : \DateTime
{
return new \DateTime(\date('Y-m-t'));
}
/**
* Calculate the difference in months between two dates
*
* @param \DateTime $d1 First datetime
* @param \DateTime $d2 Second datetime
*
* @return int
*
* @since 1.0.0
*/
public static function monthDiff(\DateTime $d1, \DateTime $d2) : int
{
$interval = $d1->diff($d2);

View File

@ -24,13 +24,35 @@ namespace phpOMS\Stdlib\Tree;
*/
class BinarySearchTree
{
/**
* Root node
*
* @param null|Node
* @since 1.0.0
*/
public ?Node $root = null;
/**
* Constructor.
*
* @param null|Node $root Root node
*
* @since 1.0.0
*/
public function __construct(Node $root = null)
{
$this->root = $root;
}
/**
* Search node by data
*
* @param mixed $data Data to search for
*
* @return null|Node
*
* @since 1.0.0
*/
public function search(mixed $data) : ?Node
{
if ($this->root === null) {
@ -48,6 +70,13 @@ class BinarySearchTree
return $this->root;
}
/**
* Find the smallest node
*
* @return null|Node
*
* @since 1.0.0
*/
public function minimum() : ?Node
{
if ($this->root === null) {
@ -61,6 +90,13 @@ class BinarySearchTree
return $this->root->left->minimum();
}
/**
* Find the largest node
*
* @return null|Node
*
* @since 1.0.0
*/
public function maximum() : ?Node
{
if ($this->root === null) {
@ -74,6 +110,15 @@ class BinarySearchTree
return $this->root->right->minimum();
}
/**
* Find the predecessor of a node
*
* @param Node $node Node
*
* @return null|Node
*
* @since 1.0.0
*/
public function predecessor(Node $node) : ?Node
{
if ($node->left !== null) {
@ -83,12 +128,21 @@ class BinarySearchTree
$top = $node->parent;
while ($top !== Null && $top->compare($node->data)) {
$node = $top;
$top = $top->parent;
$top = $top->parent;
}
return $top;
}
/**
* Find the successor of a node
*
* @param Node $node Node
*
* @return null|Node
*
* @since 1.0.0
*/
public function successor(Node $node) : ?Node
{
if ($node->right !== null) {
@ -98,18 +152,27 @@ class BinarySearchTree
$top = $node->parent;
while ($top !== null && $top->compare($node->data)) {
$node = $top;
$top = $top->parent;
$top = $top->parent;
}
return $top;
}
/**
* Insert a node
*
* @param Node $node Node
*
* @return void
*
* @since 1.0.0
*/
public function insert(Node $node) : void
{
if ($this->root === null) {
$new = new Node($node->key, $node->data);
$new = new Node($node->key, $node->data);
$new->parent = null;
$new->tree = $this;
$new->tree = $this;
$this->root = $new;
@ -122,24 +185,24 @@ class BinarySearchTree
if ($comparison < 0) {
if ($current->left === null) {
$BST = new BinarySearchTree();
$new = new Node($node->key, $node->data);
$BST = new BinarySearchTree();
$new = new Node($node->key, $node->data);
$new->parent = $current;
$new->tree = $BST;
$new->tree = $BST;
$BST->root = $new;
$BST->root = $new;
$current->left = $BST;
} else {
$current = $current->left->root;
}
} elseif ($comparison > 0) {
if ($current->right === null) {
$BST = new BinarySearchTree();
$new = new Node($node->key, $node->data);
$BST = new BinarySearchTree();
$new = new Node($node->key, $node->data);
$new->parent = $current;
$new->tree = $BST;
$new->tree = $BST;
$BST->root = $new;
$BST->root = $new;
$current->right = $BST;
} else {
$current = $current->right->root;
@ -150,6 +213,15 @@ class BinarySearchTree
}
}
/**
* Delete a node
*
* @param Node $node Node
*
* @return void
*
* @since 1.0.0
*/
public function delete(Node &$node) : void
{
if ($node->left === null && $node->right === null) {
@ -201,10 +273,10 @@ class BinarySearchTree
return;
} else {
$temp = $this->successor($node);
$node->key = $temp->key;
$node->key = $temp->key;
$node->data = $temp->data;
$this->delete($temp);
}
}
}
}

View File

@ -15,7 +15,7 @@ declare(strict_types=1);
namespace phpOMS\Stdlib\Tree;
/**
* Priority queue class.
* Tree node class.
*
* @package phpOMS\Stdlib\Tree
* @license OMS License 2.0
@ -24,26 +24,79 @@ namespace phpOMS\Stdlib\Tree;
*/
class Node
{
/**
* Key of the node
*
* @var string
* @since 1.0.0
*/
public string $key = '';
/**
* Data of the node
*
* @var mixed
* @since 1.0.0
*/
public mixed $data = null;
/**
* Sub-tree to the left
*
* @var null|BinarySearchTree
* @since 1.0.0
*/
public ?BinarySearchTree $left = null;
/**
* Sub-tree to the right
*
* @var null|BinarySearchTree
* @since 1.0.0
*/
public ?BinarySearchTree $right = null;
/**
* Parent node
*
* @var null|Node
* @since 1.0.0
*/
public ?self $parent = null;
/**
* Parent tree
*
* @var null|BinarySearchTree
* @since 1.0.0
*/
public ?BinarySearchTree $tree = null;
/**
* Constructor.
*
* @param string $key Node key
* @param mixed $data Node data
*
* @since 1.0.0
*/
public function __construct(string $key, mixed $data = null)
{
$this->key = $key;
$this->data = $data;
}
/**
* Compare node data
*
* @param mixed $data Node data to compare with
*
* @return -1|0|1
*
* @since 1.0.0
*/
public function compare(mixed $data) : int
{
return $this->data <=> $data;
}
}
}