sorted_queue.[hc] support pop_compare_func

pull/47/head
YuQing 2023-05-23 20:13:38 +08:00
parent ccbc201636
commit 8cea8632d7
3 changed files with 26 additions and 13 deletions

View File

@ -1,10 +1,11 @@
Version 1.67 2023-05-19 Version 1.67 2023-05-23
* 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
* add function uniq_skiplist_clear * add function uniq_skiplist_clear
* fast_mblock_malloc_trunk_notify_func prototype changed * fast_mblock_malloc_trunk_notify_func prototype changed
* fast_mblock_init_ex2 add parameter prealloc_trunk_count * fast_mblock_init_ex2 add parameter prealloc_trunk_count
* sorted_queue.[hc] support pop_compare_func
Version 1.66 2023-02-12 Version 1.66 2023-02-12
* struct fast_task_info add field: notify_next for nio notify queue * struct fast_task_info add field: notify_next for nio notify queue

View File

@ -18,8 +18,9 @@
#include "pthread_func.h" #include "pthread_func.h"
#include "sorted_queue.h" #include "sorted_queue.h"
int sorted_queue_init(struct sorted_queue *sq, const int dlink_offset, int sorted_queue_init_ex(struct sorted_queue *sq, const int dlink_offset,
int (*compare_func)(const void *, const void *)) int (*push_compare_func)(const void *data1, const void *data2),
int (*pop_compare_func)(const void *data, const void *less_equal))
{ {
int result; int result;
@ -29,7 +30,8 @@ int sorted_queue_init(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->compare_func = compare_func; sq->push_compare_func = push_compare_func;
sq->pop_compare_func = pop_compare_func;
return 0; return 0;
} }
@ -50,19 +52,19 @@ void sorted_queue_push_ex(struct sorted_queue *sq, void *data, bool *notify)
fc_list_add(dlink, &sq->head); fc_list_add(dlink, &sq->head);
*notify = true; *notify = true;
} else { } else {
if (sq->compare_func(data, FC_SORTED_QUEUE_DATA_PTR( if (sq->push_compare_func(data, FC_SORTED_QUEUE_DATA_PTR(
sq, sq->head.prev)) >= 0) sq, sq->head.prev)) >= 0)
{ {
fc_list_add_tail(dlink, &sq->head); fc_list_add_tail(dlink, &sq->head);
*notify = false; *notify = false;
} else if (sq->compare_func(data, FC_SORTED_QUEUE_DATA_PTR( } else if (sq->push_compare_func(data, FC_SORTED_QUEUE_DATA_PTR(
sq, sq->head.next)) < 0) sq, sq->head.next)) < 0)
{ {
fc_list_add(dlink, &sq->head); fc_list_add(dlink, &sq->head);
*notify = true; *notify = true;
} else { } else {
current = sq->head.prev->prev; current = sq->head.prev->prev;
while (sq->compare_func(data, FC_SORTED_QUEUE_DATA_PTR( while (sq->push_compare_func(data, FC_SORTED_QUEUE_DATA_PTR(
sq, current)) < 0) sq, current)) < 0)
{ {
current = current->prev; current = current->prev;
@ -100,7 +102,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->compare_func(data, less_equal) <= 0) { if (sq->pop_compare_func(data, less_equal) <= 0) {
fc_list_del_init(current); fc_list_del_init(current);
} else { } else {
data = NULL; data = NULL;
@ -133,13 +135,13 @@ void sorted_queue_pop_to_chain_ex(struct sorted_queue *sq,
FC_INIT_LIST_HEAD(head); FC_INIT_LIST_HEAD(head);
} else { } else {
current = sq->head.next; current = sq->head.next;
if (sq->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) <= 0)
{ {
head->next = current; head->next = current;
current->prev = head; current->prev = head;
current = current->next; current = current->next;
while (current != &sq->head && sq->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) <= 0)
{ {

View File

@ -26,7 +26,8 @@ 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;
int (*compare_func)(const void *, const void *); int (*push_compare_func)(const void *data1, const void *data2);
int (*pop_compare_func)(const void *data, const void *less_equal);
}; };
#define FC_SORTED_QUEUE_DLINK_PTR(sq, data) \ #define FC_SORTED_QUEUE_DLINK_PTR(sq, data) \
@ -39,8 +40,17 @@ struct sorted_queue
extern "C" { extern "C" {
#endif #endif
int sorted_queue_init(struct sorted_queue *sq, const int dlink_offset, int sorted_queue_init_ex(struct sorted_queue *sq, const int dlink_offset,
int (*compare_func)(const void *, const void *)); 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);
}
void sorted_queue_destroy(struct sorted_queue *sq); void sorted_queue_destroy(struct sorted_queue *sq);