diff --git a/pq.c b/pq.c index da6d4b49..84ee460b 100644 --- a/pq.c +++ b/pq.c @@ -100,3 +100,12 @@ pq_take(pq q) delete_min(q); return j; } + +job +pq_find(pq q, unsigned long long int id) +{ + unsigned int i; + + for (i = 0; i < q->used; i++) if (q->heap[i]->id == id) return q->heap[i]; + return NULL; +} diff --git a/pq.h b/pq.h index 1c4c2631..eec64e4f 100644 --- a/pq.h +++ b/pq.h @@ -20,4 +20,8 @@ int pq_give(pq q, job j); /* return a job if the queue contains jobs, else NULL */ job pq_take(pq q); +/* return a job that matches the given id, else NULL */ +/* This is O(n), so don't do it much. */ +job pq_find(pq q, unsigned long long int id); + #endif /*q_h*/ diff --git a/tests/test_pq.c b/tests/test_pq.c index a8f0d19f..0a20a65f 100644 --- a/tests/test_pq.c +++ b/tests/test_pq.c @@ -111,6 +111,30 @@ __CUT__pq_test_fifo_property() ASSERT(j == j3c, "j3c should come out third."); } +void +__CUT__pq_test_find_match() +{ + job j; + + q = make_pq(2); + pq_give(q, j1); + + j = pq_find(q, j1->id); + ASSERT(j == j1, "j1 should come out."); +} + +void +__CUT__pq_test_find_miss() +{ + job j; + + q = make_pq(2); + pq_give(q, j1); + + j = pq_find(q, j1->id + 1); + ASSERT(j == NULL, "no job should match."); +} + void __CUT_TAKEDOWN__pq() {