7 * Representing a vector of void* accessible via an indexing structure
8 * as levels of same-size pages. A "vector_page" is a contiguous array
9 * void*, and an index is "unsigned long" (64 bits).
12 /** ============================================================ **/
14 static int VECTOR_BITS[4] = { 8, 4, 2, 64 };
33 * Return the index part for the given level of the vector's leveling
36 * The vector variant indicates whether indexing uses 8, 4, or 2 bits
39 unsigned long VECTOR_INDEX_PART(vector *pv,vector_index *index, int level) {
40 unsigned char *px = (unsigned char *) index;
41 switch ( pv->variant ) {
43 byte *pp = (byte*)(px + level);
47 nibble *pp = (nibble*)(px + ( level / 2 ));
48 switch ( level & 1 ) {
55 bitpair *pp = (bitpair*)(px + ( level / 4 ));
56 switch ( level & 3 ) {
71 * Increment the index part at the indivated level, cyclic but not
72 * carrying over to the upper level. Returns the new level index.
74 static unsigned long VECTOR_INDEX_PART_INC(
75 vector *pv,vector_index *index, int level)
77 unsigned char *px = (unsigned char *) index;
78 switch ( pv->variant ) {
80 byte *pp = (byte*)( px + level );
84 nibble *pp = (nibble*)( px + ( level / 2 ) );
85 switch ( level & 1 ) {
86 case 0: return ++(pp->a);
87 case 1: return ++(pp->b);
92 bitpair *pp = (bitpair*)( px + level / 4 );
93 switch ( level & 0xf ) {
94 case 0: return ++(pp->a);
95 case 1: return ++(pp->b);
96 case 2: return ++(pp->c);
97 case 3: return ++(pp->d);
108 * Decrement the index part at the indicated level, cyclic but not
109 * carrying over to the upper level. Returns the prior level index.
111 static unsigned long VECTOR_INDEX_PART_DEC(
112 vector *pv,vector_index *index, int level)
114 unsigned char *px = (unsigned char *) index;
115 switch ( pv->variant ) {
117 byte *pp = (byte*)( px + level );
121 nibble *pp = (nibble*)( px + ( level / 2 ) );
122 switch ( level & 1 ) {
123 case 0: return (pp->a)--;
124 case 1: return (pp->b)--;
129 bitpair *pp = (bitpair*)( px + level / 4 );
130 switch ( level & 0xf ) {
131 case 0: return (pp->a)--;
132 case 1: return (pp->b)--;
133 case 2: return (pp->c)--;
134 case 3: return (pp->d)--;
144 #define ONES (~((vector_index) 0))
146 // Set index to last value for all index parts at level and lower.
147 static void VECTOR_INDEX_FIRST(vector *pv,vector_index *index, int level) {
148 (*index) &= ONES << ( VECTOR_BITS[ pv->variant ] * level );
151 // Set index to last value for all index parts at level and lower.
152 static void VECTOR_INDEX_LAST(vector *pv,vector_index *index, int level) {
153 (*index) |= ONES >> ( 64 - VECTOR_BITS[ pv->variant ] * level );
156 // Return number of slots for a vector variant.
157 unsigned long VECTOR_SLOTS(vector *pv) {
158 switch ( pv->variant ) {
162 case 3: return pv->size;
167 // The number of levels to span vector pv wrt its size and variant
168 static unsigned int vector_levels(vector *pv,unsigned int size) {
172 switch ( pv->variant ) {
173 case 0: return ((int)(log2( size - 1 ) / 8)) + 1;
174 case 1: return ((int)(log2( size - 1 ) / 4)) + 1;
175 case 2: return ((int)(log2( size - 1 ) / 2)) + 1;
181 /** ============================================================ **/
184 * Advances a vector index to the next used slot at or below the
185 * given level, starting from the indexed entry (inclusive) and up.
186 * The function will free any empty pages it discovers, and then
187 * update the index slots accordingly. The given index is advanced
188 * cyclically to match the found slot. The function returns a slot
189 * pointer to the used slot, if any, and 0 otherwise.
191 static void **vector_level_next_used(
198 void **p = (void**)&(*page)[ VECTOR_INDEX_PART( pv, index, level ) ];
199 for( ; *index < end; p++ ) {
202 return p; // This is a used entry
204 // *p is an index that needs to be inspected recursively
205 void **x = vector_level_next_used( pv, *p, index, level - 1, end );
207 return x; // Used slot was found; return it.
209 // If the page *p is all empty, so can/should be reclaimed.
212 VECTOR_INDEX_FIRST( pv, index, level );
215 if ( VECTOR_INDEX_PART_INC( pv, index, level ) == 0 ) {
216 break; // cycling this level => nothing found
222 // Find the next used slot at given index or later. Returns pointer to
223 // the slot. This allows for a reclaim function that may reclaim slot
224 // items on the way to next used slot.
225 void **vector_next_used(vector *pv,vector_index *index) {
226 if ( pv->entries == 0 || *index >= pv->size ) {
230 int levels = vector_levels( pv, pv->size );
231 for ( ; *index < pv->size; (*index)++ ) {
232 void **slot = vector_level_next_used(
233 pv, pv->entries, index, levels - 1, pv->size ) ;
235 *index = pv->size; // reached the end of the vector
236 } else if ( *slot == 0 ) {
246 * Advances a vector index to the prior used slot at or below the
247 * given level, starting from the indexed entry (inclusive) and down.
248 * The function will free any empty pages it discovers, and then
249 * update the index slots accordingly. The given index is advanced
250 * cyclically to match the found slot. The function returns a slot
251 * pointer to the used slot, if any, and 0 otherwise.
253 static void **vector_level_prev_used(
259 void **p = (void**)&(*page)[ VECTOR_INDEX_PART( pv, index, level ) ];
263 return p; // This is a used entry
265 // *p is an index that needs to be inspected recursively
266 void **x = vector_level_prev_used( pv, *p, index, level - 1 );
268 return x; // Used slot was found; return it.
270 // If the page *p is all empty, so can/should be reclaimed.
273 VECTOR_INDEX_LAST( pv, index, level );
277 } while ( VECTOR_INDEX_PART_DEC( pv, index, level ) != 0 );
281 // Find the next used slot at given index or later. Returns pointer to
282 // the slot. This allows for a reclaim function that may reclaim slot
283 // items on the way to next used slot.
284 void **vector_prev_used(vector *pv,vector_index *index) {
285 if ( pv->entries == 0 || *index >= pv->size ) {
289 int levels = vector_levels( pv, pv->size );
291 void **slot = vector_level_prev_used(
292 pv, pv->entries, index, levels - 1 ) ;
294 break; // reached the end of the vector
299 } while ( (*index)-- != 0 );
307 // Find the first in-use slot at or before the index, at the level
308 static void **vector_prev_used_level(vector *pv,vector_index *index,int lv) {
309 void **slot = vector_access( pv, *index, lv, 0 );
318 void **sub = vector_prev_used_level( pv, index, lv - 1 );
324 } while ( VECTOR_INDEX_PART_DEC( pv, index, lv ) != 0 );
328 // Find nearest used slot at or prior to the given index.
329 void **vector_prev_used(vector *pv,vector_index *index) {
330 if ( pv->entries == 0 || *index >= pv->size ) {
334 void **slot = vector_prev_used_level(
335 pv, index, vector_levels( pv, pv->size ) - 1 );
343 // Reclaim tree of unused pages for a given level
344 static void vector_reclaim(vector *pv,vector_page *page,unsigned int level) {
347 for ( ; i < VECTOR_SLOTS( pv ); i++ ) {
349 vector_reclaim( pv, (vector_page *) (*page)[i], level - 1 );
356 // Resize vector, using the reclaim function as needed, to handle any
357 // excess items or to veto the resize. Returns the index of the veto,
358 // if any, or <0 otherwise, with -1 indicating success and -2
361 // Note that resizing may result in the introduction/removal of
362 // indexing levels and pages, so as to keep the leveling accurate for
365 vector *pv,vector_index new_size,
366 int (*reclaim)(vector *pv,vector_index index,void *item,void *data),
373 vector_levels( pv, pv->size ),
374 vector_levels( pv, new_size )
376 vector_page *entries = 0;
377 if ( pv->entries == 0 ) {
381 // A shrinking vector might be veto-ed
382 if ( new_size < pv->size ) {
383 vector_index index = new_size;
384 void **slot = vector_next_used( pv, &index );
386 if ( *slot && reclaim && reclaim( pv, index, *slot, data ) == 0 ) {
388 slot = vector_next_used( pv, &index );
393 // At this point we know that there are no slots used after
394 // the new_size size, so now it's time to remove and reclaim
395 // any superflouous top level pages.
396 if ( pv->variant == 3 ) { // Follow vector size using realloc
397 if ( new_size > 0 ) {
398 entries = (vector_page*)
399 realloc( pv->entries, new_size * sizeof( void* ) );
400 if ( entries == 0 ) {
404 pv->entries = entries;
406 vector_page **pp = &pv->entries;
408 while ( i-- > level.new ) {
410 pp = (vector_page **)(*pp);
413 if ( pp != &pv->entries ) {
414 entries = pv->entries;
417 *pp = 0; // Detach subtree
421 vector_reclaim( pv, entries, level.old - 1 );
423 if ( new_size == 0 && pv->entries ) {
429 // vector is growing. Maybe insert levels.
430 if ( pv->variant == 3 ) { // Follow vector size using realloc
431 entries = (vector_page *)realloc(
432 pv->entries, new_size * sizeof( void* ) );
433 if ( entries == 0 ) {
436 pv->entries = entries;
437 memset( &(*entries)[ pv->size ], 0,
438 ( new_size - pv->size ) * sizeof( void* ) );
440 for ( ; level.old < level.new; level.old++ ) {
441 vector_page *p = (vector_page *)
442 calloc( VECTOR_SLOTS( pv ), sizeof( void* ) );
446 (*p)[0] = pv->entries;
448 // Should maybe change the size to match the level?
449 // otherwise recovery from OOM is impossible
457 // Return pointer to the indexed page slot at the requested level, and
458 // adding intermediate index pages if so requested. Returns 0 if
459 // addition fails (OOM), or if not requested and page is missing.
460 void **vector_access(vector *pv,vector_index index,int level,int add) {
461 if ( index >= pv->size ) {
464 void **page = (void**) &pv->entries;
465 int i = vector_levels( pv, pv->size );
466 while ( i-- > level ) {
467 if ( add && (*page) == 0 ) {
468 (*page) = calloc( VECTOR_SLOTS( pv ), sizeof( void* ) );
474 page += VECTOR_INDEX_PART( pv, &index, i );
479 // Map index into a value slot
480 void **vector_entry(vector *pv,vector_index index) {
481 return vector_access( pv, index, 0, 1 );
484 inline void vector_set(vector *pv,vector_index index,void *value) {
485 void **p = vector_entry( pv, index );
489 // Set value at index but return the old value
490 void *vector_get_set(vector *pv,vector_index index,void *value) {
491 void **p = vector_entry( pv, index );
497 inline void *vector_get(vector *pv,vector_index index) {
498 void **p = vector_entry( pv, index );
502 int vector_reclaim_any(vector *pv,vector_index ix,void *item,void *data) {
507 void vector_append(vector *pv,void *value) {
508 vector_resize( pv, pv->size + 1, 0, 0 );
509 vector_set( pv, pv->size - 1, value );
512 // copy block of n items from src[si] to dst[di]
513 // no efficiency hacks
514 void vector_copy(vector *dst,vector_index di,
515 vector *src,vector_index si,vector_index n) {
516 if ( dst != src || di < si ) {
518 vector_set( dst, di++, vector_get( src, si++ ) );
520 } else if ( di > si ){
524 vector_set( dst, di--, vector_get( src, si-- ) );
529 void vector_dump(vector *pv,
530 void (*itemdump)(const vector_index,const void *)) {
531 vector_index index = 0;
532 for ( ; index < pv->size; index++ ) {
533 void **slot = vector_next_used( pv, &index );
537 itemdump( index, *slot );
543 // Returns 1 for "in order", 0 for equal, and -1 for "wrong order"
544 typedef int (*comparfn)(const void *,const void *);
546 static void vector_qsort_part(
547 vector *pv,comparfn compar,
548 vector_index low,vector_index high)
553 vector_index lo = low;
554 vector_index m = high - 1;
560 vector_index hi = m - 1;
561 void **mp = vector_entry( pv, m );
564 // Find index of first item "above" mp scanning from lo and up
565 for ( ; lo < m; lo++ ) {
566 lop = vector_entry( pv, lo );
567 if ( compar( *lop, *mp ) < 0 ) {
571 // if lo == m, then lop is wrong!!
572 // Find index of first item "below" mp scanning from hi and down
573 for ( ; hi > lo; hi-- ) {
574 hip = vector_entry( pv, hi );
575 if ( compar( *mp, *hip ) < 0 ) {
592 vector_qsort_part( pv, compar, low, m );
593 vector_qsort_part( pv, compar, m+1, high );
596 void vector_qsort(vector *pv,comparfn compar) {
597 vector_qsort_part( pv, compar, 0, pv->size );
600 void vector_iterate(vector *pv,
602 int (*itemfn)(vector_index,void*,void*),
605 vector_index index = start;
606 while ( index < pv->size ) {
607 void **slot = vector_next_used( pv, &index );
611 int end = VECTOR_SLOTS( pv );
612 int i = index & ( end - 1 );
613 for ( ; i < end && index < pv->size; i++, index++, slot++ ) {
614 if ( itemfn( index, *slot, data ) ) {
621 // Find surrounding indexes for a given item key in a sparse vector
622 void *vector_bsearch(vector *pv,vector_index *index,const void *key,
623 int (*compare)(const void *key, const void *item)) {
625 vector_index hi = pv->size;
626 if ( hi-- == 0 || vector_prev_used( pv, &hi ) == 0 ) {
631 vector_index m = lo + ( hi - lo ) / 2;
632 void **slot = vector_next_used( pv, &m );
633 int c = compare( key, *slot );
647 // Iterator callback.
648 static int checkunused(vector_index index,void *item,void *data) {
649 vector_index *last = (vector_index*) data;
654 if ( *last > index ) {
655 // Only on the first iteration, with *last = vector_sie
661 } else if ( index == (*last) ) {
668 // Scan forward for the next unused vector slot
669 vector_index vector_next_unused(vector *pv,vector_index index) {
670 vector_index unused = vector_size( pv );
671 vector_iterate( pv, index, checkunused, &unused );