sorted_queue.[hc]: pop_compare_func support argument
parent
643ecdc906
commit
3924213c9a
3
HISTORY
3
HISTORY
|
|
@ -1,4 +1,7 @@
|
||||||
|
|
||||||
|
Version 1.68 2023-07-05
|
||||||
|
* sorted_queue.[hc]: pop_compare_func support argument
|
||||||
|
|
||||||
Version 1.67 2023-05-29
|
Version 1.67 2023-05-29
|
||||||
* lc_pair in struct fc_queue change to lcp
|
* lc_pair in struct fc_queue change to lcp
|
||||||
* sorted queue use double link chain for quick push
|
* sorted queue use double link chain for quick push
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,10 @@
|
||||||
#include "pthread_func.h"
|
#include "pthread_func.h"
|
||||||
#include "sorted_queue.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 (*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;
|
||||||
|
|
||||||
|
|
@ -30,6 +31,7 @@ int sorted_queue_init_ex(struct sorted_queue *sq, const int dlink_offset,
|
||||||
|
|
||||||
FC_INIT_LIST_HEAD(&sq->head);
|
FC_INIT_LIST_HEAD(&sq->head);
|
||||||
sq->dlink_offset = dlink_offset;
|
sq->dlink_offset = dlink_offset;
|
||||||
|
sq->arg = arg;
|
||||||
sq->push_compare_func = push_compare_func;
|
sq->push_compare_func = push_compare_func;
|
||||||
sq->pop_compare_func = pop_compare_func;
|
sq->pop_compare_func = pop_compare_func;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -102,7 +104,7 @@ void *sorted_queue_pop_ex(struct sorted_queue *sq,
|
||||||
|
|
||||||
current = sq->head.next;
|
current = sq->head.next;
|
||||||
data = FC_SORTED_QUEUE_DATA_PTR(sq, current);
|
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);
|
fc_list_del_init(current);
|
||||||
} else {
|
} else {
|
||||||
data = NULL;
|
data = NULL;
|
||||||
|
|
@ -136,14 +138,14 @@ void sorted_queue_pop_to_chain_ex(struct sorted_queue *sq,
|
||||||
} else {
|
} else {
|
||||||
current = sq->head.next;
|
current = sq->head.next;
|
||||||
if (sq->pop_compare_func(FC_SORTED_QUEUE_DATA_PTR(
|
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;
|
head->next = current;
|
||||||
current->prev = head;
|
current->prev = head;
|
||||||
current = current->next;
|
current = current->next;
|
||||||
while (current != &sq->head && sq->pop_compare_func(
|
while (current != &sq->head && sq->pop_compare_func(
|
||||||
FC_SORTED_QUEUE_DATA_PTR(sq, current),
|
FC_SORTED_QUEUE_DATA_PTR(sq, current),
|
||||||
less_equal) <= 0)
|
less_equal, sq->arg) <= 0)
|
||||||
{
|
{
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,10 @@ struct sorted_queue
|
||||||
struct fc_list_head head;
|
struct fc_list_head head;
|
||||||
pthread_lock_cond_pair_t lcp;
|
pthread_lock_cond_pair_t lcp;
|
||||||
int dlink_offset;
|
int dlink_offset;
|
||||||
|
void *arg;
|
||||||
int (*push_compare_func)(const void *data1, const void *data2);
|
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) \
|
#define FC_SORTED_QUEUE_DLINK_PTR(sq, data) \
|
||||||
|
|
@ -41,17 +43,10 @@ struct sorted_queue
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#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 (*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);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sorted_queue_destroy(struct sorted_queue *sq);
|
void sorted_queue_destroy(struct sorted_queue *sq);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,11 +36,18 @@ typedef struct {
|
||||||
static DoubleLinkNumber *numbers;
|
static DoubleLinkNumber *numbers;
|
||||||
static struct sorted_queue sq;
|
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;
|
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)
|
void set_rand_numbers(const int multiple)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
@ -175,7 +182,8 @@ int main(int argc, char *argv[])
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
if ((result=sorted_queue_init(&sq, (long)(&((DoubleLinkNumber *)
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue