Skip to content

Commit 7c9da98

Browse files
committed
refactoring
1 parent 055096b commit 7c9da98

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+256
-118
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"phpunit/phpunit": "6.5"
1919
},
2020
"require": {
21-
"doganoo/php-util": "0.0.*"
21+
"doganoo/php-util": "0.0.*",
22+
"zumba/json-serializer": "*"
2223
}
2324
}

src/Algorithm/Search/BidirectionalSearch.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@
3838
*/
3939
class BidirectionalSearch {
4040

41-
public function hasPath(Node $start, Node $end) {
41+
/**
42+
* @param Node $start
43+
* @param Node $end
44+
* @return bool
45+
*/
46+
public function hasPath(Node $start, Node $end): bool {
4247
$startList = $this->performSearch($start);
4348
$endList = $this->performSearch($end);
4449

@@ -49,7 +54,7 @@ public function hasPath(Node $start, Node $end) {
4954
$startList->retainAll($endList);
5055
return $startList->length() > 0;
5156
}
52-
57+
return false;
5358
}
5459

5560
/**

src/Algorithm/Search/DepthFirstSearch.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public function search(AbstractGraph $graph) {
5353

5454
/**
5555
* @param Node|null $node
56-
* @return mixed
56+
* @return void
5757
*/
58-
public function searchByNode(?Node $node) {
58+
public function searchByNode(?Node $node): void {
5959
if (null === $node) {
6060
return;
6161
}

src/Algorithm/Sorting/QuickSort.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* SOFTWARE.
2424
*/
2525

26-
namespace doganoo\PHPAlgorithms\Sort;
26+
namespace doganoo\PHPAlgorithms\Algorithm\Sorting;
2727

2828

2929
use doganoo\PHPAlgorithms\Common\Interfaces\ISortable;
@@ -32,7 +32,7 @@
3232
/**
3333
* Class QuickSort
3434
*
35-
* @package doganoo\PHPAlgorithms\Sort
35+
* @package doganoo\PHPAlgorithms\Sorting
3636
*/
3737
class QuickSort implements ISortable {
3838
/**

src/Algorithm/Traversal/InOrder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@
2727

2828

2929
use doganoo\PHPAlgorithms\Common\Abstracts\AbstractTraverse;
30+
use doganoo\PHPAlgorithms\Common\Abstracts\AbstractTree;
3031
use doganoo\PHPAlgorithms\Common\Interfaces\IBinaryNode;
31-
use doganoo\PHPAlgorithms\Common\Interfaces\IBinaryTree;
3232

3333
/**
3434
* Class InOrder
3535
*
3636
* @package doganoo\PHPAlgorithms\Algorithm\Traversal
3737
*/
3838
class InOrder extends AbstractTraverse {
39-
/** @var IBinaryTree|null */
39+
/** @var AbstractTree|null */
4040
private $binarySearchTree = null;
4141

4242
/**
4343
* InOrder constructor.
4444
*
45-
* @param IBinaryTree $tree
45+
* @param AbstractTree $tree
4646
*/
47-
public function __construct(IBinaryTree $tree) {
47+
public function __construct(AbstractTree $tree) {
4848
$this->binarySearchTree = $tree;
4949
}
5050

src/Algorithm/Traversal/PostOrder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,24 @@
2626
namespace doganoo\PHPAlgorithms\Algorithm\Traversal;
2727

2828
use doganoo\PHPAlgorithms\Common\Abstracts\AbstractTraverse;
29+
use doganoo\PHPAlgorithms\Common\Abstracts\AbstractTree;
2930
use doganoo\PHPAlgorithms\Common\Interfaces\IBinaryNode;
30-
use doganoo\PHPAlgorithms\Common\Interfaces\IBinaryTree;
3131

3232
/**
3333
* Class PostOrder
3434
*
3535
* @package doganoo\PHPAlgorithms\Algorithm\Traversal
3636
*/
3737
class PostOrder extends AbstractTraverse {
38-
/** @var IBinaryTree|null */
38+
/** @var AbstractTree|null */
3939
private $tree = null;
4040

4141
/**
4242
* PostOrder constructor.
4343
*
44-
* @param IBinaryTree $tree
44+
* @param AbstractTree $tree
4545
*/
46-
public function __construct(IBinaryTree $tree) {
46+
public function __construct(AbstractTree $tree) {
4747
$this->tree = $tree;
4848
}
4949

src/Algorithm/Traversal/PreOrder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,24 @@
2626
namespace doganoo\PHPAlgorithms\Algorithm\Traversal;
2727

2828
use doganoo\PHPAlgorithms\Common\Abstracts\AbstractTraverse;
29+
use doganoo\PHPAlgorithms\Common\Abstracts\AbstractTree;
2930
use doganoo\PHPAlgorithms\Common\Interfaces\IBinaryNode;
30-
use doganoo\PHPAlgorithms\Common\Interfaces\IBinaryTree;
3131

3232
/**
3333
* Class PreOrder
3434
*
3535
* @package doganoo\PHPAlgorithms\Algorithm\Traversal
3636
*/
3737
class PreOrder extends AbstractTraverse {
38-
/** @var IBinaryTree|null */
38+
/** @var AbstractTree|null */
3939
private $tree = null;
4040

4141
/**
4242
* PreOrder constructor.
4343
*
44-
* @param IBinaryTree $tree
44+
* @param AbstractTree $tree
4545
*/
46-
public function __construct(IBinaryTree $tree) {
46+
public function __construct(AbstractTree $tree) {
4747
$this->tree = $tree;
4848
}
4949

src/Common/Abstracts/AbstractLinkedList.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ public function isEmpty() {
310310
* TODO decide whether using add or append/prepend
311311
*
312312
* @param \doganoo\PHPAlgorithms\Datastructure\Lists\Node $node
313-
* @throws \ReflectionException
314313
*/
315314
public function addNode(Node $node) {
316315
$this->add($node->getKey(), $node->getValue());
@@ -479,21 +478,6 @@ public function replaceValue($key, $value): bool {
479478
return $replaced;
480479
}
481480

482-
/**
483-
* string representation of a LinkedList instance
484-
*
485-
* @return string
486-
*/
487-
public function __toString() {
488-
$head = $this->getHead();
489-
$string = "";
490-
while ($head !== null) {
491-
$string .= $head . "\n";
492-
$head = $head->getNext();
493-
}
494-
return $string;
495-
}
496-
497481
//protected function removeDuplicates() {
498482
// $node = $this->head;
499483
// $previous = $this->head;

src/Common/Interfaces/Comparable.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Interface Comparable
3030
*
3131
* @package doganoo\PHPAlgorithms\common\Interfaces
32+
* @deprecated use IComparable instead (due to name conventions)
3233
*/
3334
interface Comparable {
3435
/**

src/Common/Interfaces/IBinaryNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @package doganoo\PHPAlgorithms\Common\Interfaces
3232
*/
33-
interface IBinaryNode extends INode, Comparable {
33+
interface IBinaryNode extends INode {
3434
/**
3535
* @return mixed
3636
*/

src/Common/Interfaces/IBinaryTree.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @package doganoo\PHPAlgorithms\Common\Interfaces
3232
* @deprecated
3333
*/
34-
interface IBinaryTree {
34+
interface IBinaryTree extends IComparable {
3535
/**
3636
* @return IBinaryNode|null
3737
*/

src/Common/Interfaces/ICache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @package doganoo\PHPAlgorithms\common\Interfaces
3232
*/
33-
interface ICache {
33+
interface ICache extends IComparable {
3434
/**
3535
* adds a new key value pair
3636
*

src/Common/Interfaces/IComparable.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
namespace doganoo\PHPAlgorithms\Common\Interfaces;
27+
28+
/**
29+
* Interface IComparable
30+
*
31+
* @package doganoo\PHPAlgorithms\Common\Interfaces
32+
*/
33+
interface IComparable extends Comparable {
34+
35+
}

src/Common/Interfaces/INode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @package doganoo\PHPAlgorithms\common\Interfaces
3232
*/
33-
interface INode {
33+
interface INode extends IComparable {
3434
/**
3535
* @return mixed
3636
*/

src/Common/Interfaces/ISet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @package doganoo\PHPAlgorithms\common\Interfaces
3232
*/
33-
interface ISet {
33+
interface ISet extends IComparable {
3434
/**
3535
* Adds the specified element to this set if it is not already present (optional operation).
3636
*

src/Common/Interfaces/IUnaryNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @package doganoo\PHPAlgorithms\common\Interfaces
3232
*/
33-
interface IUnaryNode extends Comparable {
33+
interface IUnaryNode extends IComparable {
3434
/**
3535
* @return mixed
3636
*/

src/Common/Interfaces/IVector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @package doganoo\PHPAlgorithms\Common\Interfaces
3232
*/
33-
interface IVector {
33+
interface IVector extends IComparable {
3434
/**
3535
* sets a value in the vector
3636
*

src/Common/Util/Comparator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
namespace doganoo\PHPAlgorithms\Common\Util;
2727

2828

29-
use doganoo\PHPAlgorithms\Common\Interfaces\Comparable;
29+
use doganoo\PHPAlgorithms\Common\Interfaces\IComparable;
3030

3131
/**
3232
* Class Comparator
@@ -47,7 +47,7 @@ private function __construct() {
4747
* @return bool
4848
*/
4949
public static function equals($that, $other): bool {
50-
if ($that instanceof Comparable) {
50+
if ($that instanceof IComparable) {
5151
return $that->compareTo($other) == 0;
5252
}
5353
if (\is_object($that)) {
@@ -71,7 +71,7 @@ public static function equals($that, $other): bool {
7171
* @return bool
7272
*/
7373
public static function lessThan($that, $other): bool {
74-
if ($that instanceof Comparable) {
74+
if ($that instanceof IComparable) {
7575
return $that->compareTo($other) == -1;
7676
}
7777
if (\is_object($that)) {
@@ -95,7 +95,7 @@ public static function lessThan($that, $other): bool {
9595
* @return bool
9696
*/
9797
public static function lessThanEqual($that, $other): bool {
98-
if ($that instanceof Comparable) {
98+
if ($that instanceof IComparable) {
9999
return $that->compareTo($other) == -1;
100100
}
101101
if (\is_object($that)) {
@@ -119,7 +119,7 @@ public static function lessThanEqual($that, $other): bool {
119119
* @return bool
120120
*/
121121
public static function greaterThan($that, $other): bool {
122-
if ($that instanceof Comparable) {
122+
if ($that instanceof IComparable) {
123123
return $that->compareTo($other) == 1;
124124
}
125125
if (\is_object($that)) {
@@ -143,7 +143,7 @@ public static function greaterThan($that, $other): bool {
143143
* @return bool
144144
*/
145145
public static function greaterThanEqual($that, $other): bool {
146-
if ($that instanceof Comparable) {
146+
if ($that instanceof IComparable) {
147147
return $that->compareTo($other) == 1;
148148
}
149149
if (\is_object($that)) {

src/Common/Util/MapUtil.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ public static function normalizeValue($value): string {
184184
$value = $value ? "true" : "false";
185185
} else if (\is_resource($value) || $value === null) {
186186
//TODO resource/null
187-
$value = $value;
188187
} else if (\is_int($value)) {
189188
return (string)$value;
190189
} else {

src/Datastructure/Cache/LRUCache.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public function __construct($capacity = 128) {
5959
* @param $key
6060
* @param $value
6161
* @return bool
62-
* @throws \ReflectionException
6362
* @throws \doganoo\PHPAlgorithms\common\Exception\InvalidKeyTypeException
6463
* @throws \doganoo\PHPAlgorithms\common\Exception\UnsupportedKeyTypeException
6564
*/
@@ -194,4 +193,17 @@ public function delete($key): bool {
194193
}
195194
return false;
196195
}
196+
197+
/**
198+
* @param $object
199+
* @return int
200+
*/
201+
public function compareTo($object): int {
202+
if ($object instanceof LRUCache) {
203+
if (Comparator::equals($this->hashMap, $object->hashMap)) return 0;
204+
if (Comparator::lessThan($this->hashMap, $object->hashMap)) return 1;
205+
if (Comparator::greaterThan($this->hashMap, $object->hashMap)) return -1;
206+
}
207+
return -1;
208+
}
197209
}

src/Datastructure/Cache/Node.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@
2525

2626
namespace doganoo\PHPAlgorithms\Datastructure\Cache;
2727

28-
use doganoo\PHPAlgorithms\Common\Interfaces\Comparable;
2928
use doganoo\PHPAlgorithms\Common\Interfaces\INode;
3029

3130
/**
3231
* Class Node
3332
*
3433
* @package doganoo\PHPAlgorithms\Datastructure\Cache
3534
*/
36-
class Node implements INode, Comparable {
35+
class Node implements INode {
3736
private $key;
3837
private $value;
3938
private $previous;

0 commit comments

Comments
 (0)