enhance ordq
This commit is contained in:
parent
c478ea8516
commit
05abed1964
4 changed files with 18 additions and 3 deletions
|
|
@ -14,7 +14,7 @@ typedef struct _ordq_item {
|
|||
void* data;
|
||||
} ordq_item_t;
|
||||
|
||||
typedef int (*ordq_cmp_fn_t)(ordkey_t, ordkey_t);
|
||||
typedef int (*ordq_cmp_fn_t)(void*, ordkey_t, ordkey_t);
|
||||
typedef void (*ordq_data_killer_t)(void*);
|
||||
|
||||
typedef struct {
|
||||
|
|
@ -22,6 +22,7 @@ typedef struct {
|
|||
ordq_item_t* head;
|
||||
ordq_data_killer_t killer;
|
||||
ordq_cmp_fn_t cmp;
|
||||
void* ctx;
|
||||
} ordq_t;
|
||||
|
||||
#define mk_ordkey(k) (ordkey_t)((long)(k))
|
||||
|
|
@ -31,5 +32,6 @@ extern void ordq_fini(ordq_t* q);
|
|||
extern int ordq_put(ordq_t*, ordkey_t, void*);
|
||||
extern ordq_item_t* ordq_get(ordq_t*);
|
||||
extern ordq_item_t* ordq_peek(ordq_t*);
|
||||
extern void ordq_set_ctx(ordq_t*, void*);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ int ordq_init(ordq_t* q, ordq_data_killer_t killer, ordq_cmp_fn_t cmp)
|
|||
q->head = NULL;
|
||||
q->killer = killer;
|
||||
q->cmp = cmp;
|
||||
q->ctx = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -25,7 +26,7 @@ static int ordq_put_(ordq_t* q, ordq_item_t* it)
|
|||
ordq_item_t** p = &(q->head);
|
||||
|
||||
while ((*p) != NULL) {
|
||||
if (q->cmp((*p)->key, it->key) > 0) {
|
||||
if (q->cmp(q->ctx, (*p)->key, it->key) > 0) {
|
||||
/* we found the location to insert */
|
||||
break;
|
||||
}
|
||||
|
|
@ -105,3 +106,14 @@ ordq_item_t* ordq_peek(ordq_t* q)
|
|||
|
||||
return it;
|
||||
}
|
||||
|
||||
void ordq_set_ctx(ordq_t* q, void* ctx)
|
||||
{
|
||||
if (q == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
mylock_lock(&(q->lock));
|
||||
q->ctx = ctx;
|
||||
mylock_unlock(&(q->lock));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
#include "unity.h"
|
||||
#include "utils.h"
|
||||
|
||||
static int long_cmp(ordkey_t k1, ordkey_t k2)
|
||||
static int long_cmp(void* ctx, ordkey_t k1, ordkey_t k2)
|
||||
{
|
||||
if ((long)k1 < (long)k2) {
|
||||
return -1;
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include "client.h"
|
||||
#include "xce/types.h"
|
||||
#include "mylock.h"
|
||||
#include "xlist.h"
|
||||
|
||||
typedef enum {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue