X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=pvector%2Fpvector.h;h=f2236f3898b2c8beef72ea89f92a2f308f5e77ed;hb=1d6bcdac6b5bc0c7da170bea01526a601353e9ab;hp=bc42bec1e9100ed1cb11ecb1ef2607983a703361;hpb=4a9c4d9ada3ef81415854b0ea5de844d5298eb43;p=rrq%2Frrqmisc.git diff --git a/pvector/pvector.h b/pvector/pvector.h index bc42bec..f2236f3 100644 --- a/pvector/pvector.h +++ b/pvector/pvector.h @@ -6,8 +6,6 @@ * of index pages of 256 pointers. */ -//#include - /*! * Type: pvector_page * @@ -23,8 +21,8 @@ typedef void* pvector_page[256]; * implementation assumes LE integer layout. */ typedef union { - unsigned int whole; - unsigned char level[4]; + unsigned long whole; // 64-bit unsigned integer + unsigned char level[8]; } pvector_index; /*! @@ -38,24 +36,36 @@ typedef union { * to 256^N content entries. */ typedef struct _pvector { - unsigned int size; //!< Limit for the logical entries[] + unsigned long size; //!< Limit for the logical entries[] pvector_page *entries; //!< Pointer to entries indexing } pvector; -// Number of page levels for size S -#define PV_LEVELS(S) ((int)(( 39 - __builtin_clz( ((S)-1) | 1) ) / 8 )) - +// Number of slots for page S #define PV_LEVEL_SIZE(S) ((int)(exp( 256, (S) ))) // The indexing part for level part p in index i #define PV_PART(p,i) (((unsigned char*)&i)[p]) /*! - * Function: int pvector_resize( pvector *pv,unsigned int new_size, - * int (*reclaim)(pvector *,int,void*) ) + * Find the next used slot at given index or later. With a reclaim + * function, it will be invoked for verifying that the item is + * actually in use, in which case it returns 1. Otherwise it should + * reclaim any memory for the item and return 0; + */ +void **pvector_next_used( + pvector *pv,unsigned long *index, + int (*reclaim)(pvector *pv,unsigned long index,void *item,void *data), + void *data ); + +/*! + * Function: int pvector_resize( + * pvector *pv,unsigned long new_size, + * int (*reclaim)(pvector *,unsigned long,void *item,void *data), + * void *data ) * \param pv * \param new_size * \param reclaim + * \param data * * Tries to resize the given pvector to a new size. This may result in * the introduction or removal of indexing pages, so that the leveling @@ -69,18 +79,19 @@ typedef struct _pvector { * reclaim function is invoked successively for these. The reclaim * function must, in addition to memory-managing the entry, return 0 * upon success and non-zero to veto the attempted pvector size - * change. + * change. The data argument is passed on to the reclaim function. * * The pvector_resize function returns 0 on success, with the size * duly changed. Otherwise the function retains the current size and * returns -index-1 for the index of the veto-ed entry. */ int pvector_resize( - pvector *pv,unsigned int new_size, - int (*reclaim)(pvector *,int,void*) ); + pvector *pv, unsigned long new_size, + int (*reclaim)(pvector *pv,unsigned long index,void *item,void *data), + void *data ); /*! - * Function: void **pvector_entry(pvector *pv,unsigned int index) + * Function: void **pvector_entry(pvector *pv,unsigned long index) * \param pv - the pvector record * \param index - the slot index * @@ -91,19 +102,33 @@ int pvector_resize( * that slot pointers are only valid while the pvector size is * unchanged. */ -extern void **pvector_entry(pvector *pv,unsigned int index); +extern void **pvector_entry(pvector *pv,unsigned long index); /*! - * Function: unsigned int pvector_size(pvector *pv) + * Function: unsigned long pvector_size(pvector *pv) * \param pv - the pvector record * \returns the size of the pvector. */ -inline unsigned int pvector_size(pvector *pv) { +inline unsigned long pvector_size(pvector *pv) { return pv->size; } -void pvector_set(pvector *pv,unsigned int index,void *value); +void pvector_set(pvector *pv,unsigned long index,void *value); + +void *pvector_get(pvector *pv,unsigned long index); + +void pvector_append(pvector *pv,void *value); + +void pvector_copy(pvector *dst,unsigned long di, + pvector *src,unsigned long si,unsigned long n); + +void pvector_dump(pvector *pv, + int (*itemdump)(const unsigned long ,const void *)); + +void pvector_qsort(pvector *pv,int (*compar)(const void *,const void *)); -void *pvector_get(pvector *pv,unsigned int index); +void pvector_iterate(pvector *pv, + int (*itemfn)(unsigned long,void*,void*), + void*); #endif