Java Code Examples for com.google.common.hash.HashCode#asInt()

The following examples show how to use com.google.common.hash.HashCode#asInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HashUtils.java    From MHAP with Apache License 2.0 6 votes vote down vote up
public final static int[] computeSequenceHashes(final String seq, final int nGramSize, boolean doReverseCompliment)
{
	HashFunction hf = Hashing.murmur3_32(0);

	int[] hashes = new int[seq.length() - nGramSize + 1];
	for (int iter = 0; iter < hashes.length; iter++)
	{
		String str = seq.substring(iter, iter + nGramSize);
		
		String strReverse = null;
		if (doReverseCompliment)
		{
			strReverse  = Utils.rc(str);
			if (strReverse.compareTo(str)<0)
				str = strReverse;
		}

		HashCode hc = hf.newHasher().putUnencodedChars(str).hash();
		hashes[iter] = hc.asInt();
	}

	return hashes;
}
 
Example 2
Source File: ProbedLinkProvider.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Build a stringified MAC address using the ClusterMetadata hash for uniqueness.
 * Form of MAC is "02:eb" followed by four bytes of clusterMetadata hash.
 *
 * @param cm cluster metadata
 * @return stringified mac address
 */
static String fingerprintMac(ClusterMetadata cm) {
    if (cm == null) {
        return DEFAULT_MAC;
    }

    HashFunction hf = Hashing.murmur3_32();
    HashCode hc = hf.newHasher().putObject(cm, ClusterMetadata.HASH_FUNNEL).hash();
    int unqf = hc.asInt();

    StringBuilder sb = new StringBuilder();
    sb.append("02:eb");
    for (int i = 0; i < 4; i++) {
        byte b = (byte) (unqf >> i * 8);
        sb.append(String.format(":%02X", b));
    }
    return sb.toString();
}
 
Example 3
Source File: DefaultFlowRule.java    From onos with Apache License 2.0 6 votes vote down vote up
private int hash() {
    // Guava documentation recommends using putUnencodedChars to hash raw character bytes within any encoding
    // unless cross-language compatibility is needed. See the Hasher.putString documentation for more info.
    Funnel<TrafficSelector> selectorFunnel = (from, into) -> from.criteria()
            .forEach(c -> into.putUnencodedChars(c.toString()));

    HashFunction hashFunction = Hashing.murmur3_32();
    HashCode hashCode = hashFunction.newHasher()
            .putUnencodedChars(deviceId.toString())
            .putObject(selector, selectorFunnel)
            .putInt(priority)
            .putUnencodedChars(tableId.toString())
            .hash();

    return hashCode.asInt();
}
 
Example 4
Source File: ClusterManager.java    From rubix with Apache License 2.0 5 votes vote down vote up
public int getNodeIndex(int numNodes, String key)
{
  HashFunction hf = Hashing.md5();
  HashCode hc = hf.hashString(key, Charsets.UTF_8);
  int initialNodeIndex = Hashing.consistentHash(hc, numNodes);
  int finalNodeIndex = initialNodeIndex;
  if (hc.asInt() % 2 == 0) {
    finalNodeIndex = getNextRunningNodeIndex(initialNodeIndex);
  }
  else {
    finalNodeIndex = getPreviousRunningNodeIndex(initialNodeIndex);
  }

  return finalNodeIndex;
}
 
Example 5
Source File: HashUtils.java    From MHAP with Apache License 2.0 5 votes vote down vote up
public final static int[] computeHashesIntDouble(double obj, int numWords, int seed)
{
	int[] hashes = new int[numWords];

	HashFunction hf = Hashing.murmur3_32(seed);

	for (int iter = 0; iter < numWords; iter++)
	{
		HashCode hc = hf.newHasher().putDouble(obj).putInt(iter).hash();

		hashes[iter] = hc.asInt();
	}

	return hashes;
}
 
Example 6
Source File: HashUtils.java    From MHAP with Apache License 2.0 5 votes vote down vote up
public final static int[] computeHashesIntFloat(float obj, int numWords, int seed)
{
	int[] hashes = new int[numWords];

	HashFunction hf = Hashing.murmur3_32(seed);

	for (int iter = 0; iter < numWords; iter++)
	{
		HashCode hc = hf.newHasher().putFloat(obj).putInt(iter).hash();

		hashes[iter] = hc.asInt();
	}

	return hashes;
}
 
Example 7
Source File: HashUtils.java    From MHAP with Apache License 2.0 5 votes vote down vote up
public final static int[] computeHashesIntInt(int obj, int numWords, int seed)
{
	int[] hashes = new int[numWords];

	HashFunction hf = Hashing.murmur3_32(seed);

	for (int iter = 0; iter < numWords; iter++)
	{
		HashCode hc = hf.newHasher().putInt(obj).putInt(iter).hash();

		hashes[iter] = hc.asInt();
	}

	return hashes;
}
 
Example 8
Source File: HashUtils.java    From MHAP with Apache License 2.0 5 votes vote down vote up
public final static int[] computeHashesIntLong(long obj, int numWords, int seed)
{
	int[] hashes = new int[numWords];

	HashFunction hf = Hashing.murmur3_32(seed);

	for (int iter = 0; iter < numWords; iter++)
	{
		HashCode hc = hf.newHasher().putLong(obj).putInt(iter).hash();

		hashes[iter] = hc.asInt();
	}

	return hashes;
}
 
Example 9
Source File: HashUtils.java    From MHAP with Apache License 2.0 5 votes vote down vote up
public final static int[] computeHashesIntString(String obj, int numWords, int seed)
{
	int[] hashes = new int[numWords];

	HashFunction hf = Hashing.murmur3_32(seed);

	for (int iter = 0; iter < numWords; iter++)
	{
		HashCode hc = hf.newHasher().putUnencodedChars(obj).putInt(iter).hash();

		hashes[iter] = hc.asInt();
	}

	return hashes;
}
 
Example 10
Source File: HashUtils.java    From MHAP with Apache License 2.0 5 votes vote down vote up
public static double[] randomStringGuassianVector(String str, int n, int seed)
{
	int[] seeds = new int[4];
	for (int iter=0; iter<4; iter++)
	{
		HashFunction hf = Hashing.murmur3_32(seed*4+iter);
		HashCode hc = hf.newHasher().putUnencodedChars(str).hash();
		
		seeds[iter] = hc.asInt();
	}
	
	//now generate the guassian
	MersenneTwisterFast rand = new MersenneTwisterFast(seeds);
	
	double[] vec = new double[n];
	for (int iter=0; iter<n; iter++)
	{
		vec[iter] = rand.nextGaussian();
	}
	
	//normalize
	double norm = BasicMath.norm(vec);		
	if (norm<1.0e-10)
		return vec;
	
	return BasicMath.mult(vec, 1.0/norm);
}