Skip to content

Commit

Permalink
Рефакториране на RepeatedTask да наследява SimpleTask
Browse files Browse the repository at this point in the history
Последният бъг: reset() трябва да е преди SimpleTask::work(),
иначе SimpleTask::work си мисли, че задачата вече е приключена
и не инкрементира progress
[понеже current->getProgress() == current->getExecutionTime() и затова
RepeatedTask::getProgress() == RepeatedTask::getExecutionTime() ==
current->progress + SimpleTask::getProgress() * current->getExecutionTime(),
а SimpleTask::getProgress() == SimpleTask::getExecutionTime() - 1]
  • Loading branch information
triffon committed Jun 5, 2019
1 parent 5b9004d commit b5f3e01
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
28 changes: 13 additions & 15 deletions tasks/repeated_task.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#include "repeated_task.h"

RepeatedTask::RepeatedTask(char const* n, Task const& t, unsigned r) :
Task(n), repetitions(r), repetitionProgress(0), current(nullptr) {
SimpleTask(n, r), current(nullptr) {
prototype = (Task*)t.clone();
reset();
}

RepeatedTask::RepeatedTask(RepeatedTask const& rt) : Task(rt) {
RepeatedTask::RepeatedTask(RepeatedTask const& rt) : SimpleTask(rt) {
copy(rt);
}

RepeatedTask& RepeatedTask::operator=(RepeatedTask const& rt) {
if (&rt != this) {
Task::operator=(rt);
SimpleTask::operator=(rt);
destroy();
copy(rt);
}
Expand All @@ -24,8 +24,6 @@ RepeatedTask::~RepeatedTask() {
}

void RepeatedTask::copy(RepeatedTask const& rt) {
repetitions = rt.repetitions;
repetitionProgress = rt.repetitionProgress;
prototype = (Task*)rt.prototype->clone();
current = (Task*)rt.current->clone();
}
Expand All @@ -38,17 +36,17 @@ void RepeatedTask::destroy() {
void RepeatedTask::print(std::ostream& os) const {
os << "Повтаряща се ";
Task::print(os);
os << ", която повтаря " << repetitions << " пъти задачата: (";
os << ", която повтаря " << getRepetitions() << " пъти задачата: (";
current->print(os);
os << ") и вече са изпълнени " << repetitionProgress << " повторения";
os << ") и вече са изпълнени " << getRepetitionProgress() << " повторения";
}

unsigned RepeatedTask::getExecutionTime() const {
return repetitions * prototype->getExecutionTime();
return getRepetitions() * prototype->getExecutionTime();
}

unsigned RepeatedTask::getProgress() const {
return repetitionProgress * prototype->getExecutionTime() +
return getRepetitionProgress() * prototype->getExecutionTime() +
current->getProgress();
}

Expand All @@ -64,19 +62,19 @@ unsigned RepeatedTask::work(unsigned t) {
return t;

// завършили сме текущата задача
repetitionProgress++;
// "изработваме" едно повторение
reset();
SimpleTask::work();
// има ли още задачи и време?
if (isFinished())
if (isFinished() || t == 0)
// не
return t;

// има още задачи за повтаряне
// фаза 2: изпълняваме някакъв брой повторения на current
unsigned spentRepetitions = std::min(t / prototype->getExecutionTime(),
repetitions - repetitionProgress);
repetitionProgress += spentRepetitions;
t -= spentRepetitions * prototype->getExecutionTime();
unsigned leftRepetitions = SimpleTask::work(t / prototype->getExecutionTime());
(t %= prototype->getExecutionTime())
+= leftRepetitions * prototype->getExecutionTime();

// има ли още задачи?
if (isFinished() || t == 0)
Expand Down
10 changes: 6 additions & 4 deletions tasks/repeated_task.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#ifndef __REPEATED_TASK_H
#define __REPEATED_TASK_H

#include "task.h"
#include "simple_task.h"

class RepeatedTask : public Task {
class RepeatedTask : public SimpleTask {
Task const* prototype;
Task* current;
unsigned repetitions;
unsigned repetitionProgress;

void reset() {
delete current;
Expand All @@ -33,6 +31,10 @@ class RepeatedTask : public Task {
void print(std::ostream& os = std::cout) const;

Cloneable* clone() const { return new RepeatedTask(*this); }

unsigned getRepetitions() const { return SimpleTask::getExecutionTime(); }

unsigned getRepetitionProgress() const { return SimpleTask::getProgress(); }
};

#endif

0 comments on commit b5f3e01

Please sign in to comment.