+/** ============================================================ **/
+
+static int VECTOR_BITS[4] = { 8, 4, 2, 64 };
+
+typedef struct {
+ unsigned int a:2;
+ unsigned int b:2;
+ unsigned int c:2;
+ unsigned int d:2;
+} bitpair;
+
+typedef struct {
+ unsigned int a:4;
+ unsigned int b:4;
+} nibble;
+
+typedef struct {
+ unsigned int a:8;
+} byte;
+
+/**
+ * Return the index part for the given level of the vector's leveling
+ * variant.
+ *
+ * The vector variant indicates whether indexing uses 8, 4, or 2 bits
+ * per level.
+ */
+unsigned long VECTOR_INDEX_PART(vector *pv,vector_index *index, int level) {
+ unsigned char *px = (unsigned char *) index;
+ switch ( pv->variant ) {
+ case 0: {
+ byte *pp = (byte*)(px + level);
+ return pp->a;
+ }
+ case 1: {
+ nibble *pp = (nibble*)( px + ( level / 2 ) );
+ switch ( level & 1 ) {
+ case 0: return pp->a;
+ case 1: return pp->b;
+ }
+ break;
+ }
+ case 2: {
+ bitpair *pp = (bitpair*)( px + ( level / 4 ) );
+ switch ( level & 3 ) {
+ case 0: return pp->a;
+ case 1: return pp->b;
+ case 2: return pp->c;
+ case 3: return pp->d;
+ }
+ break;
+ }
+ case 3:
+ return (*index);
+ }
+ return 0;
+}
+
+/**
+ * Increment the index part at the indivated level, cyclic but not
+ * carrying over to the upper level. Returns the new level index.
+ */
+static unsigned long VECTOR_INDEX_PART_INC(
+ vector *pv,vector_index *index, int level)
+{
+ unsigned char *px = (unsigned char *) index;
+ switch ( pv->variant ) {
+ case 0: {
+ byte *pp = (byte*)( px + level );
+ return ++(pp->a);
+ }
+ case 1: {
+ nibble *pp = (nibble*)( px + ( level / 2 ) );
+ switch ( level & 1 ) {
+ case 0: return ++(pp->a);
+ case 1: return ++(pp->b);
+ }
+ break;
+ }
+ case 2: {
+ bitpair *pp = (bitpair*)( px + level / 4 );
+ switch ( level & 3 ) {
+ case 0: return ++(pp->a);
+ case 1: return ++(pp->b);
+ case 2: return ++(pp->c);
+ case 3: return ++(pp->d);
+ }
+ break;
+ }
+ case 3:
+ return ++(*index);
+ }
+ return 0;
+}
+
+/**
+ * Decrement the index part at the indicated level, cyclic but not
+ * carrying over to the upper level. Returns the prior level index.
+ */
+static unsigned long VECTOR_INDEX_PART_DEC(
+ vector *pv,vector_index *index, int level)
+{
+ unsigned char *px = (unsigned char *) index;
+ switch ( pv->variant ) {
+ case 0: {
+ byte *pp = (byte*)( px + level );
+ return (pp->a)--;
+ }
+ case 1: {
+ nibble *pp = (nibble*)( px + ( level / 2 ) );
+ switch ( level & 1 ) {
+ case 0: return (pp->a)--;
+ case 1: return (pp->b)--;
+ }
+ break;
+ }
+ case 2: {
+ bitpair *pp = (bitpair*)( px + level / 4 );
+ switch ( level & 0xf ) {
+ case 0: return (pp->a)--;
+ case 1: return (pp->b)--;
+ case 2: return (pp->c)--;
+ case 3: return (pp->d)--;
+ }
+ break;
+ }
+ case 3:
+ return (*index)--;
+ }
+ return 0;
+}