Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.comp90056;
- import java.math.BigInteger;
- import java.util.concurrent.ThreadLocalRandom;
- public class OneSparseRecovery {
- // This class is for doing the 1-sparse recovery.
- // Updates are done in such a way that deletions are handled.
- // The summation of frequencies for all items so far.
- int F1;
- // The summation of all frequencies multiplied by the item value.
- int U;
- BigInteger T;
- long prime;
- long q;
- public OneSparseRecovery() {
- F1 = 0;
- U = 0;
- T = BigInteger.ZERO;
- prime = 2;
- prime = prime << (61 - 1) ; // 2 ^ 61
- prime -= 1; // We now have a Mersenne prime (2305843009213693951)
- q = ThreadLocalRandom.current().nextLong(prime) + 1;
- }
- public void update(int item, int freq) {
- // Update F1, U and T.
- F1 += freq;
- U += (item * freq);
- // Use BigInteger in calculation of T to avoid overflow.
- BigInteger t0 = BigInteger.valueOf(q);
- t0 = t0.pow(item);
- t0 = t0.multiply(BigInteger.valueOf(freq));
- /*t0 = t0.mod(BigInteger.valueOf(prime));*/ //TODO: Do each time?
- /*T += t0.longValue();*/
- T = T.add(t0);
- /* T += ((freq * (Math.pow(q, item))) % prime);*/
- }
- public int isOneSparse() {
- // Use BigInteger in calculation to avoid overflow.
- if (F1 == 0 && U == 0) {
- return 0;
- }
- BigInteger t0 = BigInteger.valueOf(q);
- t0 = t0.pow(U/F1);
- t0 = t0.multiply(BigInteger.valueOf(F1));
- t0 = t0.mod(BigInteger.valueOf(prime));
- /*return T == ((F1 * Math.pow(q, ((double) U / F1))) % prime);*/
- if (T.mod(BigInteger.valueOf(prime)).longValue() == t0.longValue()) {
- return 1;
- } else
- return 2;
- }
- public int[] output() {
- return new int[] {U / F1, F1};
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement