org.cdlib.xtf.util
Class IntMultiMap

Object
  extended by IntMultiMap

public class IntMultiMap
extends Object

This class efficiently implements a "one to many" relationship between integer keys and multiple integer values. The maximum key ID is fixed at construction time, but the number of values can grow insanely large, without large penalties for resizing arrays, etc.

Author:
Martin Haye

Nested Class Summary
private  class IntMultiMap.Block
          Keeps track of a block of values, with links to the following values.
 
Field Summary
private static int BLOCK_SIZE
           
private  IntMultiMap.Block[] blocks
           
private  IntMultiMap.Block curBlock
           
private  short curBlockNum
           
private  short curBlockTop
           
private  int[] keyLinks
           
static Tester tester
          Basic regression test
 
Constructor Summary
IntMultiMap(int maxKey)
          Initialize the mapping table.
 
Method Summary
 void add(int key, int value)
          Add a new association between a key and a value.
 long byteSize()
          Calculate the size in bytes of the major structures of the map.
 int firstPos(int key)
          For iteration: get the first position for the given key, or -1 if it has none.
 int getValue(int pos)
          Retrieve the value for a given link.
 int nextPos(int prevPos)
          For iteration: get the next position after the given pos, or -1 if we're at the end of the chain.
 void reverseOrder()
          Reverse the order of all links.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

keyLinks

private int[] keyLinks

BLOCK_SIZE

private static final int BLOCK_SIZE
See Also:
Constant Field Values

blocks

private IntMultiMap.Block[] blocks

curBlockNum

private short curBlockNum

curBlock

private IntMultiMap.Block curBlock

curBlockTop

private short curBlockTop

tester

public static final Tester tester
Basic regression test

Constructor Detail

IntMultiMap

public IntMultiMap(int maxKey)
Initialize the mapping table.

Parameters:
maxKey - One larger than the largest key value that will ever be passed to add(int, int). Note that the mapping cannot be expanded.
Method Detail

byteSize

public long byteSize()
Calculate the size in bytes of the major structures of the map.

Returns:
Approximate size in bytes

add

public void add(int key,
                int value)
Add a new association between a key and a value. Note that each key is allowed to have multiple values associated with it.


reverseOrder

public void reverseOrder()
Reverse the order of all links. This can be helpful, since for speed reasons the normal order of iteration is reversed.


firstPos

public final int firstPos(int key)
For iteration: get the first position for the given key, or -1 if it has none.


nextPos

public final int nextPos(int prevPos)
For iteration: get the next position after the given pos, or -1 if we're at the end of the chain.


getValue

public final int getValue(int pos)
Retrieve the value for a given link.