master
ketrptr 2024-12-16 17:29:07 +01:00
parent 602da76bca
commit e5b5fd3b50
2 changed files with 21 additions and 37 deletions

View File

@ -161,6 +161,14 @@ int cmpEvent(const void *a, const void *b) {
return cmpTaskN(aa->task, bb->task); return cmpTaskN(aa->task, bb->task);
} }
void resetPrio(Task **sortedPrio, int len) {
for (int i = 0; i < len; i++) {
if ((*sortedPrio)[i].priority < 0) // ignore past deadline
continue;
printf("incrementing prio!\n");
(*sortedPrio)[i].priority += 10;
}
}
llist *genPlan(llist *head, time_t timeAvail) { llist *genPlan(llist *head, time_t timeAvail) {
// map llist to pointer arr & sort by priority // map llist to pointer arr & sort by priority
@ -170,7 +178,7 @@ llist *genPlan(llist *head, time_t timeAvail) {
printf("len: %d\n", lLen); printf("len: %d\n", lLen);
llistPrintT(head); llistPrintT(head);
// Task *sortedNames = calloc(lLen, sizeof(Task)); time_t now = time(NULL);
Task *sortedPrio = calloc(lLen, sizeof(Task)); Task *sortedPrio = calloc(lLen, sizeof(Task));
if (/*sortedNames == NULL ||*/ sortedPrio == NULL) { if (/*sortedNames == NULL ||*/ sortedPrio == NULL) {
planLog("gen plan : calloc failed!!\n", true); planLog("gen plan : calloc failed!!\n", true);
@ -180,23 +188,16 @@ llist *genPlan(llist *head, time_t timeAvail) {
// add Tasks from llist to arr // add Tasks from llist to arr
llist *c = head; llist *c = head;
for (int i = 0; c != NULL; i++) { for (int i = 0; c != NULL; i++) {
// sortedNames[i] = *copyTask(c->data);
// sortedPrio[i] = *copyTask(c->data);
// sortedNames[i] = *(Task *)c->data;
sortedPrio[i] = *(Task *)c->data; sortedPrio[i] = *(Task *)c->data;
if (sortedPrio[i].deadline + 1 < now) {
sortedPrio[i].priority = -1; // ignore past deadlines
}
c = c->next; c = c->next;
} }
// sort // sort
// qsort(sortedNames, lLen, sizeof(Task), cmpTaskN);
qsort(sortedPrio, lLen, sizeof(Task), cmpTaskP); qsort(sortedPrio, lLen, sizeof(Task), cmpTaskP);
// // test print
// planLog("sortendName", 0);
// for (int i = 0; i < lLen; i++) {
// printTask(sortedNames + i);
// }
planLog("sortendPrio", 0); planLog("sortendPrio", 0);
for (int i = 0; i < lLen; i++) { for (int i = 0; i < lLen; i++) {
printTask(sortedPrio + i); printTask(sortedPrio + i);
@ -205,16 +206,19 @@ llist *genPlan(llist *head, time_t timeAvail) {
planLog("creating eventList", false); planLog("creating eventList", false);
// genertate plan basen on priorities and available time // genertate plan basen on priorities and available time
time_t now = time(NULL);
struct tm *lc = localtime(&now); struct tm *lc = localtime(&now);
llist *events_ll = llistNew(NULL, cmpEvent); llist *events_ll = llistNew(NULL, cmpEvent);
if (events_ll == NULL) { if (events_ll == NULL) {
exit(1); exit(1);
} }
// time_t avail = mktime(timeAvail);
time_t start, end; time_t start, end;
// (mktime(lc) < mktime(timeAvail))
do { do {
if ((*sortedPrio).priority < 0) {
continue;
} else if ((*sortedPrio).priority == 0) {
printf("All tasks have priority <0!!");
resetPrio(&sortedPrio, lLen);
}
start = mktime(lc); // start now start = mktime(lc); // start now
lc->tm_min += intervalLen; lc->tm_min += intervalLen;
end = mktime(lc); // end = mktime(lc); //
@ -225,9 +229,9 @@ llist *genPlan(llist *head, time_t timeAvail) {
c = newEvent(tmp->data, start, end, c = newEvent(tmp->data, start, end,
0); // use elem with wighest priority 0); // use elem with wighest priority
} }
llistAppend(events_ll, c); llistAppend(events_ll, c);
printEvent(c); printEvent(c);
// printEvent((Event *)((events_ll)->next)->data);
// decrement priority of first elem and resort list // decrement priority of first elem and resort list
(*sortedPrio).priority -= 1; (*sortedPrio).priority -= 1;
@ -237,29 +241,12 @@ llist *genPlan(llist *head, time_t timeAvail) {
} while (mktime(lc) < timeAvail); } while (mktime(lc) < timeAvail);
// for (int i = 0; i < lLen; i++) {
// time_t start = mktime(lc);
//
// printf("start:: %s\n", ctime(&start));
// lc->tm_min += 45;
// time_t end = mktime(lc);
// printf("end:: %s\n", ctime(&end));
//
// Event *c = newEvent(sortedPrio + i, start, end, 0);
// printEvent(c);
// lc->tm_min += 15;
// llistAppend(events_ll, c);
// }
//
// free empty head // free empty head
llist *tmp = events_ll; llist *tmp = events_ll;
events_ll = events_ll->next; events_ll = events_ll->next;
tmp->next = NULL; tmp->next = NULL;
llistFreeE(tmp); llistFreeE(tmp);
printf("====EVENTSLL:\n"); // llistPrintE(events_ll);
llistPrintE(events_ll);
printf("====EVENTSLL:\n");
// update prioriteis in original llist // update prioriteis in original llist
for (int i = 0; i < lLen; i++) { for (int i = 0; i < lLen; i++) {
llist *tmp = llistGet(head, sortedPrio + i); llist *tmp = llistGet(head, sortedPrio + i);
@ -270,7 +257,6 @@ llist *genPlan(llist *head, time_t timeAvail) {
free(sortedPrio); free(sortedPrio);
planLog("END GEN PLAN", 0); planLog("END GEN PLAN", 0);
// llistFreeT(head);
// send updated tasks to db for storage // send updated tasks to db for storage
return events_ll; return events_ll;

View File

@ -19,8 +19,6 @@
#define localtime_r(T, Tm) (localtime_s(Tm, T) ? NULL : Tm) #define localtime_r(T, Tm) (localtime_s(Tm, T) ? NULL : Tm)
#endif #endif
const char *dbName = "db.csv"; const char *dbName = "db.csv";
int main(void) { int main(void) {
@ -85,7 +83,7 @@ int main(void) {
// } // }
// create deadline timestamp // create deadline timestamp
lc.tm_yday += taskdeadline_date; lc.tm_hour += (taskdeadline_date * 24);
time_t deadline = mktime(&lc); time_t deadline = mktime(&lc);
Task *newT = newTask(taskname, now, deadline, taskpriority, 0); Task *newT = newTask(taskname, now, deadline, taskpriority, 0);
if (listT == NULL) { if (listT == NULL) {