+#if 1
+/**
+ * Advances a vector index to the prior used slot at or below the
+ * given level, starting from the indexed entry (inclusive) and down.
+ * The function will free any empty pages it discovers, and then
+ * update the index slots accordingly. The given index is advanced
+ * cyclically to match the found slot. The function returns a slot
+ * pointer to the used slot, if any, and 0 otherwise.
+ */
+static void **vector_level_prev_used(
+ vector *pv,
+ vector_page *page,
+ vector_index *index,
+ int level )
+{
+ void **p = (void**)&(*page)[ VECTOR_INDEX_PART( pv, index, level ) ];
+ do {
+ if ( *p ) {
+ if ( level == 0 ) {
+ return p; // This is a used entry
+ }
+ // *p is an index that needs to be inspected recursively
+ void **x = vector_level_prev_used( pv, *p, index, level - 1 );
+ if ( x ) {
+ return x; // Used slot was found; return it.
+ }
+ // If the page *p is all empty, so can/should be reclaimed.
+ } else {
+ if ( level > 0 ) {
+ VECTOR_INDEX_LAST( pv, index, level );
+ }
+ }
+ p--;
+ } while ( VECTOR_INDEX_PART_DEC( pv, index, level ) != 0 );
+ return 0;
+}
+
+// Find the next used slot at given index or later. Returns pointer to
+// the slot. This allows for a reclaim function that may reclaim slot
+// items on the way to next used slot.
+void **vector_prev_used(vector *pv,vector_index *index) {
+ if ( pv->entries == 0 || *index >= pv->size ) {
+ *index = pv->size;
+ return 0;
+ }
+ int levels = vector_levels( pv, pv->size );
+ do {
+ void **slot = vector_level_prev_used(
+ pv, pv->entries, index, levels - 1 ) ;
+ if ( slot == 0 ) {
+ break; // reached the end of the vector
+ }
+ if ( *slot ) {
+ return slot;
+ }
+ } while ( (*index)-- != 0 );
+ *index = pv->size;
+ return 0;
+}
+
+#endif
+
+#if 0
+// Find the first in-use slot at or before the index, at the level
+static void **vector_prev_used_level(vector *pv,vector_index *index,int lv) {
+ void **slot = vector_access( pv, *index, lv, 0 );
+ if ( slot == 0 ) {
+ return 0;
+ }
+ do {
+ if ( *slot ) {
+ if ( lv == 0 ) {
+ return slot;
+ }
+ void **sub = vector_prev_used_level( pv, index, lv - 1 );
+ if ( sub ) {
+ return sub;
+ }
+ }
+ slot--;
+ } while ( VECTOR_INDEX_PART_DEC( pv, index, lv ) != 0 );
+ return 0;
+}
+
+// Find nearest used slot at or prior to the given index.
+void **vector_prev_used(vector *pv,vector_index *index) {
+ if ( pv->entries == 0 || *index >= pv->size ) {
+ *index = pv->size;
+ return 0;
+ }
+ void **slot = vector_prev_used_level(
+ pv, index, vector_levels( pv, pv->size ) - 1 );
+ if ( slot == 0 ) {
+ *index = pv->size;
+ }
+ return slot;
+}
+#endif
+
+// Reclaim tree of unused pages for a given level
+static void vector_reclaim(vector *pv,vector_page *page,unsigned int level) {