<commit_before><?php

namespace Palmtree\Collection;

use Palmtree\Collection\Exception\InvalidIndex;
use Palmtree\Collection\Exception\OutOfBoundsException;
use Palmtree\Collection\Validator\TypeValidator;

/**
 * @template TKey of array-key
 * @template T
 * @implements CollectionInterface<TKey,T>
 */
abstract class AbstractCollection implements CollectionInterface
{
    public TypeValidator $validator;

    /**
     * @var array<string|int, mixed>
     * @psalm-var array<TKey, T>
     */
    protected array $elements = [];
    /** @var array<string, Index> */
    protected array $indexes = [];

    final public function __construct(?string $type = null)
    {
        $this->validator = new TypeValidator($type);
    }

    /**
     * {@inheritDoc}
     */
    public function get($key)
    {
        if (!$this->containsKey($key)) {
            throw new OutOfBoundsException("Element with key '$key' does not exist");
        }

        return $this->elements[$key];
    }

    /**
     * {@inheritDoc}
     */
    public function contains($element, bool $strict = true): bool
    {
        return \in_array($element, $this->elements, $strict);
    }

    /**
     * {@inheritDoc}
     */
    public function containsKey($key): bool
    {
        return isset($this->elements[$key]) || \array_key_exists($key, $this->elements);
    }

    /**
     * {@inheritDoc}
     */
    public function remove($key): CollectionInterface
    {
        foreach ($this->indexes as $index) {
            $index->remove((string)$key);
        }

        unset($this->elements[$key]);

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function removeElement($element): CollectionInterface
    {
        $key = array_search($element, $this->elements, true);

        if ($key !== false) {
            $this->remove($key);
        }

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function keys(): CollectionInterface
    {
        return Sequence::fromArray(array_keys($this->elements));
    }

    /**
     * {@inheritDoc}
     */
    public function values(): CollectionInterface
    {
        return Sequence::fromArray(array_values($this->elements), $this->validator->getType());
    }

    /**
     * {@inheritDoc}
     */
    public function clear(): CollectionInterface
    {
        foreach ($this->indexes as $index) {
            $index->clear();
        }

        $this->elements = [];

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function first()
    {
        foreach ($this->elements as $element) {
            return $element;
        }

        return null;
    }

    /**
     * {@inheritDoc}
     */
    public function last()
    {
        foreach (\array_slice($this->elements, -1) as $element) {
            return $element;
        }

        return null;
    }

    /**
     * {@inheritDoc}
     */
    public function firstKey()
    {
        return array_key_first($this->elements);
    }

    /**
     * {@inheritDoc}
     */
    public function lastKey()
    {
        return array_key_last($this->elements);
    }

    /**
     * Returns the total number of elements in the collection.
     */
    public function count(): int
    {<commit_msg>Update AbstractCollection::last() to use array_key_last as it's a lot faster than using array_slice<commit_after><?php

namespace Palmtree\Collection;

use Palmtree\Collection\Exception\InvalidIndex;
use Palmtree\Collection\Exception\OutOfBoundsException;
use Palmtree\Collection\Validator\TypeValidator;

/**
 * @template TKey of array-key
 * @template T
 * @implements CollectionInterface<TKey,T>
 */
abstract class AbstractCollection implements CollectionInterface
{
    public TypeValidator $validator;

    /**
     * @var array<string|int, mixed>
     * @psalm-var array<TKey, T>
     */
    protected array $elements = [];
    /** @var array<string, Index> */
    protected array $indexes = [];

    final public function __construct(?string $type = null)
    {
        $this->validator = new TypeValidator($type);
    }

    /**
     * {@inheritDoc}
     */
    public function get($key)
    {
        if (!$this->containsKey($key)) {
            throw new OutOfBoundsException("Element with key '$key' does not exist");
        }

        return $this->elements[$key];
    }

    /**
     * {@inheritDoc}
     */
    public function contains($element, bool $strict = true): bool
    {
        return \in_array($element, $this->elements, $strict);
    }

    /**
     * {@inheritDoc}
     */
    public function containsKey($key): bool
    {
        return isset($this->elements[$key]) || \array_key_exists($key, $this->elements);
    }

    /**
     * {@inheritDoc}
     */
    public function remove($key): CollectionInterface
    {
        foreach ($this->indexes as $index) {
            $index->remove((string)$key);
        }

        unset($this->elements[$key]);

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function removeElement($element): CollectionInterface
    {
        $key = array_search($element, $this->elements, true);

        if ($key !== false) {
            $this->remove($key);
        }

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function keys(): CollectionInterface
    {
        return Sequence::fromArray(array_keys($this->elements));
    }

    /**
     * {@inheritDoc}
     */
    public function values(): CollectionInterface
    {
        return Sequence::fromArray(array_values($this->elements), $this->validator->getType());
    }

    /**
     * {@inheritDoc}
     */
    public function clear(): CollectionInterface
    {
        foreach ($this->indexes as $index) {
            $index->clear();
        }

        $this->elements = [];

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function first()
    {
        foreach ($this->elements as $element) {
            return $element;
        }

        return null;
    }

    /**
     * {@inheritDoc}
     */
    public function last()
    {
        $lastKey = $this->lastKey();

        return $lastKey === null ? null : $this->elements[$lastKey];
    }

    /**
     * {@inheritDoc}
     */
    public function firstKey()
    {
        return array_key_first($this->elements);
    }

    /**
     * {@inheritDoc}
     */
    public function lastKey()
    {
        return array_key_last($this->elements);
    }

    /**
     * Returns the total number of elements in the collection.
     */
    public function count(): int
    {
{'fraction_non_alphanumeric': 0.14489671931956258, 'fraction_numerical': 0.00015188335358444713, 'mean_word_length': 2.0799812909260993, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5494709', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1848, 'n_words': 396}