Util.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package util;
  2. import java.math.BigInteger;
  3. import java.nio.ByteBuffer;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Collections;
  7. import java.util.List;
  8. import java.util.Random;
  9. import exceptions.LengthNotMatchException;
  10. public class Util {
  11. public static boolean equal(byte[] a, byte[] b) {
  12. if (a.length != b.length)
  13. return false;
  14. return new BigInteger(a).compareTo(new BigInteger(b)) == 0;
  15. }
  16. public static byte[] nextBytes(int len, Random r) {
  17. byte[] data = new byte[len];
  18. r.nextBytes(data);
  19. return data;
  20. }
  21. public static long nextLong(long range, Random r) {
  22. long bits, val;
  23. do {
  24. bits = (r.nextLong() << 1) >>> 1;
  25. val = bits % range;
  26. } while (bits - val + (range - 1) < 0L);
  27. return val;
  28. }
  29. public static long getSubBits(long l, int end, int start) {
  30. if (start < 0)
  31. throw new IllegalArgumentException(start + " < 0");
  32. if (start > end)
  33. throw new IllegalArgumentException(start + " > " + end);
  34. long mask = (1L << (end - start)) - 1L;
  35. return (l >>> start) & mask;
  36. }
  37. public static BigInteger getSubBits(BigInteger bi, int end, int start) {
  38. if (start < 0)
  39. throw new IllegalArgumentException(start + " < 0");
  40. if (start > end)
  41. throw new IllegalArgumentException(start + " > " + end);
  42. BigInteger mask = BigInteger.ONE.shiftLeft(end - start).subtract(BigInteger.ONE);
  43. return bi.shiftRight(start).and(mask);
  44. }
  45. public static long setSubBits(long target, long input, int end, int start) {
  46. input = getSubBits(input, end - start, 0);
  47. long trash = getSubBits(target, end, start);
  48. return ((trash ^ input) << start) ^ target;
  49. }
  50. public static BigInteger setSubBits(BigInteger target, BigInteger input, int end, int start) {
  51. if (input.bitLength() > end - start)
  52. input = getSubBits(input, end - start, 0);
  53. BigInteger trash = getSubBits(target, end, start);
  54. return trash.xor(input).shiftLeft(start).xor(target);
  55. }
  56. public static byte[] rmSignBit(byte[] arr) {
  57. if (arr[0] == 0)
  58. return Arrays.copyOfRange(arr, 1, arr.length);
  59. return arr;
  60. }
  61. // c = a ^ b
  62. public static byte[] xor(byte[] a, byte[] b) {
  63. if (a.length != b.length)
  64. throw new LengthNotMatchException(a.length + " != " + b.length);
  65. byte[] c = new byte[a.length];
  66. for (int i = 0; i < a.length; i++)
  67. c[i] = (byte) (a[i] ^ b[i]);
  68. return c;
  69. }
  70. // a = a ^ b to save memory
  71. public static void setXor(byte[] a, byte[] b) {
  72. if (a.length != b.length)
  73. throw new LengthNotMatchException(a.length + " != " + b.length);
  74. for (int i = 0; i < a.length; i++)
  75. a[i] = (byte) (a[i] ^ b[i]);
  76. }
  77. public static byte[] intToBytes(int i) {
  78. ByteBuffer bb = ByteBuffer.allocate(4);
  79. bb.putInt(i);
  80. return bb.array();
  81. }
  82. public static int bytesToInt(byte[] b) {
  83. return new BigInteger(b).intValue();
  84. }
  85. public static int[] randomPermutation(int len, Random rand) {
  86. List<Integer> list = new ArrayList<Integer>(len);
  87. for (int i = 0; i < len; i++)
  88. list.add(i);
  89. Collections.shuffle(list, rand);
  90. int[] array = new int[len];
  91. for (int i = 0; i < len; i++)
  92. array[i] = list.get(i);
  93. return array;
  94. }
  95. public static <T> T[] permute(T[] original, int[] p) {
  96. @SuppressWarnings("unchecked")
  97. T[] permuted = (T[]) new Object[original.length];
  98. for (int i = 0; i < original.length; i++)
  99. permuted[p[i]] = original[i];
  100. return permuted;
  101. }
  102. public static void debug(String s) {
  103. // only to make Communication.java compile
  104. }
  105. public static void disp(String s) {
  106. // only to make Communication.java compile
  107. }
  108. public static void error(String s) {
  109. // only to make Communication.java compile
  110. }
  111. public static void error(String s, Exception e) {
  112. // only to make Communication.java compile
  113. }
  114. }