jist.swans.misc
Class Mapper

java.lang.Object
  extended by jist.swans.misc.Mapper

public final class Mapper
extends java.lang.Object

Encodes a one-to-one mapping. Used, for example, to dynamically assign port numbers consistently, but in a smaller index range, in order to reduce array sizes.

Since:
SWANS1.0
Version:
$Id: Mapper.java,v 1.8 2004/04/06 16:07:48 barr Exp $
Author:
Rimon Barr <barr+jist@cs.cornell.edu>

Field Summary
private  int limit
          Mapped (range) limit.
static int MAP_INVALID
          Invalid mapping constant.
private  int[] mapFrom
          Reverse mapping table.
private  int[] mapTo
          Forward mapping table.
private  boolean sealed
          Whether this mapping is sealed.
 
Constructor Summary
Mapper(int max)
          Create a mapping with a given index cap.
Mapper(int[] values)
          Create a sealed mapping of the provided values.
 
Method Summary
 int getLimit()
          Return limit of the range of this mapping.
 int getMap(int i)
          Return element in the range of the mapping.
 int getMapR(int j)
          Return element in the domain of the mapping.
private  void init(int max)
          Private constructor helper method.
private  int map(int i, int j)
          Define a mapping.
 int mapToNext(int i)
          Define a mapping.
 void seal()
          Seal the current mapping; prevent further changes.
 int testMapToNext(int i)
          Define a mapping if one does not already exist.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAP_INVALID

public static final int MAP_INVALID
Invalid mapping constant.

See Also:
Constant Field Values

mapTo

private int[] mapTo
Forward mapping table.


mapFrom

private int[] mapFrom
Reverse mapping table.


limit

private int limit
Mapped (range) limit.


sealed

private boolean sealed
Whether this mapping is sealed.

Constructor Detail

Mapper

public Mapper(int max)
Create a mapping with a given index cap.

Parameters:
max - map indices should be non-negative and less than max.

Mapper

public Mapper(int[] values)
Create a sealed mapping of the provided values.

Parameters:
values - array of domain values to map.
Method Detail

init

private void init(int max)
Private constructor helper method.

Parameters:
max - map indices should be non-negative and less than max.

seal

public void seal()
Seal the current mapping; prevent further changes.


map

private int map(int i,
                int j)
Define a mapping. Map i onto j.

Parameters:
i - integer in domain of mapping
j - integer in range of mapping
Returns:
j

mapToNext

public int mapToNext(int i)
Define a mapping. Map i onto one more than the highest value in the range of the mapping.

Parameters:
i - integer in the domain of mapping
Returns:
selected value in the range of the mapping

testMapToNext

public int testMapToNext(int i)
Define a mapping if one does not already exist. Map i onto one more than the highest value in the range of the mapping, unless it is already mapped.

Parameters:
i - integer in the domain of mapping
Returns:
new or corresponding integer in the range of the mapping

getMap

public int getMap(int i)
Return element in the range of the mapping.

Parameters:
i - integer in the domain of the mapping
Returns:
corresponding integer in the range of the mapping

getMapR

public int getMapR(int j)
Return element in the domain of the mapping.

Parameters:
j - integer in the range of the mapping
Returns:
corresponding integer in the domain of the mapping

getLimit

public int getLimit()
Return limit of the range of this mapping.

Returns:
range limit of mapping