Skip to content

Commit

Permalink
Make a find function for the peek command.
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith Rarick committed Sep 25, 2007
1 parent 85f8c71 commit e28c4d3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pq.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions pq.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
24 changes: 24 additions & 0 deletions tests/test_pq.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit e28c4d3

Please sign in to comment.