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 & 3 ) {
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 first 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 static unsigned long ones[] = { 255, 15, 3 };
154 unsigned long x = ones[ pv->variant ];
157 x <<= VECTOR_BITS[ pv->variant ];
160 //(*index) |= ONES >> ( 64 - ( VECTOR_BITS[ pv->variant ] * level ) );
163 // Return number of slots for a vector variant.
164 unsigned long VECTOR_SLOTS(vector *pv) {
165 switch ( pv->variant ) {
169 case 3: return pv->size;
174 // The number of levels to span vector pv wrt its size and variant
175 static unsigned int vector_levels(vector *pv,unsigned int size) {
179 switch ( pv->variant ) {
180 case 0: return ((int)(log2( size - 1 ) / 8)) + 1;
181 case 1: return ((int)(log2( size - 1 ) / 4)) + 1;
182 case 2: return ((int)(log2( size - 1 ) / 2)) + 1;
188 /** ============================================================ **/
191 * Advances a vector index to the next used slot at or below the
192 * given level, starting from the indexed entry (inclusive) and up.
193 * The function will free any empty pages it discovers, and then
194 * update the index slots accordingly. The given index is advanced
195 * cyclically to match the found slot. The function returns a slot
196 * pointer to the used slot, if any, and 0 otherwise.
197 * The last parameter is a flag that gets set when the scanning is
198 * partial (i.e. not the whole index page).
200 static void **vector_level_next_used(
207 void **p = (void**)&(*page)[ VECTOR_INDEX_PART( pv, index, level ) ];
208 if ( VECTOR_INDEX_PART( pv, index, level ) != 0 ) {
211 for( ; *index < pv->size; p++ ) {
214 return p; // This is a used entry
216 // *p is an index that needs to be inspected recursively
218 void **x = vector_level_next_used( pv, *p, index, level - 1, &w );
220 return x; // Used slot was found; return it.
222 // If the page *p is all empty, so can/should be reclaimed.
231 VECTOR_INDEX_FIRST( pv, index, level - 1 );
234 if ( VECTOR_INDEX_PART_INC( pv, index, level ) == 0 ) {
235 break; // cycling this level => nothing found
241 // Find the next used slot at given index or later. Returns pointer to
242 // the slot. This allows for a reclaim function that may reclaim slot
243 // items on the way to next used slot.
244 void **vector_next_used(vector *pv,vector_index *index) {
245 if ( pv->entries == 0 || *index >= pv->size ) {
249 int levels = vector_levels( pv, pv->size );
252 void **slot = vector_level_next_used(
253 pv, pv->entries, index, levels - 1, &partial ) ;
255 break; // reached the end of the vector
260 } while ( ++(*index) < pv->size );
261 if ( partial == 0 ) {
265 *index = pv->size; // reached the end of the vector
270 * Advances a vector index to the prior used slot at or below the
271 * given level, starting from the indexed entry (inclusive) and down.
272 * The function will free any empty pages it discovers, and then
273 * update the index slots accordingly. The given index is advanced
274 * cyclically to match the found slot. The function returns a slot
275 * pointer to the used slot, if any, and 0 otherwise.
276 * The last parameter is a flag that gets set when the scanning is
277 * partial (i.e. not the whole index page).
279 static void **vector_level_prev_used(
286 void **p = (void**)&(*page)[ VECTOR_INDEX_PART( pv, index, level ) ];
287 if ( VECTOR_INDEX_PART( pv, index, level ) != VECTOR_SLOTS( pv ) - 1 ) {
293 return p; // This is a used entry
295 // *p is an index that needs to be inspected recursively
297 void **x = vector_level_prev_used( pv, *p, index, level - 1, &w );
299 return x; // Used slot was found; return it.
301 // If the page *p is all empty, it can/should be reclaimed.
310 VECTOR_INDEX_LAST( pv, index, level );
314 } while ( VECTOR_INDEX_PART_DEC( pv, index, level ) != 0 );
318 // Find the next used slot at given index or later. Returns pointer to
319 // the slot. This allows for a reclaim function that may reclaim slot
320 // items on the way to next used slot.
321 void **vector_prev_used(vector *pv,vector_index *index) {
322 if ( pv->entries == 0 || *index >= pv->size ) {
326 int levels = vector_levels( pv, pv->size );
329 void **slot = vector_level_prev_used(
330 pv, pv->entries, index, levels - 1, &partial ) ;
332 break; // reached the end of the vector
337 } while ( (*index)-- != 0 );
338 if ( partial == 0 ) {
342 *index = pv->size; // reached the end of the vector
346 // Reclaim tree of unused pages for a given level
347 static void vector_reclaim(vector *pv,vector_page *page,unsigned int level) {
350 for ( ; i < VECTOR_SLOTS( pv ); i++ ) {
352 vector_reclaim( pv, (vector_page *) (*page)[i], level - 1 );
359 // Resize vector, using the reclaim function as needed, to handle any
360 // excess items or to veto the resize. Returns the index of the veto,
361 // if any, or <0 otherwise, with -1 indicating success and -2
364 // Note that resizing may result in the introduction/removal of
365 // indexing levels and pages, so as to keep the leveling accurate for
368 vector *pv,vector_index new_size,
369 int (*reclaim)(vector *pv,vector_index index,void *item,void *data),
376 vector_levels( pv, pv->size ),
377 vector_levels( pv, new_size )
379 vector_page *entries = 0;
380 if ( pv->entries == 0 ) {
384 // A shrinking vector might be veto-ed
385 if ( new_size < pv->size ) {
386 vector_index index = new_size;
387 void **slot = vector_next_used( pv, &index );
389 if ( *slot && reclaim && reclaim( pv, index, *slot, data ) == 0 ) {
391 slot = vector_next_used( pv, &index );
396 // At this point we know that there are no slots used after
397 // the new_size size, so now it's time to remove and reclaim
398 // any superflouous top level pages.
399 if ( pv->variant == 3 ) { // Follow vector size using realloc
400 if ( new_size > 0 ) {
401 entries = (vector_page*)
402 realloc( pv->entries, new_size * sizeof( void* ) );
403 if ( entries == 0 ) {
407 pv->entries = entries;
409 vector_page **pp = &pv->entries;
411 while ( i-- > level.new ) {
413 pp = (vector_page **)(*pp);
416 if ( pp != &pv->entries ) {
417 entries = pv->entries;
420 *pp = 0; // Detach subtree
424 vector_reclaim( pv, entries, level.old - 1 );
426 if ( new_size == 0 && pv->entries ) {
432 // vector is growing. Maybe insert levels.
433 if ( pv->variant == 3 ) { // Follow vector size using realloc
434 entries = (vector_page *)realloc(
435 pv->entries, new_size * sizeof( void* ) );
436 if ( entries == 0 ) {
439 pv->entries = entries;
440 memset( &(*entries)[ pv->size ], 0,
441 ( new_size - pv->size ) * sizeof( void* ) );
443 for ( ; level.old < level.new; level.old++ ) {
444 vector_page *p = (vector_page *)
445 calloc( VECTOR_SLOTS( pv ), sizeof( void* ) );
449 (*p)[0] = pv->entries;
451 // Should maybe change the size to match the level?
452 // otherwise recovery from OOM is impossible
460 // Return pointer to the indexed page slot at the requested level, and
461 // adding intermediate index pages if so requested. Returns 0 if
462 // addition fails (OOM), or if not requested and page is missing.
463 void **vector_access(vector *pv,vector_index index,int level,int add) {
464 if ( index >= pv->size ) {
467 void **page = (void**) &pv->entries;
468 int i = vector_levels( pv, pv->size );
469 while ( i-- > level ) {
470 if ( add && (*page) == 0 ) {
471 (*page) = calloc( VECTOR_SLOTS( pv ), sizeof( void* ) );
477 page += VECTOR_INDEX_PART( pv, &index, i );
482 // Map index into a value slot
483 void **vector_entry(vector *pv,vector_index index) {
484 return vector_access( pv, index, 0, 1 );
487 inline void vector_set(vector *pv,vector_index index,void *value) {
488 void **p = vector_entry( pv, index );
492 // Set value at index but return the old value
493 void *vector_get_set(vector *pv,vector_index index,void *value) {
494 void **p = vector_entry( pv, index );
500 inline void *vector_get(vector *pv,vector_index index) {
501 void **p = vector_entry( pv, index );
505 int vector_reclaim_any(vector *pv,vector_index ix,void *item,void *data) {
510 void vector_append(vector *pv,void *value) {
511 vector_resize( pv, pv->size + 1, 0, 0 );
512 vector_set( pv, pv->size - 1, value );
515 // copy block of n items from src[si] to dst[di]
516 // no efficiency hacks
517 void vector_copy(vector *dst,vector_index di,
518 vector *src,vector_index si,vector_index n) {
519 if ( dst != src || di < si ) {
521 vector_set( dst, di++, vector_get( src, si++ ) );
523 } else if ( di > si ){
527 vector_set( dst, di--, vector_get( src, si-- ) );
532 void vector_dump(vector *pv,
533 void (*itemdump)(const vector_index,const void *)) {
534 vector_index index = 0;
535 for ( ; index < pv->size; index++ ) {
536 void **slot = vector_next_used( pv, &index );
540 itemdump( index, *slot );
546 // Returns 1 for "in order", 0 for equal, and -1 for "wrong order"
547 typedef int (*comparfn)(const void *,const void *);
549 static void vector_qsort_part(
550 vector *pv,comparfn compar,
551 vector_index low,vector_index high)
556 vector_index lo = low;
557 vector_index m = high - 1;
563 vector_index hi = m - 1;
564 void **mp = vector_entry( pv, m );
567 // Find index of first item "above" mp scanning from lo and up
568 for ( ; lo < m; lo++ ) {
569 lop = vector_entry( pv, lo );
570 if ( compar( *lop, *mp ) < 0 ) {
574 // if lo == m, then lop is wrong!!
575 // Find index of first item "below" mp scanning from hi and down
576 for ( ; hi > lo; hi-- ) {
577 hip = vector_entry( pv, hi );
578 if ( compar( *mp, *hip ) < 0 ) {
595 vector_qsort_part( pv, compar, low, m );
596 vector_qsort_part( pv, compar, m+1, high );
599 void vector_qsort(vector *pv,comparfn compar) {
600 vector_qsort_part( pv, compar, 0, pv->size );
603 void vector_iterate(vector *pv,
605 int (*itemfn)(vector_index,void*,void*),
608 vector_index index = start;
609 while ( index < pv->size ) {
610 int end = VECTOR_SLOTS( pv );
611 int i = index & ( end - 1 );
612 for ( ; i < end && index < pv->size; i++, index++ ) {
613 void **slot = vector_access( pv, index, 0, 0 );
614 if ( itemfn( index, slot? *slot: 0, 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 );