Skip to content

Commit

Permalink
Add a function for finding an element in a priority queue with the pr…
Browse files Browse the repository at this point in the history
…iority
  • Loading branch information
byeonggiljun committed Mar 13, 2024
1 parent 2ccd7cb commit ca62f29
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 28 deletions.
3 changes: 0 additions & 3 deletions core/federated/federate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1252,9 +1252,6 @@ static void handle_provisional_tag_advance_grant() {
// (which it should be). Do not do this if the federate has not fully
// started yet.

// instant_t dummy_event_time = PTAG.time;
// microstep_t dummy_event_microstep = PTAG.microstep;

if (lf_tag_compare(env->current_tag, PTAG) == 0) {
// The current tag can equal the PTAG if we are at the start time
// or if this federate has been able to advance time to the current
Expand Down
11 changes: 0 additions & 11 deletions core/modal_models/modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,17 +410,6 @@ void _lf_process_mode_changes(environment_t* env, reactor_mode_state_t* states[]
tag_t schedule_tag = {.time = current_logical_tag.time + local_remaining_delay,
.microstep = (local_remaining_delay == 0 ? current_logical_tag.microstep + 1 : 0)};
_lf_schedule_at_tag(env, event->trigger, schedule_tag, event->token);

// Also schedule events stacked up in super dense time.
event_t* e = event;
while (e->next != NULL) {
schedule_tag.microstep++;
_lf_schedule_at_tag(env, e->next->trigger, schedule_tag, e->next->token);
event_t* tmp = e->next;
e = tmp->next;
// A fresh event was created by schedule, hence, recycle old one
lf_recycle_event(env, tmp);
}
}
// A fresh event was created by schedule, hence, recycle old one
lf_recycle_event(env, event);
Expand Down
2 changes: 1 addition & 1 deletion core/utils/pqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "util.h"
#include "lf_types.h"

int in_reverse_order(pqueue_pri_t thiz, pqueue_pri_t that) { return 1 ? (thiz > that) : 0 ? (thiz == that) : -1; }
int in_reverse_order(pqueue_pri_t thiz, pqueue_pri_t that) { return (thiz > that) ? 1 : (thiz < that) ? -1 : 0; }

int in_no_particular_order(pqueue_pri_t thiz, pqueue_pri_t that) { return 0; }

Expand Down
39 changes: 37 additions & 2 deletions core/utils/pqueue_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,39 @@ void* find_equal(pqueue_t* q, void* e, int pos, pqueue_pri_t max) {
return NULL;
}

void* find_same_priority(pqueue_t* q, void* e, int pos) {
if (pos < 0) {
lf_print_error_and_exit("find_same_priority() called with a negative pos index.");
}

// Stop the recursion when we've reached the end of the
// queue. This has to be done before accessing the queue
// to avoid segmentation fault.
if (!q || (size_t)pos >= q->size) {
return NULL;
}

void* rval;
void* curr = q->d[pos];

// Stop the recursion once we've surpassed the priority of the element
// we're looking for.
if (!curr || q->cmppri(q->getpri(curr), q->getpri(e)) == 1) {
return NULL;
}

if (q->cmppri(q->getpri(curr), q->getpri(e)) == 0) {
return curr;
} else {
rval = find_same_priority(q, e, LF_LEFT(pos));
if (rval)
return rval;
else
return find_same_priority(q, e, LF_RIGHT(pos));
}
return NULL;
}

void* find_equal_same_priority(pqueue_t* q, void* e, int pos) {
if (pos < 0) {
lf_print_error_and_exit("find_equal_same_priority() called with a negative pos index.");
Expand Down Expand Up @@ -194,10 +227,12 @@ static void percolate_down(pqueue_t* q, size_t i) {
q->setpos(moving_node, i);
}

void* pqueue_find_equal_same_priority(pqueue_t* q, void* e) { return find_equal_same_priority(q, e, 1); }

void* pqueue_find_equal(pqueue_t* q, void* e, pqueue_pri_t max) { return find_equal(q, e, 1, max); }

void* pqueue_find_same_priority(pqueue_t* q, void* e) { return find_same_priority(q, e, 1); }

void* pqueue_find_equal_same_priority(pqueue_t* q, void* e) { return find_equal_same_priority(q, e, 1); }

int pqueue_insert(pqueue_t* q, void* d) {
void** tmp;
size_t i;
Expand Down
7 changes: 3 additions & 4 deletions core/utils/pqueue_tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,10 @@ int pqueue_tag_insert_tag(pqueue_tag_t* q, tag_t t) {
}

pqueue_tag_element_t* pqueue_tag_find_with_tag(pqueue_tag_t* q, tag_t t) {
// Create elements on the stack. These elements are only needed during
// the duration of this function call, so putting them on the stack is OK.
// Create an element on the stack. This element is only needed during
// the duration of this function call, so putting it on the stack is OK.
pqueue_tag_element_t element = {.tag = t, .pos = 0, .is_dynamic = false};
pqueue_tag_element_t forever = {.tag = FOREVER_TAG, .pos = 0, .is_dynamic = false};
return pqueue_find_equal((pqueue_t*)q, (void*)&element, (pqueue_pri_t)&forever);
return pqueue_find_same_priority((pqueue_t*)q, (void*)&element);
}

pqueue_tag_element_t* pqueue_tag_find_equal_same_tag(pqueue_tag_t* q, pqueue_tag_element_t* e) {
Expand Down
22 changes: 15 additions & 7 deletions include/core/utils/pqueue_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,31 @@ void* pqueue_pop(pqueue_t* q);
void pqueue_empty_into(pqueue_t** dest, pqueue_t** src);

/**
* Find the highest-ranking item with the same priority that matches the
* supplied entry.
* Find the highest-ranking item with priority up to and including the given
* maximum priority that matches the supplied entry.
* @param q the queue
* @param e the entry to compare against
* @param max_priority the maximum priority to consider
* @return NULL if no matching event has been found, otherwise the entry
*/
void* pqueue_find_equal_same_priority(pqueue_t* q, void* e);
void* pqueue_find_equal(pqueue_t* q, void* e, pqueue_pri_t max_priority);

/**
* Find the highest-ranking item with priority up to and including the given
* maximum priority that matches the supplied entry.
* Find the highest-ranking item with the same priority.
* @param q the queue
* @param e the entry to compare against
* @param max_priority the maximum priority to consider
* @return NULL if no matching event has been found, otherwise the entry
*/
void* pqueue_find_equal(pqueue_t* q, void* e, pqueue_pri_t max_priority);
void* pqueue_find_same_priority(pqueue_t* q, void* e);

/**
* Find the highest-ranking item with the same priority that matches the
* supplied entry.
* @param q the queue
* @param e the entry to compare against
* @return NULL if no matching event has been found, otherwise the entry
*/
void* pqueue_find_equal_same_priority(pqueue_t* q, void* e);

/**
* Remove an item from the queue.
Expand Down

0 comments on commit ca62f29

Please sign in to comment.