working on the block expression compilation
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
122
lib/hcl-rbt.h
122
lib/hcl-rbt.h
@ -31,37 +31,37 @@
|
||||
|
||||
/** \file
|
||||
* This file provides a red-black tree encapsulated in the #hcl_rbt_t type that
|
||||
* implements a self-balancing binary search tree.Its interface is very close
|
||||
* implements a self-balancing binary search tree.Its interface is very close
|
||||
* to #hcl_htb_t.
|
||||
*
|
||||
* This sample code adds a series of keys and values and print them
|
||||
* in descending key order.
|
||||
* \code
|
||||
* #include <hcl-rbt.h>
|
||||
*
|
||||
*
|
||||
* static hcl_rbt_walk_t walk (hcl_rbt_t* rbt, hcl_rbt_pair_t* pair, void* ctx)
|
||||
* {
|
||||
* hcl_printf (HCL_T("key = %d, value = %d\n"),
|
||||
* *(int*)HCL_RBT_KPTR(pair), *(int*)HCL_RBT_VPTR(pair));
|
||||
* return HCL_RBT_WALK_FORWARD;
|
||||
* }
|
||||
*
|
||||
*
|
||||
* int main ()
|
||||
* {
|
||||
* hcl_rbt_t* s1;
|
||||
* int i;
|
||||
*
|
||||
*
|
||||
* s1 = hcl_rbt_open(HCL_MMGR_GETDFL(), 0, 1, 1); // error handling skipped
|
||||
* hcl_rbt_setstyle(s1, hcl_get_rbt_style(HCL_RBT_STYLE_INLINE_COPIERS));
|
||||
*
|
||||
*
|
||||
* for (i = 0; i < 20; i++)
|
||||
* {
|
||||
* int x = i * 20;
|
||||
* hcl_rbt_insert (s1, &i, HCL_SIZEOF(i), &x, HCL_SIZEOF(x)); // eror handling skipped
|
||||
* }
|
||||
*
|
||||
*
|
||||
* hcl_rbt_rwalk (s1, walk, HCL_NULL);
|
||||
*
|
||||
*
|
||||
* hcl_rbt_close (s1);
|
||||
* return 0;
|
||||
* }
|
||||
@ -71,7 +71,7 @@
|
||||
typedef struct hcl_rbt_t hcl_rbt_t;
|
||||
typedef struct hcl_rbt_pair_t hcl_rbt_pair_t;
|
||||
|
||||
/**
|
||||
/**
|
||||
* The hcl_rbt_walk_t type defines values that the callback function can
|
||||
* return to control hcl_rbt_walk() and hcl_rbt_rwalk().
|
||||
*/
|
||||
@ -98,7 +98,7 @@ typedef enum hcl_rbt_id_t hcl_rbt_id_t;
|
||||
*/
|
||||
typedef void* (*hcl_rbt_copier_t) (
|
||||
hcl_rbt_t* rbt /**< red-black tree */,
|
||||
void* dptr /**< pointer to a key or a value */,
|
||||
void* dptr /**< pointer to a key or a value */,
|
||||
hcl_oow_t dlen /**< length of a key or a value */
|
||||
);
|
||||
|
||||
@ -119,17 +119,17 @@ typedef void (*hcl_rbt_freeer_t) (
|
||||
* key is greater than the second key, -1 otherwise.
|
||||
*/
|
||||
typedef int (*hcl_rbt_comper_t) (
|
||||
const hcl_rbt_t* rbt, /**< red-black tree */
|
||||
const hcl_rbt_t* rbt, /**< red-black tree */
|
||||
const void* kptr1, /**< key pointer */
|
||||
hcl_oow_t klen1, /**< key length */
|
||||
hcl_oow_t klen1, /**< key length */
|
||||
const void* kptr2, /**< key pointer */
|
||||
hcl_oow_t klen2 /**< key length */
|
||||
);
|
||||
|
||||
/**
|
||||
* The hcl_rbt_keeper_t type defines a value keeper that is called when
|
||||
* The hcl_rbt_keeper_t type defines a value keeper that is called when
|
||||
* a value is retained in the context that it should be destroyed because
|
||||
* it is identical to a new value. Two values are identical if their
|
||||
* it is identical to a new value. Two values are identical if their
|
||||
* pointers and lengths are equal.
|
||||
*/
|
||||
typedef void (*hcl_rbt_keeper_t) (
|
||||
@ -149,12 +149,12 @@ typedef hcl_rbt_walk_t (*hcl_rbt_walker_t) (
|
||||
|
||||
/**
|
||||
* The hcl_rbt_cbserter_t type defines a callback function for hcl_rbt_cbsert().
|
||||
* The hcl_rbt_cbserter() function calls it to allocate a new pair for the
|
||||
* The hcl_rbt_cbserter() function calls it to allocate a new pair for the
|
||||
* key pointed to by \a kptr of the length \a klen and the callback context
|
||||
* \a ctx. The second parameter \a pair is passed the pointer to the existing
|
||||
* pair for the key or #HCL_NULL in case of no existing key. The callback
|
||||
* must return a pointer to a new or a reallocated pair. When reallocating the
|
||||
* existing pair, this callback must destroy the existing pair and return the
|
||||
* existing pair, this callback must destroy the existing pair and return the
|
||||
* newly reallocated pair. It must return #HCL_NULL for failure.
|
||||
*/
|
||||
typedef hcl_rbt_pair_t* (*hcl_rbt_cbserter_t) (
|
||||
@ -174,10 +174,10 @@ enum hcl_rbt_pair_color_t
|
||||
typedef enum hcl_rbt_pair_color_t hcl_rbt_pair_color_t;
|
||||
|
||||
/**
|
||||
* The hcl_rbt_pair_t type defines red-black tree pair. A pair is composed
|
||||
* of a key and a value. It maintains pointers to the beginning of a key and
|
||||
* a value plus their length. The length is scaled down with the scale factor
|
||||
* specified in an owning tree. Use macros defined in the
|
||||
* The hcl_rbt_pair_t type defines red-black tree pair. A pair is composed
|
||||
* of a key and a value. It maintains pointers to the beginning of a key and
|
||||
* a value plus their length. The length is scaled down with the scale factor
|
||||
* specified in an owning tree. Use macros defined in the
|
||||
*/
|
||||
struct hcl_rbt_pair_t
|
||||
{
|
||||
@ -202,8 +202,8 @@ struct hcl_rbt_pair_t
|
||||
typedef struct hcl_rbt_style_t hcl_rbt_style_t;
|
||||
|
||||
/**
|
||||
* The hcl_rbt_style_t type defines callback function sets for key/value
|
||||
* pair manipulation.
|
||||
* The hcl_rbt_style_t type defines callback function sets for key/value
|
||||
* pair manipulation.
|
||||
*/
|
||||
struct hcl_rbt_style_t
|
||||
{
|
||||
@ -337,10 +337,10 @@ HCL_EXPORT const hcl_rbt_style_t* hcl_rbt_getstyle (
|
||||
);
|
||||
|
||||
/**
|
||||
* The hcl_rbt_setstyle() function sets internal manipulation callback
|
||||
* The hcl_rbt_setstyle() function sets internal manipulation callback
|
||||
* functions for data construction, destruction, comparison, etc.
|
||||
* The callback structure pointed to by \a style must outlive the tree
|
||||
* pointed to by \a htb as the tree doesn't copy the contents of the
|
||||
* pointed to by \a htb as the tree doesn't copy the contents of the
|
||||
* structure.
|
||||
*/
|
||||
HCL_EXPORT void hcl_rbt_setstyle (
|
||||
@ -356,10 +356,10 @@ HCL_EXPORT hcl_oow_t hcl_rbt_getsize (
|
||||
);
|
||||
|
||||
/**
|
||||
* The hcl_rbt_search() function searches red-black tree to find a pair with a
|
||||
* The hcl_rbt_search() function searches red-black tree to find a pair with a
|
||||
* matching key. It returns the pointer to the pair found. If it fails
|
||||
* to find one, it returns HCL_NULL.
|
||||
* \return pointer to the pair with a maching key,
|
||||
* \return pointer to the pair with a maching key,
|
||||
* or HCL_NULL if no match is found.
|
||||
*/
|
||||
HCL_EXPORT hcl_rbt_pair_t* hcl_rbt_search (
|
||||
@ -369,12 +369,12 @@ HCL_EXPORT hcl_rbt_pair_t* hcl_rbt_search (
|
||||
);
|
||||
|
||||
/**
|
||||
* The hcl_rbt_upsert() function searches red-black tree for the pair with a
|
||||
* The hcl_rbt_upsert() function searches red-black tree for the pair with a
|
||||
* matching key. If one is found, it updates the pair. Otherwise, it inserts
|
||||
* a new pair with the key and the value given. It returns the pointer to the
|
||||
* a new pair with the key and the value given. It returns the pointer to the
|
||||
* pair updated or inserted.
|
||||
* \return a pointer to the updated or inserted pair on success,
|
||||
* HCL_NULL on failure.
|
||||
* \return a pointer to the updated or inserted pair on success,
|
||||
* HCL_NULL on failure.
|
||||
*/
|
||||
HCL_EXPORT hcl_rbt_pair_t* hcl_rbt_upsert (
|
||||
hcl_rbt_t* rbt, /**< red-black tree */
|
||||
@ -386,9 +386,9 @@ HCL_EXPORT hcl_rbt_pair_t* hcl_rbt_upsert (
|
||||
|
||||
/**
|
||||
* The hcl_rbt_ensert() function inserts a new pair with the key and the value
|
||||
* given. If there exists a pair with the key given, the function returns
|
||||
* given. If there exists a pair with the key given, the function returns
|
||||
* the pair containing the key.
|
||||
* \return pointer to a pair on success, HCL_NULL on failure.
|
||||
* \return pointer to a pair on success, HCL_NULL on failure.
|
||||
*/
|
||||
HCL_EXPORT hcl_rbt_pair_t* hcl_rbt_ensert (
|
||||
hcl_rbt_t* rbt, /**< red-black tree */
|
||||
@ -400,9 +400,9 @@ HCL_EXPORT hcl_rbt_pair_t* hcl_rbt_ensert (
|
||||
|
||||
/**
|
||||
* The hcl_rbt_insert() function inserts a new pair with the key and the value
|
||||
* given. If there exists a pair with the key given, the function returns
|
||||
* given. If there exists a pair with the key given, the function returns
|
||||
* HCL_NULL without channging the value.
|
||||
* \return pointer to the pair created on success, HCL_NULL on failure.
|
||||
* \return pointer to the pair created on success, HCL_NULL on failure.
|
||||
*/
|
||||
HCL_EXPORT hcl_rbt_pair_t* hcl_rbt_insert (
|
||||
hcl_rbt_t* rbt, /**< red-black tree */
|
||||
@ -426,7 +426,7 @@ HCL_EXPORT hcl_rbt_pair_t* hcl_rbt_update (
|
||||
);
|
||||
|
||||
/**
|
||||
* The hcl_rbt_cbsert() function inserts a key/value pair by delegating pair
|
||||
* The hcl_rbt_cbsert() function inserts a key/value pair by delegating pair
|
||||
* allocation to a callback function. Depending on the callback function,
|
||||
* it may behave like hcl_rbt_insert(), hcl_rbt_upsert(), hcl_rbt_update(),
|
||||
* hcl_rbt_ensert(), or totally differently. The sample code below inserts
|
||||
@ -441,7 +441,7 @@ HCL_EXPORT hcl_rbt_pair_t* hcl_rbt_update (
|
||||
* (int)HCL_RBT_VLEN(pair), HCL_RBT_VPTR(pair), (int)HCL_RBT_VLEN(pair));
|
||||
* return HCL_RBT_WALK_FORWARD;
|
||||
* }
|
||||
*
|
||||
*
|
||||
* hcl_rbt_pair_t* cbserter (
|
||||
* hcl_rbt_t* rbt, hcl_rbt_pair_t* pair,
|
||||
* void* kptr, hcl_oow_t klen, void* ctx)
|
||||
@ -449,53 +449,53 @@ HCL_EXPORT hcl_rbt_pair_t* hcl_rbt_update (
|
||||
* hcl_cstr_t* v = (hcl_cstr_t*)ctx;
|
||||
* if (pair == HCL_NULL)
|
||||
* {
|
||||
* // no existing key for the key
|
||||
* // no existing key for the key
|
||||
* return hcl_rbt_allocpair (rbt, kptr, klen, v->ptr, v->len);
|
||||
* }
|
||||
* else
|
||||
* {
|
||||
* // a pair with the key exists.
|
||||
* // in this sample, i will append the new value to the old value
|
||||
* // a pair with the key exists.
|
||||
* // in this sample, i will append the new value to the old value
|
||||
* // separated by a comma
|
||||
* hcl_rbt_pair_t* new_pair;
|
||||
* hcl_ooch_t comma = HCL_T(',');
|
||||
* hcl_oob_t* vptr;
|
||||
*
|
||||
* // allocate a new pair, but without filling the actual value.
|
||||
* // note vptr is given HCL_NULL for that purpose
|
||||
*
|
||||
* // allocate a new pair, but without filling the actual value.
|
||||
* // note vptr is given HCL_NULL for that purpose
|
||||
* new_pair = hcl_rbt_allocpair (
|
||||
* rbt, kptr, klen, HCL_NULL, pair->vlen + 1 + v->len);
|
||||
* rbt, kptr, klen, HCL_NULL, pair->vlen + 1 + v->len);
|
||||
* if (new_pair == HCL_NULL) return HCL_NULL;
|
||||
*
|
||||
* // fill in the value space
|
||||
*
|
||||
* // fill in the value space
|
||||
* vptr = new_pair->vptr;
|
||||
* hcl_memcpy (vptr, pair->vptr, pair->vlen*HCL_SIZEOF(hcl_ooch_t));
|
||||
* vptr += pair->vlen*HCL_SIZEOF(hcl_ooch_t);
|
||||
* hcl_memcpy (vptr, &comma, HCL_SIZEOF(hcl_ooch_t));
|
||||
* vptr += HCL_SIZEOF(hcl_ooch_t);
|
||||
* hcl_memcpy (vptr, v->ptr, v->len*HCL_SIZEOF(hcl_ooch_t));
|
||||
*
|
||||
* // this callback requires the old pair to be destroyed
|
||||
*
|
||||
* // this callback requires the old pair to be destroyed
|
||||
* hcl_rbt_freepair (rbt, pair);
|
||||
*
|
||||
* // return the new pair
|
||||
*
|
||||
* // return the new pair
|
||||
* return new_pair;
|
||||
* }
|
||||
* }
|
||||
*
|
||||
*
|
||||
* int main ()
|
||||
* {
|
||||
* hcl_rbt_t* s1;
|
||||
* int i;
|
||||
* hcl_ooch_t* keys[] = { HCL_T("one"), HCL_T("two"), HCL_T("three") };
|
||||
* hcl_ooch_t* vals[] = { HCL_T("1"), HCL_T("2"), HCL_T("3"), HCL_T("4"), HCL_T("5") };
|
||||
*
|
||||
*
|
||||
* s1 = hcl_rbt_open (
|
||||
* HCL_MMGR_GETDFL(), 0,
|
||||
* HCL_SIZEOF(hcl_ooch_t), HCL_SIZEOF(hcl_ooch_t)
|
||||
* ); // note error check is skipped
|
||||
* ); // note error check is skipped
|
||||
* hcl_rbt_setstyle (s1, &style1);
|
||||
*
|
||||
*
|
||||
* for (i = 0; i < HCL_COUNTOF(vals); i++)
|
||||
* {
|
||||
* hcl_cstr_t ctx;
|
||||
@ -506,7 +506,7 @@ HCL_EXPORT hcl_rbt_pair_t* hcl_rbt_update (
|
||||
* ); // note error check is skipped
|
||||
* }
|
||||
* hcl_rbt_walk (s1, print_map_pair, HCL_NULL);
|
||||
*
|
||||
*
|
||||
* hcl_rbt_close (s1);
|
||||
* return 0;
|
||||
* }
|
||||
@ -521,7 +521,7 @@ HCL_EXPORT hcl_rbt_pair_t* hcl_rbt_cbsert (
|
||||
);
|
||||
|
||||
/**
|
||||
* The hcl_rbt_delete() function deletes a pair with a matching key
|
||||
* The hcl_rbt_delete() function deletes a pair with a matching key
|
||||
* \return 0 on success, -1 on failure
|
||||
*/
|
||||
HCL_EXPORT int hcl_rbt_delete (
|
||||
@ -538,7 +538,7 @@ HCL_EXPORT void hcl_rbt_clear (
|
||||
);
|
||||
|
||||
/**
|
||||
* The hcl_rbt_walk() function traverses a red-black tree in preorder
|
||||
* The hcl_rbt_walk() function traverses a red-black tree in preorder
|
||||
* from the leftmost child.
|
||||
*/
|
||||
HCL_EXPORT void hcl_rbt_walk (
|
||||
@ -548,7 +548,7 @@ HCL_EXPORT void hcl_rbt_walk (
|
||||
);
|
||||
|
||||
/**
|
||||
* The hcl_rbt_walk() function traverses a red-black tree in preorder
|
||||
* The hcl_rbt_walk() function traverses a red-black tree in preorder
|
||||
* from the rightmost child.
|
||||
*/
|
||||
HCL_EXPORT void hcl_rbt_rwalk (
|
||||
@ -558,11 +558,11 @@ HCL_EXPORT void hcl_rbt_rwalk (
|
||||
);
|
||||
|
||||
/**
|
||||
* The hcl_rbt_allocpair() function allocates a pair for a key and a value
|
||||
* The hcl_rbt_allocpair() function allocates a pair for a key and a value
|
||||
* given. But it does not chain the pair allocated into the red-black tree \a rbt.
|
||||
* Use this function at your own risk.
|
||||
* Use this function at your own risk.
|
||||
*
|
||||
* Take note of he following special behavior when the copier is
|
||||
* Take note of he following special behavior when the copier is
|
||||
* #HCL_RBT_COPIER_INLINE.
|
||||
* - If \a kptr is #HCL_NULL, the key space of the size \a klen is reserved but
|
||||
* not propagated with any data.
|
||||
@ -571,7 +571,7 @@ HCL_EXPORT void hcl_rbt_rwalk (
|
||||
*/
|
||||
HCL_EXPORT hcl_rbt_pair_t* hcl_rbt_allocpair (
|
||||
hcl_rbt_t* rbt,
|
||||
void* kptr,
|
||||
void* kptr,
|
||||
hcl_oow_t klen,
|
||||
void* vptr,
|
||||
hcl_oow_t vlen
|
||||
@ -595,7 +595,7 @@ HCL_EXPORT int hcl_rbt_dflcomp (
|
||||
const void* kptr1,
|
||||
hcl_oow_t klen1,
|
||||
const void* kptr2,
|
||||
hcl_oow_t klen2
|
||||
hcl_oow_t klen2
|
||||
);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
|
Reference in New Issue
Block a user