Skip to content

Commit

Permalink
Differentiate between entry index and work address
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd committed Dec 1, 2017
1 parent 9614a29 commit fad926b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 26 deletions.
Expand Up @@ -27,12 +27,15 @@
* Table implementation for handling primitive int/long keys and hop bits. The quantized unit is int so a
* multiple of ints will be used for every entry.
*
* In this class, <code>index</code> refers to the index of an entry (key + value + hop bits), while
* <code>address</code> refers to the position of an int word in the internal <code>table</code>.
*
* @param <VALUE> essentially ignored, since no values are stored in this table. Although subclasses can.
*/
public abstract class IntArrayBasedKeyTable<VALUE> extends PowerOfTwoQuantizedTable<VALUE>
{
protected int[] table;
protected final VALUE singleValue;
protected final VALUE singleValue; // used as a pointer to pass around a primitive value in concrete subclasses
private final int itemsPerEntry;

protected IntArrayBasedKeyTable( int capacity, int itemsPerEntry, int h, VALUE singleValue )
Expand Down Expand Up @@ -72,17 +75,17 @@ protected long getLong( int actualIndex )
@Override
public void put( int index, long key, VALUE value )
{
int actualIndex = index( index );
internalPut( actualIndex, key, value );
int address = address( index );
internalPut( address, key, value );
size++;
}

@Override
public VALUE remove( int index )
{
int actualIndex = index( index );
int address = address( index );
VALUE value = value( index );
internalRemove( actualIndex );
internalRemove( address );
size--;
return value;
}
Expand All @@ -91,13 +94,13 @@ public VALUE remove( int index )
public long move( int fromIndex, int toIndex )
{
long key = key( fromIndex );
int actualFromIndex = index( fromIndex );
int actualToIndex = index( toIndex );
int fromAddress = address( fromIndex );
int toAddress = address( toIndex );
for ( int i = 0; i < itemsPerEntry - 1; i++ )
{
int tempValue = table[actualFromIndex + i];
table[actualFromIndex + i] = table[actualToIndex + i];
table[actualToIndex + i] = tempValue;
int tempValue = table[fromAddress + i];
table[fromAddress + i] = table[toAddress + i];
table[toAddress + i] = tempValue;
}
return key;
}
Expand All @@ -124,7 +127,7 @@ public VALUE putValue( int index, VALUE value )
@Override
public long hopBits( int index )
{
return ~(table[index( index ) + itemsPerEntry - 1] | 0xFFFFFFFF00000000L);
return ~(table[address( index ) + itemsPerEntry - 1] | 0xFFFFFFFF00000000L);
}

private int hopBit( int hd )
Expand All @@ -135,22 +138,22 @@ private int hopBit( int hd )
@Override
public void putHopBit( int index, int hd )
{
table[index( index ) + itemsPerEntry - 1] &= ~hopBit( hd );
table[address( index ) + itemsPerEntry - 1] &= ~hopBit( hd );
}

@Override
public void moveHopBit( int index, int hd, int delta )
{
table[index( index ) + itemsPerEntry - 1] ^= hopBit( hd ) | hopBit( hd + delta );
table[address( index ) + itemsPerEntry - 1] ^= hopBit( hd ) | hopBit( hd + delta );
}

@Override
public void removeHopBit( int index, int hd )
{
table[index( index ) + itemsPerEntry - 1] |= hopBit( hd );
table[address( index ) + itemsPerEntry - 1] |= hopBit( hd );
}

protected int index( int index )
protected int address( int index )
{
return index * itemsPerEntry;
}
Expand Down
Expand Up @@ -31,7 +31,7 @@ public IntKeyLongValueTable( int capacity )
@Override
public long key( int index )
{
return table[index( index )];
return table[address( index )];
}

@Override
Expand All @@ -44,14 +44,14 @@ protected void internalPut( int actualIndex, long key, long[] valueHolder )
@Override
public long[] value( int index )
{
singleValue[0] = getLong( index( index ) + 1 );
singleValue[0] = getLong( address( index ) + 1 );
return singleValue;
}

@Override
public long[] putValue( int index, long[] value )
{
singleValue[0] = putLong( index( index ) + 1, value[0] );
singleValue[0] = putLong( address( index ) + 1, value[0] );
return singleValue;
}

Expand Down
Expand Up @@ -29,7 +29,7 @@ public IntKeyTable( int capacity, VALUE singleValue )
@Override
public long key( int index )
{
return table[index( index )];
return table[address( index )];
}

@Override
Expand Down
Expand Up @@ -31,7 +31,7 @@ public LongKeyIntValueTable( int capacity )
@Override
public long key( int index )
{
return getLong( index( index ) );
return getLong( address( index ) );
}

@Override
Expand All @@ -44,7 +44,7 @@ protected void internalPut( int actualIndex, long key, int[] value )
@Override
public int[] putValue( int index, int[] value )
{
int actualIndex = index( index ) + 2;
int actualIndex = address( index ) + 2;
int previous = table[actualIndex];
table[actualIndex] = value[0];
return pack( previous );
Expand All @@ -53,7 +53,7 @@ public int[] putValue( int index, int[] value )
@Override
public int[] value( int index )
{
return pack( table[index( index ) + 2] );
return pack( table[address( index ) + 2] );
}

@Override
Expand Down
Expand Up @@ -32,7 +32,7 @@ public LongKeyLongValueTable( int capacity )
@Override
public long key( int index )
{
return getLong( index( index ) );
return getLong( address( index ) );
}

@Override
Expand All @@ -45,7 +45,7 @@ protected void internalPut( int actualIndex, long key, long[] value )
@Override
public long[] putValue( int index, long[] value )
{
int actualValueIndex = index( index ) + 2;
int actualValueIndex = address( index ) + 2;
long previous = getLong( actualValueIndex );
putLong( actualValueIndex, value[0] );
return pack( previous );
Expand All @@ -54,7 +54,7 @@ public long[] putValue( int index, long[] value )
@Override
public long[] value( int index )
{
return pack( getLong( index( index ) + 2 ) );
return pack( getLong( address( index ) + 2 ) );
}

@Override
Expand Down
Expand Up @@ -30,7 +30,7 @@ public LongKeyTable( int capacity, VALUE singleValue )
@Override
public long key( int index )
{
return getLong( index( index ) );
return getLong( address( index ) );
}

@Override
Expand Down

0 comments on commit fad926b

Please sign in to comment.