5 * A vector is a dynamic pointer array implemented as an access tree
6 * of index pages. The indexing is done using "unsigned long" indexes.
9 #ifndef VECTOR_LEVEL_BITS
11 * Macro: VECTOR_LEVEL_BITS
12 * This defines the number of bits in the indexing bit field.
14 #define VECTOR_LEVEL_BITS 8
19 * This is the general indexing used for vector access.
21 typedef unsigned long vector_index;
24 * Macro: VECTOR_INDEX_BITS
25 * This defines the number of bits of a vector index
27 #define VECTOR_INDEX_BITS ( sizeof( vector_index ) * 8 )
30 * Macro: VECTOR_INDEX_FIELDS
31 * This defines the number of bit fields in an vector index
33 #define VECTOR_INDEX_FIELDS \
34 ( ( VECTOR_INDEX_BITS - 1 ) / VECTOR_LEVEL_BITS + 1 )
38 * This defines the number of slots spanned by an index level
40 #define VECTOR_SLOTS ( 1 << VECTOR_LEVEL_BITS )
45 * A vector_page is an array of 16 void* items.
47 typedef void* vector_page[ VECTOR_SLOTS ];
52 * A vector is a compound of a size and a vector_page pointer, which
53 * when non-null points out the top-most page of the vector. The
54 * number of levels is derived from its size with level 0 being the
55 * leaf level of actual content. E.g., a vector larger than 16
56 * items, has at least two levels, and generally N levels may span up
57 * to 16^N content entries.
59 typedef struct _vector {
60 vector_index size; //!< Limit for the logical entries[]
61 vector_page *entries; //!< Pointer to entries indexing
65 * Find the next used slot at given index or later. With a reclaim
66 * function, it will be invoked for verifying that the item is
67 * actually in use, in which case it returns 1. Otherwise it should
68 * reclaim any memory for the item and return 0;
70 void **vector_next_used(
71 vector *pv,vector_index *index,
72 int (*reclaim)(vector *pv,vector_index index,void *item,void *data),
76 * Function: int vector_resize(
77 * vector *pv,vector_index new_size,
78 * int (*reclaim)(vector *,vector_index,void *item,void *data),
85 * Tries to resize the given vector to a new size. This may result in
86 * the introduction or removal of indexing pages, so that the leveling
87 * is consistent with the vector size. Thus, if it grows into a new
88 * 16^N level, then one or more new upper level pages are inserted as
89 * needed. If it shrinks below the current level, then top-level pages
92 * Also, if the new size is smaller than currently, then the now
93 * excess tail of entries is scanned for any used slots and the given
94 * reclaim function is invoked successively for these. The reclaim
95 * function must, in addition to memory-managing the entry, return 0
96 * upon success and non-zero to veto the attempted vector size
97 * change. The data argument is passed on to the reclaim function.
99 * The vector_resize function returns 0 on success, with the size
100 * duly changed. Otherwise the function retains the current size and
101 * returns -index-1 for the index of the veto-ed entry.
104 vector *pv, vector_index new_size,
105 int (*reclaim)(vector *pv,vector_index index,void *item,void *data),
109 * Function: void **vector_entry(vector *pv,vector_index index)
110 * \param pv - the vector record
111 * \param index - the slot index
113 * [pgix,epix] = modulo( index, pv->page );
115 * \returns a direct pointer to the slot of the given index in the
116 * array, or 0 if the index is beyond the array limits (0-limit). Note
117 * that slot pointers are only valid while the vector size is
120 extern void **vector_entry(vector *pv,vector_index index);
123 * Function: vector_index vector_size(vector *pv)
124 * \param pv - the vector record
125 * \returns the size of the vector.
127 inline vector_index vector_size(vector *pv) {
131 void vector_set(vector *pv,vector_index index,void *value);
133 void *vector_get(vector *pv,vector_index index);
135 void vector_append(vector *pv,void *value);
137 void vector_copy(vector *dst,vector_index di,
138 vector *src,vector_index si,vector_index n);
140 void vector_dump(vector *pv,
141 int (*itemdump)(const vector_index ,const void *));
143 void vector_qsort(vector *pv,int (*compar)(const void *,const void *));
145 void vector_iterate(vector *pv,
146 int (*itemfn)(vector_index,void*,void*),