/**
This is a class for treating nonnegative integers
as values supporting lexicographical ordering, in
such a way that this ordering becomes identical
with ordinary integer ordering.
In effect, values are padded with 0's at the left
so that they all have the same length. This
length must be the maximum length of any
integer in the class, and each integer needs
to know this maximum length. The maximum length
maxLength has not been made a static
variable, since different collections of TrieIntegers
may have different maximum values. It is therefore
up to users of the class to make sure that all
integers in the same collection have the same
maxLength value.
*/
public class TrieInteger implements Lexicographical
{
private int data; // the integer being represented
private int alphabetSize = 11; // the total number of characters
private int maxLength; // the padded integer length
/** A constructor.
@param d the integer to be represented
@param mx the maximum length of the integer,
which is the length of the integer
after left padding with zeros.
Note that any excess digits will be effectively
truncated from the right.
*/
public TrieInteger(int d, int mx) {
data=d;
maxLength=mx; }
/** @return the length of the integer in digits.
The length returned is the actual length
of the integer, and therefore may be
greater than the maxLength value.
*/
public int length() {
return maxLength; }
public int elementAt(int i) {
if (data < 0 || i > maxLength)
return 0;
else
return elementAt(data, i); }
// A private, recursive method to return
// the digit at a particular position.
// Since values are assumed to be padded on
// the left with zeroes so that they all
// have the maximum length, the algorithm
// merely throws away digits from the right
// (by dividing by 10) until the given
// position appears at the end of the
// value. It then finds the rightmost
// digit by reducing mod 10, and returns
// a character code one larger than this
// digit (since the terminator character
// has character code 0).
// It will return a value (rather than crashing)
// for a negative first argument.
private int elementAt(int d, int posn) {
for (int i=posn; i