Util.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 == 0 && b.length == 0)
  13. return true;
  14. if (a.length != b.length)
  15. return false;
  16. return new BigInteger(a).compareTo(new BigInteger(b)) == 0;
  17. }
  18. public static byte[] nextBytes(int len, Random r) {
  19. byte[] data = new byte[len];
  20. r.nextBytes(data);
  21. return data;
  22. }
  23. public static long nextLong(long range, Random r) {
  24. long bits, val;
  25. do {
  26. bits = (r.nextLong() << 1) >>> 1;
  27. val = bits % range;
  28. } while (bits - val + (range - 1) < 0L);
  29. return val;
  30. }
  31. public static long getSubBits(long l, int end, int start) {
  32. if (start < 0)
  33. throw new IllegalArgumentException(start + " < 0");
  34. if (start > end)
  35. throw new IllegalArgumentException(start + " > " + end);
  36. long mask = (1L << (end - start)) - 1L;
  37. return (l >>> start) & mask;
  38. }
  39. public static BigInteger getSubBits(BigInteger bi, int end, int start) {
  40. if (start < 0)
  41. throw new IllegalArgumentException(start + " < 0");
  42. if (start > end)
  43. throw new IllegalArgumentException(start + " > " + end);
  44. BigInteger mask = BigInteger.ONE.shiftLeft(end - start).subtract(BigInteger.ONE);
  45. return bi.shiftRight(start).and(mask);
  46. }
  47. public static long setSubBits(long target, long input, int end, int start) {
  48. input = getSubBits(input, end - start, 0);
  49. long trash = getSubBits(target, end, start);
  50. return ((trash ^ input) << start) ^ target;
  51. }
  52. public static BigInteger setSubBits(BigInteger target, BigInteger input, int end, int start) {
  53. if (input.bitLength() > end - start)
  54. input = getSubBits(input, end - start, 0);
  55. BigInteger trash = getSubBits(target, end, start);
  56. return trash.xor(input).shiftLeft(start).xor(target);
  57. }
  58. public static byte[] rmSignBit(byte[] arr) {
  59. if (arr[0] == 0)
  60. return Arrays.copyOfRange(arr, 1, arr.length);
  61. return arr;
  62. }
  63. // c = a ^ b
  64. public static byte[] xor(byte[] a, byte[] b) {
  65. if (a.length != b.length)
  66. throw new LengthNotMatchException(a.length + " != " + b.length);
  67. byte[] c = new byte[a.length];
  68. for (int i = 0; i < a.length; i++)
  69. c[i] = (byte) (a[i] ^ b[i]);
  70. return c;
  71. }
  72. // a = a ^ b to save memory
  73. public static void setXor(byte[] a, byte[] b) {
  74. if (a.length != b.length)
  75. throw new LengthNotMatchException(a.length + " != " + b.length);
  76. for (int i = 0; i < a.length; i++)
  77. a[i] = (byte) (a[i] ^ b[i]);
  78. }
  79. public static BigInteger[] xor(BigInteger[] a, BigInteger[] b) {
  80. if (a.length != b.length)
  81. throw new LengthNotMatchException(a.length + " != " + b.length);
  82. BigInteger[] c = new BigInteger[a.length];
  83. for (int i = 0; i < a.length; i++)
  84. c[i] = a[i].xor(b[i]);
  85. return c;
  86. }
  87. public static byte[] intToBytes(int i) {
  88. ByteBuffer bb = ByteBuffer.allocate(4);
  89. bb.putInt(i);
  90. return bb.array();
  91. }
  92. public static int bytesToInt(byte[] b) {
  93. return new BigInteger(b).intValue();
  94. }
  95. public static int[] randomPermutation(int len, Random rand) {
  96. List<Integer> list = new ArrayList<Integer>(len);
  97. for (int i = 0; i < len; i++)
  98. list.add(i);
  99. Collections.shuffle(list, rand);
  100. int[] array = new int[len];
  101. for (int i = 0; i < len; i++)
  102. array[i] = list.get(i);
  103. return array;
  104. }
  105. public static int[] inversePermutation(int[] p) {
  106. int[] ip = new int[p.length];
  107. for (int i = 0; i < p.length; i++)
  108. ip[p[i]] = i;
  109. return ip;
  110. }
  111. @SuppressWarnings("unchecked")
  112. public static <T> T[] permute(T[] original, int[] p) {
  113. T[] permuted = (T[]) new Object[original.length];
  114. for (int i = 0; i < original.length; i++)
  115. permuted[p[i]] = original[i];
  116. return (T[]) Arrays.copyOf(permuted, permuted.length, original.getClass());
  117. }
  118. public static byte[] longToBytes(long l, int numBytes) {
  119. byte[] bytes = BigInteger.valueOf(l).toByteArray();
  120. if (bytes.length == numBytes)
  121. return bytes;
  122. else if (bytes.length > numBytes)
  123. return Arrays.copyOfRange(bytes, bytes.length - numBytes, bytes.length);
  124. else {
  125. byte[] out = new byte[numBytes];
  126. System.arraycopy(bytes, 0, out, numBytes - bytes.length, bytes.length);
  127. return out;
  128. }
  129. }
  130. public static int[] getXorPermutation(BigInteger b, int bits) {
  131. int len = (int) Math.pow(2, bits);
  132. int[] p = new int[len];
  133. for (int i = 0; i < len; i++) {
  134. p[i] = BigInteger.valueOf(i).xor(b).intValue();
  135. }
  136. return p;
  137. }
  138. public static String addZeros(String a, int n) {
  139. String out = a;
  140. for (int i = 0; i < n - a.length(); i++)
  141. out = "0" + out;
  142. return out;
  143. }
  144. public static void debug(String s) {
  145. // only to make Communication.java compile
  146. }
  147. public static void disp(String s) {
  148. // only to make Communication.java compile
  149. }
  150. public static void error(String s) {
  151. // only to make Communication.java compile
  152. }
  153. public static void error(String s, Exception e) {
  154. // only to make Communication.java compile
  155. }
  156. }