Skip to content

Commit d782d84

Browse files
committed
restructuring folders
1 parent 7f2df34 commit d782d84

36 files changed

+212
-62
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/common/NodeKey.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar, <[email protected]>
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;
27+
28+
29+
use doganoo\PHPAlgorithms\Common\Util\MapUtil;
30+
31+
class NodeKey {
32+
/**
33+
* @var int
34+
*/
35+
private $key = 0;
36+
37+
/**
38+
* NodeKey constructor.
39+
*
40+
* @param $value
41+
* @throws Exception\InvalidKeyTypeException
42+
* @throws Exception\UnsupportedKeyTypeException
43+
*/
44+
public function __construct($value) {
45+
$key = MapUtil::normalizeKey($value);
46+
$this->key = $key;
47+
}
48+
49+
public function __toString() {
50+
$string = (string)$this->key;
51+
return $string;
52+
}
53+
54+
}

src/common/NodeValue.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar, <[email protected]>
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;
27+
28+
29+
use doganoo\PHPAlgorithms\Common\Util\MapUtil;
30+
31+
class NodeValue {
32+
private $value = null;
33+
34+
/**
35+
* NodeValue constructor.
36+
*
37+
* @param $value
38+
* @throws Exception\InvalidKeyTypeException
39+
* @throws Exception\UnsupportedKeyTypeException
40+
*/
41+
public function __construct($value) {
42+
$this->value = MapUtil::normalizeValue($value);
43+
}
44+
45+
public function equals(NodeValue $value): bool {
46+
return $this->value === $value->get();
47+
}
48+
49+
public function get(): string {
50+
return $this->value;
51+
}
52+
}

src/Exception/IndexOutOfBoundsException.php renamed to src/common/exception/IndexOutOfBoundsException.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
* SOFTWARE.
2424
*/
2525

