added a comparator to ase_sll_t

This commit is contained in:
hyung-hwan 2008-09-29 00:02:10 +00:00
parent b2ad40e12a
commit 275c6fb8ec
2 changed files with 81 additions and 2 deletions

View File

@ -22,10 +22,41 @@ typedef struct ase_sll_node_t ase_sll_node_t;
typedef enum ase_sll_walk_t ase_sll_walk_t; typedef enum ase_sll_walk_t ase_sll_walk_t;
/* data copier */ /* 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 */ /* 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 */ /* node visitor */
typedef ase_sll_walk_t (*ase_sll_walker_t) ( typedef ase_sll_walk_t (*ase_sll_walker_t) (
@ -46,6 +77,7 @@ struct ase_sll_t
ase_sll_copier_t copier; ase_sll_copier_t copier;
ase_sll_freeer_t freeer; ase_sll_freeer_t freeer;
ase_sll_comper_t comper;
ase_byte_t scale; ase_byte_t scale;
ase_size_t size; 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 * NAME: Gets the head(first) node
* RETURN: the tail node of a singly linked list * 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 */ 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 /****f* ase.cmn.sll/ase_sll_insert
* NAME * NAME
* ase_sll_insert - insert data before a positional node given * ase_sll_insert - insert data before a positional node given

View File

@ -11,6 +11,7 @@
#define node_t ase_sll_node_t #define node_t ase_sll_node_t
#define copier_t ase_sll_copier_t #define copier_t ase_sll_copier_t
#define freeer_t ase_sll_freeer_t #define freeer_t ase_sll_freeer_t
#define comper_t ase_sll_comper_t
#define walker_t ase_sll_walker_t #define walker_t ase_sll_walker_t
#define HEAD(s) ASE_SLL_HEAD(s) #define HEAD(s) ASE_SLL_HEAD(s)
@ -26,6 +27,15 @@
#define size_t ase_size_t #define size_t ase_size_t
#define mmgr_t ase_mmgr_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* ase_sll_open (mmgr_t* mmgr, size_t ext)
{ {
sll_t* sll; sll_t* sll;
@ -59,6 +69,8 @@ sll_t* ase_sll_init (sll_t* sll, mmgr_t* mmgr)
sll->mmgr = mmgr; sll->mmgr = mmgr;
sll->size = 0; sll->size = 0;
sll->scale = 1; sll->scale = 1;
sll->comper = comp_data;
return sll; return sll;
} }
@ -133,6 +145,16 @@ void ase_sll_setfreeer (sll_t* sll, freeer_t freeer)
sll->freeer = 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) static node_t* alloc_node (sll_t* sll, void* dptr, size_t dlen)
{ {
node_t* n; node_t* n;