-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PriorityQueue: Finish the demo program.
- Loading branch information
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,121 @@ | ||
#include "cds.h" | ||
|
||
|
||
typedef struct Tuple_ { | ||
int first; | ||
int second; | ||
} Tuple; | ||
|
||
|
||
/* Let the integer with minimum value reside at the top position. */ | ||
int CompareNumerics(const void* lhs, const void* rhs) | ||
{ | ||
if ((intptr_t)lhs == (intptr_t)rhs) | ||
return 0; | ||
return ((intptr_t)lhs >= (intptr_t)rhs)? 1 : (-1); | ||
} | ||
|
||
/* Let the object with maximum value reside at the top position. */ | ||
int CompareObjects(const void* lhs, const void* rhs) | ||
{ | ||
Tuple* tpl_lhs = (Tuple*)lhs; | ||
Tuple* tpl_rhs = (Tuple*)rhs; | ||
if (tpl_lhs->first == tpl_rhs->first) | ||
return 0; | ||
return (tpl_lhs->first > tpl_rhs->first)? (-1) : 1; | ||
} | ||
|
||
void CleanObject(void* element) | ||
{ | ||
free(element); | ||
} | ||
|
||
|
||
void ManipulateNumerics() | ||
{ | ||
/* We should initialize the container before any operations. */ | ||
PriorityQueue* queue = PriorityQueueInit(); | ||
queue->set_compare(queue, CompareNumerics); | ||
|
||
/* Push integer elements to the queue. */ | ||
queue->push(queue, (void*)(intptr_t)4); | ||
queue->push(queue, (void*)(intptr_t)3); | ||
queue->push(queue, (void*)(intptr_t)2); | ||
queue->push(queue, (void*)(intptr_t)1); | ||
|
||
/* Pop elements from the queue and check the top elements. */ | ||
void* elem; | ||
queue->top(queue, &elem); | ||
assert((int)(intptr_t)elem == 1); | ||
queue->pop(queue); | ||
|
||
queue->top(queue, &elem); | ||
assert((int)(intptr_t)elem == 2); | ||
queue->pop(queue); | ||
|
||
queue->top(queue, &elem); | ||
assert((int)(intptr_t)elem == 3); | ||
queue->pop(queue); | ||
|
||
/* Check the number of stored elements. */ | ||
unsigned size = queue->size(queue); | ||
assert(size == 1); | ||
|
||
PriorityQueueDeinit(queue); | ||
} | ||
|
||
void ManipulateObjects() | ||
{ | ||
/* We should initialize the container before any operations. */ | ||
PriorityQueue* queue = PriorityQueueInit(); | ||
queue->set_compare(queue, CompareObjects); | ||
queue->set_clean(queue, CleanObject); | ||
|
||
/* Push integer elements to the queue. */ | ||
Tuple* tuple = (Tuple*)malloc(sizeof(Tuple)); | ||
tuple->first = 4; | ||
tuple->second = -4; | ||
queue->push(queue, tuple); | ||
|
||
tuple = (Tuple*)malloc(sizeof(Tuple)); | ||
tuple->first = 3; | ||
tuple->second = -3; | ||
queue->push(queue, tuple); | ||
|
||
tuple = (Tuple*)malloc(sizeof(Tuple)); | ||
tuple->first = 2; | ||
tuple->second = -2; | ||
queue->push(queue, tuple); | ||
|
||
tuple = (Tuple*)malloc(sizeof(Tuple)); | ||
tuple->first = 1; | ||
tuple->second = -1; | ||
queue->push(queue, tuple); | ||
|
||
/* Pop elements from the queue and check the top elements. */ | ||
void* elem; | ||
queue->top(queue, &elem); | ||
assert(((Tuple*)elem)->first == 4); | ||
queue->pop(queue); | ||
|
||
queue->top(queue, &elem); | ||
assert(((Tuple*)elem)->first == 3); | ||
queue->pop(queue); | ||
|
||
queue->top(queue, &elem); | ||
assert(((Tuple*)elem)->first == 2); | ||
queue->pop(queue); | ||
|
||
/* Check the number of stored elements. */ | ||
unsigned size = queue->size(queue); | ||
assert(size == 1); | ||
|
||
PriorityQueueDeinit(queue); | ||
} | ||
|
||
int main() | ||
{ | ||
ManipulateNumerics(); | ||
ManipulateObjects(); | ||
return 0; | ||
} |