# HG changeset patch # User Nathan Phillip Brink # Date 2010-10-18 19:38:35 # Node ID 54bfd4a095e6d4ab08aabe1e0fa3090486eb9be4 # Parent 21a52190eb20908de2e44046993ead1dcbb655f5 Move the conflict checking functionality into the Section class (no functional change though -- if this change causes different permutation results, I did something wrong). diff --git a/class.schedule.php b/class.schedule.php --- a/class.schedule.php +++ b/class.schedule.php @@ -132,29 +132,15 @@ class Schedule for ($downCounter = $this->nclasses - 1; $downCounter > $upCounter && !$conflict; $downCounter --) { - $start1 = $this->classStorage[$upCounter]->getSection($cs[$upCounter])->getStartTime(); - $end1 = $this->classStorage[$upCounter]->getSection($cs[$upCounter])->getEndTime(); - $start2 = $this->classStorage[$downCounter]->getSection($cs[$downCounter])->getStartTime(); - $end2 = $this->classStorage[$downCounter]->getSection($cs[$downCounter])->getEndTime(); - - // If the two times overlap, then check if days overlap. - if ((($start1 >= $start2) && ($start1 <= $end2)) || (($start2 >= $start1) && ($start2 <= $end1))) - { - - for($day=0; $day<5; $day++) - { - if(!$conflict - && ($this->classStorage[$upCounter]->getSection($cs[$upCounter])->getDay($day) - && $this->classStorage[$downCounter]->getSection($cs[$downCounter])->getDay($day))) - { - $conflict = TRUE; - } - } - + if ($this->classStorage[$upCounter]->getSection($cs[$upCounter]) + ->conflictsWith($this->classStorage[$downCounter]->getSection($cs[$downCounter]))) + { + $conflict = TRUE; + break; + } } } - } - + // Store to storage if no conflict is found. if(!$conflict) { diff --git a/class.section.php b/class.section.php --- a/class.section.php +++ b/class.section.php @@ -199,6 +199,45 @@ class Section { return $this->bdays[$i]; } + + /** + * \brief + * Check if this section conflicts withthe given section. + * + * \param $that + * The other section for which I should check for conflicts. + * \return + * TRUE if there is a conflict, FALSE otherwise. + */ + public function conflictsWith(Section $that) + { + /* + * The two sections can't conflict if the start/end times don't + * overlap. Also, use >= or <= here so that one can say ``I have + * gym from 10 through 11 and then latin from 11 though 12''. + */ + if ($this->getStartTime() >= $that->getEndTime() + || $this->getEndTime() <= $that->getStartTime()) + { + return FALSE; + } + + /* + * Now we know that the sections overlap in start/end times. But + * if they don't both meet on the same day at least once, they + * don't conflict. + */ + for ($day = 0; $day < 5; $day ++) + { + if ($this->getDay($day) && $that->getDay($day)) + return TRUE; + } + + /* + * The sections don't both share a day of the week. + */ + return FALSE; + } /** * \brief