From 6f8e77eb76c4311d69ac99ba3bcc704a216896c6 Mon Sep 17 00:00:00 2001 From: Nicola Mettifogo Date: Tue, 10 Apr 2007 18:46:34 +0000 Subject: Moved Jobs to ManagedList. Since Jobs must be ordered according to their priority, a new insertSorted method has been added to the implementation. svn-id: r26451 --- engines/parallaction/defs.h | 14 ++++++++++++ engines/parallaction/parallaction.cpp | 43 ++++++++++++++++++----------------- engines/parallaction/parallaction.h | 7 ++++-- 3 files changed, 41 insertions(+), 23 deletions(-) (limited to 'engines/parallaction') diff --git a/engines/parallaction/defs.h b/engines/parallaction/defs.h index 41450bfbbe..37495e8f46 100644 --- a/engines/parallaction/defs.h +++ b/engines/parallaction/defs.h @@ -51,6 +51,8 @@ public: typedef typename Common::List Common_List; typedef typename Common::List::iterator iterator; + typedef int (*CompareFunction) (const T& a, const T& b); + ~ManagedList() { clear(); } @@ -71,6 +73,17 @@ public: return Common_List::erase(first, last); } + // keeps list ordered in *ascending* order, as expressed by the compare function + void insertSorted(const T& element, CompareFunction compare) { + iterator it = Common_List::begin(); + for ( ; it != Common_List::end(); it++) + if (compare(element, *it) < 0) break; + + if (it == Common_List::end()) + Common_List::push_back(element); + else + Common_List::insert(it, element); + } }; } // namespace Parallaction @@ -80,3 +93,4 @@ public: + diff --git a/engines/parallaction/parallaction.cpp b/engines/parallaction/parallaction.cpp index fc0399f853..58ee2dcd78 100644 --- a/engines/parallaction/parallaction.cpp +++ b/engines/parallaction/parallaction.cpp @@ -834,6 +834,14 @@ void removeNode(Node *n) { return; } +/* + helper function to provide *descending* ordering of the job list + (higher priorities values comes first in the list) +*/ +int compareJobPriority(const JobPointer &j1, const JobPointer &j2) { + if (j1->_tag == j2->_tag) return 0; + return (j1->_tag >= j2->_tag ? -1 : 1); +} Job *Parallaction::addJob(JobFn fn, void *parm, uint16 tag) { @@ -845,14 +853,7 @@ Job *Parallaction::addJob(JobFn fn, void *parm, uint16 tag) { v8->_finished = 0; v8->_count = 0; - Job *v4 = &_jobs; - - // TODO (LIST): the loop will be useless once we have an ordered - // list _jobs. So this code will just be: _jobs.insert(v8) - while (v4->_next && ((Job*)(v4->_next))->_tag > tag) { - v4 = (Job*)v4->_next; - } - addNode(v4, v8); + _jobs.insertSorted(v8, compareJobPriority); return v8; } @@ -876,22 +877,22 @@ void Parallaction::runJobs() { if (_engineFlags & kEnginePauseJobs) return; - Job *j = (Job*)_jobs._next; - while (j) { - debugC(3, kDebugJobs, "runJobs: %i", j->_tag); - - Job *v4 = (Job*)j->_next; - - if (j->_finished == 1) { - removeNode(j); - delete j; - } else { - (*j->_fn)(j->_parm, j); - } + JobList::iterator it = _jobs.begin(); + while (it != _jobs.end()) { + if ((*it)->_finished == 1) + it = _jobs.erase(it); + else + it++; + } - j = v4; + it = _jobs.begin(); + while (it != _jobs.end()) { + debugC(3, kDebugJobs, "runJobs: %i", (*it)->_tag); + (*(*it)->_fn)((*it)->_parm, (*it)); + it++; } + return; } diff --git a/engines/parallaction/parallaction.h b/engines/parallaction/parallaction.h index 5ac53c932f..8786998eea 100644 --- a/engines/parallaction/parallaction.h +++ b/engines/parallaction/parallaction.h @@ -135,7 +135,7 @@ struct PARALLACTIONGameDescription; struct Job; typedef void (*JobFn)(void*, Job*); -struct Job : public Node { +struct Job { uint16 _count; // # of executions left uint16 _tag; // used for ordering uint16 _finished; @@ -147,6 +147,9 @@ public: } }; +typedef Job* JobPointer; +typedef ManagedList JobList; + struct Credit { const char *_role; const char *_name; @@ -399,7 +402,7 @@ protected: // data int16 _keyDown; - Job _jobs; + JobList _jobs; Node helperNode; // used for freeZones: to be removed -- cgit v1.2.3