homedukeAlgorithmen & Datenstrukturen mit Java: ds.util.Integer Prof. Dr. Uwe Schmidt FH Wedel

ds.util.Integer

   1package ds.util;
   2
   3import static ds.util.Boolean.toInt;
   4
   5
   6public
   7    class Integer {
   8    // min and max become obsolete with Java 8
   9
  10    public static int max(int iint j) {
  11        return
  12            i <= j ? j : i;
  13    }
  14    public static int min(int iint j) {
  15        return
  16            j <= i ? j : i;
  17    }
  18
  19    // second try
  20    public static int compareUnsigned(int i1int i2) {
  21        long l1 = (long)i1 & 0xFFFFFFFFl;
  22        long l2 = (long)i2 & 0xFFFFFFFFl;
  23
  24        return
  25            toInt(l1 >= l2) - toInt(l1 <= l2);
  26    }
  27
  28    //----------------------------------------
  29    // bitstring operations
  30    // unsed in IntMap implementation
  31
  32    // access the leading bits of a word
  33    public static int getPrefix(int bsint mask) {
  34        return
  35            bs & (~ (mask - 1) ^ mask);
  36    }
  37
  38    // compute the mask identifying the common prefix of two words
  39    public static int commonPrefixMask(int bs1int bs2) {
  40        return
  41            zeroPrefix(bs1 ^ bs2);
  42    }
  43
  44    // compare the leading part of a word with a prefix
  45    public static boolean matchPrefix(int bsint pxint mask) {
  46        return
  47            getPrefix(bsmask) == px;
  48    }
  49
  50    // remove all bits set except the most significant
  51    public static int zeroPrefix(int bs1) {
  52        bs1 |= bs1 >>>  1;
  53        bs1 |= bs1 >>>  2;
  54        bs1 |= bs1 >>>  4;
  55        bs1 |= bs1 >>>  8;
  56        bs1 |= bs1 >>> 16;
  57        // bs1 |= bs1 >>> 32; // needed for long
  58
  59        return
  60            bs1 ^ (bs1 >>> 1);
  61    }
  62
  63    public static boolean shorterMask(int mask1int mask2) {
  64        return
  65            compareUnsigned(mask1mask2) > 0;
  66    }
  67
  68    //----------------------------------------
  69    // a mask is a int which has set a single bit
  70    // the mask is used for indexing a single bit within a word
  71    // or to access all leading bit downto the bit index
  72
  73    public static boolean invMask(int m) {
  74        return
  75            ( m != 0
  76              &&
  77              (m ^ (m & (~m + 1))) == 0 );
  78    }
  79
  80    //----------------------------------------
  81    // test output for bitstrings
  82
  83    static final int LEN_BITSTRING = 32; // int
  84
  85    public static String toStringBS(int bs) {
  86        StringBuffer res   = new StringBuffer();
  87        int          i     = LEN_BITSTRING;
  88        boolean      blank = false;
  89
  90        while ( i > 0 ) {
  91            if ( blank )
  92                res.append(' ');
  93            --i;
  94            res.append((char)(((bs >>> i) & 0x1) + (int)'0'));
  95            blank = i % 8 == 0;
  96        }
  97        return
  98            new String(res);
  99    }
 100
 101    public static String toStringPX(int pxint mask) {
 102        StringBuffer res   = new StringBuffer();
 103        int          i     = LEN_BITSTRING;
 104        boolean      blank = false;
 105
 106        while ( (((int)<< (i - 1)) & mask) == 0 ) {
 107            if ( blank )
 108                res.append(' ');
 109            --i;
 110            res.append((char)(((px >>> i) & 0x1) + (int)'0'));
 111            blank = i % 8 == 0;
 112        }
 113        return
 114            new String(res);
 115    }
 116
 117}

Die Quelle: Integer.java


Letzte Änderung: 15.10.2015
© Prof. Dr. Uwe Schmidt
Prof. Dr. Uwe Schmidt FH Wedel