diff --git a/Makefile b/Makefile index b01c539..f78d3a6 100644 --- a/Makefile +++ b/Makefile @@ -78,7 +78,8 @@ test: test-unit test-functional codestyle phpstan .PHONY: coverage coverage: - COVERAGE_OUTPUT_STYLE=html make test + COVERAGE_OUTPUT_STYLE=html make test-unit + COVERAGE_OUTPUT_STYLE=html make test-functional .PHONY: test-unit ifdef PHPUNIT_COVERAGE_OPTION diff --git a/src/DataStructure/Naming-conventions-and-Mapping.MD b/src/DataStructure/Naming-conventions-and-Mapping.MD new file mode 100644 index 0000000..ef3505e --- /dev/null +++ b/src/DataStructure/Naming-conventions-and-Mapping.MD @@ -0,0 +1,101 @@ +# Naming conventions & mapping + +## SplHeap +SplMinHeap / SplMaxHeap + +Construct โž™ ๐‘‚โŸฎ๐‘›โŸฏ + +#### Add an element +`add()`โž™`insert()` + +TC: ๐‘‚โŸฎใ’ ๐‘›โŸฏ
+SC: ๐‘‚โŸฎ๐ŸทโŸฏ + +#### Pop an element +`pop()`โž™`extract()` + +TC: ๐‘‚โŸฎใ’ ๐‘›โŸฏ
+SC: ๐‘‚โŸฎ๐ŸทโŸฏ + +#### Peek element +`peek()`โž™`top()` + +TC: ๐‘‚โŸฎ๐ŸทโŸฏ
+SC: ๐‘‚โŸฎ๐ŸทโŸฏ + +#### DS size +`size()`โž™`count()` + +TC: ๐‘‚โŸฎ๐ŸทโŸฏ
+SC: ๐‘‚โŸฎ๐ŸทโŸฏ + + +## SplPriorityQueue +โ„น๏ธ Implemented using a `SplMaxHeap` + +#### Add an element +`add()`โž™`insert()` + +TC: ๐‘‚โŸฎใ’ ๐‘›โŸฏ
+SC: ๐‘‚โŸฎ๐ŸทโŸฏ + +#### Pop an element +`pop()`โž™`extract()` + +TC: ๐‘‚โŸฎใ’ ๐‘›โŸฏ
+SC: ๐‘‚โŸฎ๐ŸทโŸฏ + +#### Peek element +`peek()`โž™`top()` + +TC: ๐‘‚โŸฎ๐ŸทโŸฏ
+SC: ๐‘‚โŸฎ๐ŸทโŸฏ + +#### DS size +`size()`โž™`count()` + +TC: ๐‘‚โŸฎ๐ŸทโŸฏ
+SC: ๐‘‚โŸฎ๐ŸทโŸฏ + + +## SplQueue +โ„น๏ธ Implemented using a `SplDoublyLinkedList` + +#### Enqueue an element +`enqueue()` + +#### Dequeue an element +`dequeue()` + +#### Peek element +`peek()`โž™`bottom()` + +#### DS size +`size()`โž™`count()` + +#### Front / Rear +โš ๏ธ Not actually available on "real" queue ! + +`front()`โž™`bottom()` โ€” `peek()`
+`rear()`โž™`top()` + +## SplStack +โ„น๏ธ Implemented using a `SplDoublyLinkedList` + +#### push an element +`push()` + +#### pop an element +`pop()` + +#### Peek element +`peek()`โž™`top()` + +#### DS size +`size()`โž™`count()` + +#### Front / Rear +โš ๏ธ Not actually available on "real" stack ! + +`front()`โž™`top()` โ€” `peek()`
+`rear()`โž™`bottom()` diff --git a/src/Helper/ArrayHelper.php b/src/Helper/ArrayHelper.php new file mode 100644 index 0000000..5063fd9 --- /dev/null +++ b/src/Helper/ArrayHelper.php @@ -0,0 +1,43 @@ + $list โš  Must be a 0 indexed list, 0 to n consecutive indexes ! + * @phpstan-param list $list + * @param int $index โš  Expected to be between 0 and ๐‘› ! + * @param mixed $value + */ + public static function insertAt(array &$list, int $index, mixed $value): void + { + $tailIdx = count($list) - 1; + + // 1. Move values until the end of original list + $prevValue = $value; + while ($index <= $tailIdx) { + // Backup original value at $index + replace original value by the previous value + [$prevValue, $list[$index]] = [$list[$index], $prevValue]; // @phpstan-ignore parameterByRef.type + ++$index; + } + + // 2. Append the original tail value at the end of the list (=new index !) + $list[$index] = $prevValue; // @phpstan-ignore parameterByRef.type + } +} diff --git a/tests/Technical/Helper/ArrayHelperTest.php b/tests/Technical/Helper/ArrayHelperTest.php new file mode 100644 index 0000000..b9d85b2 --- /dev/null +++ b/tests/Technical/Helper/ArrayHelperTest.php @@ -0,0 +1,60 @@ + [ + 'list' => [1,2,3,4,5,7,8,9], + 'index' => 5, + 'value' => 6, + 'expected' => [1,2,3,4,5,6,7,8,9], + ], + 'Replace head value' => [ + 'list' => [2,3,4,5,6,7,8,9], + 'index' => 0, + 'value' => 1, + 'expected' => [1,2,3,4,5,6,7,8,9], + ], + 'Replace tail value' => [ + 'list' => [1,2,3,4,5,6,7,9], + 'index' => 7, + 'value' => 8, + 'expected' => [1,2,3,4,5,6,7,8,9], + ], + 'As new tail value' => [ + 'list' => [1,2,3,4,5,6,7,8], + 'index' => 8, + 'value' => 9, + 'expected' => [1,2,3,4,5,6,7,8,9], + ], + 'Empty list' => [ + 'list' => [], + 'index' => 0, + 'value' => 1, + 'expected' => [1], + ], + ]; + } +}