Skip to content

Commit

Permalink
Refactor and test
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Sep 2, 2024
1 parent 060b18d commit 0d6687c
Show file tree
Hide file tree
Showing 9 changed files with 1,367 additions and 188 deletions.
1 change: 1 addition & 0 deletions infection.json5.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"source": {
"directories": [
"src/ChangeDetector",
"src/DataStructure",
"src/Type",
"src/TypedMap",
]
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<file>tests/Reflection/Internal/NativeAdapter/AdapterCompatibilityTest.php</file>
</testsuite>

<testsuite name="DataStructure">
<directory>tests/DataStructure</directory>
</testsuite>

<testsuite name="infection">
<directory>tests/ChangeDetector</directory>
<file>tests/Reflection/Internal/Inheritance/TypeResolversTest.php</file>
Expand Down
140 changes: 12 additions & 128 deletions src/DataStructure/Internal/ArrayMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Typhoon\DataStructure\KVPair;
use Typhoon\DataStructure\MutableMap;
use Typhoon\DataStructure\Sequence;

/**
* @internal
Expand All @@ -17,76 +16,10 @@
*/
final class ArrayMap extends MutableMap
{
/**
* @template NK
* @template NV
* @param iterable<NK, NV>|\Closure(): iterable<NK, NV> $values
* @return self<NK, NV>
*/
public static function of(iterable|\Closure $values = []): self
{
/** @var self<NK, NV> */
$map = new self();
$map->putAll($values);

return $map;
}

/**
* @template NK
* @template NV
* @param KVPair<NK, NV> ...$kvPairs
* @return self<NK, NV>
*/
public static function fromPairs(KVPair ...$kvPairs): self
{
/** @var self<NK, NV> */
$map = new self();
$map->putPairs(...$kvPairs);

return $map;
}

/**
* @template NK
* @template NV
* @param iterable<NK> $keys
* @param callable(NK): NV $value
* @return self<NK, NV>
*/
public static function fromKeys(iterable $keys, callable $value): self
{
$map = new self();

foreach ($keys as $key) {
$map->put($key, $value($key));
}

return $map;
}

/**
* @template NK
* @template NV
* @param iterable<NV> $values
* @param callable(NV): NK $key
* @return self<NK, NV>
*/
public static function fromValues(iterable $values, callable $key): self
{
$map = new self();

foreach ($values as $value) {
$map->put($key($value), $value);
}

return $map;
}

/**
* @param array<KVPair<K, V>> $kvPairs
*/
private function __construct(
public function __construct(
private array $kvPairs = [],
) {}

Expand All @@ -110,14 +43,10 @@ public function putPairs(KVPair ...$kvPairs): void
}
}

public function putAll(iterable|\Closure $values): void
protected function doPutAll(iterable $values): void
{
if ($values instanceof \Closure) {
$values = $values();
}

if ($values instanceof self) {
$this->kvPairs = [...$this->kvPairs, ...$values->kvPairs];
$this->kvPairs = array_replace($this->kvPairs, $values->kvPairs);

return;
}
Expand Down Expand Up @@ -207,7 +136,7 @@ public function reduceKV(callable $operation): mixed
* @param V|R $accumulator
* @param KVPair<K,V> $kv
*/
static fn (mixed $accumulator, KVPair $kv): mixed => $operation($accumulator, $kv->key, $kv->value),
static fn(mixed $accumulator, KVPair $kv): mixed => $operation($accumulator, $kv->key, $kv->value),
$initial->value,
);
}
Expand All @@ -227,47 +156,22 @@ public function foldKV(mixed $initial, callable $operation): mixed
* @param I|R $accumulator
* @param KVPair<K,V> $kv
*/
static fn (mixed $accumulator, KVPair $kv): mixed => $operation($accumulator, $kv->key, $kv->value),
static fn(mixed $accumulator, KVPair $kv): mixed => $operation($accumulator, $kv->key, $kv->value),
$initial,
);
}

public function filterKV(callable $predicate): static
{
return new self(array_filter($this->kvPairs, static fn (KVPair $kv): bool => $predicate($kv->key, $kv->value)));
return new self(array_filter($this->kvPairs, static fn(KVPair $kv): bool => $predicate($kv->key, $kv->value)));
}

public function mapKV(callable $mapper): static
{
return new self(array_map(static fn (KVPair $kv): KVPair => $kv->withValue($mapper($kv->key, $kv->value)), $this->kvPairs));
}

public function reindexKV(callable $mapper): static
{
$map = new self();

foreach ($this->kvPairs as $kvPair) {
$key = $mapper($kvPair->key, $kvPair->value);
$map->kvPairs[ArrayMapKeyEncoder::encode($key)] = $kvPair->withKey($key);
}

return $map;
}

public function flip(): static
{
$map = new self();

foreach ($this->kvPairs as $kvPair) {
$map->kvPairs[ArrayMapKeyEncoder::encode($kvPair->value)] = $kvPair->flip();
}

return $map;
}

public function reverse(): static
{
return new self(array_reverse($this->kvPairs, preserve_keys: true));
return new self(array_map(
static fn(KVPair $kv): KVPair => $kv->withValue($mapper($kv->key, $kv->value)),
$this->kvPairs,
));
}

public function usortKV(callable $comparator): static
Expand All @@ -279,35 +183,15 @@ public function usortKV(callable $comparator): static
* @param KVPair<K, V> $kv1
* @param KVPair<K, V> $kv2
*/
static fn (KVPair $kv1, KVPair $kv2) => $comparator($kv1->key, $kv1->value, $kv2->key, $kv2->value)
static fn(KVPair $kv1, KVPair $kv2) => $comparator($kv1->key, $kv1->value, $kv2->key, $kv2->value),
);

return new self($kvPairs);
}

public function slice(int $offset, ?int $length = null): static
{
return new self(\array_slice($this->kvPairs, $offset, $length));
}

public function keys(): Sequence
{
throw new \LogicException('TODO');
}

public function values(): Sequence
{
throw new \LogicException('TODO');
}

public function pairs(): Sequence
{
throw new \LogicException('TODO');
}

public function toArray(): array
{
return iterator_to_array($this->getIterator());
return new self(\array_slice($this->kvPairs, $offset, $length, preserve_keys: true));
}

/**
Expand Down
Loading

0 comments on commit 0d6687c

Please sign in to comment.