<commit_before><?php

namespace Psets;

class Interval
{
    protected $_start;
    protected $_end;

    public function __construct(\DateTime $start, \DateTime $end)
    {
        if($end < $start) {
            list($start, $end) = [$end, $start];
        }

        $this->_start = $start;
        $this->_end = $end;

    }

    public function getStart()
    {
        return $this->_start;
    }

    public function getEnd()
    {
        return $this->_end;
    }

    public function getPeriod()
    {
        return $this->_end->format('U') - $this->_start->format('U');
    }

    public function overlaps(Interval $other)
    {
        if($this->getEnd() <= $other->getStart() OR $other->getEnd() <= $this->getStart()) {
            return false;
        } else {
            return $this->getEnd() > $other->getStart() OR $other->getEnd() > $this->getStart();
        }
    }

    public function diff(Interval $other)
    {
        if(!$this->overlaps($other)) {
            return clone($this);
        }

        $thisStart = clone($this->getStart());
        $thisEnd = clone($this->getEnd());
        $otherStart = clone($other->getStart());
        $otherEnd = clone($other->getEnd());

        if($thisStart < $otherStart AND $thisEnd > $otherEnd) {
            return new IntervalSet([
                new Interval($thisStart, $otherStart),
                new Interval($otherEnd, $thisEnd),
            ]);
        }

        if($thisStart >= $otherStart AND $thisEnd <= $otherEnd) {
            return new IntervalSet;
        }

        if($thisStart <= $otherStart) {
            return new Interval($thisStart, $otherStart);
        } else {
            return new Interval($otherEnd, $thisEnd);
        }

    }

    public function union(Interval $other)
    {
        $thisStart = clone($this->getStart());
        $thisEnd = clone($this->getEnd());
        $otherStart = clone($other->getStart());
        $otherEnd = clone($other->getEnd());

        if($this->overlaps($other) OR $this->isAdjacent($other)) {
            $start = $thisStart < $otherStart? $thisStart: $otherStart;
            $end = $thisEnd > $otherEnd? $thisEnd: $otherEnd;
            return new Interval($start, $end);
        }

        if($thisEnd < $otherStart) {
            return new IntervalSet([
                new Interval($thisStart, $thisEnd),
                new Interval($otherStart, $otherEnd),
            ]);
        } else {
            return new IntervalSet([
                new Interval($otherStart, $otherEnd),
                new Interval($thisStart, $thisEnd),
            ]);            
        }
    }

    public function isAdjacent(Interval $other)
    {
        $thisStart = $this->getStart();
        $thisEnd = $this->getEnd();
        $otherStart = $other->getStart();
        $otherEnd = $other->getEnd();

        if($this->overlaps($other)) {
            return false;
        } else if($thisStart == $otherEnd OR $otherStart == $thisEnd) {
            return true;
        }

        return false;
    }

    public function comesBefore(Interval $other)
    {
        $thisStart = $this->getStart();
        $thisEnd = $this->getEnd();
        $otherStart = $other->getStart();
        $otherEnd = $other->getEnd();

        if($this->isAdjacent($other) AND $thisStart < $otherStart) {
            return true;
        } else if($this->overlaps($other)) {
            return false;
        } else if($thisStart > $otherEnd OR $otherStart > $thisEnd) {
            return true;
        }

        return false;
    }

    public function intersect(Interval $other)
    {
        $thisStart = $this->getStart();
        $thisEnd = $this->getEnd();
        $otherStart = $other->getStart();
        $otherEnd = $other->getEnd();

        if(!$this->overlaps($other)) {
            return false;
        }

        if($thisEnd > $otherEnd AND $thisStart < $otherStart) {
            // this contains other
            return clone($other);
        } else if($otherEnd > $thisEnd AND $otherStart < $thisStart) {
            // other contains this
            return clone($this);
        }

        if($thisStart < $otherStart) {
            $intersectionStart = $otherStart;
            $intersectionEnd = $thisEnd;
        } else {
            $intersectionStart = $thisStart;
            $intersectionEnd = $otherEnd;
        }

        return new Interval($intersectionStart, $intersectionEnd);
    }
}<commit_msg>refactor Interval class to avoid duplication<commit_after><?php

namespace Psets;

class Interval
{
    protected $_start;
    protected $_end;

