sorted_queue.[hc]: pop_compare_func support argument

pull/47/head
YuQing 2023-07-05 16:39:57 +08:00
parent 643ecdc906
commit 3924213c9a
4 changed files with 31 additions and 23 deletions

View File

@ -1,4 +1,7 @@
Version 1.68 2023-07-05
* sorted_queue.[hc]: pop_compare_func support argument
Version 1.67 2023-05-29
* lc_pair in struct fc_queue change to lcp
* sorted queue use double link chain for quick push

View File

@ -18,21 +18,23 @@
#include "pthread_func.h"
#include "sorted_queue.h"
int sorted_queue_init_ex(struct sorted_queue *sq, const int dlink_offset,
int sorted_queue_init(struct sorted_queue *sq, const int dlink_offset,
int (*push_compare_func)(const void *data1, const void *data2),
int (*pop_compare_func)(const void *data, const void *less_equal))
int (*pop_compare_func)(const void *data, const
void *less_equal, void *arg), void *arg)
{
int result;
int result;
if ((result=init_pthread_lock_cond_pair(&sq->lcp)) != 0) {
return result;
}
if ((result=init_pthread_lock_cond_pair(&sq->lcp)) != 0) {
return result;
}
FC_INIT_LIST_HEAD(&sq->head);
sq->dlink_offset = dlink_offset;
sq->arg = arg;
sq->push_compare_func = push_compare_func;
sq->pop_compare_func = pop_compare_func;
return 0;
return 0;
}
void sorted_queue_destroy(struct sorted_queue *sq)
@ -102,7 +104,7 @@ void *sorted_queue_pop_ex(struct sorted_queue *sq,
current = sq->head.next;
data = FC_SORTED_QUEUE_DATA_PTR(sq, current);
if (sq->pop_compare_func(data, less_equal) <= 0) {
if (sq->pop_compare_func(data, less_equal, sq->arg) <= 0) {
fc_list_del_init(current);
} else {
data = NULL;
@ -136,14 +138,14 @@ void sorted_queue_pop_to_chain_ex(struct sorted_queue *sq,
} else {
current = sq->head.next;
if (sq->pop_compare_func(FC_SORTED_QUEUE_DATA_PTR(
sq, current), less_equal) <= 0)
sq, current), less_equal, sq->arg) <= 0)
{
head->next = current;
current->prev = head;
current = current->next;
while (current != &sq->head && sq->pop_compare_func(
FC_SORTED_QUEUE_DATA_PTR(sq, current),
less_equal) <= 0)
less_equal, sq->arg) <= 0)
{
current = current->next;
}

View File

@ -27,8 +27,10 @@ struct sorted_queue
struct fc_list_head head;
pthread_lock_cond_pair_t lcp;
int dlink_offset;
void *arg;
int (*push_compare_func)(const void *data1, const void *data2);
int (*pop_compare_func)(const void *data, const void *less_equal);
int (*pop_compare_func)(const void *data,
const void *less_equal, void *arg);
};
#define FC_SORTED_QUEUE_DLINK_PTR(sq, data) \
@ -41,17 +43,10 @@ struct sorted_queue
extern "C" {
#endif
int sorted_queue_init_ex(struct sorted_queue *sq, const int dlink_offset,
int sorted_queue_init(struct sorted_queue *sq, const int dlink_offset,
int (*push_compare_func)(const void *data1, const void *data2),
int (*pop_compare_func)(const void *data, const void *less_equal));
static inline int sorted_queue_init(struct sorted_queue *sq,
const int dlink_offset, int (*compare_func)
(const void *data1, const void *data2))
{
return sorted_queue_init_ex(sq, dlink_offset,
compare_func, compare_func);
}
int (*pop_compare_func)(const void *data, const
void *less_equal, void *arg), void *arg);
void sorted_queue_destroy(struct sorted_queue *sq);

View File

@ -36,11 +36,18 @@ typedef struct {
static DoubleLinkNumber *numbers;
static struct sorted_queue sq;
static int compare_func(const void *p1, const void *p2)
static int push_compare_func(const void *p1, const void *p2)
{
return ((DoubleLinkNumber *)p1)->n - ((DoubleLinkNumber *)p2)->n;
}
static int pop_compare_func(const void *data,
const void *less_equal, void *arg)
{
return ((DoubleLinkNumber *)data)->n -
((DoubleLinkNumber *)less_equal)->n;
}
void set_rand_numbers(const int multiple)
{
int i;
@ -175,7 +182,8 @@ int main(int argc, char *argv[])
srand(time(NULL));
if ((result=sorted_queue_init(&sq, (long)(&((DoubleLinkNumber *)
NULL)->dlink), compare_func)) != 0)
NULL)->dlink), push_compare_func,
pop_compare_func, NULL)) != 0)
{
return result;
}