Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. package com.comp90056;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.concurrent.ThreadLocalRandom;
  5.  
  6. public class OneSparseRecovery {
  7.  
  8. // This class is for doing the 1-sparse recovery.
  9. // Updates are done in such a way that deletions are handled.
  10.  
  11. // The summation of frequencies for all items so far.
  12. int F1;
  13.  
  14. // The summation of all frequencies multiplied by the item value.
  15. int U;
  16.  
  17. BigInteger T;
  18.  
  19. long prime;
  20.  
  21. long q;
  22.  
  23. public OneSparseRecovery() {
  24. F1 = 0;
  25. U = 0;
  26. T = BigInteger.ZERO;
  27.  
  28. prime = 2;
  29. prime = prime << (61 - 1) ; // 2 ^ 61
  30. prime -= 1; // We now have a Mersenne prime (2305843009213693951)
  31. q = ThreadLocalRandom.current().nextLong(prime) + 1;
  32. }
  33.  
  34. public void update(int item, int freq) {
  35. // Update F1, U and T.
  36. F1 += freq;
  37. U += (item * freq);
  38.  
  39. // Use BigInteger in calculation of T to avoid overflow.
  40. BigInteger t0 = BigInteger.valueOf(q);
  41. t0 = t0.pow(item);
  42. t0 = t0.multiply(BigInteger.valueOf(freq));
  43. /*t0 = t0.mod(BigInteger.valueOf(prime));*/ //TODO: Do each time?
  44. /*T += t0.longValue();*/
  45. T = T.add(t0);
  46.  
  47. /* T += ((freq * (Math.pow(q, item))) % prime);*/
  48. }
  49.  
  50. public int isOneSparse() {
  51. // Use BigInteger in calculation to avoid overflow.
  52. if (F1 == 0 && U == 0) {
  53. return 0;
  54. }
  55.  
  56. BigInteger t0 = BigInteger.valueOf(q);
  57. t0 = t0.pow(U/F1);
  58. t0 = t0.multiply(BigInteger.valueOf(F1));
  59. t0 = t0.mod(BigInteger.valueOf(prime));
  60.  
  61. /*return T == ((F1 * Math.pow(q, ((double) U / F1))) % prime);*/
  62.  
  63. if (T.mod(BigInteger.valueOf(prime)).longValue() == t0.longValue()) {
  64. return 1;
  65. } else
  66. return 2;
  67. }
  68.  
  69. public int[] output() {
  70. return new int[] {U / F1, F1};
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement