<commit_before><?php

namespace Gliph\Visitor;

/**
 * Basic depth-first visitor.
 *
 * This visitor records reachability data for each vertex and creates a
 * topologically sorted list.
 */
class DepthFirstBasicVisitor implements DepthFirstVisitorInterface {

    /**
     * @var \SplObjectStorage
     */
    public $active;

    /**
     * @var \SplObjectStorage
     */
    public $paths;

    /**
     * @var \SplQueue
     */
    public $tsl;

    public function __construct() {
        $this->active = new \SplObjectStorage();
        $this->paths = new \SplObjectStorage();
        $this->tsl = new \SplQueue();
    }

    public function onBackEdge($vertex, \Closure $visit) {
        throw new \RuntimeException(sprintf('Cycle detected in provided graph.'));
    }

    public function onInitializeVertex($vertex, $source, \SplQueue $queue) {
        $this->paths[$vertex] = new \SplQueue();
    }

    public function onStartVertex($vertex, \Closure $visit) {
        $this->active->attach($vertex);
        if (!isset($this->paths[$vertex])) {
            $this->paths[$vertex] = new \SplQueue();
        }
    }

    public function onExamineEdge($from, $to, \Closure $visit) {
        foreach ($this->active as $vertex) {
           $this->paths[$vertex]->enqueue($to);
        }
    }

    public function onFinishVertex($vertex, \Closure $visit) {
        $this->tsl->push($vertex);
    }
}<commit_msg>Switch to using arrays instead of SplQueues in DF basic visitor.<commit_after><?php

namespace Gliph\Visitor;

/**
 * Basic depth-first visitor.
 *
 * This visitor records reachability data for each vertex and creates a
 * topologically sorted list.
 */
class DepthFirstBasicVisitor implements DepthFirstVisitorInterface {

    /**
     * @var \SplObjectStorage
     */
    public $active;

    /**
     * @var \SplObjectStorage
     */
    protected $paths;

    /**
     * @var array
     */
    protected $tsl;

    public function __construct() {
        $this->active = new \SplObjectStorage();
        $this->paths = new \SplObjectStorage();
        $this->tsl = array();
    }

    public function onBackEdge($vertex, \Closure $visit) {
        throw new \RuntimeException(sprintf('Cycle detected in provided graph.'));
    }

    public function onInitializeVertex($vertex, $source, \SplQueue $queue) {
        $this->paths[$vertex] = array();
    }

    public function onStartVertex($vertex, \Closure $visit) {
        $this->active->attach($vertex);
        if (!isset($this->paths[$vertex])) {
            $this->paths[$vertex] = array();
        }
    }

    public function onExamineEdge($from, $to, \Closure $visit) {
        foreach ($this->active as $vertex) {
            // TODO this check makes this much less efficient - find a better algo
            if (!in_array($to, $this->paths[$vertex])) {
                $path = $this->paths[$vertex];
                $path[] = $to;
                $this->paths[$vertex] = $path;
            }
        }
    }

    public function onFinishVertex($vertex, \Closure $visit) {
        $this->tsl[] = $vertex;
    }

    /**
     * Returns valid topological sort of the visited graph as an array.
     *
     * @return array
     */
    public function getTsl() {
        return $this->tsl;
    }

    /**
     * Returns a queue of all vertices reachable from the given vertex.
     *
     * This should only be called after the visitor has been used in a
     * depth-first traversal.
     *
     * @param object $vertex
     *   A vertex present in the graph for
     *
     * @return array
     *
     * @throws \OutOfRangeException
     */
    public function getReachable($vertex) {
        if (!isset($this->paths[$vertex])) {
            throw new \OutOfRangeException('Unknown vertex provided.');
        }

        return $this->paths[$vertex];
    }
}
{'fraction_non_alphanumeric': 0.13284709559781194, 'fraction_numerical': 0.0, 'mean_word_length': 2.2625318606627016, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 32, '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': '3944631', 'n_tokens_mistral': 1120, 'n_tokens_neox': 1074, 'n_words': 292}