Util.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 byte[] intToBytes(int i) {
  80. ByteBuffer bb = ByteBuffer.allocate(4);
  81. bb.putInt(i);
  82. return bb.array();
  83. }
  84. public static int bytesToInt(byte[] b) {
  85. return new BigInteger(b).intValue();
  86. }
  87. public static int[] randomPermutation(int len, Random rand) {
  88. List<Integer> list = new ArrayList<Integer>(len);
  89. for (int i = 0; i < len; i++)
  90. list.add(i);
  91. Collections.shuffle(list, rand);
  92. int[] array = new int[len];
  93. for (int i = 0; i < len; i++)
  94. array[i] = list.get(i);
  95. return array;
  96. }
  97. public static int[] inversePermutation(int[] p) {
  98. int[] ip = new int[p.length];
  99. for (int i = 0; i < p.length; i++)
  100. ip[p[i]] = i;
  101. return ip;
  102. }
  103. @SuppressWarnings("unchecked")
  104. public static <T> T[] permute(T[] original, int[] p) {
  105. T[] permuted = (T[]) new Object[original.length];
  106. for (int i = 0; i < original.length; i++)
  107. permuted[p[i]] = original[i];
  108. return (T[]) Arrays.copyOf(permuted, permuted.length, original.getClass());
  109. }
  110. public static byte[] longToBytes(long l, int numBytes) {
  111. byte[] bytes = BigInteger.valueOf(l).toByteArray();
  112. if (bytes.length == numBytes)
  113. return bytes;
  114. else if (bytes.length > numBytes)
  115. return Arrays.copyOfRange(bytes, bytes.length - numBytes, bytes.length);
  116. else {
  117. byte[] out = new byte[numBytes];
  118. System.arraycopy(bytes, 0, out, numBytes - bytes.length, bytes.length);
  119. return out;
  120. }
  121. }
  122. public static void debug(String s) {
  123. // only to make Communication.java compile
  124. }
  125. public static void disp(String s) {
  126. // only to make Communication.java compile
  127. }
  128. public static void error(String s) {
  129. // only to make Communication.java compile
  130. }
  131. public static void error(String s, Exception e) {
  132. // only to make Communication.java compile
  133. }
  134. }