package sort; /** Maps elements to buckets using a subset of the digits of each element. */ public class RadixBucketizer implements Bucketizer { // Offset of digits to use (in multiples of digits, from LSD). private int rad = 0; // Number of digits to use. private int digits; /** Creates a RadixBucketizer that uses the given * number of digits at a time. * @param digits the number of digits used at a time */ public RadixBucketizer(int digits) { this.digits = digits; } /** Creates a RadixBucketizer that uses 4 digits at * a time. */ public RadixBucketizer() { this(4); } /** Sets the offset of digits to use. * @param rad the new offset */ public void setRad(int rad) { this.rad = rad; } /** Computes the bucket into which the given element should be placed. */ public int getPos(int x) { return (x >> (digits * rad)) & (0xffffffff >>> (32 - digits)); } }