    public function __construct(\DateTime $start, \DateTime $end)
    {
        if($end < $start) {
            list($start, $end) = [$end, $start];
        }

        $this->_start = $start;
        $this->_end = $end;

    }

    public function getStart()
    {
        return $this->_start;
    }

    public function getEnd()
    {
        return $this->_end;
    }

    public function getPeriod()
    {
        return $this->_end->format('U') - $this->_start->format('U');
    }

    public function overlaps(Interval $other)
    {
        if($this->getEnd() <= $other->getStart() OR $other->getEnd() <= $this->getStart()) {
            return false;
        } else {
            return $this->getEnd() > $other->getStart() OR $other->getEnd() > $this->getStart();
        }
    }

    public function diff(Interval $other)
    {
        if(!$this->overlaps($other)) {
            return clone($this);
        }

        list($thisStart, $thisEnd, $otherStart, $otherEnd) =
            $this->_listStartEnd($this, $other);

        if($thisStart < $otherStart AND $thisEnd > $otherEnd) {
            return new IntervalSet([
                new Interval($thisStart, $otherStart),
                new Interval($otherEnd, $thisEnd),
            ]);
        }

        if($thisStart >= $otherStart AND $thisEnd <= $otherEnd) {
            return new IntervalSet;
        }

        if($thisStart <= $otherStart) {
            return new Interval($thisStart, $otherStart);
        } else {
            return new Interval($otherEnd, $thisEnd);
        }

    }

    public function union(Interval $other)
    {
        list($thisStart, $thisEnd, $otherStart, $otherEnd) =
            $this->_listStartEnd($this, $other);

        if($this->overlaps($other) OR $this->isAdjacent($other)) {
            $start = $thisStart < $otherStart? $thisStart: $otherStart;
            $end = $thisEnd > $otherEnd? $thisEnd: $otherEnd;
            return new Interval($start, $end);
        }

        if($thisEnd < $otherStart) {
            return new IntervalSet([
                new Interval($thisStart, $thisEnd),
                new Interval($otherStart, $otherEnd),
            ]);
        } else {
            return new IntervalSet([
                new Interval($otherStart, $otherEnd),
                new Interval($thisStart, $thisEnd),
            ]);            
        }
    }

    public function isAdjacent(Interval $other)
    {
        list($thisStart, $thisEnd, $otherStart, $otherEnd) =
            $this->_listStartEnd($this, $other);

        if($this->overlaps($other)) {
            return false;
        } else if($thisStart == $otherEnd OR $otherStart == $thisEnd) {
            return true;
        }

        return false;
    }

    public function comesBefore(Interval $other)
    {
        list($thisStart, $thisEnd, $otherStart, $otherEnd) =
            $this->_listStartEnd($this, $other);

        if($this->isAdjacent($other) AND $thisStart < $otherStart) {
            return true;
        } else if($this->overlaps($other)) {
            return false;
        } else if($thisStart > $otherEnd OR $otherStart > $thisEnd) {
            return true;
        }

        return false;
    }

    public function intersect(Interval $other)
    {
        list($thisStart, $thisEnd, $otherStart, $otherEnd) =
            $this->_listStartEnd($this, $other);

        if(!$this->overlaps($other)) {
            return false;
        }

        if($thisEnd > $otherEnd AND $thisStart < $otherStart) {
            // this contains other
            return clone($other);
        } else if($otherEnd > $thisEnd AND $otherStart < $thisStart) {
            // other contains this
            return clone($this);
        }

        if($thisStart < $otherStart) {
            $intersectionStart = $otherStart;
            $intersectionEnd = $thisEnd;
        } else {
            $intersectionStart = $thisStart;
            $intersectionEnd = $otherEnd;
        }

        return new Interval($intersectionStart, $intersectionEnd);
    }

    protected function _listStartEnd(Interval $interval1, Interval $interval2)
    {
        $interval1Start = clone($interval1->getStart());
        $interval1End = clone($interval1->getEnd());
        $interval2Start = clone($interval2->getStart());
        $interval2End = clone($interval2->getEnd());

        return [$interval1Start, $interval1End, $interval2Start, $interval2End];
    }
}
{'fraction_non_alphanumeric': 0.14123107525693446, 'fraction_numerical': 0.0015471322798099238, 'mean_word_length': 1.9165323880116016, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 96, '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': '4395156', 'n_tokens_mistral': 2623, 'n_tokens_neox': 2547, 'n_words': 544}