improved Array: use std::vector

This commit is contained in:
2023-12-03 13:41:29 +03:00
parent 6b7bde1471
commit c4b8e2dd7a
8 changed files with 42 additions and 45 deletions

View File

@@ -3,42 +3,35 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <vector>
template <class T> template <class T>
class Array { class Array {
protected: private:
long length; std::vector<T*> elements;
T** elements;
public: public:
Array() { Array() { }
length = 0;
elements = NULL;
}
virtual ~Array() { virtual ~Array() {
if (elements != NULL) { for (auto& elem : elements)
for (long i = 0; i < length; ++i) delete elem;
delete elements[i]; elements.clear();
delete[] elements;
}
} }
void add(T* new_line) { void add(T* new_line) {
T** buf = new T * [length + 1]; elements.push_back(new_line);
for (long i = 0; i < length; ++i) {
buf[i] = elements[i];
}
buf[length] = new_line;
length++;
delete[] elements;
elements = buf;
buf = NULL;
} }
long getLength() {
return length; long getLength() const {
return elements.size();
} }
T* get(long i) { T* get(long i) {
return elements[i]; return elements[i];
} }
T** getElements() {
const std::vector<T*>& getElements() const {
return elements; return elements;
} }
}; };

View File

@@ -9,7 +9,7 @@ public:
this->init("compilationTasks", 4); this->init("compilationTasks", 4);
} }
CompilationTask* getTaskById(long task_id) { CompilationTask* getTaskById(long task_id) {
for (long i = 0; i < length; ++i) { for (long i = 0; i < getLength(); ++i) {
CompilationTask* task = get(i); CompilationTask* task = get(i);
if (task->getId() == task_id) if (task->getId() == task_id)
return task; return task;

View File

@@ -13,11 +13,12 @@ public:
void setMakefileText(String* makefile_text_in) { void setMakefileText(String* makefile_text_in) {
makefile_text = String(makefile_text_in->getCharArray(), '|'); makefile_text = String(makefile_text_in->getCharArray(), '|');
} }
virtual void print() { virtual void print() const {
printf("id=%ld; maxtime=%d; test_id=%s\n", id, maxtime, printf("id=%ld; maxtime=%d; test_id=%s\n", id, maxtime,
test_id.getCharArray()); test_id.getCharArray());
printf("makefile_text=%s\n", makefile_text.getCharArray()); printf("makefile_text=%s\n", makefile_text.getCharArray());
} }
CompilationTask(Text* lines, int offset) :Task(lines, offset) { CompilationTask(Text* lines, int offset) :Task(lines, offset) {
setTestId(lines->get(offset + 2)); setTestId(lines->get(offset + 2));
setMakefileText(lines->get(offset + 3)); setMakefileText(lines->get(offset + 3));

View File

@@ -8,7 +8,7 @@ public:
RunSupervisor(CompilationSupervisor* compilationSupervisor) { RunSupervisor(CompilationSupervisor* compilationSupervisor) {
this->init("runTasks", 8); this->init("runTasks", 8);
//проверить отмененные задачи. //проверить отмененные задачи.
for (long i = 0; i < this->length; ++i) { for (long i = 0; i < getLength(); ++i) {
RunTask* task = this->get(i); RunTask* task = this->get(i);
CompilationTask* parent = compilationSupervisor->getTaskById(task->getTestCompilationTaskId()); CompilationTask* parent = compilationSupervisor->getTaskById(task->getTestCompilationTaskId());
task->setState((parent->getState() == Done) ? Waiting : Canceled); task->setState((parent->getState() == Done) ? Waiting : Canceled);

View File

@@ -11,7 +11,7 @@ class RunTask : public Task {
String args; String args;
CompilationTask* parent; CompilationTask* parent;
public: public:
virtual void print() { virtual void print() const {
printf("id=%ld; maxtime=%d; testcompilationtask_id=%ld; matrix=%s; environments=%s; usr_par=%s; args=%s kernels=%d\n", printf("id=%ld; maxtime=%d; testcompilationtask_id=%ld; matrix=%s; environments=%s; usr_par=%s; args=%s kernels=%d\n",
id, id,
maxtime, maxtime,

View File

@@ -38,18 +38,19 @@ public:
} }
//- //-
void print() { void print() {
for (long i = 0; i < this->length; ++i) for (auto& elem : this->getElements())
this->elements[i]->print(); elem->print();
} }
void init(const char* fileName, int recordSize) { void init(const char* fileName, int recordSize) {
state = WorkspacesCreation; state = WorkspacesCreation;
File* packedTasks = new File(fileName); File* packedTasks = new File(fileName);
Text* lines = packedTasks->readLines(); Text* lines = packedTasks->readLines();
this->length = lines->getLength() / recordSize;
this->elements = new T * [this->length]; const long length = lines->getLength() / recordSize;
int offset = 0; int offset = 0;
for (int i = 0; i < this->length; ++i) { for (int i = 0; i < length; ++i) {
this->elements[i] = new T(lines, offset); this->add(new T(lines, offset));
offset += recordSize; offset += recordSize;
} }
delete packedTasks; delete packedTasks;
@@ -59,13 +60,13 @@ public:
saveState(); saveState();
long activeCount = 0; long activeCount = 0;
//todo обязательно убрать отладочную печать. //todo обязательно убрать отладочную печать.
printf("tasks count = %ld\n", this->length); printf("tasks count = %ld\n", this->getLength());
while (this->state != End) { while (this->state != End) {
// printf("state=%d\n", this->state); // printf("state=%d\n", this->state);
// printf("max=%d; busy=%d; free=%d\n", maxKernels, busyKernels, freeKernels); // printf("max=%d; busy=%d; free=%d\n", maxKernels, busyKernels, freeKernels);
activeCount = 0; activeCount = 0;
for (long i = 0; i < this->length; ++i) { for (long i = 0; i < this->getLength(); ++i) {
T* task = this->elements[i]; T* task = this->get(i);
switch (this->state) { switch (this->state) {
case WorkspacesCreation: case WorkspacesCreation:
if (task->getState() == Waiting) { if (task->getState() == Waiting) {

View File

@@ -104,7 +104,7 @@ public:
setMaxtime(lines->get(offset + 1)); setMaxtime(lines->get(offset + 1));
workspace = packageWorkspace + "/" + String(id); workspace = packageWorkspace + "/" + String(id);
} }
virtual void print() = 0; virtual void print() const = 0;
//- //-
virtual void prepareWorkspace() {} virtual void prepareWorkspace() {}
virtual String getLaunchScriptText() = 0; virtual String getLaunchScriptText() = 0;

View File

@@ -5,18 +5,20 @@
class Text : public Array<String> { class Text : public Array<String> {
public: public:
void Print() { void Print() const {
printf("text length=%ld\n", length); printf("text length=%ld\n", this->getLength());
for (long i = 0; i < length; ++i) { auto elems = this->getElements();
printf("i=%ld; [%s]\n", i, elements[i]->getCharArray()); for (size_t i = 0; i < elems.size(); ++i) {
printf("i=%ld; [%s]\n", i, elems[i]->getCharArray());
// elements[i]->println(); // elements[i]->println();
} }
} }
bool hasMatch(const String& s) {
for (long i = 0; i < length; ++i) { bool hasMatch(const String& s) const {
if (s.contains(*elements[i])) {
for (auto& elem : this->getElements()) {
if (s.contains(*elem)) {
//printf("match: [%s]\n", elements[i]->getCharArray()); //printf("match: [%s]\n", elements[i]->getCharArray());
return true; return true;
} }