diff --git a/composer.json b/composer.json index 765159972..d06b6651e 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,6 @@ "opis/closure": "^3.6", "pear/archive_tar": "^1.4.9", "pear/pear-core-minimal": "^v1.10", - "php-ds/php-ds": "^1.3", "php-http/guzzle7-adapter": "^1.0.0", "php-opencloud/openstack": "^3.1", "phpseclib/phpseclib": "^2.0.40", diff --git a/composer.lock b/composer.lock index 051e9140e..333cf7240 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0203f3418c220ca38e196e438cd1f4b4", + "content-hash": "ec3b1ef205684d4a965763a2d8c8136f", "packages": [ { "name": "aws/aws-crt-php", @@ -2769,57 +2769,6 @@ }, "time": "2021-03-21T15:43:46+00:00" }, - { - "name": "php-ds/php-ds", - "version": "v1.3.0", - "source": { - "type": "git", - "url": "https://github.com/php-ds/polyfill.git", - "reference": "b98396862fb8a13cbdbbaf4d18be28ee5c01ed3c" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/php-ds/polyfill/zipball/b98396862fb8a13cbdbbaf4d18be28ee5c01ed3c", - "reference": "b98396862fb8a13cbdbbaf4d18be28ee5c01ed3c", - "shasum": "" - }, - "require": { - "ext-json": "*", - "php": ">=7.0.0" - }, - "provide": { - "ext-ds": "1.3.0" - }, - "require-dev": { - "php-ds/tests": "^1.3" - }, - "suggest": { - "ext-ds": "to improve performance and reduce memory usage" - }, - "type": "library", - "autoload": { - "psr-4": { - "Ds\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Rudi Theunissen", - "email": "rudolf.theunissen@gmail.com" - } - ], - "keywords": [ - "data structures", - "ds", - "php", - "polyfill" - ], - "time": "2020-10-14T04:23:31+00:00" - }, { "name": "php-http/guzzle7-adapter", "version": "1.0.0", diff --git a/composer/autoload_classmap.php b/composer/autoload_classmap.php index 91cc4e4a9..8be84e637 100644 --- a/composer/autoload_classmap.php +++ b/composer/autoload_classmap.php @@ -1342,21 +1342,6 @@ 'Doctrine\\DBAL\\VersionAwarePlatformDriver' => $vendorDir . '/doctrine/dbal/src/VersionAwarePlatformDriver.php', 'Doctrine\\Deprecations\\Deprecation' => $vendorDir . '/doctrine/deprecations/lib/Doctrine/Deprecations/Deprecation.php', 'Doctrine\\Deprecations\\PHPUnit\\VerifyDeprecations' => $vendorDir . '/doctrine/deprecations/lib/Doctrine/Deprecations/PHPUnit/VerifyDeprecations.php', - 'Ds\\Collection' => $vendorDir . '/php-ds/php-ds/src/Collection.php', - 'Ds\\Deque' => $vendorDir . '/php-ds/php-ds/src/Deque.php', - 'Ds\\Hashable' => $vendorDir . '/php-ds/php-ds/src/Hashable.php', - 'Ds\\Map' => $vendorDir . '/php-ds/php-ds/src/Map.php', - 'Ds\\Pair' => $vendorDir . '/php-ds/php-ds/src/Pair.php', - 'Ds\\PriorityQueue' => $vendorDir . '/php-ds/php-ds/src/PriorityQueue.php', - 'Ds\\Queue' => $vendorDir . '/php-ds/php-ds/src/Queue.php', - 'Ds\\Sequence' => $vendorDir . '/php-ds/php-ds/src/Sequence.php', - 'Ds\\Set' => $vendorDir . '/php-ds/php-ds/src/Set.php', - 'Ds\\Stack' => $vendorDir . '/php-ds/php-ds/src/Stack.php', - 'Ds\\Traits\\Capacity' => $vendorDir . '/php-ds/php-ds/src/Traits/Capacity.php', - 'Ds\\Traits\\GenericCollection' => $vendorDir . '/php-ds/php-ds/src/Traits/GenericCollection.php', - 'Ds\\Traits\\GenericSequence' => $vendorDir . '/php-ds/php-ds/src/Traits/GenericSequence.php', - 'Ds\\Traits\\SquaredCapacity' => $vendorDir . '/php-ds/php-ds/src/Traits/SquaredCapacity.php', - 'Ds\\Vector' => $vendorDir . '/php-ds/php-ds/src/Vector.php', 'Egulias\\EmailValidator\\EmailLexer' => $vendorDir . '/egulias/email-validator/src/EmailLexer.php', 'Egulias\\EmailValidator\\EmailParser' => $vendorDir . '/egulias/email-validator/src/EmailParser.php', 'Egulias\\EmailValidator\\EmailValidator' => $vendorDir . '/egulias/email-validator/src/EmailValidator.php', diff --git a/composer/autoload_psr4.php b/composer/autoload_psr4.php index a62ecbef1..6052728ee 100644 --- a/composer/autoload_psr4.php +++ b/composer/autoload_psr4.php @@ -81,7 +81,6 @@ 'Fusonic\\OpenGraph\\' => array($vendorDir . '/fusonic/opengraph/src'), 'FG\\' => array($vendorDir . '/fgrosse/phpasn1/lib'), 'Egulias\\EmailValidator\\' => array($vendorDir . '/egulias/email-validator/src'), - 'Ds\\' => array($vendorDir . '/php-ds/php-ds/src'), 'Doctrine\\Deprecations\\' => array($vendorDir . '/doctrine/deprecations/lib/Doctrine/Deprecations'), 'Doctrine\\DBAL\\' => array($vendorDir . '/doctrine/dbal/src'), 'Doctrine\\Common\\Lexer\\' => array($vendorDir . '/doctrine/lexer/lib/Doctrine/Common/Lexer'), diff --git a/composer/autoload_static.php b/composer/autoload_static.php index a2bcdb37f..d32742400 100644 --- a/composer/autoload_static.php +++ b/composer/autoload_static.php @@ -263,7 +263,6 @@ class ComposerStaticInit2f23f73bc0cc116b4b1eee1521aa8652 ), 'D' => array ( - 'Ds\\' => 3, 'Doctrine\\Deprecations\\' => 22, 'Doctrine\\DBAL\\' => 14, 'Doctrine\\Common\\Lexer\\' => 22, @@ -592,10 +591,6 @@ class ComposerStaticInit2f23f73bc0cc116b4b1eee1521aa8652 array ( 0 => __DIR__ . '/..' . '/egulias/email-validator/src', ), - 'Ds\\' => - array ( - 0 => __DIR__ . '/..' . '/php-ds/php-ds/src', - ), 'Doctrine\\Deprecations\\' => array ( 0 => __DIR__ . '/..' . '/doctrine/deprecations/lib/Doctrine/Deprecations', @@ -2014,21 +2009,6 @@ class ComposerStaticInit2f23f73bc0cc116b4b1eee1521aa8652 'Doctrine\\DBAL\\VersionAwarePlatformDriver' => __DIR__ . '/..' . '/doctrine/dbal/src/VersionAwarePlatformDriver.php', 'Doctrine\\Deprecations\\Deprecation' => __DIR__ . '/..' . '/doctrine/deprecations/lib/Doctrine/Deprecations/Deprecation.php', 'Doctrine\\Deprecations\\PHPUnit\\VerifyDeprecations' => __DIR__ . '/..' . '/doctrine/deprecations/lib/Doctrine/Deprecations/PHPUnit/VerifyDeprecations.php', - 'Ds\\Collection' => __DIR__ . '/..' . '/php-ds/php-ds/src/Collection.php', - 'Ds\\Deque' => __DIR__ . '/..' . '/php-ds/php-ds/src/Deque.php', - 'Ds\\Hashable' => __DIR__ . '/..' . '/php-ds/php-ds/src/Hashable.php', - 'Ds\\Map' => __DIR__ . '/..' . '/php-ds/php-ds/src/Map.php', - 'Ds\\Pair' => __DIR__ . '/..' . '/php-ds/php-ds/src/Pair.php', - 'Ds\\PriorityQueue' => __DIR__ . '/..' . '/php-ds/php-ds/src/PriorityQueue.php', - 'Ds\\Queue' => __DIR__ . '/..' . '/php-ds/php-ds/src/Queue.php', - 'Ds\\Sequence' => __DIR__ . '/..' . '/php-ds/php-ds/src/Sequence.php', - 'Ds\\Set' => __DIR__ . '/..' . '/php-ds/php-ds/src/Set.php', - 'Ds\\Stack' => __DIR__ . '/..' . '/php-ds/php-ds/src/Stack.php', - 'Ds\\Traits\\Capacity' => __DIR__ . '/..' . '/php-ds/php-ds/src/Traits/Capacity.php', - 'Ds\\Traits\\GenericCollection' => __DIR__ . '/..' . '/php-ds/php-ds/src/Traits/GenericCollection.php', - 'Ds\\Traits\\GenericSequence' => __DIR__ . '/..' . '/php-ds/php-ds/src/Traits/GenericSequence.php', - 'Ds\\Traits\\SquaredCapacity' => __DIR__ . '/..' . '/php-ds/php-ds/src/Traits/SquaredCapacity.php', - 'Ds\\Vector' => __DIR__ . '/..' . '/php-ds/php-ds/src/Vector.php', 'Egulias\\EmailValidator\\EmailLexer' => __DIR__ . '/..' . '/egulias/email-validator/src/EmailLexer.php', 'Egulias\\EmailValidator\\EmailParser' => __DIR__ . '/..' . '/egulias/email-validator/src/EmailParser.php', 'Egulias\\EmailValidator\\EmailValidator' => __DIR__ . '/..' . '/egulias/email-validator/src/EmailValidator.php', diff --git a/composer/installed.json b/composer/installed.json index c08eef742..f09170e37 100644 --- a/composer/installed.json +++ b/composer/installed.json @@ -2889,60 +2889,6 @@ }, "install-path": "../pear/pear_exception" }, - { - "name": "php-ds/php-ds", - "version": "v1.3.0", - "version_normalized": "1.3.0.0", - "source": { - "type": "git", - "url": "https://github.com/php-ds/polyfill.git", - "reference": "b98396862fb8a13cbdbbaf4d18be28ee5c01ed3c" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/php-ds/polyfill/zipball/b98396862fb8a13cbdbbaf4d18be28ee5c01ed3c", - "reference": "b98396862fb8a13cbdbbaf4d18be28ee5c01ed3c", - "shasum": "" - }, - "require": { - "ext-json": "*", - "php": ">=7.0.0" - }, - "provide": { - "ext-ds": "1.3.0" - }, - "require-dev": { - "php-ds/tests": "^1.3" - }, - "suggest": { - "ext-ds": "to improve performance and reduce memory usage" - }, - "time": "2020-10-14T04:23:31+00:00", - "type": "library", - "installation-source": "dist", - "autoload": { - "psr-4": { - "Ds\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Rudi Theunissen", - "email": "rudolf.theunissen@gmail.com" - } - ], - "keywords": [ - "data structures", - "ds", - "php", - "polyfill" - ], - "install-path": "../php-ds/php-ds" - }, { "name": "php-http/guzzle7-adapter", "version": "1.0.0", @@ -6992,6 +6938,6 @@ "install-path": "../web-auth/webauthn-lib" } ], - "dev": false, + "dev": true, "dev-package-names": [] } diff --git a/composer/installed.php b/composer/installed.php index 95fbd57a1..aa6157312 100644 --- a/composer/installed.php +++ b/composer/installed.php @@ -3,11 +3,11 @@ 'name' => 'nextcloud/3rdparty', 'pretty_version' => 'dev-master', 'version' => 'dev-master', - 'reference' => 'e6a429dcd19e45c9c81528bf96a604f391519a85', + 'reference' => '5e0dab110ac3bbf4d62ab0b808467b8d3350274e', 'type' => 'library', 'install_path' => __DIR__ . '/../', 'aliases' => array(), - 'dev' => false, + 'dev' => true, ), 'versions' => array( 'aws/aws-crt-php' => array( @@ -328,7 +328,7 @@ 'nextcloud/3rdparty' => array( 'pretty_version' => 'dev-master', 'version' => 'dev-master', - 'reference' => 'e6a429dcd19e45c9c81528bf96a604f391519a85', + 'reference' => '5e0dab110ac3bbf4d62ab0b808467b8d3350274e', 'type' => 'library', 'install_path' => __DIR__ . '/../', 'aliases' => array(), @@ -397,15 +397,6 @@ 'aliases' => array(), 'dev_requirement' => false, ), - 'php-ds/php-ds' => array( - 'pretty_version' => 'v1.3.0', - 'version' => '1.3.0.0', - 'reference' => 'b98396862fb8a13cbdbbaf4d18be28ee5c01ed3c', - 'type' => 'library', - 'install_path' => __DIR__ . '/../php-ds/php-ds', - 'aliases' => array(), - 'dev_requirement' => false, - ), 'php-http/async-client-implementation' => array( 'dev_requirement' => false, 'provided' => array( diff --git a/php-ds/php-ds/CONTRIBUTING.md b/php-ds/php-ds/CONTRIBUTING.md deleted file mode 100644 index 1bf340ec4..000000000 --- a/php-ds/php-ds/CONTRIBUTING.md +++ /dev/null @@ -1,21 +0,0 @@ -# Contributing - -Contributions are accepted via [pull requests](https://github.com/php-ds/ext/pulls). If you would like to report a bug, please create an [issue](https://github.com/php-ds/ext/issues) instead. - -## Issues - -- **How to reproduce** - Provide an easy way to reproduce the bug. This makes it easier for others to debug. - -- **Platform details** - Specify your platform and your PHP version, eg. "PHP 7.0.2 on Ubuntu 14.04 64x". - -## Pull Requests - -- **Add tests** - Your patch won't be accepted if it doesn't have tests where appropriate. - -- **Document any change in behaviour** - Make sure the README and any other relevant documentation updated. - -- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. - -- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting. - -- **Coding style** - Try to match the style of the rest of the source wherever possible. Your patch won't be accepted if the style is significantly different. diff --git a/php-ds/php-ds/LICENSE b/php-ds/php-ds/LICENSE deleted file mode 100644 index 5f2a77787..000000000 --- a/php-ds/php-ds/LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -The MIT License (MIT) -Copyright (c) 2016 Rudi Theunissen - -Permission is hereby granted, free of charge, to any person obtaining a -copy of this software and associated documentation files (the "Software"), -to deal in the Software without restriction, including without limitation -the rights to use, copy, modify, merge, publish, distribute, sublicense, -and/or sell copies of the Software, and to permit persons to whom the -Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH -THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/php-ds/php-ds/src/Collection.php b/php-ds/php-ds/src/Collection.php deleted file mode 100644 index 17f512be5..000000000 --- a/php-ds/php-ds/src/Collection.php +++ /dev/null @@ -1,52 +0,0 @@ -= $this->capacity; - } -} diff --git a/php-ds/php-ds/src/Hashable.php b/php-ds/php-ds/src/Hashable.php deleted file mode 100644 index d4b31fb42..000000000 --- a/php-ds/php-ds/src/Hashable.php +++ /dev/null @@ -1,34 +0,0 @@ -putAll($values); - } - } - - /** - * Updates all values by applying a callback function to each value. - * - * @param callable $callback Accepts two arguments: key and value, should - * return what the updated value will be. - */ - public function apply(callable $callback) - { - foreach ($this->pairs as &$pair) { - $pair->value = $callback($pair->key, $pair->value); - } - } - - /** - * @inheritDoc - */ - public function clear() - { - $this->pairs = []; - $this->capacity = self::MIN_CAPACITY; - } - - /** - * Return the first Pair from the Map - * - * @return Pair - * - * @throws UnderflowException - */ - public function first(): Pair - { - if ($this->isEmpty()) { - throw new UnderflowException(); - } - - return $this->pairs[0]; - } - - /** - * Return the last Pair from the Map - * - * @return Pair - * - * @throws UnderflowException - */ - public function last(): Pair - { - if ($this->isEmpty()) { - throw new UnderflowException(); - } - - return $this->pairs[count($this->pairs) - 1]; - } - - /** - * Return the pair at a specified position in the Map - * - * @param int $position - * - * @return Pair - * - * @throws OutOfRangeException - */ - public function skip(int $position): Pair - { - if ($position < 0 || $position >= count($this->pairs)) { - throw new OutOfRangeException(); - } - - return $this->pairs[$position]->copy(); - } - - /** - * Returns the result of associating all keys of a given traversable object - * or array with their corresponding values, as well as those of this map. - * - * @param array|\Traversable $values - * - * @return Map - */ - public function merge($values): Map - { - $merged = new self($this); - $merged->putAll($values); - return $merged; - } - - /** - * Creates a new map containing the pairs of the current instance whose keys - * are also present in the given map. In other words, returns a copy of the - * current map with all keys removed that are not also in the other map. - * - * @param Map $map The other map. - * - * @return Map A new map containing the pairs of the current instance - * whose keys are also present in the given map. In other - * words, returns a copy of the current map with all keys - * removed that are not also in the other map. - */ - public function intersect(Map $map): Map - { - return $this->filter(function($key) use ($map) { - return $map->hasKey($key); - }); - } - - /** - * Returns the result of removing all keys from the current instance that - * are present in a given map. - * - * @param Map $map The map containing the keys to exclude. - * - * @return Map The result of removing all keys from the current instance - * that are present in a given map. - */ - public function diff(Map $map): Map - { - return $this->filter(function($key) use ($map) { - return ! $map->hasKey($key); - }); - } - - /** - * Determines whether two keys are equal. - * - * @param mixed $a - * @param mixed $b - * - * @return bool - */ - private function keysAreEqual($a, $b): bool - { - if (is_object($a) && $a instanceof Hashable) { - return get_class($a) === get_class($b) && $a->equals($b); - } - - return $a === $b; - } - - /** - * Attempts to look up a key in the table. - * - * @param $key - * - * @return Pair|null - */ - private function lookupKey($key) - { - foreach ($this->pairs as $pair) { - if ($this->keysAreEqual($pair->key, $key)) { - return $pair; - } - } - } - - /** - * Attempts to look up a value in the table. - * - * @param $value - * - * @return Pair|null - */ - private function lookupValue($value) - { - foreach ($this->pairs as $pair) { - if ($pair->value === $value) { - return $pair; - } - } - } - - /** - * Returns whether an association a given key exists. - * - * @param mixed $key - * - * @return bool - */ - public function hasKey($key): bool - { - return $this->lookupKey($key) !== null; - } - - /** - * Returns whether an association for a given value exists. - * - * @param mixed $value - * - * @return bool - */ - public function hasValue($value): bool - { - return $this->lookupValue($value) !== null; - } - - /** - * @inheritDoc - */ - public function count(): int - { - return count($this->pairs); - } - - /** - * Returns a new map containing only the values for which a predicate - * returns true. A boolean test will be used if a predicate is not provided. - * - * @param callable|null $callback Accepts a key and a value, and returns: - * true : include the value, - * false: skip the value. - * - * @return Map - */ - public function filter(callable $callback = null): Map - { - $filtered = new self(); - - foreach ($this as $key => $value) { - if ($callback ? $callback($key, $value) : $value) { - $filtered->put($key, $value); - } - } - - return $filtered; - } - - /** - * Returns the value associated with a key, or an optional default if the - * key is not associated with a value. - * - * @param mixed $key - * @param mixed $default - * - * @return mixed The associated value or fallback default if provided. - * - * @throws OutOfBoundsException if no default was provided and the key is - * not associated with a value. - */ - public function get($key, $default = null) - { - if (($pair = $this->lookupKey($key))) { - return $pair->value; - } - - // Check if a default was provided. - if (func_num_args() === 1) { - throw new OutOfBoundsException(); - } - - return $default; - } - - /** - * Returns a set of all the keys in the map. - * - * @return Set - */ - public function keys(): Set - { - $key = function($pair) { - return $pair->key; - }; - - return new Set(array_map($key, $this->pairs)); - } - - /** - * Returns a new map using the results of applying a callback to each value. - * - * The keys will be equal in both maps. - * - * @param callable $callback Accepts two arguments: key and value, should - * return what the updated value will be. - * - * @return Map - */ - public function map(callable $callback): Map - { - $mapped = new self(); - foreach ($this->pairs as $pair) { - $mapped->put($pair->key, $callback($pair->key, $pair->value)); - } - - return $mapped; - } - - /** - * Returns a sequence of pairs representing all associations. - * - * @return Sequence - */ - public function pairs(): Sequence - { - $copy = function($pair) { - return $pair->copy(); - }; - - return new Vector(array_map($copy, $this->pairs)); - } - - /** - * Associates a key with a value, replacing a previous association if there - * was one. - * - * @param mixed $key - * @param mixed $value - */ - public function put($key, $value) - { - $pair = $this->lookupKey($key); - - if ($pair) { - $pair->value = $value; - - } else { - $this->checkCapacity(); - $this->pairs[] = new Pair($key, $value); - } - } - - /** - * Creates associations for all keys and corresponding values of either an - * array or iterable object. - * - * @param \Traversable|array $values - */ - public function putAll($values) - { - foreach ($values as $key => $value) { - $this->put($key, $value); - } - } - - /** - * Iteratively reduces the map to a single value using a callback. - * - * @param callable $callback Accepts the carry, key, and value, and - * returns an updated carry value. - * - * @param mixed|null $initial Optional initial carry value. - * - * @return mixed The carry value of the final iteration, or the initial - * value if the map was empty. - */ - public function reduce(callable $callback, $initial = null) - { - $carry = $initial; - - foreach ($this->pairs as $pair) { - $carry = $callback($carry, $pair->key, $pair->value); - } - - return $carry; - } - - /** - * Completely removes a pair from the internal array by position. It is - * important to remove it from the array and not just use 'unset'. - */ - private function delete(int $position) - { - $pair = $this->pairs[$position]; - $value = $pair->value; - - array_splice($this->pairs, $position, 1, null); - $this->checkCapacity(); - - return $value; - } - - /** - * Removes a key's association from the map and returns the associated value - * or a provided default if provided. - * - * @param mixed $key - * @param mixed $default - * - * @return mixed The associated value or fallback default if provided. - * - * @throws \OutOfBoundsException if no default was provided and the key is - * not associated with a value. - */ - public function remove($key, $default = null) - { - foreach ($this->pairs as $position => $pair) { - if ($this->keysAreEqual($pair->key, $key)) { - return $this->delete($position); - } - } - - // Check if a default was provided - if (func_num_args() === 1) { - throw new \OutOfBoundsException(); - } - - return $default; - } - - /** - * Returns a reversed copy of the map. - * - * @return Map - */ - public function reverse() - { - $this->pairs = array_reverse($this->pairs); - } - - /** - * Returns a reversed copy of the map. - * - * @return Map - */ - public function reversed(): Map - { - $reversed = new self(); - $reversed->pairs = array_reverse($this->pairs); - - return $reversed; - } - - /** - * Returns a sub-sequence of a given length starting at a specified offset. - * - * @param int $offset If the offset is non-negative, the map will - * start at that offset in the map. If offset is - * negative, the map will start that far from the - * end. - * - * @param int|null $length If a length is given and is positive, the - * resulting set will have up to that many pairs in - * it. If the requested length results in an - * overflow, only pairs up to the end of the map - * will be included. - * - * If a length is given and is negative, the map - * will stop that many pairs from the end. - * - * If a length is not provided, the resulting map - * will contains all pairs between the offset and - * the end of the map. - * - * @return Map - */ - public function slice(int $offset, int $length = null): Map - { - $map = new self(); - - if (func_num_args() === 1) { - $slice = array_slice($this->pairs, $offset); - } else { - $slice = array_slice($this->pairs, $offset, $length); - } - - foreach ($slice as $pair) { - $map->put($pair->key, $pair->value); - } - - return $map; - } - - /** - * Sorts the map in-place, based on an optional callable comparator. - * - * The map will be sorted by value. - * - * @param callable|null $comparator Accepts two values to be compared. - */ - public function sort(callable $comparator = null) - { - if ($comparator) { - usort($this->pairs, function($a, $b) use ($comparator) { - return $comparator($a->value, $b->value); - }); - - } else { - usort($this->pairs, function($a, $b) { - return $a->value <=> $b->value; - }); - } - } - - /** - * Returns a sorted copy of the map, based on an optional callable - * comparator. The map will be sorted by value. - * - * @param callable|null $comparator Accepts two values to be compared. - * - * @return Map - */ - public function sorted(callable $comparator = null): Map - { - $copy = $this->copy(); - $copy->sort($comparator); - return $copy; - } - - /** - * Sorts the map in-place, based on an optional callable comparator. - * - * The map will be sorted by key. - * - * @param callable|null $comparator Accepts two keys to be compared. - */ - public function ksort(callable $comparator = null) - { - if ($comparator) { - usort($this->pairs, function($a, $b) use ($comparator) { - return $comparator($a->key, $b->key); - }); - - } else { - usort($this->pairs, function($a, $b) { - return $a->key <=> $b->key; - }); - } - } - - /** - * Returns a sorted copy of the map, based on an optional callable - * comparator. The map will be sorted by key. - * - * @param callable|null $comparator Accepts two keys to be compared. - * - * @return Map - */ - public function ksorted(callable $comparator = null): Map - { - $copy = $this->copy(); - $copy->ksort($comparator); - return $copy; - } - - /** - * Returns the sum of all values in the map. - * - * @return int|float The sum of all the values in the map. - */ - public function sum() - { - return $this->values()->sum(); - } - - /** - * @inheritDoc - */ - public function toArray(): array - { - $array = []; - - foreach ($this->pairs as $pair) { - $array[$pair->key] = $pair->value; - } - - return $array; - } - - /** - * Returns a sequence of all the associated values in the Map. - * - * @return Sequence - */ - public function values(): Sequence - { - $value = function($pair) { - return $pair->value; - }; - - return new Vector(array_map($value, $this->pairs)); - } - - /** - * Creates a new map that contains the pairs of the current instance as well - * as the pairs of another map. - * - * @param Map $map The other map, to combine with the current instance. - * - * @return Map A new map containing all the pairs of the current - * instance as well as another map. - */ - public function union(Map $map): Map - { - return $this->merge($map); - } - - /** - * Creates a new map using keys of either the current instance or of another - * map, but not of both. - * - * @param Map $map - * - * @return Map A new map containing keys in the current instance as well - * as another map, but not in both. - */ - public function xor(Map $map): Map - { - return $this->merge($map)->filter(function($key) use ($map) { - return $this->hasKey($key) ^ $map->hasKey($key); - }); - } - - /** - * @inheritDoc - */ - public function getIterator() - { - foreach ($this->pairs as $pair) { - yield $pair->key => $pair->value; - } - } - - /** - * Returns a representation to be used for var_dump and print_r. - */ - public function __debugInfo() - { - return $this->pairs()->toArray(); - } - - /** - * @inheritdoc - */ - public function offsetSet($offset, $value) - { - $this->put($offset, $value); - } - - /** - * @inheritdoc - * - * @throws OutOfBoundsException - */ - public function &offsetGet($offset) - { - $pair = $this->lookupKey($offset); - - if ($pair) { - return $pair->value; - } - throw new OutOfBoundsException(); - } - - /** - * @inheritdoc - */ - public function offsetUnset($offset) - { - $this->remove($offset, null); - } - - /** - * @inheritdoc - */ - public function offsetExists($offset) - { - return $this->get($offset, null) !== null; - } - - /** - * Returns a representation that can be natively converted to JSON, which is - * called when invoking json_encode. - * - * @return mixed - * - * @see \JsonSerializable - */ - public function jsonSerialize() - { - return (object) $this->toArray(); - } -} diff --git a/php-ds/php-ds/src/Pair.php b/php-ds/php-ds/src/Pair.php deleted file mode 100644 index bab0b662c..000000000 --- a/php-ds/php-ds/src/Pair.php +++ /dev/null @@ -1,141 +0,0 @@ -key = $key; - $this->value = $value; - } - - /** - * - * @param mixed $name - * - * @return mixed|null - */ - public function __isset($name) - { - if ($name === 'key' || $name === 'value') { - return $this->$name !== null; - } - return false; - } - - /** - * This allows unset($pair->key) to not completely remove the property, - * but be set to null instead. - * - * @param mixed $name - * - * @return mixed|null - */ - public function __unset($name) - { - if ($name === 'key' || $name === 'value') { - $this->$name = null; - return; - } - throw new OutOfBoundsException(); - } - - /** - * @param mixed $name - * - * @return mixed|null - */ - public function &__get($name) - { - if ($name === 'key' || $name === 'value') { - return $this->$name; - } - throw new OutOfBoundsException(); - } - - /** - * @param mixed $name - * @param mixed $value - * - * @return mixed|null - */ - public function __set($name, $value) - { - if ($name === 'key' || $name === 'value') { - $this->$name = $value; - return; - } - throw new OutOfBoundsException(); - } - - /** - * Returns a copy of the Pair - */ - public function copy(): self - { - return new self($this->key, $this->value); - } - - /** - * Returns a representation to be used for var_dump and print_r. - * - * @return array - */ - public function __debugInfo() - { - return $this->toArray(); - } - - /** - * @return array - */ - public function toArray(): array - { - return [ - 'key' => $this->key, - 'value' => $this->value, - ]; - } - - /** - * @inheritDoc - */ - public function jsonSerialize() - { - return $this->toArray(); - } - - /** - * Returns a string representation of the pair. - */ - public function __toString() - { - return 'object(' . get_class($this) . ')'; - } -} diff --git a/php-ds/php-ds/src/PriorityQueue.php b/php-ds/php-ds/src/PriorityQueue.php deleted file mode 100644 index 04ba8c831..000000000 --- a/php-ds/php-ds/src/PriorityQueue.php +++ /dev/null @@ -1,338 +0,0 @@ -heap = []; - $this->stamp = 0; - $this->capacity = self::MIN_CAPACITY; - } - - /** - * @inheritDoc - */ - public function copy(): self - { - $copy = new PriorityQueue(); - - $copy->heap = $this->heap; - $copy->stamp = $this->stamp; - $copy->capacity = $this->capacity; - - return $copy; - } - - /** - * @inheritDoc - */ - public function count(): int - { - return count($this->heap); - } - - /** - * Returns the value with the highest priority in the priority queue. - * - * @return mixed - * - * @throw UnderflowException - */ - public function peek() - { - if ($this->isEmpty()) { - throw new UnderflowException(); - } - - return $this->heap[0]->value; - } - - /** - * Returns the index of a node's left leaf. - * - * @param int $index The index of the node. - * - * @return int The index of the left leaf. - */ - private function left(int $index): int - { - return ($index * 2) + 1; - } - - /** - * Returns the index of a node's right leaf. - * - * @param int $index The index of the node. - * - * @return int The index of the right leaf. - */ - private function right(int $index): int - { - return ($index * 2) + 2; - } - - /** - * Returns the index of a node's parent node. - * - * @param int $index The index of the node. - * - * @return int The index of the parent. - */ - private function parent(int $index): int - { - return ($index - 1) / 2; - } - - /** - * Compares two indices of the heap. - * - * @param int $a - * @param int $b - * - * @return int - */ - private function compare(int $a, int $b) - { - $x = $this->heap[$a]; - $y = $this->heap[$b]; - - // Compare priority, using insertion stamp as fallback. - return ($x->priority <=> $y->priority) ?: ($y->stamp <=> $x->stamp); - } - - /** - * Swaps the nodes at two indices of the heap. - * - * @param int $a - * @param int $b - */ - private function swap(int $a, int $b) - { - $temp = $this->heap[$a]; - $this->heap[$a] = $this->heap[$b]; - $this->heap[$b] = $temp; - } - - /** - * Returns the index of a node's largest leaf node. - * - * @param int $parent the parent node. - * - * @return int the index of the node's largest leaf node. - */ - private function getLargestLeaf(int $parent) - { - $left = $this->left($parent); - $right = $this->right($parent); - - if ($right < count($this->heap) && $this->compare($left, $right) < 0) { - return $right; - } - - return $left; - } - - /** - * Starts the process of sifting down a given node index to ensure that - * the heap's properties are preserved. - * - * @param int $node - */ - private function siftDown(int $node) - { - $last = floor(count($this->heap) / 2); - - for ($parent = $node; $parent < $last; $parent = $leaf) { - - // Determine the largest leaf to potentially swap with the parent. - $leaf = $this->getLargestLeaf($parent); - - // Done if the parent is not greater than its largest leaf - if ($this->compare($parent, $leaf) > 0) { - break; - } - - $this->swap($parent, $leaf); - } - } - - /** - * Sets the root node and sifts it down the heap. - * - * @param PriorityNode $node - */ - private function setRoot(PriorityNode $node) - { - $this->heap[0] = $node; - $this->siftDown(0); - } - - /** - * Returns the root node of the heap. - * - * @return PriorityNode - */ - private function getRoot(): PriorityNode - { - return $this->heap[0]; - } - - /** - * Returns and removes the value with the highest priority in the queue. - * - * @return mixed - */ - public function pop() - { - if ($this->isEmpty()) { - throw new UnderflowException(); - } - - // Last leaf of the heap to become the new root. - $leaf = array_pop($this->heap); - - if (empty($this->heap)) { - return $leaf->value; - } - - // Cache the current root value to return before replacing with next. - $value = $this->getRoot()->value; - - // Replace the root, then sift down. - $this->setRoot($leaf); - $this->checkCapacity(); - - return $value; - } - - /** - * Sifts a node up the heap until it's in the right position. - * - * @param int $leaf - */ - private function siftUp(int $leaf) - { - for (; $leaf > 0; $leaf = $parent) { - $parent = $this->parent($leaf); - - // Done when parent priority is greater. - if ($this->compare($leaf, $parent) < 0) { - break; - } - - $this->swap($parent, $leaf); - } - } - - /** - * Pushes a value into the queue, with a specified priority. - * - * @param mixed $value - * @param int $priority - */ - public function push($value, int $priority) - { - $this->checkCapacity(); - - // Add new leaf, then sift up to maintain heap, - $this->heap[] = new PriorityNode($value, $priority, $this->stamp++); - $this->siftUp(count($this->heap) - 1); - } - - /** - * @inheritDoc - */ - public function toArray(): array - { - $heap = $this->heap; - $array = []; - - while ( ! $this->isEmpty()) { - $array[] = $this->pop(); - } - - $this->heap = $heap; - return $array; - } - - /** - * @inheritDoc - */ - public function getIterator() - { - while ( ! $this->isEmpty()) { - yield $this->pop(); - } - } -} - -/** - * @internal - */ -final class PriorityNode -{ - /** - * @var mixed - */ - public $value; - - /** - * @var int - */ - public $priority; - - /** - * @var int - */ - public $stamp; - - /** - * @param mixed $value - * @param int $priority - * @param int $stamp - */ - public function __construct($value, int $priority, int $stamp) - { - $this->value = $value; - $this->priority = $priority; - $this->stamp = $stamp; - } -} diff --git a/php-ds/php-ds/src/Queue.php b/php-ds/php-ds/src/Queue.php deleted file mode 100644 index c1f8fa5ec..000000000 --- a/php-ds/php-ds/src/Queue.php +++ /dev/null @@ -1,179 +0,0 @@ -deque = new Deque($values ?: []); - } - - /** - * Ensures that enough memory is allocated for a specified capacity. This - * potentially reduces the number of reallocations as the size increases. - * - * @param int $capacity The number of values for which capacity should be - * allocated. Capacity will stay the same if this value - * is less than or equal to the current capacity. - */ - public function allocate(int $capacity) - { - $this->deque->allocate($capacity); - } - - /** - * Returns the current capacity of the queue. - * - * @return int - */ - public function capacity(): int - { - return $this->deque->capacity(); - } - - /** - * @inheritDoc - */ - public function clear() - { - $this->deque->clear(); - } - - /** - * @inheritDoc - */ - public function copy(): self - { - return new self($this->deque); - } - - /** - * @inheritDoc - */ - public function count(): int - { - return count($this->deque); - } - - /** - * Returns the value at the front of the queue without removing it. - * - * @return - */ - public function peek() - { - return $this->deque->first(); - } - - /** - * Returns and removes the value at the front of the Queue. - * - * @return mixed - */ - public function pop() - { - return $this->deque->shift(); - } - - /** - * Pushes zero or more values into the front of the queue. - * - * @param mixed ...$values - */ - public function push(...$values) - { - $this->deque->push(...$values); - } - - /** - * @inheritDoc - */ - public function toArray(): array - { - return $this->deque->toArray(); - } - - /** - * Get iterator - */ - public function getIterator() - { - while ( ! $this->isEmpty()) { - yield $this->pop(); - } - } - - - /** - * @inheritdoc - * - * @throws OutOfBoundsException - */ - public function offsetSet($offset, $value) - { - if ($offset === null) { - $this->push($value); - } else { - throw new Error(); - } - } - - /** - * @inheritdoc - * - * @throws Error - */ - public function offsetGet($offset) - { - throw new Error(); - } - - /** - * @inheritdoc - * - * @throws Error - */ - public function offsetUnset($offset) - { - throw new Error(); - } - - /** - * @inheritdoc - * - * @throws Error - */ - public function offsetExists($offset) - { - throw new Error(); - } - - /** - * Ensures that the internal sequence will be cloned too. - */ - public function __clone() - { - $this->deque = clone $this->deque; - } -} diff --git a/php-ds/php-ds/src/Sequence.php b/php-ds/php-ds/src/Sequence.php deleted file mode 100644 index 88011ae06..000000000 --- a/php-ds/php-ds/src/Sequence.php +++ /dev/null @@ -1,277 +0,0 @@ - b. - */ - function sort(callable $comparator = null); - - /** - * Returns a sorted copy of the sequence, based on an optional callable - * comparator. Natural ordering will be used if a comparator is not given. - * - * @param callable|null $comparator Accepts two values to be compared. - * Should return the result of a <=> b. - * - * @return Sequence - */ - function sorted(callable $comparator = null): Sequence; - - /** - * Returns the sum of all values in the sequence. - * - * @return int|float The sum of all the values in the sequence. - */ - function sum(); - - /** - * Adds zero or more values to the front of the sequence. - * - * @param mixed ...$values - */ - function unshift(...$values); -} diff --git a/php-ds/php-ds/src/Set.php b/php-ds/php-ds/src/Set.php deleted file mode 100644 index d041b9140..000000000 --- a/php-ds/php-ds/src/Set.php +++ /dev/null @@ -1,467 +0,0 @@ -table = new Map(); - - if (func_num_args()) { - $this->add(...$values); - } - } - - /** - * Adds zero or more values to the set. - * - * @param mixed ...$values - */ - public function add(...$values) - { - foreach ($values as $value) { - $this->table->put($value, null); - } - } - - /** - * Ensures that enough memory is allocated for a specified capacity. This - * potentially reduces the number of reallocations as the size increases. - * - * @param int $capacity The number of values for which capacity should be - * allocated. Capacity will stay the same if this value - * is less than or equal to the current capacity. - */ - public function allocate(int $capacity) - { - $this->table->allocate($capacity); - } - - /** - * Returns the current capacity of the set. - * - * @return int - */ - public function capacity(): int - { - return $this->table->capacity(); - } - - /** - * Clear all elements in the Set - */ - public function clear() - { - $this->table->clear(); - } - - /** - * Determines whether the set contains all of zero or more values. - * - * @param mixed ...$values - * - * @return bool true if at least one value was provided and the set - * contains all given values, false otherwise. - */ - public function contains(...$values): bool - { - foreach ($values as $value) { - if ( ! $this->table->hasKey($value)) { - return false; - } - } - - return true; - } - - /** - * @inheritDoc - */ - public function copy(): self - { - return new self($this); - } - - /** - * Returns the number of elements in the Stack - * - * @return int - */ - public function count(): int - { - return count($this->table); - } - - /** - * Creates a new set using values from this set that aren't in another set. - * - * Formally: A \ B = {x ∈ A | x ∉ B} - * - * @param Set $set - * - * @return Set - */ - public function diff(Set $set): Set - { - return $this->table->diff($set->table)->keys(); - } - - /** - * Creates a new set using values in either this set or in another set, - * but not in both. - * - * Formally: A ⊖ B = {x : x ∈ (A \ B) ∪ (B \ A)} - * - * @param Set $set - * - * @return Set - */ - public function xor(Set $set): Set - { - return $this->table->xor($set->table)->keys(); - } - - /** - * Returns a new set containing only the values for which a callback - * returns true. A boolean test will be used if a callback is not provided. - * - * @param callable|null $callback Accepts a value, returns a boolean: - * true : include the value, - * false: skip the value. - * - * @return Set - */ - public function filter(callable $callback = null): Set - { - return new self(array_filter($this->toArray(), $callback ?: 'boolval')); - } - - /** - * Returns the first value in the set. - * - * @return mixed the first value in the set. - */ - public function first() - { - return $this->table->first()->key; - } - - /** - * Returns the value at a specified position in the set. - * - * @param int $position - * - * @return mixed|null - * - * @throws OutOfRangeException - */ - public function get(int $position) - { - return $this->table->skip($position)->key; - } - - /** - * Creates a new set using values common to both this set and another set. - * - * In other words, returns a copy of this set with all values removed that - * aren't in the other set. - * - * Formally: A ∩ B = {x : x ∈ A ∧ x ∈ B} - * - * @param Set $set - * - * @return Set - */ - public function intersect(Set $set): Set - { - return $this->table->intersect($set->table)->keys(); - } - - /** - * @inheritDoc - */ - public function isEmpty(): bool - { - return $this->table->isEmpty(); - } - - /** - * Joins all values of the set into a string, adding an optional 'glue' - * between them. Returns an empty string if the set is empty. - * - * @param string $glue - * - * @return string - */ - public function join(string $glue = null): string - { - return implode($glue, $this->toArray()); - } - - /** - * Returns the last value in the set. - * - * @return mixed the last value in the set. - */ - public function last() - { - return $this->table->last()->key; - } - - /** - * Iteratively reduces the set to a single value using a callback. - * - * @param callable $callback Accepts the carry and current value, and - * returns an updated carry value. - * - * @param mixed|null $initial Optional initial carry value. - * - * @return mixed The carry value of the final iteration, or the initial - * value if the set was empty. - */ - public function reduce(callable $callback, $initial = null) - { - $carry = $initial; - - foreach ($this as $value) { - $carry = $callback($carry, $value); - } - - return $carry; - } - - /** - * Removes zero or more values from the set. - * - * @param mixed ...$values - */ - public function remove(...$values) - { - foreach ($values as $value) { - $this->table->remove($value, null); - } - } - - /** - * Reverses the set in-place. - */ - public function reverse() - { - $this->table->reverse(); - } - - /** - * Returns a reversed copy of the set. - * - * @return Set - */ - public function reversed(): Set - { - $reversed = $this->copy(); - $reversed->table->reverse(); - - return $reversed; - } - - /** - * Returns a subset of a given length starting at a specified offset. - * - * @param int $offset If the offset is non-negative, the set will start - * at that offset in the set. If offset is negative, - * the set will start that far from the end. - * - * @param int $length If a length is given and is positive, the resulting - * set will have up to that many values in it. - * If the requested length results in an overflow, only - * values up to the end of the set will be included. - * - * If a length is given and is negative, the set - * will stop that many values from the end. - * - * If a length is not provided, the resulting set - * will contains all values between the offset and the - * end of the set. - * - * @return Set - */ - public function slice(int $offset, int $length = null): Set - { - $sliced = new self(); - $sliced->table = $this->table->slice($offset, $length); - - return $sliced; - } - - /** - * Sorts the set in-place, based on an optional callable comparator. - * - * @param callable|null $comparator Accepts two values to be compared. - * Should return the result of a <=> b. - */ - public function sort(callable $comparator = null) - { - $this->table->ksort($comparator); - } - - /** - * Returns a sorted copy of the set, based on an optional callable - * comparator. Natural ordering will be used if a comparator is not given. - * - * @param callable|null $comparator Accepts two values to be compared. - * Should return the result of a <=> b. - * - * @return Set - */ - public function sorted(callable $comparator = null): Set - { - $sorted = $this->copy(); - $sorted->table->ksort($comparator); - - return $sorted; - } - - /** - * Returns the result of adding all given values to the set. - * - * @param array|\Traversable $values - * - * @return \Ds\Set - */ - public function merge($values): Set - { - $merged = $this->copy(); - - foreach ($values as $value) { - $merged->add($value); - } - - return $merged; - } - - /** - * @inheritDoc - */ - public function toArray(): array - { - return iterator_to_array($this); - } - - /** - * Returns the sum of all values in the set. - * - * @return int|float The sum of all the values in the set. - */ - public function sum() - { - return array_sum($this->toArray()); - } - - /** - * Creates a new set that contains the values of this set as well as the - * values of another set. - * - * Formally: A ∪ B = {x: x ∈ A ∨ x ∈ B} - * - * @param Set $set - * - * @return Set - */ - public function union(Set $set): Set - { - $union = new self(); - - foreach ($this as $value) { - $union->add($value); - } - - foreach ($set as $value) { - $union->add($value); - } - - return $union; - } - - /** - * Get iterator - */ - public function getIterator() - { - foreach ($this->table as $key => $value) { - yield $key; - } - } - - /** - * @inheritdoc - * - * @throws OutOfBoundsException - */ - public function offsetSet($offset, $value) - { - if ($offset === null) { - $this->add($value); - return; - } - throw new Error(); - } - - /** - * @inheritdoc - */ - public function offsetGet($offset) - { - return $this->table->skip($offset)->key; - } - - /** - * @inheritdoc - * - * @throws Error - */ - public function offsetExists($offset) - { - throw new Error(); - } - - /** - * @inheritdoc - * - * @throws Error - */ - public function offsetUnset($offset) - { - throw new Error(); - } - - /** - * Ensures that the internal table will be cloned too. - */ - public function __clone() - { - $this->table = clone $this->table; - } -} diff --git a/php-ds/php-ds/src/Stack.php b/php-ds/php-ds/src/Stack.php deleted file mode 100644 index 18fdb873f..000000000 --- a/php-ds/php-ds/src/Stack.php +++ /dev/null @@ -1,184 +0,0 @@ -vector = new Vector($values ?: []); - } - - /** - * Clear all elements in the Stack - */ - public function clear() - { - $this->vector->clear(); - } - - /** - * @inheritdoc - */ - public function copy(): self - { - return new self($this->vector); - } - - /** - * Returns the number of elements in the Stack - * - * @return int - */ - public function count(): int - { - return count($this->vector); - } - - /** - * Ensures that enough memory is allocated for a specified capacity. This - * potentially reduces the number of reallocations as the size increases. - * - * @param int $capacity The number of values for which capacity should be - * allocated. Capacity will stay the same if this value - * is less than or equal to the current capacity. - */ - public function allocate(int $capacity) - { - $this->vector->allocate($capacity); - } - - /** - * Returns the current capacity of the stack. - * - * @return int - */ - public function capacity(): int - { - return $this->vector->capacity(); - } - - /** - * Returns the value at the top of the stack without removing it. - * - * @return mixed - * - * @throws \UnderflowException if the stack is empty. - */ - public function peek() - { - return $this->vector->last(); - } - - /** - * Returns and removes the value at the top of the stack. - * - * @return mixed - * - * @throws \UnderflowException if the stack is empty. - */ - public function pop() - { - return $this->vector->pop(); - } - - /** - * Pushes zero or more values onto the top of the stack. - * - * @param mixed ...$values - */ - public function push(...$values) - { - $this->vector->push(...$values); - } - - /** - * @inheritDoc - */ - public function toArray(): array - { - return array_reverse($this->vector->toArray()); - } - - /** - * - */ - public function getIterator() - { - while ( ! $this->isEmpty()) { - yield $this->pop(); - } - } - - /** - * @inheritdoc - * - * @throws OutOfBoundsException - */ - public function offsetSet($offset, $value) - { - if ($offset === null) { - $this->push($value); - } else { - throw new Error(); - } - } - - /** - * @inheritdoc - * - * @throws Error - */ - public function offsetGet($offset) - { - throw new Error(); - } - - /** - * @inheritdoc - * - * @throws Error - */ - public function offsetUnset($offset) - { - throw new Error(); - } - - /** - * @inheritdoc - * - * @throws Error - */ - public function offsetExists($offset) - { - throw new Error(); - } - - /** - * Ensures that the internal vector will be cloned too. - */ - public function __clone() - { - $this->vector = clone $this->vector; - } -} diff --git a/php-ds/php-ds/src/Traits/Capacity.php b/php-ds/php-ds/src/Traits/Capacity.php deleted file mode 100644 index 690801a67..000000000 --- a/php-ds/php-ds/src/Traits/Capacity.php +++ /dev/null @@ -1,132 +0,0 @@ -capacity; - } - - /** - * Ensures that enough memory is allocated for a specified capacity. This - * potentially reduces the number of reallocations as the size increases. - * - * @param int $capacity The number of values for which capacity should be - * allocated. Capacity will stay the same if this value - * is less than or equal to the current capacity. - */ - public function allocate(int $capacity) - { - $this->capacity = max($capacity, $this->capacity); - } - - /** - * @return float the structures growth factor. - */ - protected function getGrowthFactor(): float - { - return 2; - } - - /** - * @return float to multiply by when decreasing capacity. - */ - protected function getDecayFactor(): float - { - return 0.5; - } - - /** - * @return float the ratio between size and capacity when capacity should be - * decreased. - */ - protected function getTruncateThreshold(): float - { - return 0.25; - } - - /** - * Checks and adjusts capacity if required. - */ - protected function checkCapacity() - { - if ($this->shouldIncreaseCapacity()) { - $this->increaseCapacity(); - } else { - if ($this->shouldDecreaseCapacity()) { - $this->decreaseCapacity(); - } - } - } - - /** - * @param int $total - */ - protected function ensureCapacity(int $total) - { - if ($total > $this->capacity()) { - $this->capacity = max($total, $this->nextCapacity()); - } - } - - /** - * @return bool whether capacity should be increased. - */ - protected function shouldIncreaseCapacity(): bool - { - return $this->count() >= $this->capacity(); - } - - protected function nextCapacity(): int - { - return $this->capacity() * $this->getGrowthFactor(); - } - - /** - * Called when capacity should be increased to accommodate new values. - */ - protected function increaseCapacity() - { - $this->capacity = max( - $this->count(), - $this->nextCapacity() - ); - } - - /** - * Called when capacity should be decrease if it drops below a threshold. - */ - protected function decreaseCapacity() - { - $this->capacity = max( - self::MIN_CAPACITY, - $this->capacity() * $this->getDecayFactor() - ); - } - - /** - * @return bool whether capacity should be increased. - */ - protected function shouldDecreaseCapacity(): bool - { - return count($this) <= $this->capacity() * $this->getTruncateThreshold(); - } -} diff --git a/php-ds/php-ds/src/Traits/GenericCollection.php b/php-ds/php-ds/src/Traits/GenericCollection.php deleted file mode 100644 index 3b08d8709..000000000 --- a/php-ds/php-ds/src/Traits/GenericCollection.php +++ /dev/null @@ -1,74 +0,0 @@ -toArray(); - } - - /** - * Creates a shallow copy of the collection. - * - * @return static a shallow copy of the collection. - */ - public function copy(): self - { - return new static($this); - } - - /** - * Returns an array representation of the collection. - * - * The format of the returned array is implementation-dependent. Some - * implementations may throw an exception if an array representation - * could not be created (for example when object are used as keys). - * - * @return array - */ - abstract public function toArray(): array; - - /** - * Invoked when calling var_dump. - * - * @return array - */ - public function __debugInfo() - { - return $this->toArray(); - } - - /** - * Returns a string representation of the collection, which is invoked when - * the collection is converted to a string. - */ - public function __toString() - { - return 'object(' . get_class($this) . ')'; - } -} diff --git a/php-ds/php-ds/src/Traits/GenericSequence.php b/php-ds/php-ds/src/Traits/GenericSequence.php deleted file mode 100644 index 3705fa804..000000000 --- a/php-ds/php-ds/src/Traits/GenericSequence.php +++ /dev/null @@ -1,403 +0,0 @@ -push(...$values); - } - } - - /** - * @inheritdoc - */ - public function toArray(): array - { - return $this->array; - } - - /** - * @inheritdoc - */ - public function apply(callable $callback) - { - foreach ($this->array as &$value) { - $value = $callback($value); - } - } - - /** - * @inheritdoc - */ - public function merge($values): Sequence - { - $copy = $this->copy(); - $copy->push(...$values); - return $copy; - } - - /** - * @inheritdoc - */ - public function count(): int - { - return count($this->array); - } - - /** - * @inheritDoc - */ - public function contains(...$values): bool - { - foreach ($values as $value) { - if ($this->find($value) === false) { - return false; - } - } - - return true; - } - - /** - * @inheritDoc - */ - public function filter(callable $callback = null): Sequence - { - return new self(array_filter($this->array, $callback ?: 'boolval')); - } - - /** - * @inheritDoc - */ - public function find($value) - { - return array_search($value, $this->array, true); - } - - /** - * @inheritDoc - */ - public function first() - { - if ($this->isEmpty()) { - throw new UnderflowException(); - } - - return $this->array[0]; - } - - /** - * @inheritDoc - */ - public function get(int $index) - { - if ( ! $this->validIndex($index)) { - throw new OutOfRangeException(); - } - - return $this->array[$index]; - } - - /** - * @inheritDoc - */ - public function insert(int $index, ...$values) - { - if ( ! $this->validIndex($index) && $index !== count($this)) { - throw new OutOfRangeException(); - } - - array_splice($this->array, $index, 0, $values); - $this->checkCapacity(); - } - - /** - * @inheritDoc - */ - public function join(string $glue = null): string - { - return implode($glue, $this->array); - } - - /** - * @inheritDoc - */ - public function last() - { - if ($this->isEmpty()) { - throw new UnderflowException(); - } - - return $this->array[count($this) - 1]; - } - - /** - * @inheritDoc - */ - public function map(callable $callback): Sequence - { - return new self(array_map($callback, $this->array)); - } - - /** - * @inheritDoc - */ - public function pop() - { - if ($this->isEmpty()) { - throw new UnderflowException(); - } - - $value = array_pop($this->array); - $this->checkCapacity(); - - return $value; - } - - /** - * @inheritDoc - */ - public function push(...$values) - { - $this->ensureCapacity($this->count() + count($values)); - - foreach ($values as $value) { - $this->array[] = $value; - } - } - - /** - * @inheritDoc - */ - public function reduce(callable $callback, $initial = null) - { - return array_reduce($this->array, $callback, $initial); - } - - /** - * @inheritDoc - */ - public function remove(int $index) - { - if ( ! $this->validIndex($index)) { - throw new OutOfRangeException(); - } - - $value = array_splice($this->array, $index, 1, null)[0]; - $this->checkCapacity(); - - return $value; - } - - /** - * @inheritDoc - */ - public function reverse() - { - $this->array = array_reverse($this->array); - } - - /** - * @inheritDoc - */ - public function reversed(): Sequence - { - return new self(array_reverse($this->array)); - } - - /** - * Converts negative or large rotations into the minimum positive number - * of rotations required to rotate the sequence by a given $r. - */ - private function normalizeRotations(int $r) - { - $n = count($this); - - if ($n < 2) return 0; - if ($r < 0) return $n - (abs($r) % $n); - - return $r % $n; - } - - /** - * @inheritDoc - */ - public function rotate(int $rotations) - { - for ($r = $this->normalizeRotations($rotations); $r > 0; $r--) { - array_push($this->array, array_shift($this->array)); - } - } - - /** - * @inheritDoc - */ - public function set(int $index, $value) - { - if ( ! $this->validIndex($index)) { - throw new OutOfRangeException(); - } - - $this->array[$index] = $value; - } - - /** - * @inheritDoc - */ - public function shift() - { - if ($this->isEmpty()) { - throw new UnderflowException(); - } - - $value = array_shift($this->array); - $this->checkCapacity(); - - return $value; - } - - /** - * @inheritDoc - */ - public function slice(int $offset, int $length = null): Sequence - { - if (func_num_args() === 1) { - $length = count($this); - } - - return new self(array_slice($this->array, $offset, $length)); - } - - /** - * @inheritDoc - */ - public function sort(callable $comparator = null) - { - if ($comparator) { - usort($this->array, $comparator); - } else { - sort($this->array); - } - } - - /** - * @inheritDoc - */ - public function sorted(callable $comparator = null): Sequence - { - $copy = $this->copy(); - $copy->sort($comparator); - return $copy; - } - - /** - * @inheritDoc - */ - public function sum() - { - return array_sum($this->array); - } - - /** - * @inheritDoc - */ - public function unshift(...$values) - { - if ($values) { - $this->array = array_merge($values, $this->array); - $this->checkCapacity(); - } - } - - /** - * - */ - private function validIndex(int $index) - { - return $index >= 0 && $index < count($this); - } - - /** - * - */ - public function getIterator() - { - foreach ($this->array as $value) { - yield $value; - } - } - - /** - * @inheritdoc - */ - public function clear() - { - $this->array = []; - $this->capacity = self::MIN_CAPACITY; - } - - /** - * @inheritdoc - */ - public function offsetSet($offset, $value) - { - if ($offset === null) { - $this->push($value); - } else { - $this->set($offset, $value); - } - } - - /** - * @inheritdoc - */ - public function &offsetGet($offset) - { - if ( ! $this->validIndex($offset)) { - throw new OutOfRangeException(); - } - - return $this->array[$offset]; - } - - /** - * @inheritdoc - */ - public function offsetUnset($offset) - { - if (is_integer($offset) && $this->validIndex($offset)) { - $this->remove($offset); - } - } - - /** - * @inheritdoc - */ - public function offsetExists($offset) - { - return is_integer($offset) - && $this->validIndex($offset) - && $this->get($offset) !== null; - } -} diff --git a/php-ds/php-ds/src/Traits/SquaredCapacity.php b/php-ds/php-ds/src/Traits/SquaredCapacity.php deleted file mode 100644 index 01bd25aac..000000000 --- a/php-ds/php-ds/src/Traits/SquaredCapacity.php +++ /dev/null @@ -1,58 +0,0 @@ -capacity = max($this->square($capacity), $this->capacity); - } - - /** - * Called when capacity should be increased to accommodate new values. - */ - protected function increaseCapacity() - { - $this->capacity = $this->square( - max( - count($this) + 1, - $this->capacity * $this->getGrowthFactor() - ) - ); - } - - /** - * @param int $total - */ - protected function ensureCapacity(int $total) - { - while ($total > $this->capacity()) { - $this->increaseCapacity(); - } - } -} diff --git a/php-ds/php-ds/src/Vector.php b/php-ds/php-ds/src/Vector.php deleted file mode 100644 index 3821658a7..000000000 --- a/php-ds/php-ds/src/Vector.php +++ /dev/null @@ -1,32 +0,0 @@ - $this->capacity; - } -}