net.innig.collect
Class IntegralRadix

java.lang.Object
  extended by net.innig.collect.IntegralRadix
All Implemented Interfaces:
java.io.Serializable, java.util.Comparator, Radix

public class IntegralRadix
extends java.lang.Object
implements Radix, java.io.Serializable

A radix which supports Byte, Short, Integer, and Long.

Maturity: All the radix utilities in innig-util are completely experimental. They mostly work, but perform poorly. They may stay; they may improve; they may go away.
Plans: Experiment.

See Also:
Radix, Serialized Form

Constructor Summary
IntegralRadix(int numberBits, int digitBits, boolean signed)
          Creates a new radix for n-bit integers.
 
Method Summary
static int bitsNeeded(long n, boolean signed)
          Returns the number of bits necessary to represent n.
 int compare(java.lang.Object aObj, java.lang.Object bObj)
           
 int digit(java.lang.Object o, int pos)
          Returns the digits of the radix representation of the given number.
 boolean equals(java.lang.Object that)
           
static Radix forType(java.lang.Class numberType)
          Returns a base-256 radix which handles numbers of the given type in their natural form (signed, and all bits used).
 int getBase()
          Returns the base (the number of digit values) in this radix.
 int getMaxPosition(java.lang.Object o)
          Returns the position of the most significant digit for this radix.
 int getMaxPositionForAll(java.util.Collection values)
          Returns the position of the most significant digit for this radix.
 int getMinPosition(java.lang.Object o)
          Returns zero.
 int getMinPositionForAll(java.util.Collection values)
          Returns zero.
 int hashCode()
           
 java.lang.Object objectFromDigits(int[] digits)
          Constructs a number from the given digits.
 java.lang.Object objectFromDigits(int[] digits, int offset, int len)
          Constructs a number from the given digits.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IntegralRadix

public IntegralRadix(int numberBits,
                     int digitBits,
                     boolean signed)
Creates a new radix for n-bit integers.

Parameters:
numberBits - the number of bits in the integers this radix handles. Although the standard integral types come in powers of two (e.g. 8 for Byte, 16 for Short), any number of bits between 1 and 64 is legal.
digitBits - the number of bits in each digit of the radix. This parameter allows you to split large integers into several digits, which keeps bucket-based radix algorithms reasonable. This parameter must divide numberBits, and must be <= 30.
signed - if true, the radix treats numbers as signed, and gives the digits of the two's complement representation.
Method Detail

bitsNeeded

public static int bitsNeeded(long n,
                             boolean signed)
Returns the number of bits necessary to represent n.


forType

public static Radix forType(java.lang.Class numberType)
Returns a base-256 radix which handles numbers of the given type in their natural form (signed, and all bits used).

Parameters:
numberType - the class Byte, Short, Integer, or Long.
Throws:
java.lang.IllegalArgumentException - if numberType is not one of these classes.

getBase

public int getBase()
Returns the base (the number of digit values) in this radix.

Specified by:
getBase in interface Radix

digit

public int digit(java.lang.Object o,
                 int pos)
Returns the digits of the radix representation of the given number. Lower positions are less significant digits.

Specified by:
digit in interface Radix
Parameters:
o - a Byte, Short, Integer, or Long object.
pos - a digit position within o.
Returns:
a digit d, with -1 <= x < getBase(). A variable-length radix uses the digit -1 signifies that the position is beyond the end of the object's representation.
Throws:
java.lang.ClassCastException - if o is not a Number.

getMaxPosition

public int getMaxPosition(java.lang.Object o)
Returns the position of the most significant digit for this radix.

Specified by:
getMaxPosition in interface Radix

getMinPosition

public int getMinPosition(java.lang.Object o)
Returns zero.

Specified by:
getMinPosition in interface Radix

getMaxPositionForAll

public int getMaxPositionForAll(java.util.Collection values)
Returns the position of the most significant digit for this radix.

Specified by:
getMaxPositionForAll in interface Radix

getMinPositionForAll

public int getMinPositionForAll(java.util.Collection values)
Returns zero.

Specified by:
getMinPositionForAll in interface Radix

objectFromDigits

public java.lang.Object objectFromDigits(int[] digits)
Constructs a number from the given digits.

Specified by:
objectFromDigits in interface Radix
Throws:
java.lang.IllegalArgumentException - if the digits don't represent a valid number.

objectFromDigits

public java.lang.Object objectFromDigits(int[] digits,
                                         int offset,
                                         int len)
Constructs a number from the given digits.

Specified by:
objectFromDigits in interface Radix
Throws:
java.lang.IllegalArgumentException - if the digits don't represent a valid number.

compare

public int compare(java.lang.Object aObj,
                   java.lang.Object bObj)
Specified by:
compare in interface java.util.Comparator

equals

public boolean equals(java.lang.Object that)
Specified by:
equals in interface java.util.Comparator
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object