26-
namespace doganoo\PHPAlgorithms\Exception;
26+
namespace doganoo\PHPAlgorithms\Common\Exception;
2727
/**
2828
* Class IndexOutOfBoundsException
2929
*
30-
* @package doganoo\PHPAlgorithms\Exception
30+
* @package doganoo\PHPAlgorithms\common\Exception
3131
*/
3232
class IndexOutOfBoundsException extends \Exception {
3333

src/Exception/InvalidKeyTypeException.php renamed to src/common/exception/InvalidKeyTypeException.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
* SOFTWARE.
2424
*/
2525

26-
namespace doganoo\PHPAlgorithms\Exception;
26+
namespace doganoo\PHPAlgorithms\Common\Exception;
2727

2828
/**
2929
* Class InvalidKeyTypeException
3030
*
31-
* @package doganoo\PHPAlgorithms\Exception
31+
* @package doganoo\PHPAlgorithms\common\Exception
3232
*/
3333
class InvalidKeyTypeException extends \Exception {
3434

src/Exception/UnsupportedKeyTypeException.php renamed to src/common/exception/UnsupportedKeyTypeException.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
* SOFTWARE.
2424
*/
2525

26-
namespace doganoo\PHPAlgorithms\Exception;
26+
namespace doganoo\PHPAlgorithms\common\Exception;
2727

2828
/**
2929
* Class UnsupportedKeyTypeException
3030
*
31-
* @package doganoo\PHPAlgorithms\Exception
31+
* @package doganoo\PHPAlgorithms\common\Exception
3232
*/
3333
class UnsupportedKeyTypeException extends \Exception {
3434

src/Util/Logger.php renamed to src/common/util/Logger.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* SOFTWARE.
2424
*/
2525

26-
namespace doganoo\PHPAlgorithms\Util;
26+
namespace doganoo\PHPAlgorithms\Common\Util;
2727

2828
/**
2929
* Class Logger

src/Util/MapUtil.php renamed to src/common/util/MapUtil.php

+47-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
* SOFTWARE.
2424
*/
2525

26-
namespace doganoo\PHPAlgorithms\Util;
26+
namespace doganoo\PHPAlgorithms\Common\Util;
2727

28-
use doganoo\PHPAlgorithms\Exception\InvalidKeyTypeException;
29-
use doganoo\PHPAlgorithms\Exception\UnsupportedKeyTypeException;
28+
use doganoo\PHPAlgorithms\Common\Exception\InvalidKeyTypeException;
29+
use doganoo\PHPAlgorithms\Common\Exception\UnsupportedKeyTypeException;
3030

3131
/**
3232
* Class Util
@@ -148,4 +148,48 @@ public static function doubleToKey(double $double): int {
148148
public static function booleanToKey(bool $bool): int {
149149
return $bool ? 1 : 0;
150150
}
151+
152+
/**
153+
* normalizes a given key to a int. This method converts currently the following
154+
* types to a int:
155+
*
156+
* <ul>objects</ul>
157+
* <ul>arrays</ul>
158+
* <ul>doubles</ul>
159+
* <ul>floats</ul>
160+
* <ul>boolean</ul>
161+
* <ul>resource|null</ul>
162+
* <ul>int</ul>
163+
*
164+
* @param $value
165+
* @return string
166+
* @throws InvalidKeyTypeException
167+
* @throws UnsupportedKeyTypeException
168+
*/
169+
public static function normalizeValue($value): string {
170+
/* ensuring that the key is an integer.
171+
* Therefore, some helper methods convert the key if
172+
* necessary.
173+
*/
174+
if (\is_object($value)) {
175+
$value = \serialize($value);
176+
} else if (\is_array($value)) {
177+
$value = \json_encode($value);
178+
} else if (\is_double($value)) {
179+
$value = (string)$value;
180+
} else if (\is_float($value)) {
181+
$value = (string)$value;
182+
} else if (\is_bool($value)) {
183+
$value = $value ? "true" : "false";
184+
} else if (\is_resource($value) || $value === null) {
185+
//TODO resource/null
186+
$value = $value;
187+
} else if (\is_int($value)) {
188+
return (string)$value;
189+
} else {
190+
throw new UnsupportedKeyTypeException();
191+
}
192+
return $value;
193+
}
194+
151195
}

src/Maps/HashMap.php renamed to src/datastructure/Maps/HashMap.php

+14-13
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
* SOFTWARE.
2424
*/
2525

26-
namespace doganoo\PHPAlgorithms\Maps;
26+
namespace doganoo\PHPAlgorithms\Datastructure\Maps;
2727

28-
use doganoo\PHPAlgorithms\Lists\LinkedLists\SinglyLinkedList;
29-
use doganoo\PHPAlgorithms\Util\MapUtil;
28+
use doganoo\PHPAlgorithms\Common\Util\MapUtil;
29+
use doganoo\PHPAlgorithms\Datastructure\Lists\LinkedLists\SinglyLinkedList;
30+
use doganoo\PHPAlgorithms\Datastructure\lists\Node;
3031

3132
/**
3233
* HashMap class - implementation of a map using hashes in order to avoid collisions
@@ -73,8 +74,8 @@ private function initializeBucket() {
7374
*
7475
* @param Node $node
7576
* @return bool
76-
* @throws \doganoo\PHPAlgorithms\Exception\InvalidKeyTypeException
77-
* @throws \doganoo\PHPAlgorithms\Exception\UnsupportedKeyTypeException
77+
* @throws \doganoo\PHPAlgorithms\common\Exception\InvalidKeyTypeException
78+
* @throws \doganoo\PHPAlgorithms\common\Exception\UnsupportedKeyTypeException
7879
*/
7980
public function addNode(Node $node): bool {
8081
$added = $this->add($node->getKey(), $node->getValue());
@@ -88,8 +89,8 @@ public function addNode(Node $node): bool {
8889
* @param $key
8990
* @param $value
9091
* @return bool
91-
* @throws \doganoo\PHPAlgorithms\Exception\InvalidKeyTypeException
92-
* @throws \doganoo\PHPAlgorithms\Exception\UnsupportedKeyTypeException
92+
* @throws \doganoo\PHPAlgorithms\common\Exception\InvalidKeyTypeException
93+
* @throws \doganoo\PHPAlgorithms\common\Exception\UnsupportedKeyTypeException
9394
*/
9495
public function add($key, $value): bool {
9596
$arrayIndex = $this->getBucketIndex($key);
@@ -125,8 +126,8 @@ public function add($key, $value): bool {
125126
*
126127
* @param $key
127128
* @return int
128-
* @throws \doganoo\PHPAlgorithms\Exception\InvalidKeyTypeException
129-
* @throws \doganoo\PHPAlgorithms\Exception\UnsupportedKeyTypeException
129+
* @throws \doganoo\PHPAlgorithms\common\Exception\InvalidKeyTypeException
130+
* @throws \doganoo\PHPAlgorithms\common\Exception\UnsupportedKeyTypeException
130131
*/
131132
private function getBucketIndex($key) {
132133
/*
@@ -232,8 +233,8 @@ public function getNodeByValue($value): ?Node {
232233
*
233234
* @param $key
234235
* @return Node|null
235-
* @throws \doganoo\PHPAlgorithms\Exception\InvalidKeyTypeException
236-
* @throws \doganoo\PHPAlgorithms\Exception\UnsupportedKeyTypeException
236+
* @throws \doganoo\PHPAlgorithms\common\Exception\InvalidKeyTypeException
237+
* @throws \doganoo\PHPAlgorithms\common\Exception\UnsupportedKeyTypeException
237238
*/
238239
public function getNodeByKey($key): ?Node {
239240
$arrayIndex = $this->getBucketIndex($key);
@@ -260,8 +261,8 @@ public function getNodeByKey($key): ?Node {
260261
*
261262
* @param $key
262263
* @return bool
263-
* @throws \doganoo\PHPAlgorithms\Exception\InvalidKeyTypeException
264-
* @throws \doganoo\PHPAlgorithms\Exception\UnsupportedKeyTypeException
264+
* @throws \doganoo\PHPAlgorithms\common\Exception\InvalidKeyTypeException
265+
* @throws \doganoo\PHPAlgorithms\common\Exception\UnsupportedKeyTypeException
265266
*/
266267
public function remove($key): bool {
267268
//get the corresponding index to key

src/StackQueue/Queue.php renamed to src/datastructure/StackQueue/Queue.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* SOFTWARE.
2424
*/
2525

26-
namespace doganoo\PHPAlgorithms\StackQueue;
26+
namespace doganoo\PHPAlgorithms\Datastructure\StackQueue;
2727

2828

2929
/**

src/StackQueue/Stack.php renamed to src/datastructure/StackQueue/Stack.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* SOFTWARE.
2424
*/
2525

26-
namespace doganoo\PHPAlgorithms\StackQueue;
26+
namespace doganoo\PHPAlgorithms\Datastructure\StackQueue;
2727

2828

2929
/**
File renamed without changes.
File renamed without changes.

src/Lists/ArrayLists/ArrayList.php renamed to src/datastructure/lists/ArrayLists/ArrayList.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@
2323
* SOFTWARE.
2424
*/
2525

26-
namespace doganoo\PHPAlgorithms\Lists\ArrayLists;
26+
namespace doganoo\PHPAlgorithms\Datastructure\Lists\ArrayLists;
2727

2828

29-
use doganoo\PHPAlgorithms\Exception\IndexOutOfBoundsException;
30-
use doganoo\PHPAlgorithms\Util\Logger;
29+
use doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException;
3130
use Traversable;
3231

3332
/**

src/Lists/ArrayLists/StringBuilder.php renamed to src/datastructure/lists/ArrayLists/StringBuilder.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* SOFTWARE.
2424
*/
2525

26-
namespace doganoo\PHPAlgorithms\Lists\ArrayLists;
26+
namespace doganoo\PHPAlgorithms\Datastructure\Lists\ArrayLists;
2727

2828
/**
2929
* Class StringBuilder
@@ -114,7 +114,7 @@ public function indexOf(string $value) {
114114
*
115115
* @param int $index
116116
* @param string $string
117-
* @throws \doganoo\PHPAlgorithms\Exception\IndexOutOfBoundsException
117+
* @throws \doganoo\PHPAlgorithms\common\Exception\IndexOutOfBoundsException
118118
*/
119119
public function insert(int $index, string $string) {
120120
if (\strlen($string) > 1) {

src/Lists/LinkedLists/DoublyLinkedList.php renamed to src/datastructure/lists/LinkedLists/DoublyLinkedList.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
* SOFTWARE.
2424
*/
2525

26-
namespace doganoo\PHPAlgorithms\Lists\LinkedLists;
26+
namespace doganoo\PHPAlgorithms\Datastructure\Lists\LinkedLists;
2727

2828

29-
use doganoo\PHPAlgorithms\Maps\Node;
29+
use doganoo\PHPAlgorithms\Datastructure\lists\Node;
3030

3131
/**
3232
* Class DoublyLinkedList
@@ -69,7 +69,7 @@ public function append(?Node $node): bool {
6969
/**
7070
* prepends a node on top of the list
7171
*
72-
* @param Node|null $node
72+
* @param \doganoo\PHPAlgorithms\Datastructure\lists\Node|null $node
7373
* @return bool
7474
*/
7575
public function prepend(?Node $node): bool {

0 commit comments

Comments
 (0)