<commit_before><?php
namespace Scheb\Tombstone\Analyzer;

use Scheb\Tombstone\Tombstone;

class TombstoneIndex implements \Countable, \Iterator
{
    /**
     * @var string
     */
    private $sourceDir;

    /**
     * @param string $sourceDir
     */
    public function __construct($sourceDir)
    {
        $this->sourceDir = $sourceDir;
    }

    /**
     * @var Tombstone[]
     */
    private $tombstones = array();

    /**
     * @var Tombstone[]
     */
    private $fileLineIndex = array();

    /**
     * @var Tombstone[][]
     */
    private $methodIndex = array();

    /**
     * @param Tombstone $tombstone
     */
    public function addTombstone(Tombstone $tombstone)
    {
        $this->tombstones[] = $tombstone;
        $position = FilePosition::createPosition($tombstone->getFile(), $tombstone->getLine());
        $this->fileLineIndex[$position] = $tombstone;
        $methodName = $tombstone->getMethod();
        if (!isset($this->methodIndex[$methodName])) {
            $this->methodIndex[$methodName] = array();
        }
        $this->methodIndex[$methodName][] = $tombstone;
    }

    /**
     * @param string $method
     * @return Tombstone[]
     */
    public function getInMethod($method)
    {
        if (isset($this->methodIndex[$method])) {
            return $this->methodIndex[$method];
        }

        return null;
    }

    /**
     * @param string $file
     * @param int $line
     * @return Tombstone
     */
    public function getInFileAndLine($file, $line)
    {
        $pos = FilePosition::createPosition($file, $line);
        if (isset($this->fileLineIndex[$pos])) {
            return $this->fileLineIndex[$pos];
        }

        return null;
    }

    /**
     * @return int
     */
    public function count()
    {<commit_msg>Support matching by relative path<commit_after><?php
namespace Scheb\Tombstone\Analyzer;

use Scheb\Tombstone\Tombstone;
use Scheb\Tombstone\Tracing\PathNormalizer;

class TombstoneIndex implements \Countable, \Iterator
{
    /**
     * @var string
     */
    private $sourceDir;

    /**
     * @var Tombstone[]
     */
    private $tombstones = array();

    /**
     * @var Tombstone[]
     */
    private $fileLineIndex = array();

    /**
     * @var Tombstone[]
     */
    private $relativeFileLineIndex = array();

    /**
     * @var Tombstone[][]
     */
    private $methodIndex = array();

    /**
     * @param string $sourceDir
     */
    public function __construct($sourceDir)
    {
        $this->sourceDir = PathNormalizer::normalizeDirectorySeparator($sourceDir);
    }

    /**
     * @param Tombstone $tombstone
     */
    public function addTombstone(Tombstone $tombstone)
    {
        $this->tombstones[] = $tombstone;

        $position = FilePosition::createPosition($tombstone->getFile(), $tombstone->getLine());
        $this->fileLineIndex[$position] = $tombstone;

        $relativePosition = FilePosition::createPosition($this->normalizeAndRelativePath($tombstone->getFile()), $tombstone->getLine());
        $this->relativeFileLineIndex[$relativePosition] = $tombstone;

        $methodName = $tombstone->getMethod();
        if (!isset($this->methodIndex[$methodName])) {
            $this->methodIndex[$methodName] = array();
        }
        $this->methodIndex[$methodName][] = $tombstone;
    }

    /**
     * @param string $method
     * @return Tombstone[]
     */
    public function getInMethod($method)
    {
        if (isset($this->methodIndex[$method])) {
            return $this->methodIndex[$method];
        }

        return null;
    }

    /**
     * @param string $file
     * @param int $line
     * @return Tombstone
     */
    public function getInFileAndLine($file, $line)
    {
        $pos = FilePosition::createPosition($file, $line);
        if (isset($this->fileLineIndex[$pos])) {
            return $this->fileLineIndex[$pos];
        }

        $pos = FilePosition::createPosition($file, $line);
        if (isset($this->relativeFileLineIndex[$pos])) {
            return $this->relativeFileLineIndex[$pos];
        }

        return null;
    }

    /**
     * @param string $path
     *
     * @return string
     */
    private function normalizeAndRelativePath($path)
    {
        $path = PathNormalizer::normalizeDirectorySeparator($path);
        return PathNormalizer::makeRelativeTo($path, $this->sourceDir);
    }

    /**
     * @return int
     */
    public function count()
    {
{'fraction_non_alphanumeric': 0.1491031390134529, 'fraction_numerical': 0.0, 'mean_word_length': 2.3241430700447094, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 36, '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': '1742353', 'n_tokens_mistral': 1355, 'n_tokens_neox': 1327, 'n_words': 234}