added a comparator to ase_sll_t
This commit is contained in:
parent
b2ad40e12a
commit
275c6fb8ec
@ -22,10 +22,41 @@ typedef struct ase_sll_node_t ase_sll_node_t;
|
||||
typedef enum ase_sll_walk_t ase_sll_walk_t;
|
||||
|
||||
/* data copier */
|
||||
typedef void* (*ase_sll_copier_t) (ase_sll_t* sll, void* dptr, ase_size_t dlen);
|
||||
typedef void* (*ase_sll_copier_t) (
|
||||
ase_sll_t* sll,
|
||||
void* dptr,
|
||||
ase_size_t dlen
|
||||
);
|
||||
|
||||
/* data freeer */
|
||||
typedef void (*ase_sll_freeer_t) (ase_sll_t* sll, void* dptr, ase_size_t dlen);
|
||||
typedef void (*ase_sll_freeer_t) (
|
||||
ase_sll_t* sll,
|
||||
void* dptr,
|
||||
ase_size_t dlen
|
||||
);
|
||||
|
||||
/****t* ase.cmn.sll/ase_sll_comper_t
|
||||
* NAME
|
||||
* ase_sll_comper_t - define a data comparator
|
||||
*
|
||||
* DESCRIPTION
|
||||
* The ase_sll_comper_t type defines a key comparator that is called when
|
||||
* the list needs to compare data. A singly linked list is created with a
|
||||
* default comparator that performs bitwise comparison.
|
||||
*
|
||||
* The comparator should return 0 if the data are the same and a non-zero
|
||||
* integer otherwise.
|
||||
*
|
||||
* SYNOPSIS
|
||||
*/
|
||||
typedef int (*ase_sll_comper_t) (
|
||||
ase_sll_t* sll /* a singly linked list */,
|
||||
const void* dptr1 /* a data pointer */,
|
||||
ase_size_t dlen1 /* a data length */,
|
||||
const void* dptr2 /* a data pointer */,
|
||||
ase_size_t dlen2 /* a data length */
|
||||
);
|
||||
/******/
|
||||
|
||||
/* node visitor */
|
||||
typedef ase_sll_walk_t (*ase_sll_walker_t) (
|
||||
@ -46,6 +77,7 @@ struct ase_sll_t
|
||||
|
||||
ase_sll_copier_t copier;
|
||||
ase_sll_freeer_t freeer;
|
||||
ase_sll_comper_t comper;
|
||||
ase_byte_t scale;
|
||||
|
||||
ase_size_t size;
|
||||
@ -241,6 +273,15 @@ void ase_sll_setfreeer (
|
||||
);
|
||||
/******/
|
||||
|
||||
ase_sll_comper_t ase_sll_getcomper (
|
||||
ase_sll_t* sll
|
||||
);
|
||||
|
||||
void ase_sll_setcomper (
|
||||
ase_sll_t* sll /* a singly linked list */,
|
||||
ase_sll_comper_t comper /* a comparator */
|
||||
);
|
||||
|
||||
/*
|
||||
* NAME: Gets the head(first) node
|
||||
* RETURN: the tail node of a singly linked list
|
||||
@ -257,6 +298,22 @@ ase_sll_node_t* ase_sll_gettail (
|
||||
ase_sll_t* sll /* a singly linked list */
|
||||
);
|
||||
|
||||
/****f* ase.cmn.sll/ase_sll_search
|
||||
* NAME
|
||||
* ase_sll_search - find a node
|
||||
*
|
||||
* DESCRIPTION
|
||||
* The ase_sll_search() function traverses the list to find a node containing
|
||||
* the same value as the the data pointer and length.
|
||||
*
|
||||
* SYNOPSIS
|
||||
*/
|
||||
ase_sll_node_t* ase_sll_search (
|
||||
ase_sll_t* sll /* a singly linked list */,
|
||||
const void* dptr /* a data pointer */,
|
||||
ase_size_t dlen /* a data length */
|
||||
);
|
||||
|
||||
/****f* ase.cmn.sll/ase_sll_insert
|
||||
* NAME
|
||||
* ase_sll_insert - insert data before a positional node given
|
||||
|
@ -11,6 +11,7 @@
|
||||
#define node_t ase_sll_node_t
|
||||
#define copier_t ase_sll_copier_t
|
||||
#define freeer_t ase_sll_freeer_t
|
||||
#define comper_t ase_sll_comper_t
|
||||
#define walker_t ase_sll_walker_t
|
||||
|
||||
#define HEAD(s) ASE_SLL_HEAD(s)
|
||||
@ -26,6 +27,15 @@
|
||||
#define size_t ase_size_t
|
||||
#define mmgr_t ase_mmgr_t
|
||||
|
||||
static int comp_data (sll_t* sll,
|
||||
const void* dptr1, size_t dlen1,
|
||||
const void* dptr2, size_t dlen2)
|
||||
{
|
||||
if (dlen1 == dlen2) return ASE_MEMCMP (dptr1, dptr2, TOB(sll,dlen1));
|
||||
/* it just returns 1 to indicate that they are different. */
|
||||
return 1;
|
||||
}
|
||||
|
||||
sll_t* ase_sll_open (mmgr_t* mmgr, size_t ext)
|
||||
{
|
||||
sll_t* sll;
|
||||
@ -59,6 +69,8 @@ sll_t* ase_sll_init (sll_t* sll, mmgr_t* mmgr)
|
||||
sll->mmgr = mmgr;
|
||||
sll->size = 0;
|
||||
sll->scale = 1;
|
||||
|
||||
sll->comper = comp_data;
|
||||
return sll;
|
||||
}
|
||||
|
||||
@ -133,6 +145,16 @@ void ase_sll_setfreeer (sll_t* sll, freeer_t freeer)
|
||||
sll->freeer = freeer;
|
||||
}
|
||||
|
||||
comper_t ase_sll_getcomper (sll_t* sll)
|
||||
{
|
||||
return sll->comper;
|
||||
}
|
||||
|
||||
void ase_sll_setcomper (sll_t* sll, comper_t comper)
|
||||
{
|
||||
sll->comper = comper;
|
||||
}
|
||||
|
||||
static node_t* alloc_node (sll_t* sll, void* dptr, size_t dlen)
|
||||
{
|
||||
node_t* n;
|
||||
|
Loading…
Reference in New Issue
Block a user