mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-08 05:18:40 +00:00
Draft
All these files need further edits + other optimization files + moving of files
This commit is contained in:
parent
b49bda0387
commit
78bc28b045
62
Math/Fibunacci.php
Normal file
62
Math/Fibunacci.php
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Orange Management
|
||||||
|
*
|
||||||
|
* PHP Version 7.0
|
||||||
|
*
|
||||||
|
* @category TBD
|
||||||
|
* @package TBD
|
||||||
|
* @author OMS Development Team <dev@oms.com>
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
* @copyright 2013 Dennis Eichhorn
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @version 1.0.0
|
||||||
|
* @link http://orange-management.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace phpOMS\Math;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Well known functions class.
|
||||||
|
*
|
||||||
|
* @category Framework
|
||||||
|
* @package phpOMS\DataStorage\Database
|
||||||
|
* @author OMS Development Team <dev@oms.com>
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @link http://orange-management.com
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
class Fibunacci
|
||||||
|
{
|
||||||
|
public static function isFibunacci(int $n)
|
||||||
|
{
|
||||||
|
return Functions::isSquare(5*$n**2+4) || Functions::isSquare(5*$n**2-4);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fibunacci(int $n, int $start = 1) : int
|
||||||
|
{
|
||||||
|
if($n < 2) {
|
||||||
|
return 0;
|
||||||
|
} elseif ($n < 4) {
|
||||||
|
return $start;
|
||||||
|
}
|
||||||
|
|
||||||
|
$old_1 = 0;
|
||||||
|
$old_2 = $start;
|
||||||
|
$fib = 0;
|
||||||
|
|
||||||
|
for($i = 4; $i < $n; $i++) {
|
||||||
|
$fib = $old_1 + $old_2
|
||||||
|
$old_1 = $old_2;
|
||||||
|
$old_2 = $fib;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $fib;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function binet(int $n) : int
|
||||||
|
{
|
||||||
|
return (int) (((1+sqrt(5))**$n - (1-sqrt(5))**$n)/(2**$n * sqrt(5)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -115,4 +115,105 @@ class Functions
|
||||||
|
|
||||||
return $fact / $fact2;
|
return $fact / $fact2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function ackermann(int $m, int $n)
|
||||||
|
{
|
||||||
|
if($m === 0) {
|
||||||
|
return $n+1;
|
||||||
|
} elseif($n === 0) {
|
||||||
|
return self::ackermann($m-1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ackermann($m-1, ackermann($m, $n-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isSquare(int $x) {
|
||||||
|
$goodMask; // 0xC840C04048404040 computed below
|
||||||
|
|
||||||
|
for ($i = 0; $i < 64; ++$i) {
|
||||||
|
$goodMask |= PHP_INT_MIN >>> ($i*$i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This tests if the 6 least significant bits are right.
|
||||||
|
// Moving the to be tested bit to the highest position saves us masking.
|
||||||
|
if ($goodMask << $x >= 0) return false;
|
||||||
|
|
||||||
|
$numberOfTrailingZeros = self::countTrailingZeros($x);
|
||||||
|
// Each square ends with an even number of zeros.
|
||||||
|
if (($numberOfTrailingZeros & 1) !== 0) return false;
|
||||||
|
|
||||||
|
$x >>= $numberOfTrailingZeros;
|
||||||
|
// Now x is either 0 or odd.
|
||||||
|
// In binary each odd square ends with 001.
|
||||||
|
// Postpone the sign test until now; handle zero in the branch.
|
||||||
|
if (($x&7) != 1 | $x <= 0) return $x === 0;
|
||||||
|
// Do it in the classical way.
|
||||||
|
// The correctness is not trivial as the conversion from long to double is lossy!
|
||||||
|
$tst = (int) sqrt($x);
|
||||||
|
|
||||||
|
return $tst * $tst == $x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function countTrailingZeros(int $n) : int
|
||||||
|
{
|
||||||
|
$count = 0;
|
||||||
|
while ($n !== 0) {
|
||||||
|
if ($n & 1 == 1) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
$count++;
|
||||||
|
$n = $n >> 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function greatestCommonDivisor(int $n, int $m) : int
|
||||||
|
{
|
||||||
|
while(true) {
|
||||||
|
if($n === $m) {
|
||||||
|
return $m;
|
||||||
|
} if($n > $m) {
|
||||||
|
$n -= $m;
|
||||||
|
} else {
|
||||||
|
$m -= $n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function invMod($a,$n){
|
||||||
|
if ($n < 0) {
|
||||||
|
$n = -$n;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($a < 0) {
|
||||||
|
$a = $n - (-$a % $n);
|
||||||
|
}
|
||||||
|
|
||||||
|
$t = 0;
|
||||||
|
$nt = 1;
|
||||||
|
$r = $n;
|
||||||
|
$nr = $a % $n;
|
||||||
|
|
||||||
|
while ($nr != 0) {
|
||||||
|
$quot = intval($r/$nr);
|
||||||
|
$tmp = $nt;
|
||||||
|
$nt = $t - $quot*$nt;
|
||||||
|
$t = $tmp;
|
||||||
|
$tmp = $nr;
|
||||||
|
$nr = $r - $quot*$nr;
|
||||||
|
$r = $tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($r > 1) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($t < 0) {
|
||||||
|
$t += $n;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $t;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
28
Math/Number/Numbers.php
Normal file
28
Math/Number/Numbers.php
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class Number
|
||||||
|
{
|
||||||
|
public static function perfect(int $n) : bool
|
||||||
|
{
|
||||||
|
$sum = 0;
|
||||||
|
|
||||||
|
for($i = 1; $i < $n; $i++) {
|
||||||
|
if($n % $i == 0) {
|
||||||
|
$sum += $i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $sum == $n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function selfdescribing(int $n) : bool
|
||||||
|
{
|
||||||
|
foreach (str_split($n) as $place => $value) {
|
||||||
|
if (substr_count($number, $place) != $value) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
0
Math/Optimization/EdgeInterface.php
Normal file
0
Math/Optimization/EdgeInterface.php
Normal file
61
Math/Optimization/GaussianElimination.php
Normal file
61
Math/Optimization/GaussianElimination.php
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class GaussianElimination
|
||||||
|
{
|
||||||
|
private function swapRows(&$a, &$b, $r1, $r2)
|
||||||
|
{
|
||||||
|
if ($r1 == $r2) return;
|
||||||
|
|
||||||
|
$tmp = $a[$r1];
|
||||||
|
$a[$r1] = $a[$r2];
|
||||||
|
$a[$r2] = $tmp;
|
||||||
|
|
||||||
|
$tmp = $b[$r1];
|
||||||
|
$b[$r1] = $b[$r2];
|
||||||
|
$b[$r2] = $tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function solve(array $A, array $b, int $limit) : array
|
||||||
|
{
|
||||||
|
for ($col = 0; $col < $limit; $col++) {
|
||||||
|
$j = $col;
|
||||||
|
$max = $A[$j][$j];
|
||||||
|
|
||||||
|
for ($i = $col + 1; $i < $limit; $i++) {
|
||||||
|
$tmp = abs($A[$i][$col]);
|
||||||
|
|
||||||
|
if ($tmp > $max) {
|
||||||
|
$j = $i;
|
||||||
|
$max = $tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->swapRows($A, $b, $col, $j);
|
||||||
|
|
||||||
|
for ($i = $col + 1; $i < $limit; $i++) {
|
||||||
|
$tmp = $A[$i][$col] / $A[$col][$col];
|
||||||
|
|
||||||
|
for ($j = $col + 1; $j < $limit; $j++) {
|
||||||
|
$A[$i][$j] -= $tmp * $A[$col][$j];
|
||||||
|
}
|
||||||
|
|
||||||
|
$A[$i][$col] = 0;
|
||||||
|
$b[$i] -= $tmp * $b[$col];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$x = [];
|
||||||
|
|
||||||
|
for ($col = $limit - 1; $col >= 0; $col--) {
|
||||||
|
$tmp = $b[$col];
|
||||||
|
|
||||||
|
for ($j = $limit - 1; $j > $col; $j--) {
|
||||||
|
$tmp -= $x[$j] * $A[$col][$j];
|
||||||
|
}
|
||||||
|
|
||||||
|
$x[$col] = $tmp / $A[$col][$col];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $x;
|
||||||
|
}
|
||||||
|
}
|
||||||
68
Math/Optimization/Graph/Dijkstra.php
Normal file
68
Math/Optimization/Graph/Dijkstra.php
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace phpOMS\Math\Optimization\Graph;
|
||||||
|
|
||||||
|
class Dijkstra {
|
||||||
|
public static function dijkstra(Graph $graph, $source, $target) {
|
||||||
|
$vertices = array();
|
||||||
|
$neighbours = array();
|
||||||
|
|
||||||
|
foreach ($graph_array as $edge) {
|
||||||
|
array_push($vertices, $edge[0], $edge[1]);
|
||||||
|
$neighbours[$edge[0]][] = array("end" => $edge[1], "cost" => $edge[2]);
|
||||||
|
$neighbours[$edge[1]][] = array("end" => $edge[0], "cost" => $edge[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$vertices = array_unique($vertices);
|
||||||
|
|
||||||
|
foreach ($vertices as $vertex) {
|
||||||
|
$dist[$vertex] = INF;
|
||||||
|
$previous[$vertex] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
$dist[$source] = 0;
|
||||||
|
$Q = $vertices;
|
||||||
|
|
||||||
|
while (count($Q) > 0) {
|
||||||
|
|
||||||
|
// TODO - Find faster way to get minimum
|
||||||
|
$min = INF;
|
||||||
|
|
||||||
|
foreach ($Q as $vertex){
|
||||||
|
if ($dist[$vertex] < $min) {
|
||||||
|
$min = $dist[$vertex];
|
||||||
|
$u = $vertex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$Q = array_diff($Q, array($u));
|
||||||
|
|
||||||
|
if ($dist[$u] == INF || $u == $target) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($neighbours[$u])) {
|
||||||
|
foreach ($neighbours[$u] as $arr) {
|
||||||
|
$alt = $dist[$u] + $arr["cost"];
|
||||||
|
|
||||||
|
if ($alt < $dist[$arr["end"]]) {
|
||||||
|
$dist[$arr["end"]] = $alt;
|
||||||
|
$previous[$arr["end"]] = $u;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$path = array();
|
||||||
|
$u = $target;
|
||||||
|
|
||||||
|
while (isset($previous[$u])) {
|
||||||
|
array_unshift($path, $u);
|
||||||
|
$u = $previous[$u];
|
||||||
|
}
|
||||||
|
|
||||||
|
array_unshift($path, $u);
|
||||||
|
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
}
|
||||||
0
Math/Optimization/Graph/FloydWarshall.php
Normal file
0
Math/Optimization/Graph/FloydWarshall.php
Normal file
69
Math/Optimization/GraphAbstract.php
Normal file
69
Math/Optimization/GraphAbstract.php
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Orange Management
|
||||||
|
*
|
||||||
|
* PHP Version 7.0
|
||||||
|
*
|
||||||
|
* @category TBD
|
||||||
|
* @package TBD
|
||||||
|
* @author OMS Development Team <dev@oms.com>
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
* @copyright 2013 Dennis Eichhorn
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @version 1.0.0
|
||||||
|
* @link http://orange-management.com
|
||||||
|
*/
|
||||||
|
namespace phpOMS\Math\Optimization;
|
||||||
|
|
||||||
|
class GraphAbstract {
|
||||||
|
private $vertices = [];
|
||||||
|
|
||||||
|
private $edges = null;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->edges = new MultiMap(KeyType::STRICT, OrderType::LOOSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addVertice(VerticeInterface $Vertice) : bool
|
||||||
|
{
|
||||||
|
if(!isset($this->vertices[$Vertice->getId()])) {
|
||||||
|
$this->vertices[$Vertice->getId()] = $Vertice;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeVertice($id) : bool
|
||||||
|
{
|
||||||
|
if(isset($this->vertices[$id])) {
|
||||||
|
unset($this->vertices[$id]);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getVertice($id) : VerticeInterface
|
||||||
|
{
|
||||||
|
return $this->vertices[$id] ?? new NullVertice();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getEdge($a, $b) : EdgeInterface
|
||||||
|
{
|
||||||
|
return $this->edges->get($a, $b) ?? new NullEdge();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function countVertices() : int
|
||||||
|
{
|
||||||
|
return count($this->vertices);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function countEdges() : int
|
||||||
|
{
|
||||||
|
return count($this->edges);
|
||||||
|
}
|
||||||
|
}
|
||||||
0
Math/Optimization/NullEdge.php
Normal file
0
Math/Optimization/NullEdge.php
Normal file
0
Math/Optimization/NullVertice.php
Normal file
0
Math/Optimization/NullVertice.php
Normal file
0
Math/Optimization/VerticeInterface.php
Normal file
0
Math/Optimization/VerticeInterface.php
Normal file
24
Math/Permutation.php
Normal file
24
Math/Permutation.php
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class Permutation
|
||||||
|
{
|
||||||
|
// usage: permut(['a', 'b', 'c']);
|
||||||
|
public static function permut(array $toPermute, array $result = []) : array
|
||||||
|
{
|
||||||
|
$permutations = [];
|
||||||
|
|
||||||
|
if(empty($toPermute)){
|
||||||
|
$permutations[] = implode("", $result);
|
||||||
|
}else{
|
||||||
|
foreach($toPermute as $key => $val){
|
||||||
|
$newArr = $toPermute;
|
||||||
|
$newres = $result;
|
||||||
|
$newres[] = $val;
|
||||||
|
unset($newArr[$key]);
|
||||||
|
$permutations += permut($newArr, $newres);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $permutations;
|
||||||
|
}
|
||||||
|
}
|
||||||
88
Math/Prime.php
Normal file
88
Math/Prime.php
Normal file
|
|
@ -0,0 +1,88 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Orange Management
|
||||||
|
*
|
||||||
|
* PHP Version 7.0
|
||||||
|
*
|
||||||
|
* @category TBD
|
||||||
|
* @package TBD
|
||||||
|
* @author OMS Development Team <dev@oms.com>
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
* @copyright 2013 Dennis Eichhorn
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @version 1.0.0
|
||||||
|
* @link http://orange-management.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace phpOMS\Math;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Well known functions class.
|
||||||
|
*
|
||||||
|
* @category Framework
|
||||||
|
* @package phpOMS\DataStorage\Database
|
||||||
|
* @author OMS Development Team <dev@oms.com>
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @link http://orange-management.com
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
class Prime
|
||||||
|
{
|
||||||
|
public static function isMersenne(int $n)
|
||||||
|
{
|
||||||
|
$mersenne = log($n+1)/2;
|
||||||
|
|
||||||
|
return $mersenne - (int) $mersenne < 0.00001
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function mersenne(int $p)
|
||||||
|
{
|
||||||
|
return power(2, $p) - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function rabinTest(int $n, int $k) : bool
|
||||||
|
{
|
||||||
|
if ($n == 2) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($n < 2 || $n % 2 == 0){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$d = $n - 1;
|
||||||
|
$s = 0;
|
||||||
|
|
||||||
|
while ($d % 2 == 0) {
|
||||||
|
$d /= 2;
|
||||||
|
$s++;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ($i = 0; $i < $k; $i++) {
|
||||||
|
$a = mt_rand(2, $n-1);
|
||||||
|
|
||||||
|
$x = bcpowmod($a, $d, $n);
|
||||||
|
|
||||||
|
if ($x == 1 || $x == $n-1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ($j = 1; $j < $s; $j++) {
|
||||||
|
$x = bcmod(bcmul($x, $x), $n);
|
||||||
|
|
||||||
|
if ($x == 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($x == $n-1) {
|
||||||
|
continue 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
1
Security/Encryption/Ceasar.php
Normal file
1
Security/Encryption/Ceasar.php
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
<?php
|
||||||
0
Security/Encryption/EncryptionInterface.php
Normal file
0
Security/Encryption/EncryptionInterface.php
Normal file
0
Utils/Compression/CompressionInterface.php
Normal file
0
Utils/Compression/CompressionInterface.php
Normal file
70
Utils/Compression/LZW.php
Normal file
70
Utils/Compression/LZW.php
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class LZW implements CompressionInterface
|
||||||
|
{
|
||||||
|
public static function compress($unc) {
|
||||||
|
$i;$c;$wc;
|
||||||
|
$w = "";
|
||||||
|
$dictionary = [];
|
||||||
|
$result = [];
|
||||||
|
$dictSize = 256;
|
||||||
|
|
||||||
|
for ($i = 0; $i < 256; $i += 1) {
|
||||||
|
$dictionary[chr($i)] = $i;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ($i = 0; $i < strlen($unc); $i++) {
|
||||||
|
$c = $unc[$i];
|
||||||
|
$wc = $w.$c;
|
||||||
|
|
||||||
|
if (array_key_exists($w. $c, $dictionary)) {
|
||||||
|
$w = $w . $c;
|
||||||
|
} else {
|
||||||
|
array_push($result, $dictionary[$w]);
|
||||||
|
$dictionary[$wc] = $dictSize++;
|
||||||
|
$w = (string) $c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($w !== "") {
|
||||||
|
array_push($result,$dictionary[$w]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode(",",$result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function decompress($com) {
|
||||||
|
$com = explode(",",$com);
|
||||||
|
$i;$w;$k;$result;
|
||||||
|
$dictionary = [];
|
||||||
|
$entry = "";
|
||||||
|
$dictSize = 256;
|
||||||
|
|
||||||
|
for ($i = 0; $i < 256; $i++) {
|
||||||
|
$dictionary[$i] = chr($i);
|
||||||
|
}
|
||||||
|
|
||||||
|
$w = chr($com[0]);
|
||||||
|
$result = $w;
|
||||||
|
|
||||||
|
for ($i = 1; $i < count($com);$i++) {
|
||||||
|
$k = $com[$i];
|
||||||
|
|
||||||
|
if ($dictionary[$k]) {
|
||||||
|
$entry = $dictionary[$k];
|
||||||
|
} else {
|
||||||
|
if ($k === $dictSize) {
|
||||||
|
$entry = $w.$w[0];
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$result .= $entry;
|
||||||
|
$dictionary[$dictSize++] = $w + $entry[0];
|
||||||
|
$w = $entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
0
Utils/Encoding/EncodingInterface.php
Normal file
0
Utils/Encoding/EncodingInterface.php
Normal file
19
Utils/Encoding/Gray.php
Normal file
19
Utils/Encoding/Gray.php
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class Gray {
|
||||||
|
public static function encode(int $source) : int
|
||||||
|
{
|
||||||
|
return $source ^ ($source >> 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function decode(int $gray) : int
|
||||||
|
{
|
||||||
|
$source = $gray;
|
||||||
|
|
||||||
|
while($gray >>= 1) {
|
||||||
|
$source ^= $gray;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $source;
|
||||||
|
}
|
||||||
|
}
|
||||||
6
Utils/Encoding/Huffman.php
Normal file
6
Utils/Encoding/Huffman.php
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
final class Huffman
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -45,14 +45,22 @@ class MultiMap implements \Countable
|
||||||
*/
|
*/
|
||||||
private $keys = [];
|
private $keys = [];
|
||||||
|
|
||||||
|
private $uids = [];
|
||||||
|
|
||||||
|
private $keyType = KeyType::LOOSE;
|
||||||
|
|
||||||
|
private $orderType = OrderType::LOOSE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct(int $key = KeyType::LOOSE, int $order = OrderType::LOOSE)
|
||||||
{
|
{
|
||||||
|
$this->keyType = $key;
|
||||||
|
$this->orderType = $order;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
30
Utils/Random/ArrayRandomize.php
Normal file
30
Utils/Random/ArrayRandomize.php
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class ArrayRandomize
|
||||||
|
{
|
||||||
|
public static function yates($arr) : array
|
||||||
|
{
|
||||||
|
$shuffled = [];
|
||||||
|
|
||||||
|
while($arr){
|
||||||
|
$rnd = array_rand($arr);
|
||||||
|
$shuffled[] = $arr[$rnd];
|
||||||
|
array_splice($arr, $rnd, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $shuffled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function knuth($arr) : array
|
||||||
|
{
|
||||||
|
$shuffled = [];
|
||||||
|
|
||||||
|
for($i = count($arr)-1; $i > 0; $i--){
|
||||||
|
$rnd = mt_rand(0, $i);
|
||||||
|
$shuffled[$i] = $arr[$rnd];
|
||||||
|
$shuffled[$rand] = $arr[$i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $shuffled;
|
||||||
|
}
|
||||||
|
}
|
||||||
17
Utils/Random/LinearCongruentialGenerator.php
Normal file
17
Utils/Random/LinearCongruentialGenerator.php
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class LinearCongruentialGenerator
|
||||||
|
{
|
||||||
|
public static function bsd(int $seed)
|
||||||
|
{
|
||||||
|
return function() use(&$seed) {
|
||||||
|
return $seed = (1103515245 * $seed + 12345) % (1 << 31);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function msvcrt(int $seed) {
|
||||||
|
return function() use (&$seed) {
|
||||||
|
return ($seed = (214013 * $seed + 2531011) % (1 << 31)) >> 16;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -71,4 +71,20 @@ abstract class CreditCard extends ValidatorAbstract
|
||||||
// If the total mod 10 equals 0, the value is valid
|
// If the total mod 10 equals 0, the value is valid
|
||||||
return ($total % 10 == 0) ? true : false;
|
return ($total % 10 == 0) ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function luhnTest(string $num) {
|
||||||
|
$len = strlen($num);
|
||||||
|
|
||||||
|
for ($i = $len-1; $i >= 0; $i--) {
|
||||||
|
$ord = ord($num[$i]);
|
||||||
|
|
||||||
|
if (($len - 1) & $i) {
|
||||||
|
$sum += $ord;
|
||||||
|
} else {
|
||||||
|
$sum += $ord / 5 + (2 * $ord) % 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $sum % 10 == 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,7 @@ abstract class IBAN extends ValidatorAbstract
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
// todo: this is bad see wiki for better checks...
|
||||||
public static function isValid($value)
|
public static function isValid($value)
|
||||||
{
|
{
|
||||||
$value = strtolower(str_replace(' ', '', $value));
|
$value = strtolower(str_replace(' ', '', $value));
|
||||||
|
|
|
||||||
66
Validation/BitcoinValidator.php
Normal file
66
Validation/BitcoinValidator.php
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Orange Management
|
||||||
|
*
|
||||||
|
* PHP Version 7.0
|
||||||
|
*
|
||||||
|
* @category TBD
|
||||||
|
* @package TBD
|
||||||
|
* @author OMS Development Team <dev@oms.com>
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
* @copyright 2013 Dennis Eichhorn
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @version 1.0.0
|
||||||
|
* @link http://orange-management.com
|
||||||
|
*/
|
||||||
|
namespace phpOMS\Validation;
|
||||||
|
|
||||||
|
class BitcoinValidator
|
||||||
|
{
|
||||||
|
public static function validate(string $addr) : bool
|
||||||
|
{
|
||||||
|
$decoded = decodeBase58($address);
|
||||||
|
|
||||||
|
$d1 = hash("sha256", substr($decoded,0,21), true);
|
||||||
|
$d2 = hash("sha256", $d1, true);
|
||||||
|
|
||||||
|
if(substr_compare($decoded, $d2, 21, 4)){
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function decodeBase58(string $input) : string
|
||||||
|
{
|
||||||
|
$alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
||||||
|
|
||||||
|
$out = array_fill(0, 25, 0);
|
||||||
|
$len = strlen($input);
|
||||||
|
|
||||||
|
for($i = 0; $i < $len; $i++){
|
||||||
|
if(($p=strpos($alphabet, $input[$i]))===false){
|
||||||
|
throw new \Exception("invalid character found");
|
||||||
|
}
|
||||||
|
|
||||||
|
$c = $p;
|
||||||
|
for ($j = 25; $j--; ) {
|
||||||
|
$c += (int)(58 * $out[$j]);
|
||||||
|
$out[$j] = (int)($c % 256);
|
||||||
|
$c /= 256;
|
||||||
|
$c = (int)$c;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($c !== 0){
|
||||||
|
throw new \Exception("address too long");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = "";
|
||||||
|
foreach($out as $val){
|
||||||
|
$result .= chr($val);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user